]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
gh-132386: Fix a crash when passing a dict subclass to `exec` (GH-132412)
authorTomas R. <tomas.roun8@gmail.com>
Fri, 11 Apr 2025 21:05:03 +0000 (23:05 +0200)
committerGitHub <noreply@github.com>
Fri, 11 Apr 2025 21:05:03 +0000 (05:05 +0800)
* Fix crash when passing a dict subclass to exec

* Add news entry

Lib/test/test_compile.py
Misc/NEWS.d/next/Core_and_Builtins/2025-04-11-18-46-37.gh-issue-132386.pMBFTe.rst [new file with mode: 0644]
Python/ceval.c

index ce9c060641d6c51081688c030188985ae6444b95..9cc025d85e168a6f3b18555e32c37f630492ba05 100644 (file)
@@ -1636,6 +1636,16 @@ class TestSpecifics(unittest.TestCase):
                     pass
             [[]]
 
+    def test_globals_dict_subclass(self):
+        # gh-132386
+        class WeirdDict(dict):
+            pass
+
+        ns = {}
+        exec('def foo(): return a', WeirdDict(), ns)
+
+        self.assertRaises(NameError, ns['foo'])
+
 class TestBooleanExpression(unittest.TestCase):
     class Value:
         def __init__(self):
diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2025-04-11-18-46-37.gh-issue-132386.pMBFTe.rst b/Misc/NEWS.d/next/Core_and_Builtins/2025-04-11-18-46-37.gh-issue-132386.pMBFTe.rst
new file mode 100644 (file)
index 0000000..65ba7fc
--- /dev/null
@@ -0,0 +1,2 @@
+Fix crash when passing a dict subclass as the ``globals`` parameter to
+:func:`exec`.
index 8ab0c6318c1c910008d65effe9b0f09cf3ed4a21..e4a63ad9287783d288eb2cbfe2d411ba23f2a25b 100644 (file)
@@ -3312,6 +3312,8 @@ _PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name
                 _PyEval_FormatExcCheckArg(
                             PyThreadState_GET(), PyExc_NameError,
                             NAME_ERROR_MSG, name);
+                *writeto = PyStackRef_NULL;
+                return;
             }
         }
         *writeto = PyStackRef_FromPyObjectSteal(res);