Add helper function for checking and converting list of hypos

This commit is contained in:
John J. Camilleri
2021-09-22 15:37:33 +02:00
parent 21ee96da9b
commit 4611d831ff
3 changed files with 57 additions and 22 deletions

View File

@@ -116,21 +116,16 @@ Transaction_createCategory(TransactionObject *self, PyObject *args)
PyErr_SetString(PyExc_TypeError, "hypos must be a list");
return NULL;
}
// Py_INCREF(hypos);
PgfText *catname = (PgfText *)PyMem_Malloc(sizeof(PgfText)+size+1);
memcpy(catname->text, s, size+1);
catname->size = size;
Py_ssize_t n_hypos = PyList_Size(hypos);
PgfTypeHypo context[n_hypos];
// PgfTypeHypo *context = alloca(sizeof(PgfTypeHypo)*n_hypos);
for (Py_ssize_t i = 0; i < n_hypos; i++) {
PyObject *hytup = 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);
}
Py_ssize_t n_hypos;
PgfTypeHypo *context = PyList_AsHypos(hypos, &n_hypos);
if (PyErr_Occurred())
return NULL;
PgfExn err;
pgf_create_category(self->pgf->db, self->revision, catname, n_hypos, context, prob, &marshaller, &err);