Add functionIsConstructor function

This commit is contained in:
John J. Camilleri
2021-09-13 15:15:16 +02:00
parent c83a31708d
commit 3d25efd38a
3 changed files with 38 additions and 3 deletions

View File

@@ -1,11 +1,9 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include <math.h>
#include <stdbool.h>
#include <pgf/pgf.h>
#include "./compat.h"
#include "./expr.h"

View File

@@ -1,6 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "structmember.h"
#include <structmember.h>
#include <pgf/pgf.h>
#include "./compat.h"
@@ -2047,6 +2047,28 @@ PGF_functionType(PGFObject *self, PyObject *args)
return (TypeObject *)type;
}
static PyObject *
PGF_functionIsConstructor(PGFObject *self, PyObject *args)
{
const char *s;
Py_ssize_t size;
if (!PyArg_ParseTuple(args, "s#", &s, &size))
return NULL;
PgfText *funname = (PgfText *)PyMem_Malloc(sizeof(PgfText)+size+1);
memcpy(funname->text, s, size+1);
funname->size = size;
PgfExn err;
int isCon = pgf_function_is_constructor(self->db, self->revision, funname, &err);
PyMem_Free(funname);
if (handleError(err) != PGF_EXN_NONE) {
return NULL;
}
return PyBool_FromLong(isCon);
}
// static IterObject*
// PGF_generateAll(PGFObject* self, PyObject *args, PyObject *keywds)
// {
@@ -2382,6 +2404,9 @@ static PyMethodDef PGF_methods[] = {
{"functionType", (PyCFunction)PGF_functionType, METH_VARARGS,
"Returns the type of a function"
},
{"functionIsConstructor", (PyCFunction)PGF_functionIsConstructor, METH_VARARGS,
"Checks whether a function is a constructor"
},
// {"generateAll", (PyCFunction)PGF_generateAll, METH_VARARGS | METH_KEYWORDS,
// "Generates abstract syntax trees of given category in decreasing probability order"
// },

View File

@@ -100,6 +100,18 @@ def test_categoryContext_3(PGF):
def test_categoryContext_4(PGF):
assert PGF.categoryContext("X") == []
def test_functionIsConstructor_1(PGF):
assert PGF.functionIsConstructor("s") == True
def test_functionIsConstructor_2(PGF):
assert PGF.functionIsConstructor("z") == True
def test_functionIsConstructor_3(PGF):
assert PGF.functionIsConstructor("c") == True
def test_functionIsConstructor_4(PGF):
assert PGF.functionIsConstructor("ind") == False
# types
def test_readType_invalid():