aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--libshiboken/basewrapper.cpp8
-rw-r--r--libshiboken/basewrapper.h2
-rwxr-xr-xtests/samplebinding/weakref_test.py49
3 files changed, 58 insertions, 1 deletions
diff --git a/libshiboken/basewrapper.cpp b/libshiboken/basewrapper.cpp
index 512722b84..2eaff65cb 100644
--- a/libshiboken/basewrapper.cpp
+++ b/libshiboken/basewrapper.cpp
@@ -132,6 +132,7 @@ PyObject* SbkBaseWrapper_New(SbkBaseWrapperType* instanceType,
self->validCppObject = 1;
self->parentInfo = 0;
self->ob_dict = 0;
+ self->weakreflist = 0;
BindingManager::instance().registerWrapper(self);
return reinterpret_cast<PyObject*>(self);
}
@@ -146,6 +147,7 @@ PyObject* SbkBaseWrapper_TpNew(PyTypeObject* subtype, PyObject*, PyObject*)
self->validCppObject = 0;
self->parentInfo = 0;
self->ob_dict = 0;
+ self->weakreflist = 0;
return reinterpret_cast<PyObject*>(self);
}
@@ -159,6 +161,10 @@ bool cppObjectIsInvalid(PyObject* wrapper)
void SbkBaseWrapper_Dealloc_PrivateDtor(PyObject* self)
{
+
+ if (((SbkBaseWrapper *)self)->weakreflist)
+ PyObject_ClearWeakRefs(self);
+
BindingManager::instance().releaseWrapper(self);
Py_TYPE(reinterpret_cast<SbkBaseWrapper*>(self))->tp_free(self);
}
@@ -265,7 +271,7 @@ SbkBaseWrapperType SbkBaseWrapper_Type = { { {
/*tp_traverse*/ 0,
/*tp_clear*/ 0,
/*tp_richcompare*/ 0,
- /*tp_weaklistoffset*/ 0,
+ /*tp_weaklistoffset*/ offsetof(SbkBaseWrapper, weakreflist),
/*tp_iter*/ 0,
/*tp_iternext*/ 0,
/*tp_methods*/ 0,
diff --git a/libshiboken/basewrapper.h b/libshiboken/basewrapper.h
index 32a978f51..381562e39 100644
--- a/libshiboken/basewrapper.h
+++ b/libshiboken/basewrapper.h
@@ -101,6 +101,8 @@ struct LIBSHIBOKEN_API SbkBaseWrapper
unsigned int validCppObject : 1;
/// Information about the object parents and children, can be null.
ShiboParentInfo* parentInfo;
+ /// List of weak references
+ PyObject *weakreflist;
};
LIBSHIBOKEN_API PyAPI_FUNC(void) init_shiboken();
diff --git a/tests/samplebinding/weakref_test.py b/tests/samplebinding/weakref_test.py
new file mode 100755
index 000000000..bdf2a479a
--- /dev/null
+++ b/tests/samplebinding/weakref_test.py
@@ -0,0 +1,49 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+#
+# This file is part of the Shiboken Python Bindings Generator project.
+#
+# Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
+#
+# Contact: PySide team <contact@pyside.org>
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU Lesser General Public License
+# version 2.1 as published by the Free Software Foundation. Please
+# review the following information to ensure the GNU Lesser General
+# Public License version 2.1 requirements will be met:
+# http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
+# #
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# Lesser General Public License for more details.
+#
+# You should have received a copy of the GNU Lesser General Public
+# License along with this program; if not, write to the Free Software
+# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+# 02110-1301 USA
+
+'''Test weakref support'''
+
+import weakref
+import unittest
+
+from sample import ObjectType
+
+
+class WeakrefBasicTest(unittest.TestCase):
+ '''Simple test case of using a weakref'''
+
+ def cb(self, *args):
+ print 'callback', args
+ self.called = True
+
+ def testBasic(self):
+ '''ObjectType weakref'''
+ obj = ObjectType()
+ ref = weakref.ref(obj, self.cb)
+
+
+if __name__ == '__main__':
+ unittest.main()