From: Benjamin Peterson Date: Tue, 30 Sep 2014 02:46:57 +0000 (-0400) Subject: clear BufferedRWPair weakrefs on deallocation (closes #22517) X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=1c873bf77d855e821d7c1b4d9a2489b2a1b85f5f;p=python clear BufferedRWPair weakrefs on deallocation (closes #22517) --- diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index af3b90ab80..2a3b4a3b14 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -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 diff --git a/Misc/NEWS b/Misc/NEWS index 47de844425..888abf5a10 100644 --- 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. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index 758edf4208..62a350b691 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -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);