]> 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>
Wed, 5 Oct 2011 17:53:43 +0000 (19:53 +0200)
committerCharles-François Natali <neologix@free.fr>
Wed, 5 Oct 2011 17:53:43 +0000 (19:53 +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 53cabbbd1598e11dd051830a132a43e9cd6098b1..eb2ac5fe99d82499e94cd118aa12cdf98ee01750 100644 (file)
@@ -2414,6 +2414,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 6b558b49e84dbdc1fcb81030b1968733c57f7ea8..c4aba0dfea50f9cd455f1abf9b37c550e9c1f379 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -98,6 +98,10 @@ Tests
 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 #13058: ossaudiodev: fix a file descriptor leak on error. Patch by
index bd1aae5ab6c1d0f3b3a8fb9490560b13878e93e4..c979ac2336d19f3742eb9afecc13a95637ff07ce 100644 (file)
@@ -2212,6 +2212,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);
 }