From 2cf20fde1b2f3494b8bd12e73fb9ccc17959f536 Mon Sep 17 00:00:00 2001 From: Campbell Barton Date: Fri, 25 Feb 2011 14:32:35 +0000 Subject: patch [#26215] Python weak reference (weakref) support for game objects by Alex Fraser (z0r) --- source/gameengine/Expressions/PyObjectPlus.cpp | 37 +++++++++++++++++++------- 1 file changed, 27 insertions(+), 10 deletions(-) (limited to 'source/gameengine/Expressions/PyObjectPlus.cpp') diff --git a/source/gameengine/Expressions/PyObjectPlus.cpp b/source/gameengine/Expressions/PyObjectPlus.cpp index a3c1e24cdf6..84d6a1cba4b 100644 --- a/source/gameengine/Expressions/PyObjectPlus.cpp +++ b/source/gameengine/Expressions/PyObjectPlus.cpp @@ -108,19 +108,26 @@ void PyObjectPlus::InvalidateProxy() // check typename of each parent PyTypeObject PyObjectPlus::Type = { PyVarObject_HEAD_INIT(NULL, 0) - "PyObjectPlus", /*tp_name*/ + "PyObjectPlus", /*tp_name*/ sizeof(PyObjectPlus_Proxy), /*tp_basicsize*/ - 0, /*tp_itemsize*/ + 0, /*tp_itemsize*/ /* methods */ - py_base_dealloc, - 0, - 0, - 0, + py_base_dealloc, /* tp_dealloc */ + 0, /* printfunc tp_print; */ + 0, /* getattrfunc tp_getattr; */ + 0, /* setattrfunc tp_setattr; */ + 0, /* tp_compare */ /* DEPRECATED in python 3.0! */ + py_base_repr, /* tp_repr */ + 0,0,0,0,0,0,0,0,0, /* Method suites for standard classes */ + Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE,/* long tp_flags; */ + 0,0,0,0, + /* weak reference enabler */ +#ifdef USE_WEAKREFS + offsetof(PyObjectPlus_Proxy, in_weakreflist), /* long tp_weaklistoffset; */ +#else 0, - py_base_repr, - 0,0,0,0,0,0,0,0,0, - Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, - 0,0,0,0,0,0,0, +#endif + 0,0, Methods, 0, 0, @@ -209,8 +216,16 @@ PyObject * PyObjectPlus::py_base_new(PyTypeObject *type, PyObject *args, PyObjec return (PyObject *)ret; } +/** + * @param self A PyObjectPlus_Proxy + */ void PyObjectPlus::py_base_dealloc(PyObject *self) // python wrapper { +#ifdef USE_WEAKREFS + if (BGE_PROXY_WKREF(self) != NULL) + PyObject_ClearWeakRefs((PyObject *) self); +#endif + if (BGE_PROXY_PYREF(self)) { PyObjectPlus *self_plus= BGE_PROXY_REF(self); if(self_plus) { @@ -1107,6 +1122,7 @@ PyObject *PyObjectPlus::GetProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, v self->m_proxy = reinterpret_castPyObject_NEW( PyObjectPlus_Proxy, tp); BGE_PROXY_PYOWNS(self->m_proxy) = false; BGE_PROXY_PYREF(self->m_proxy) = true; + BGE_PROXY_WKREF(self->m_proxy) = NULL; } //PyObject_Print(self->m_proxy, stdout, 0); //printf("ref %d\n", self->m_proxy->ob_refcnt); @@ -1127,6 +1143,7 @@ PyObject *PyObjectPlus::NewProxyPlus_Ext(PyObjectPlus *self, PyTypeObject *tp, v BGE_PROXY_PYOWNS(proxy) = py_owns; BGE_PROXY_REF(proxy) = NULL; BGE_PROXY_PTR(proxy) = ptr; + BGE_PROXY_WKREF(self->m_proxy) = NULL; return proxy; } if (self->m_proxy) -- cgit v1.2.3