summaryrefslogtreecommitdiff
path: root/Cython/Compiler/ModuleNode.py
diff options
context:
space:
mode:
Diffstat (limited to 'Cython/Compiler/ModuleNode.py')
-rw-r--r--Cython/Compiler/ModuleNode.py10
1 files changed, 7 insertions, 3 deletions
diff --git a/Cython/Compiler/ModuleNode.py b/Cython/Compiler/ModuleNode.py
index 2b7939e84..2d4144b62 100644
--- a/Cython/Compiler/ModuleNode.py
+++ b/Cython/Compiler/ModuleNode.py
@@ -1475,12 +1475,16 @@ class ModuleNode(Nodes.Node, Nodes.BlockNode):
UtilityCode.load_cached("PyTrashcan", "ExtensionTypes.c"))
code.putln("__Pyx_TRASHCAN_BEGIN(o, %s)" % slot_func_cname)
- # call the user's __dealloc__
- self.generate_usr_dealloc_call(scope, code)
-
if weakref_slot:
+ # We must clean the weakreferences before calling the user's __dealloc__
+ # because if the __dealloc__ releases the GIL, a weakref can be
+ # dereferenced accessing the object in an inconsistent state or
+ # resurrecting it.
code.putln("if (p->__weakref__) PyObject_ClearWeakRefs(o);")
+ # call the user's __dealloc__
+ self.generate_usr_dealloc_call(scope, code)
+
if dict_slot:
code.putln("if (p->__dict__) PyDict_Clear(p->__dict__);")