https://github.com/python/cpython/commit/582d1ef4639ae2284a4302955582c45bd41ba834
commit: 582d1ef4639ae2284a4302955582c45bd41ba834
branch: 3.13
author: Bartosz Sławecki <bart...@ilikepython.com>
committer: Eclips4 <kirill.ba...@mail.ru>
date: 2025-04-17T09:21:56Z
summary:

[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 <m...@sobolevn.me>
(cherry picked from commit 954b2cf031fb84ff3386251d5c45281f47229003)

files:
A Misc/NEWS.d/next/Core and 
Builtins/2025-02-13-05-09-31.gh-issue-130070.C8c9gK.rst
M Lib/test/test_builtin.py
M Python/bltinmodule.c

diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index d4da29150d5fed..12a1c48732fab1 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -974,8 +974,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,
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
index 00000000000000..f9e135f1902b8a
--- /dev/null
+++ b/Misc/NEWS.d/next/Core and 
Builtins/2025-02-13-05-09-31.gh-issue-130070.C8c9gK.rst 
@@ -0,0 +1 @@
+Fixed an assertion error for :func:`exec` passed a string ``source`` and a 
non-``None`` ``closure``. Patch by Bartosz Sławecki.
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 06bf4d38f9eab9..cc93424664c69c 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1154,6 +1154,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;

_______________________________________________
Python-checkins mailing list -- python-checkins@python.org
To unsubscribe send an email to python-checkins-le...@python.org
https://mail.python.org/mailman3/lists/python-checkins.python.org/
Member address: arch...@mail-archive.com

Reply via email to