diff Objects/typeobject.c @ 73980:da1d7f8cd487 2.7

Fix the fix for issue #12149: it was incorrect, although it had the side effect of appearing to resolve the issue. Thanks to Mark Shannon for noticing.
author Antoine Pitrou <solipsis@pitrou.net>
date Thu, 15 Dec 2011 14:15:31 +0100
parents 369487785e9f
children c7623da4e2af
line wrap: on
line diff
--- a/Objects/typeobject.c	Wed Dec 14 15:05:42 2011 -0800
+++ b/Objects/typeobject.c	Thu Dec 15 14:15:31 2011 +0100
@@ -1013,8 +1013,6 @@
     assert(basedealloc);
     basedealloc(self);
 
-    PyType_Modified(type);
-
     /* Can't reference self beyond this point */
     Py_DECREF(type);
 
@@ -2707,15 +2705,16 @@
        for heaptypes. */
     assert(type->tp_flags & Py_TPFLAGS_HEAPTYPE);
 
-    /* The only field we need to clear is tp_mro, which is part of a
-       hard cycle (its first element is the class itself) that won't
-       be broken otherwise (it's a tuple and tuples don't have a
+    /* We need to invalidate the method cache carefully before clearing
+       the dict, so that other objects caught in a reference cycle
+       don't start calling destroyed methods.
+
+       Otherwise, the only field we need to clear is tp_mro, which is
+       part of a hard cycle (its first element is the class itself) that
+       won't be broken otherwise (it's a tuple and tuples don't have a
        tp_clear handler).  None of the other fields need to be
        cleared, and here's why:
 
-       tp_dict:
-           It is a dict, so the collector will call its tp_clear.
-
        tp_cache:
            Not used; if it were, it would be a dict.
 
@@ -2732,6 +2731,9 @@
            A tuple of strings can't be part of a cycle.
     */
 
+    PyType_Modified(type);
+    if (type->tp_dict)
+        PyDict_Clear(type->tp_dict);
     Py_CLEAR(type->tp_mro);
 
     return 0;