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 3bd40dbab6
commit 35c11d5f5a

View File

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