]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-130070: Fix `exec(<string>, closure=<non-None>)` unexpected path (#130071)
authorBartosz Sławecki <bartosz@ilikepython.com>
Thu, 17 Apr 2025 07:14:55 +0000 (09:14 +0200)
committerGitHub <noreply@github.com>
Thu, 17 Apr 2025 07:14:55 +0000 (10:14 +0300)
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>
Lib/test/test_builtin.py
Misc/NEWS.d/next/Core_and_Builtins/2025-02-13-05-09-31.gh-issue-130070.C8c9gK.rst [new file with mode: 0644]
Python/bltinmodule.c

index 27b14c36b51b5083cf50a530412596db72894f6f..31597a320d418adc2e2c3a688966ef7fe432b237 100644 (file)
@@ -1061,8 +1061,24 @@ class BuiltinTest(ComplexesAreIdenticalMixin, 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,
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-02-13-05-09-31.gh-issue-130070.C8c9gK.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-02-13-05-09-31.gh-issue-130070.C8c9gK.rst
new file mode 100644 (file)
index 0000000..f9e135f
--- /dev/null
@@ -0,0 +1 @@
+Fixed an assertion error for :func:`exec` passed a string ``source`` and a non-``None`` ``closure``. Patch by Bartosz Sławecki.
index 10e415fa052f64631fe2a9e135bb28346db92284..8ed0a96270b4eec0cb1460de481c14f0dc107b09 100644 (file)
@@ -1169,6 +1169,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;