summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorkr.angelov <kr.angelov@gmail.com>2015-01-05 11:24:00 +0000
committerkr.angelov <kr.angelov@gmail.com>2015-01-05 11:24:00 +0000
commit35c11d5f5a2cffa40ef5a5dc2d6ba962b7df3cce (patch)
treec44e50ae9cfcca523dd36c49a63646f6790094dc /src
parent3bd40dbab68c8354d8cfceb6dad32d24b13bc723 (diff)
fix the reference counting for callbacks in Python
Diffstat (limited to 'src')
-rw-r--r--src/runtime/python/pypgf.c13
1 files changed, 10 insertions, 3 deletions
diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c
index c38a483f7..4725695ff 100644
--- a/src/runtime/python/pypgf.c
+++ b/src/runtime/python/pypgf.c
@@ -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,