[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 954b2cf031)
This commit is contained in:
Bartosz Sławecki 2025-04-17 11:21:56 +02:00 committed by GitHub
parent db7ad1c89f
commit 582d1ef463
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 18 additions and 0 deletions

View file

@ -974,8 +974,24 @@ class BuiltinTest(unittest.TestCase):
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,