changeset: 92644:9b4673d7b046 branch: 2.7 parent: 92636:d9cd11eda152 user: Benjamin Peterson date: Mon Sep 29 22:46:57 2014 -0400 files: Lib/test/test_io.py Misc/NEWS Modules/_io/bufferedio.c description: clear BufferedRWPair weakrefs on deallocation (closes #22517) diff -r d9cd11eda152 -r 9b4673d7b046 Lib/test/test_io.py --- a/Lib/test/test_io.py Mon Sep 29 19:01:18 2014 -0400 +++ b/Lib/test/test_io.py Mon Sep 29 22:46:57 2014 -0400 @@ -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 -r d9cd11eda152 -r 9b4673d7b046 Misc/NEWS --- a/Misc/NEWS Mon Sep 29 19:01:18 2014 -0400 +++ b/Misc/NEWS Mon Sep 29 22:46:57 2014 -0400 @@ -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 -r d9cd11eda152 -r 9b4673d7b046 Modules/_io/bufferedio.c --- a/Modules/_io/bufferedio.c Mon Sep 29 19:01:18 2014 -0400 +++ b/Modules/_io/bufferedio.c Mon Sep 29 22:46:57 2014 -0400 @@ -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);