From bbd0a323aeb1222d216fa10a9e1d9c91945ad1e6 Mon Sep 17 00:00:00 2001 From: Benjamin Peterson Date: Mon, 29 Sep 2014 22:46:57 -0400 Subject: [PATCH] clear BufferedRWPair weakrefs on deallocation (closes #22517) --- 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 2a96b7b8e79..4b49d33a0b1 100644 --- a/Lib/test/test_io.py +++ b/Lib/test/test_io.py @@ -1524,6 +1524,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 0df66795ee3..890c25103e0 100644 --- a/Misc/NEWS +++ b/Misc/NEWS @@ -17,6 +17,9 @@ Core and Builtins 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 index c28e2892778..ea0302bfd3d 100644 --- a/Modules/_io/bufferedio.c +++ b/Modules/_io/bufferedio.c @@ -2254,6 +2254,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);