1
0
forked from GitHub/gf-core

Add getFunctionsByCat to Python bindings

This commit is contained in:
John J. Camilleri
2021-08-30 23:25:18 +02:00
parent b7bd5a4561
commit aecaa422ec
2 changed files with 47 additions and 33 deletions

View File

@@ -3026,7 +3026,7 @@ PGF_getCategories(PGFObject *self, void *closure)
// } // }
static void static void
pgf_collect_funs(PgfItor* fn, const void* key, void* value) pgf_collect_funs(PgfItor* fn, PgfText* key, void* value)
{ {
PgfText* name = key; PgfText* name = key;
PyPGFClosure* clo = (PyPGFClosure*) fn; PyPGFClosure* clo = (PyPGFClosure*) fn;
@@ -3072,35 +3072,41 @@ PGF_getFunctions(PGFObject *self, void *closure)
return functions; return functions;
} }
// static PyObject* static PyObject*
// PGF_functionsByCat(PGFObject* self, PyObject *args) PGF_functionsByCat(PGFObject* self, PyObject *args)
// { {
// PgfCId catname; const char* c;
// if (!PyArg_ParseTuple(args, "s", &catname)) if (!PyArg_ParseTuple(args, "s", &c))
// return NULL; return NULL;
//
// PyObject* functions = PyList_New(0); const size_t s = strlen(c);
// if (functions == NULL) {
// return NULL; PgfText* catname = (PgfText*) alloca(sizeof(PgfText)+s);
// } strcpy(catname->text, c);
// catname->size = s;
// GuPool *tmp_pool = gu_local_pool();
// PyObject* functions = PyList_New(0);
// // Create an exception frame that catches all errors. if (functions == NULL) {
// GuExn* err = gu_new_exn(tmp_pool); return NULL;
// }
// PyPGFClosure clo = { { pgf_collect_funs }, self, functions };
// pgf_iter_functions_by_cat(self->pgf, catname, &clo.fn, err); // GuPool *tmp_pool = gu_local_pool();
// if (!gu_ok(err)) {
// Py_DECREF(functions); // Create an exception frame that catches all errors.
// gu_pool_free(tmp_pool); // GuExn* err = gu_new_exn(tmp_pool);
// return NULL;
// } PyPGFClosure clo = { { pgf_collect_funs }, self, functions };
// pgf_iter_functions_by_cat(self->pgf, catname, &clo.fn);
// gu_pool_free(tmp_pool); // if (!gu_ok(err)) {
// return functions; // Py_DECREF(functions);
// } // gu_pool_free(tmp_pool);
// // return NULL;
// }
//
// gu_pool_free(tmp_pool);
return functions;
}
// static TypeObject* // static TypeObject*
// PGF_functionType(PGFObject* self, PyObject *args) // PGF_functionType(PGFObject* self, PyObject *args)
// { // {
@@ -3452,9 +3458,9 @@ static PyMemberDef PGF_members[] = {
}; };
static PyMethodDef PGF_methods[] = { static PyMethodDef PGF_methods[] = {
// {"functionsByCat", (PyCFunction)PGF_functionsByCat, METH_VARARGS, {"functionsByCat", (PyCFunction)PGF_functionsByCat, METH_VARARGS,
// "Returns the list of functions for a given category" "Returns the list of functions for a given category"
// }, },
// {"functionType", (PyCFunction)PGF_functionType, METH_VARARGS, // {"functionType", (PyCFunction)PGF_functionType, METH_VARARGS,
// "Returns the type of a function" // "Returns the type of a function"
// }, // },

View File

@@ -75,3 +75,11 @@ def test_categories(PGF):
def test_functions(PGF): def test_functions(PGF):
assert PGF.functions == ["c","ind","s","z"] assert PGF.functions == ["c","ind","s","z"]
def test_functionsByCat_1(PGF):
assert PGF.functionsByCat("N") == ["s","z"]
def test_functionsByCat_2(PGF):
assert PGF.functionsByCat("S") == ["c"]
def test_functionsByCat_non_existant(PGF):
assert PGF.functionsByCat("X") == []