From 84fa796de4e9b5443a2857eae0878bf26c605cc0 Mon Sep 17 00:00:00 2001 From: "kr.angelov" Date: Tue, 29 Jan 2013 09:41:12 +0000 Subject: [PATCH] bugfix in the reference counting for Python --- src/runtime/python/pypgf.c | 22 ++++++++++++++++------ 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/runtime/python/pypgf.c b/src/runtime/python/pypgf.c index 98ca25e3a..997f3d3f7 100644 --- a/src/runtime/python/pypgf.c +++ b/src/runtime/python/pypgf.c @@ -35,6 +35,12 @@ gu2py_string(GuString s) { return PyString_FromStringAndSize(src, len); } +typedef struct { + PyObject_HEAD + GuPool* pool; + PgfPGF* pgf; +} PGFObject; + typedef struct { PyObject_HEAD PyObject* master; @@ -411,6 +417,7 @@ Expr_getattro(ExprObject *self, PyObject *attr_name) { typedef struct { PyObject_HEAD + PGFObject* grammar; GuPool* pool; int max_count; int counter; @@ -422,6 +429,7 @@ ExprIter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) { ExprIterObject* self = (ExprIterObject *)type->tp_alloc(type, 0); if (self != NULL) { + self->grammar = NULL; self->pool = NULL; self->max_count = -1; self->counter = 0; @@ -437,6 +445,8 @@ ExprIter_dealloc(ExprIterObject* self) if (self->pool != NULL) gu_pool_free(self->pool); + Py_XDECREF(self->grammar); + self->ob_type->tp_free((PyObject*)self); } @@ -525,12 +535,6 @@ static PyTypeObject pgf_ExprIterType = { (newfunc) ExprIter_new, /*tp_new */ }; -typedef struct { - PyObject_HEAD - GuPool* pool; - PgfPGF* pgf; -} PGFObject; - typedef struct { PyObject_HEAD PGFObject* grammar; @@ -596,6 +600,9 @@ Concr_parse(ConcrObject* self, PyObject *args, PyObject *keywds) return NULL; } + pyres->grammar = self->grammar; + Py_XINCREF(pyres->grammar); + pyres->pool = gu_new_pool(); pyres->max_count = max_count; pyres->counter = 0; @@ -1040,6 +1047,9 @@ PGF_generate(PGFObject* self, PyObject *args, PyObject *keywds) return NULL; } + pyres->grammar = self; + Py_INCREF(self); + pyres->pool = gu_new_pool(); pyres->max_count = max_count; pyres->counter = 0;