Skip to content

Commit 582d1ef

Browse files
bswcksobolevn
andauthored
[3.13] gh-130070: Fix exec(<string>, closure=<non-None>) unexpected path (GH-130071) (#132627)
gh-130070: Fix `exec(<string>, closure=<non-None>)` unexpected path (#130071) Fixed an assertion error (so, it could be reproduced only in builds with assertions enabled) for `exec` when the `source` argument is a string and the `closure` argument is not `None`. Co-authored-by: sobolevn <mail@sobolevn.me> (cherry picked from commit 954b2cf)
1 parent db7ad1c commit 582d1ef

File tree

3 files changed

+18
-0
lines changed

3 files changed

+18
-0
lines changed

Lib/test/test_builtin.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,8 +974,24 @@ def four_freevars():
974974
three_freevars.__code__,
975975
three_freevars.__globals__,
976976
closure=my_closure)
977+
my_closure = tuple(my_closure)
978+
979+
# should fail: anything passed to closure= isn't allowed
980+
# when the source is a string
981+
self.assertRaises(TypeError,
982+
exec,
983+
"pass",
984+
closure=int)
985+
986+
# should fail: correct closure= argument isn't allowed
987+
# when the source is a string
988+
self.assertRaises(TypeError,
989+
exec,
990+
"pass",
991+
closure=my_closure)
977992

978993
# should fail: closure tuple with one non-cell-var
994+
my_closure = list(my_closure)
979995
my_closure[0] = int
980996
my_closure = tuple(my_closure)
981997
self.assertRaises(TypeError,
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed an assertion error for :func:`exec` passed a string ``source`` and a non-``None`` ``closure``. Patch by Bartosz Sławecki.

Python/bltinmodule.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,7 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
11541154
if (closure != NULL) {
11551155
PyErr_SetString(PyExc_TypeError,
11561156
"closure can only be used when source is a code object");
1157+
goto error;
11571158
}
11581159
PyObject *source_copy;
11591160
const char *str;

0 commit comments

Comments
 (0)