]> 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 2a96b7b8e7934106ffca659768dc31b4897ba286..4b49d33a0b14008f14b56f3efe311d9197665806 100644 (file)
@@ -1524,6 +1524,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 0df66795ee364ec1e753fc2299f08402681e5c69..890c25103e041b839e2a5bb5d8668636a1ea57df 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@ Core and Builtins
 Library
 -------
 
+- Issue #22517: When a io.BufferedRWPair object is deallocated, clear its
+  weakrefs.
+
 - Issue #22419: Limit the length of incoming HTTP request in wsgiref server to
   65536 bytes and send a 414 error code for higher lengths. Patch contributed
   by Devin Cook.
index c28e28927781633864908161715cdced13623c6f..ea0302bfd3d74f280338ea6b37bf6a5592dcf1f1 100644 (file)
@@ -2254,6 +2254,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);