8000 gh-130070: Fix `exec(<string>, closure=<non-None>)` unexpected path (… · python/cpython@954b2cf · GitHub
[go: up one dir, main page]

Skip to content

Commit 954b2cf

Browse files
bswcksobolevn
andauthored
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>
1 parent d7df781 commit 954b2cf

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
@@ -1061,8 +1061,24 @@ def four_freevars():
10611061
three_freevars.__code__,
10621062
three_freevars.__globals__,
10631063
closure=my_closure)
1064+
my_closure = tuple(my_closure)
1065+
1066+
# should fail: anything passed to closure= isn't allowed
1067+
# when the source is a string
1068+
self.assertRaises(TypeError,
1069+
exec,
1070+
"pass",
1071+
closure=int)
1072+
1073+
# should fail: correct closure= argument isn't allowed
1074+
# when the source is a string
1075+
self.assertRaises(TypeError,
1076+
exec,
1077+
"pass",
1078+
closure=my_closure)
10641079

10651080
# should fail: closure tuple with one non-cell-var
1081+
my_closure = list(my_closure)
10661082
my_closure[0] = int
10671083
my_closure = tuple(my_closure)
10681084
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
@@ -1169,6 +1169,7 @@ builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
11691169
if (closure != NULL) {
11701170
PyErr_SetString(PyExc_TypeError,
11711171
"closure can only be used when source is a code object");
1172+
goto error;
11721173
}
11731174
PyObject *source_copy;
11741175
const char *str;

0 commit comments

Comments
 (0)
0