From 21bf3f942be920f3b051f6af43f7c37b9aa5cff3 Mon Sep 17 00:00:00 2001 From: Georg Brandl Date: Tue, 30 Sep 2014 14:54:39 +0200 Subject: [PATCH] Issue #22517: When a io.BufferedRWPair object is deallocated, clear its weakrefs. --- Lib/test/test_io.py | 6 ++++++ Misc/NEWS | 3 +++ Modules/_io/bufferedio.c | 2 ++ 3 files changed, 11 insertions(+) diff --git a/Lib/test/test_io.py b/Lib/test/test_io.py index 8ef314e55d..72a844feac 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1454,6 +1454,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 44b0e96410..c6df72b0bb 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -10,6 +10,9 @@ What's New in Python 3.2.6? Library ------- +- Issue #22517: When a io.BufferedRWPair object is deallocated, clear its + weakrefs. + - Issue #16041: CVE-2013-1752: poplib: Limit maximum line lengths to 2048 to prevent readline() calls from consuming too much memory. Patch by Jyrki Pulliainen. diff --git a/Modules/_io/bufferedio.c b/Modules/_io/bufferedio.c index a8278d466c..f788e5cede 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -2141,6 +2141,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); -- 2.40.0