[Python-checkins] cpython (merge 3.2 -> default): Issue #13070: Fix a crash when a TextIOWrapper caught in a reference cycle
charles-francois.natali
python-checkins at python.org
Wed Oct 5 19:53:35 CEST 2011
http://hg.python.org/cpython/rev/7defc1e5d13a
changeset: 72702:7defc1e5d13a
parent: 72700:5f11621a6f51
parent: 72701:d60c00015f01
user: Charles-François Natali <neologix at free.fr>
date: Wed Oct 05 19:55:56 2011 +0200
summary:
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.
files:
Lib/test/test_io.py | 15 +++++++++++++++
Misc/NEWS | 4 ++++
Modules/_io/bufferedio.c | 5 +++++
3 files changed, 24 insertions(+), 0 deletions(-)
diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py
--- a/Lib/test/test_io.py
+++ b/Lib/test/test_io.py
@@ -2421,6 +2421,21 @@
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
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -1314,6 +1314,10 @@
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
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -2307,6 +2307,11 @@
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);
}
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list