[Python-checkins] cpython (2.7): 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/9b4673d7b046
changeset:   92644:9b4673d7b046
branch:      2.7
parent:      92636:d9cd11eda152
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
@@ -1474,6 +1474,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
@@ -29,6 +29,9 @@
 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.
 
diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c
--- a/Modules/_io/bufferedio.c
+++ b/Modules/_io/bufferedio.c
@@ -2120,6 +2120,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