]> granicus.if.org Git - python/commitdiff
clear BufferedRWPair weakrefs on deallocation (closes #22517)
authorBenjamin Peterson <benjamin@python.org>
Tue, 30 Sep 2014 02:46:57 +0000 (22:46 -0400)
committerBenjamin Peterson <benjamin@python.org>
Tue, 30 Sep 2014 02:46:57 +0000 (22:46 -0400)
Lib/test/test_io.py
Misc/NEWS
Modules/_io/bufferedio.c

index af3b90ab80af1616eb26db9dca929d14d82570d8..2a3b4a3b14a32f44972ade9f25aae2949d0f80b7 100644 (file)
@@ -1474,6 +1474,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 47de8444256781c82c692bc9c2653ca2e6756be9..888abf5a10800eec35f3a309ca9247277e5d9d8b 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -29,6 +29,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #22517: When a io.BufferedRWPair object is deallocated, clear its
+  weakrefs.
+
 - Issue #10510: distutils register and upload methods now use HTML standards
   compliant CRLF line endings.
 
index 758edf4208718d50387ac401e1fc6c09570d33f7..62a350b691f1a74e08f64e59613600473d7f4fef 100644 (file)
@@ -2120,6 +2120,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);