]> granicus.if.org Git - python/commitdiff
Issue #12149: Update the method cache after a type's dictionnary gets
authorAntoine Pitrou <solipsis@pitrou.net>
Tue, 12 Jul 2011 20:04:20 +0000 (22:04 +0200)
committerAntoine Pitrou <solipsis@pitrou.net>
Tue, 12 Jul 2011 20:04:20 +0000 (22:04 +0200)
cleared by the garbage collector.  This fixes a segfault when an instance
and its type get caught in a reference cycle, and the instance's
deallocator calls one of the methods on the type (e.g. when subclassing
IOBase).

Diagnosis and patch by Davide Rizzo.

Lib/test/test_io.py
Misc/ACKS
Misc/NEWS
Objects/typeobject.c

index 0bc5800b3340a7d6cbc5f58b4e37f67fbb6cdb19..c423dd67047f8a63a69d3f9a210d71d679590893 100644 (file)
@@ -585,7 +585,24 @@ class IOTest(unittest.TestCase):
         self.assertEqual(rawio.read(2), b"")
 
 class CIOTest(IOTest):
-    pass
+
+    def test_IOBase_finalize(self):
+        # Issue #12149: segmentation fault on _PyIOBase_finalize when both a
+        # class which inherits IOBase and an object of this class are caught
+        # in a reference cycle and close() is already in the method cache.
+        class MyIO(self.IOBase):
+            def close(self):
+                pass
+
+        # create an instance to populate the method cache
+        MyIO()
+        obj = MyIO()
+        obj.obj = obj
+        wr = weakref.ref(obj)
+        del MyIO
+        del obj
+        support.gc_collect()
+        self.assertTrue(wr() is None, wr)
 
 class PyIOTest(IOTest):
     test_array_writes = unittest.skip(
index 709ded3b73555136bcbcc14cfb2be154317f4bb7..665f71fa08dc15623ba709e8d821786554c2d493 100644 (file)
--- a/Misc/ACKS
+++ b/Misc/ACKS
@@ -686,6 +686,7 @@ Armin Rigo
 Nicholas Riley
 Jean-Claude Rimbault
 Juan M. Bello Rivas
+Davide Rizzo
 Anthony Roach
 Mark Roberts
 Jim Robinson
index 97a4acb7ef8aaf24f3ca5ada38a756013f8ac6e6..4aa48aed47c0e0e50136f430d5489e5dfe0c6673 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -9,6 +9,12 @@ What's New in Python 2.7.3?
 Core and Builtins
 -----------------
 
+- Issue #12149: Update the method cache after a type's dictionnary gets
+  cleared by the garbage collector.  This fixes a segfault when an instance
+  and its type get caught in a reference cycle, and the instance's
+  deallocator calls one of the methods on the type (e.g. when subclassing
+  IOBase).  Diagnosis and patch by Davide Rizzo.
+
 - Issue #12501: Adjust callable() warning: callable() is only not supported in
   Python 3.1. callable() is again supported in Python 3.2.
 
index 49c7e071e92302c495927722e0c5dcb867f417bd..8326d073f101e25ee46c69577528003866e82bbe 100644 (file)
@@ -1013,6 +1013,8 @@ subtype_dealloc(PyObject *self)
     assert(basedealloc);
     basedealloc(self);
 
+    PyType_Modified(type);
+
     /* Can't reference self beyond this point */
     Py_DECREF(type);