mirror of https://github.com/python/cpython
gh-102356: Add thrashcan macros to filter object dealloc (#102426)
Add thrashcan macros to the deallocator of the filter objects to protect against deeply nested destruction of chains of nested filters.
This commit is contained in:
parent
5da379ca7d
commit
66aa78cbe6
|
@ -926,6 +926,16 @@ class BuiltinTest(unittest.TestCase):
|
||||||
f2 = filter(filter_char, "abcdeabcde")
|
f2 = filter(filter_char, "abcdeabcde")
|
||||||
self.check_iter_pickle(f1, list(f2), proto)
|
self.check_iter_pickle(f1, list(f2), proto)
|
||||||
|
|
||||||
|
def test_filter_dealloc(self):
|
||||||
|
# Tests recursive deallocation of nested filter objects using the
|
||||||
|
# thrashcan mechanism. See gh-102356 for more details.
|
||||||
|
max_iters = 1000000
|
||||||
|
i = filter(bool, range(max_iters))
|
||||||
|
for _ in range(max_iters):
|
||||||
|
i = filter(bool, i)
|
||||||
|
del i
|
||||||
|
gc.collect()
|
||||||
|
|
||||||
def test_getattr(self):
|
def test_getattr(self):
|
||||||
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
|
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
|
||||||
self.assertRaises(TypeError, getattr)
|
self.assertRaises(TypeError, getattr)
|
||||||
|
|
|
@ -637,6 +637,7 @@ Tim Golden
|
||||||
Yonatan Goldschmidt
|
Yonatan Goldschmidt
|
||||||
Mark Gollahon
|
Mark Gollahon
|
||||||
Mikhail Golubev
|
Mikhail Golubev
|
||||||
|
Marta Gómez Macías
|
||||||
Guilherme Gonçalves
|
Guilherme Gonçalves
|
||||||
Tiago Gonçalves
|
Tiago Gonçalves
|
||||||
Chris Gonnerman
|
Chris Gonnerman
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
Fix a bug that caused a crash when deallocating deeply nested filter
|
||||||
|
objects. Patch by Marta Gómez Macías.
|
|
@ -553,9 +553,11 @@ static void
|
||||||
filter_dealloc(filterobject *lz)
|
filter_dealloc(filterobject *lz)
|
||||||
{
|
{
|
||||||
PyObject_GC_UnTrack(lz);
|
PyObject_GC_UnTrack(lz);
|
||||||
|
Py_TRASHCAN_BEGIN(lz, filter_dealloc)
|
||||||
Py_XDECREF(lz->func);
|
Py_XDECREF(lz->func);
|
||||||
Py_XDECREF(lz->it);
|
Py_XDECREF(lz->it);
|
||||||
Py_TYPE(lz)->tp_free(lz);
|
Py_TYPE(lz)->tp_free(lz);
|
||||||
|
Py_TRASHCAN_END
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
|
|
Loading…
Reference in New Issue