]> granicus.if.org Git - python/commitdiff
Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
authorCharles-François Natali <neologix@free.fr>
Thu, 6 Oct 2011 17:09:45 +0000 (19:09 +0200)
committerCharles-François Natali <neologix@free.fr>
Thu, 6 Oct 2011 17:09:45 +0000 (19:09 +0200)
would be finalized after the reference to its underlying BufferedRWPair's
writer got cleared by the GC.

Lib/test/test_io.py
Misc/NEWS
Modules/_io/bufferedio.c

index 608fc414ca1fccabf5399946c846a279863893d6..003a4f34bada2045e21397bcf5eba587f25c851d 100644 (file)
@@ -2338,6 +2338,21 @@ class CTextIOWrapperTest(TextIOWrapperTest):
         with self.open(support.TESTFN, "rb") as f:
             self.assertEqual(f.read(), b"456def")
 
+    def test_rwpair_cleared_before_textio(self):
+        # Issue 13070: TextIOWrapper's finalization would crash when called
+        # after the reference to the underlying BufferedRWPair's writer got
+        # cleared by the GC.
+        for i in range(1000):
+            b1 = self.BufferedRWPair(self.MockRawIO(), self.MockRawIO())
+            t1 = self.TextIOWrapper(b1, encoding="ascii")
+            b2 = self.BufferedRWPair(self.MockRawIO(), self.MockRawIO())
+            t2 = self.TextIOWrapper(b2, encoding="ascii")
+            # circular references
+            t1.buddy = t2
+            t2.buddy = t1
+        support.gc_collect()
+
+
 class PyTextIOWrapperTest(TextIOWrapperTest):
     pass
 
index 71a346d6e9d03a521c21a41042ac62f9ebf2a4ea..d89ef68e70f80486bef4aa194e85c390915bf036 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -217,6 +217,10 @@ Library
 Extension Modules
 -----------------
 
+- Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
+  would be finalized after the reference to its underlying BufferedRWPair's
+  writer got cleared by the GC.
+
 - Issue #12881: ctypes: Fix segfault with large structure field names.
 
 - Issue #13013: ctypes: Fix a reference leak in PyCArrayType_from_ctype.
index 73f924e8404e5c29ed7045dac906e419c9ef6356..1b81647b49d1708fc29ce3ba8898967d6eeb55b9 100644 (file)
@@ -2179,6 +2179,11 @@ bufferedrwpair_isatty(rwpair *self, PyObject *args)
 static PyObject *
 bufferedrwpair_closed_get(rwpair *self, void *context)
 {
+    if (self->writer == NULL) {
+        PyErr_SetString(PyExc_RuntimeError,
+                "the BufferedRWPair object is being garbage-collected");
+        return NULL;
+    }
     return PyObject_GetAttr((PyObject *) self->writer, _PyIO_str_closed);
 }