[Python-checkins] cpython (3.3): clear BufferedRWPair weakrefs on deallocation (closes #22517)

benjamin.peterson python-checkins at python.org
Tue Sep 30 04:49:40 CEST 2014


https://hg.python.org/cpython/rev/e834b32f0422
changeset:   92645:e834b32f0422
branch:      3.3
parent:      92640:226740b14f1c
user:        Benjamin Peterson <benjamin at python.org>
date:        Mon Sep 29 22:46:57 2014 -0400
summary:
  clear BufferedRWPair weakrefs on deallocation (closes #22517)

files:
  Lib/test/test_io.py      |  6 ++++++
  Misc/NEWS                |  3 +++
  Modules/_io/bufferedio.c |  2 ++
  3 files changed, 11 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
@@ -1524,6 +1524,12 @@
         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
 
diff --git a/Misc/NEWS b/Misc/NEWS
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -17,6 +17,9 @@
 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.
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -2254,6 +2254,8 @@
 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);

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list