Skip to content

gh-130070: Fix exec(<string>, closure=<non-None>) unexpected path #130071

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

Merged
merged 8 commits into from
Apr 17, 2025
Merged
Show file tree
Hide file tree
Changes from 5 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
16 changes: 16 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -1013,8 +1013,24 @@ def four_freevars():
three_freevars.__code__,
three_freevars.__globals__,
closure=my_closure)
my_closure = tuple(my_closure)

# should fail: anything passed to closure= isn't allowed
# when the source is a string
self.assertRaises(TypeError,
exec,
"pass",
closure=int)

# should fail: correct closure= argument isn't allowed
# when the source is a string
self.assertRaises(TypeError,
exec,
"pass",
closure=my_closure)

# should fail: closure tuple with one non-cell-var
my_closure = list(my_closure)
my_closure[0] = int
my_closure = tuple(my_closure)
self.assertRaises(TypeError,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
``exec()`` called with a string ``source`` argument and non-``None`` ``closure`` argument no longer fails with a segmentation
fault. Patch by Bartosz Sławecki.
1 change: 1 addition & 0 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -1175,6 +1175,7 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
if (closure != NULL) {
PyErr_SetString(PyExc_TypeError,
"closure can only be used when source is a code object");
goto error;
}
PyObject *source_copy;
const char *str;
Expand Down
Loading