]> granicus.if.org Git - python/commitdiff
Issue #22517: When a io.BufferedRWPair object is deallocated, clear its
authorGeorg Brandl <georg@python.org>
Tue, 30 Sep 2014 12:54:39 +0000 (14:54 +0200)
committerGeorg Brandl <georg@python.org>
Tue, 30 Sep 2014 12:54:39 +0000 (14:54 +0200)
weakrefs.

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

index 8ef314e55d20ea3073197af75d4616a19f349fb0..72a844feac1767b7bcc282df9e9bdd9979283bf5 100644 (file)
@@ -1454,6 +1454,12 @@ class BufferedRWPairTest(unittest.TestCase):
         pair = self.tp(SelectableIsAtty(True), SelectableIsAtty(True))
         self.assertTrue(pair.isatty())
 
+    def test_weakref_clearing(self):
+        brw = self.tp(self.MockRawIO(), self.MockRawIO())
+        ref = weakref.ref(brw)
+        brw = None
+        ref = None # Shouldn't segfault.
+
 class CBufferedRWPairTest(BufferedRWPairTest):
     tp = io.BufferedRWPair
 
index 44b0e96410fe83649387f3e9b703c9a320f09457..c6df72b0bb04d50a76b14c1658d1440a40441658 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -10,6 +10,9 @@ What's New in Python 3.2.6?
 Library
 -------
 
+- Issue #22517: When a io.BufferedRWPair object is deallocated, clear its
+  weakrefs.
+
 - Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to
   prevent readline() calls from consuming too much memory.  Patch by Jyrki
   Pulliainen.
index a8278d466ce1d25bc8c4a7458bf1cbe5a25d5824..f788e5cedec8408b0ebebd294917c4b9b6f24d9a 100644 (file)
@@ -2141,6 +2141,8 @@ static void
 bufferedrwpair_dealloc(rwpair *self)
 {
     _PyObject_GC_UNTRACK(self);
+    if (self->weakreflist != NULL)
+        PyObject_ClearWeakRefs((PyObject *)self);
     Py_CLEAR(self->reader);
     Py_CLEAR(self->writer);
     Py_CLEAR(self->dict);