Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/pya/pya/pyaCallables.cc
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,11 @@ namespace pya
void
pya_object_deallocate (PyObject *self)
{
// Clear weak refs - needed for signal binding and other purposes
#if PY_VERSION_HEX > 0x03020000
PyObject_ClearWeakRefs (self);
#endif

// This avoids an assertion in debug builds (Python, gcmodule.c - update_refs).
// In short, the GC expects not to see objects with refcount 0 and asserts.
// However, due to triggering of signals or similar, the destructor call below
Expand Down
5 changes: 5 additions & 0 deletions src/pya/pya/pyaModule.cc
Original file line number Diff line number Diff line change
Expand Up @@ -337,6 +337,11 @@ class PythonClassGenerator
#if PY_VERSION_HEX >= 0x030D0000
// crashes with this option set
type->tp_flags &= ~Py_TPFLAGS_INLINE_VALUES;
#endif
#if PY_VERSION_HEX < 0x03000000
type->tp_flags |= Py_TPFLAGS_HAVE_WEAKREFS;
type->tp_weaklistoffset = type->tp_basicsize;
type->tp_basicsize += sizeof (PyObject *);
#endif
type->tp_basicsize += sizeof (PYAObjectBase);
type->tp_init = &pya_object_init;
Expand Down
15 changes: 15 additions & 0 deletions testdata/python/basic.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import sys
import gc
import copy
import weakref

# Set this to True to disable some tests involving exceptions
leak_check = "TEST_LEAK_CHECK" in os.environ
Expand Down Expand Up @@ -3352,6 +3353,20 @@ def test_93(self):
self.assertEqual(bc.str(), "xyz")
self.assertEqual(bnc.str(), "xyz")

# weak refs
def test_94(self):

b = pya.B()
b.set_str("abc")

r = weakref.ref(b)
self.assertEqual(r() is None, False)
self.assertEqual(r().str(), "abc")

b = None
self.assertEqual(r() is None, True)


# run unit tests
if __name__ == '__main__':
suite = unittest.TestSuite()
Expand Down
Loading