1
0
forked from GitHub/gf-core

fix the reference counting for callbacks in Python

This commit is contained in:
kr.angelov
2015-01-05 11:24:00 +00:00
parent 34e2a1b82d
commit 6acf100ab1

View File

@@ -1212,16 +1212,21 @@ pypgf_literal_callback_match(PgfLiteralCallback* self,
PyObject* result =
PyObject_CallFunction(callback->pycallback, "isi",
lin_idx, sentence, *poffset);
if (result == NULL || result == Py_None)
if (result == NULL)
return NULL;
if (result == Py_None) {
Py_DECREF(result);
return NULL;
}
PgfExprProb* ep = gu_new(PgfExprProb, out_pool);
ExprObject* pyexpr;
if (!PyArg_ParseTuple(result, "Ofi", &pyexpr, &ep->prob, poffset))
return NULL;
ep->expr = pyexpr->expr;
{
// This is an uggly hack. We first show the expression ep->expr
// and then we read it back but in out_pool. The whole purpose
@@ -1251,7 +1256,7 @@ pypgf_literal_callback_match(PgfLiteralCallback* self,
gu_pool_free(tmp_pool);
}
Py_DECREF(pyexpr);
Py_DECREF(result);
return ep;
}
@@ -1300,6 +1305,8 @@ pypgf_new_callbacks_map(PgfConcr* concr, PyObject *py_callbacks,
callback->pycallback = pycallback;
callback->fin.fn = pypgf_literal_callback_fin;
Py_XINCREF(callback->pycallback);
gu_pool_finally(pool, &callback->fin);
pgf_callbacks_map_add_literal(concr, callbacks,