Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

gh-102356: Add trashcan macros to filter object dealloc #102426

Merged
merged 3 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -926,6 +926,16 @@ def test_filter_pickle(self):
f2 = filter(filter_char, "abcdeabcde")
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):
self.assertTrue(getattr(sys, 'stdout') is sys.stdout)
self.assertRaises(TypeError, getattr)
Expand Down
1 change: 1 addition & 0 deletions Misc/ACKS
Original file line number Diff line number Diff line change
Expand Up @@ -637,6 +637,7 @@ Tim Golden
Yonatan Goldschmidt
Mark Gollahon
Mikhail Golubev
Marta Gómez Macías
Guilherme Gonçalves
Tiago Gonçalves
Chris Gonnerman
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix a bug that caused a crash when deallocating deeply nested filter
objects. Patch by Marta Gómez Macías.
2 changes: 2 additions & 0 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -553,9 +553,11 @@ static void
filter_dealloc(filterobject *lz)
{
PyObject_GC_UnTrack(lz);
Py_TRASHCAN_BEGIN(lz, filter_dealloc)
Py_XDECREF(lz->func);
Py_XDECREF(lz->it);
Py_TYPE(lz)->tp_free(lz);
Py_TRASHCAN_END
}

static int
Expand Down