Merge branch 'majestic' of github.com:GrammaticalFramework/gf-core into majestic

This commit is contained in:
krangelov
2021-09-22 18:14:44 +02:00
4 changed files with 117 additions and 52 deletions

View File

@@ -27,7 +27,7 @@ PgfExnType handleError(PgfExn err)
} }
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// string conversions // conversions
PgfText * PgfText *
PyUnicode_AsPgfText(PyObject *pystr) PyUnicode_AsPgfText(PyObject *pystr)
@@ -54,6 +54,51 @@ PyUnicode_FromPgfText(PgfText *text)
return PyUnicode_FromStringAndSize(text->text, text->size); return PyUnicode_FromStringAndSize(text->text, text->size);
} }
PgfTypeHypo *
PyList_AsHypos(PyObject *pylist, Py_ssize_t *n_hypos)
{
if (!PyList_Check(pylist)) {
PyErr_SetString(PyExc_TypeError, "PyList_AsHypos: must be a list");
return NULL;
}
Py_ssize_t n = PyList_Size(pylist);
*n_hypos = n;
PgfTypeHypo *hypos = PyMem_Malloc(sizeof(PgfTypeHypo)*n);
for (Py_ssize_t i = 0; i < n; i++) {
PyObject *hytup = PyList_GetItem(pylist, i);
if (!PyTuple_Check(hytup)) {
PyErr_SetString(PyExc_TypeError, "PyList_AsHypos: item must be a tuple");
return NULL;
}
PyObject *t0 = PyTuple_GetItem(hytup, 0);
if (!PyLong_Check(t0)) {
PyErr_SetString(PyExc_TypeError, "PyList_AsHypos: first element must be an integer");
return NULL;
}
hypos[i].bind_type = PyLong_AsLong(t0);
PyObject *t1 = PyTuple_GetItem(hytup, 1);
if (!PyUnicode_Check(t1)) {
PyErr_SetString(PyExc_TypeError, "PyList_AsHypos: second element must be a string");
return NULL;
}
hypos[i].cid = PyUnicode_AsPgfText(t1);
PyObject *t2 = PyTuple_GetItem(hytup, 2);
if (!PyObject_TypeCheck(t2, &pgf_TypeType)) {
PyErr_SetString(PyExc_TypeError, "PyList_AsHypos: third element must be a Type");
return NULL;
}
hypos[i].type = (PgfType) t2;
Py_INCREF(hypos[i].type);
}
return hypos;
}
// ---------------------------------------------------------------------------- // ----------------------------------------------------------------------------
// unmarshaller // unmarshaller
@@ -322,16 +367,10 @@ match_type(PgfMarshaller *this, PgfUnmarshaller *u, PgfType ty)
{ {
TypeObject *type = (TypeObject *)ty; TypeObject *type = (TypeObject *)ty;
Py_ssize_t n_hypos = PyList_Size(type->hypos); Py_ssize_t n_hypos;
PgfTypeHypo hypos[n_hypos]; PgfTypeHypo *hypos = PyList_AsHypos(type->hypos, &n_hypos);
// PgfTypeHypo *hypos = alloca(sizeof(PgfTypeHypo)*n_hypos); if (PyErr_Occurred())
for (Py_ssize_t i = 0; i < n_hypos; i++) { return 0;
PyObject *hytup = (PyObject *)PyList_GetItem(type->hypos, i);
hypos[i].bind_type = PyLong_AsLong(PyTuple_GetItem(hytup, 0));
hypos[i].cid = PyUnicode_AsPgfText(PyTuple_GetItem(hytup, 1));
hypos[i].type = (PgfType) PyTuple_GetItem(hytup, 2);
Py_INCREF(hypos[i].type);
}
PgfText *cat = PyUnicode_AsPgfText(type->cat); PgfText *cat = PyUnicode_AsPgfText(type->cat);
if (cat == NULL) { if (cat == NULL) {
@@ -340,7 +379,6 @@ match_type(PgfMarshaller *this, PgfUnmarshaller *u, PgfType ty)
Py_ssize_t n_exprs = PyList_Size(type->exprs); Py_ssize_t n_exprs = PyList_Size(type->exprs);
PgfExpr exprs[n_exprs]; PgfExpr exprs[n_exprs];
// PgfExpr *exprs = alloca(sizeof(PgfExpr)*n_exprs);
for (Py_ssize_t i = 0; i < n_exprs; i++) { for (Py_ssize_t i = 0; i < n_exprs; i++) {
exprs[i] = (PgfExpr)PyList_GetItem(type->exprs, i); exprs[i] = (PgfExpr)PyList_GetItem(type->exprs, i);
Py_INCREF(exprs[i]); Py_INCREF(exprs[i]);

View File

@@ -18,6 +18,8 @@ PgfExnType handleError(PgfExn err);
PgfText *PyUnicode_AsPgfText(PyObject *pystr); PgfText *PyUnicode_AsPgfText(PyObject *pystr);
PyObject *PyUnicode_FromPgfText(PgfText *text); PyObject *PyUnicode_FromPgfText(PgfText *text);
PgfTypeHypo *PyList_AsHypos(PyObject *pylist, Py_ssize_t *n_hypos);
PgfUnmarshaller unmarshaller; PgfUnmarshaller unmarshaller;
PgfMarshaller marshaller; PgfMarshaller marshaller;

View File

@@ -7,24 +7,25 @@ prob = math.pi
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def gr1(): def gr1():
return readPGF("../haskell/tests/basic.pgf") gr = readPGF("../haskell/tests/basic.pgf")
yield gr
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def gr2(gr1): def gr2():
t = gr1.newTransaction() gr = readPGF("../haskell/tests/basic.pgf")
t.createFunction("foo", ty, 0, prob), t = gr.newTransaction()
t.createFunction("foo", ty, 0, prob)
t.createCategory("Q", [(BIND_TYPE_EXPLICIT, "x", ty)], prob) t.createCategory("Q", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
assert t.commit() t.commit()
return gr1 yield gr
@pytest.fixture(scope="module") @pytest.fixture(scope="module")
def gr3(): def gr3():
# TODO how to avoid reloading from file? gr = readPGF("../haskell/tests/basic.pgf")
gr1 = readPGF("../haskell/tests/basic.pgf") with gr.newTransaction("bar_branch") as t:
with gr1.newTransaction("bar_branch") as t: t.createFunction("bar", ty, 0, prob)
t.createFunction("bar", ty, 0, prob),
t.createCategory("R", [(BIND_TYPE_EXPLICIT, "x", ty)], prob) t.createCategory("R", [(BIND_TYPE_EXPLICIT, "x", ty)], prob)
return gr1 yield gr
# gr1 # gr1
@@ -67,5 +68,11 @@ def test_extended_function_prob(gr2):
def test_branched_functions(gr3): def test_branched_functions(gr3):
assert gr3.functions == ["bar", "c", "ind", "s", "z"] assert gr3.functions == ["bar", "c", "ind", "s", "z"]
def test_branched_categories(gr3):
assert gr3.categories == ["Float","Int","N","P","R","S","String"]
def test_extended_category_context(gr3):
assert gr3.categoryContext("R") == [(BIND_TYPE_EXPLICIT, "x", ty)]
def test_branched_function_type(gr3): def test_branched_function_type(gr3):
assert gr3.functionType("bar") == ty assert gr3.functionType("bar") == ty

View File

@@ -49,8 +49,9 @@ Transaction_commit(TransactionObject *self, PyObject *args)
pgf_free_revision(self->pgf->db, self->pgf->revision); pgf_free_revision(self->pgf->db, self->pgf->revision);
self->pgf->revision = self->revision; self->pgf->revision = self->revision;
Py_INCREF(self->pgf->db);
Py_RETURN_TRUE; Py_RETURN_NONE;
} }
static PyObject * static PyObject *
@@ -107,23 +108,24 @@ Transaction_createCategory(TransactionObject *self, PyObject *args)
Py_ssize_t size; Py_ssize_t size;
PyObject *hypos; PyObject *hypos;
float prob = 0.0; float prob = 0.0;
if (!PyArg_ParseTuple(args, "s#O!f", &s, &size, &PyList_Type, &hypos, prob)) // if (!PyArg_ParseTuple(args, "s#O!f", &s, &size, &PyList_Type, &hypos, prob)) // segfaults in Python 3.8 but not 3.7
// return NULL;
if (!PyArg_ParseTuple(args, "s#Of", &s, &size, &hypos, prob))
return NULL; return NULL;
if (!PyObject_TypeCheck(hypos, &PyList_Type)) {
PyErr_SetString(PyExc_TypeError, "hypos must be a list");
return NULL;
}
// Py_INCREF(hypos);
PgfText *catname = (PgfText *)PyMem_Malloc(sizeof(PgfText)+size+1); PgfText *catname = (PgfText *)PyMem_Malloc(sizeof(PgfText)+size+1);
memcpy(catname->text, s, size+1); memcpy(catname->text, s, size+1);
catname->size = size; catname->size = size;
Py_ssize_t n_hypos = PyList_Size(hypos); Py_ssize_t n_hypos;
// PgfTypeHypo context[n_hypos]; PgfTypeHypo *context = PyList_AsHypos(hypos, &n_hypos);
PgfTypeHypo *context = alloca(sizeof(PgfTypeHypo)*n_hypos); if (PyErr_Occurred())
for (Py_ssize_t i = 0; i < n_hypos; i++) { return NULL;
PyObject *hytup = (PyObject *)PyList_GetItem(hypos, i);
context[i].bind_type = PyLong_AsLong(PyTuple_GetItem(hytup, 0));
context[i].cid = PyUnicode_AsPgfText(PyTuple_GetItem(hytup, 1));
context[i].type = (PgfType) PyTuple_GetItem(hytup, 2);
Py_INCREF(context[i].type);
}
PgfExn err; PgfExn err;
pgf_create_category(self->pgf->db, self->revision, catname, n_hypos, context, prob, &marshaller, &err); pgf_create_category(self->pgf->db, self->revision, catname, n_hypos, context, prob, &marshaller, &err);
@@ -160,35 +162,51 @@ Transaction_dropCategory(TransactionObject *self, PyObject *args)
static TransactionObject * static TransactionObject *
Transaction_enter(TransactionObject *self, PyObject *Py_UNUSED(ignored)) Transaction_enter(TransactionObject *self, PyObject *Py_UNUSED(ignored))
{ {
Py_INCREF(self);
return self; return self;
} }
static PyObject *
Transaction_exit_impl(TransactionObject *self, PyObject *exc_type, PyObject *exc_value, PyObject *exc_tb)
{
if (exc_type == Py_None && exc_value == Py_None && exc_tb == Py_None) {
return Transaction_commit(self, NULL);
} else {
PyErr_SetObject(exc_type, exc_value);
return NULL;
}
}
// cpython/Modules/_multiprocessing/clinic/semaphore.c.h
// cpython/Modules/_sqlite/connection.c
static PyObject * static PyObject *
Transaction_exit(TransactionObject *self, PyObject *const *args, Py_ssize_t nargs) Transaction_exit(TransactionObject *self, PyObject *const *args, Py_ssize_t nargs)
{ {
// PyObject *exc_type = Py_None; PyObject *return_value = NULL;
// PyObject *exc_value = Py_None; PyObject *exc_type = Py_None;
// PyObject *exc_tb = Py_None; PyObject *exc_value = Py_None;
PyObject *exc_tb = Py_None;
// if (!_PyArg_CheckPositional("__exit__", nargs, 0, 3)) { if (nargs < 0 || nargs > 3) {
// Py_RETURN_FALSE; goto exit;
// } }
if (nargs < 1) { if (nargs < 1) {
goto skip_optional; goto skip_optional;
} }
// exc_type = args[0]; exc_type = args[0];
// if (nargs < 2) { if (nargs < 2) {
// goto skip_optional; goto skip_optional;
// } }
// exc_value = args[1]; exc_value = args[1];
// if (nargs < 3) { if (nargs < 3) {
// goto skip_optional; goto skip_optional;
// } }
// exc_tb = args[2]; exc_tb = args[2];
skip_optional: skip_optional:
// TODO check exception return_value = Transaction_exit_impl(self, exc_type, exc_value, exc_tb);
return Transaction_commit(self, NULL); exit:
return return_value;
} }
// static void // static void