https://github.com/python/cpython/commit/c0ecf211b26978859c5112458bcd01d883e04b42
commit: c0ecf211b26978859c5112458bcd01d883e04b42
branch: main
author: Victor Stinner <[email protected]>
committer: vstinner <[email protected]>
date: 2026-03-05T12:35:43+01:00
summary:

gh-145055: Accept frozendict for globals in exec() and eval() (#145072)

files:
A 
Misc/NEWS.d/next/Core_and_Builtins/2026-02-21-12-16-46.gh-issue-145055.VyT-zI.rst
M Doc/library/functions.rst
M Include/internal/pycore_dict.h
M Lib/test/test_builtin.py
M Objects/codeobject.c
M Objects/dictobject.c
M Objects/funcobject.c
M Python/_warnings.c
M Python/bltinmodule.c
M Python/ceval.c
M Python/import.c
M Python/pythonrun.c

diff --git a/Doc/library/functions.rst b/Doc/library/functions.rst
index 65b8ffdb23111d..d9a2eff667ebe1 100644
--- a/Doc/library/functions.rst
+++ b/Doc/library/functions.rst
@@ -594,7 +594,7 @@ are always available.  They are listed here in alphabetical 
order.
 
    :param globals:
       The global namespace (default: ``None``).
-   :type globals: :class:`dict` | ``None``
+   :type globals: :class:`dict` | :class:`frozendict` | ``None``
 
    :param locals:
       The local namespace (default: ``None``).
@@ -660,6 +660,10 @@ are always available.  They are listed here in 
alphabetical order.
       The semantics of the default *locals* namespace have been adjusted as
       described for the :func:`locals` builtin.
 
+   .. versionchanged:: next
+
+      *globals* can now be a :class:`frozendict`.
+
 .. index:: pair: built-in function; exec
 
 .. function:: exec(source, /, globals=None, locals=None, *, closure=None)
@@ -737,6 +741,10 @@ are always available.  They are listed here in 
alphabetical order.
       The semantics of the default *locals* namespace have been adjusted as
       described for the :func:`locals` builtin.
 
+   .. versionchanged:: next
+
+      *globals* can now be a :class:`frozendict`.
+
 
 .. function:: filter(function, iterable, /)
 
diff --git a/Include/internal/pycore_dict.h b/Include/internal/pycore_dict.h
index 1aeec32f55a7f3..a2c5ee41c37784 100644
--- a/Include/internal/pycore_dict.h
+++ b/Include/internal/pycore_dict.h
@@ -372,7 +372,7 @@ _PyDict_UniqueId(PyDictObject *mp)
 static inline void
 _Py_INCREF_DICT(PyObject *op)
 {
-    assert(PyDict_Check(op));
+    assert(PyAnyDict_Check(op));
     Py_ssize_t id = _PyDict_UniqueId((PyDictObject *)op);
     _Py_THREAD_INCREF_OBJECT(op, id);
 }
@@ -380,7 +380,7 @@ _Py_INCREF_DICT(PyObject *op)
 static inline void
 _Py_DECREF_DICT(PyObject *op)
 {
-    assert(PyDict_Check(op));
+    assert(PyAnyDict_Check(op));
     Py_ssize_t id = _PyDict_UniqueId((PyDictObject *)op);
     _Py_THREAD_DECREF_OBJECT(op, id);
 }
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index eabfdcd447f2bb..844656eb0e2c2e 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -784,6 +784,16 @@ def __getitem__(self, key):
                 raise ValueError
         self.assertRaises(ValueError, eval, "foo", {}, X())
 
+    def test_eval_frozendict(self):
+        ns = frozendict(x=1, data=[], __builtins__=__builtins__)
+        eval("data.append(x)", ns, ns)
+        self.assertEqual(ns['data'], [1])
+
+        ns = frozendict()
+        errmsg = "cannot assign __builtins__ to frozendict globals"
+        with self.assertRaisesRegex(TypeError, errmsg):
+            eval("", ns, ns)
+
     def test_eval_kwargs(self):
         data = {"A_GLOBAL_VALUE": 456}
         self.assertEqual(eval("globals()['A_GLOBAL_VALUE']", globals=data), 
456)
@@ -882,6 +892,21 @@ def test_exec(self):
             del l['__builtins__']
         self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
 
+    def test_exec_frozendict(self):
+        ns = frozendict(x=1, data=[], __builtins__=__builtins__)
+        exec("data.append(x)", ns, ns)
+        self.assertEqual(ns['data'], [1])
+
+        ns = frozendict(__builtins__=__builtins__)
+        errmsg = "'frozendict' object does not support item assignment"
+        with self.assertRaisesRegex(TypeError, errmsg):
+            exec("x = 1", ns, ns)
+
+        ns = frozendict()
+        errmsg = "cannot assign __builtins__ to frozendict globals"
+        with self.assertRaisesRegex(TypeError, errmsg):
+            exec("", ns, ns)
+
     def test_exec_kwargs(self):
         g = {}
         exec('global z\nz = 1', globals=g)
diff --git 
a/Misc/NEWS.d/next/Core_and_Builtins/2026-02-21-12-16-46.gh-issue-145055.VyT-zI.rst
 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-21-12-16-46.gh-issue-145055.VyT-zI.rst
new file mode 100644
index 00000000000000..c9daaa27717ed0
--- /dev/null
+++ 
b/Misc/NEWS.d/next/Core_and_Builtins/2026-02-21-12-16-46.gh-issue-145055.VyT-zI.rst
@@ -0,0 +1,2 @@
+:func:`exec` and :func:`eval` now accept :class:`frozendict` for *globals*.
+Patch by Victor Stinner.
diff --git a/Objects/codeobject.c b/Objects/codeobject.c
index 520190824fbf1a..d26516f7c2ff66 100644
--- a/Objects/codeobject.c
+++ b/Objects/codeobject.c
@@ -1830,7 +1830,7 @@ identify_unbound_names(PyThreadState *tstate, 
PyCodeObject *co,
     assert(attrnames != NULL);
     assert(PySet_Check(attrnames));
     assert(PySet_GET_SIZE(attrnames) == 0 || counts != NULL);
-    assert(globalsns == NULL || PyDict_Check(globalsns));
+    assert(globalsns == NULL || PyAnyDict_Check(globalsns));
     assert(builtinsns == NULL || PyDict_Check(builtinsns));
     assert(counts == NULL || counts->total == 0);
     struct co_unbound_counts unbound = {0};
diff --git a/Objects/dictobject.c b/Objects/dictobject.c
index e0127f04249f6b..14019e4f1d926f 100644
--- a/Objects/dictobject.c
+++ b/Objects/dictobject.c
@@ -2687,7 +2687,7 @@ _PyDict_LoadGlobalStackRef(PyDictObject *globals, 
PyDictObject *builtins, PyObje
 PyObject *
 _PyDict_LoadBuiltinsFromGlobals(PyObject *globals)
 {
-    if (!PyDict_Check(globals)) {
+    if (!PyAnyDict_Check(globals)) {
         PyErr_BadInternalCall();
         return NULL;
     }
diff --git a/Objects/funcobject.c b/Objects/funcobject.c
index efe27a2b70c4de..fc32826fb3a861 100644
--- a/Objects/funcobject.c
+++ b/Objects/funcobject.c
@@ -150,7 +150,7 @@ PyObject *
 PyFunction_NewWithQualName(PyObject *code, PyObject *globals, PyObject 
*qualname)
 {
     assert(globals != NULL);
-    assert(PyDict_Check(globals));
+    assert(PyAnyDict_Check(globals));
     _Py_INCREF_DICT(globals);
 
     PyCodeObject *code_obj = (PyCodeObject *)code;
diff --git a/Python/_warnings.c b/Python/_warnings.c
index d44d414bc93a04..0ea785772f03b9 100644
--- a/Python/_warnings.c
+++ b/Python/_warnings.c
@@ -1045,7 +1045,7 @@ setup_context(Py_ssize_t stack_level,
 
     /* Setup registry. */
     assert(globals != NULL);
-    assert(PyDict_Check(globals));
+    assert(PyAnyDict_Check(globals));
     int rc = PyDict_GetItemRef(globals, &_Py_ID(__warningregistry__),
                                registry);
     if (rc < 0) {
@@ -1269,10 +1269,11 @@ warnings_warn_explicit_impl(PyObject *module, PyObject 
*message,
     }
 
     if (module_globals && module_globals != Py_None) {
-        if (!PyDict_Check(module_globals)) {
+        if (!PyAnyDict_Check(module_globals)) {
             PyErr_Format(PyExc_TypeError,
-                         "module_globals must be a dict, not '%.200s'",
-                         Py_TYPE(module_globals)->tp_name);
+                         "module_globals must be a dict or a frozendict, "
+                         "not %T",
+                         module_globals);
             return NULL;
         }
 
diff --git a/Python/bltinmodule.c b/Python/bltinmodule.c
index 301125051f3b0e..fc69f6372028a6 100644
--- a/Python/bltinmodule.c
+++ b/Python/bltinmodule.c
@@ -1040,10 +1040,11 @@ builtin_eval_impl(PyObject *module, PyObject *source, 
PyObject *globals,
         PyErr_SetString(PyExc_TypeError, "locals must be a mapping");
         return NULL;
     }
-    if (globals != Py_None && !PyDict_Check(globals)) {
+    if (globals != Py_None && !PyAnyDict_Check(globals)) {
         PyErr_SetString(PyExc_TypeError, PyMapping_Check(globals) ?
-            "globals must be a real dict; try eval(expr, {}, mapping)"
-            : "globals must be a dict");
+            "globals must be a real dict or a frozendict; "
+            "try eval(expr, {}, mapping)"
+            : "globals must be a dict or a frozendict");
         return NULL;
     }
 
@@ -1197,9 +1198,10 @@ builtin_exec_impl(PyObject *module, PyObject *source, 
PyObject *globals,
         locals = Py_NewRef(globals);
     }
 
-    if (!PyDict_Check(globals)) {
-        PyErr_Format(PyExc_TypeError, "exec() globals must be a dict, not 
%.100s",
-                     Py_TYPE(globals)->tp_name);
+    if (!PyAnyDict_Check(globals)) {
+        PyErr_Format(PyExc_TypeError,
+                     "exec() globals must be a dict or a frozendict, not %T",
+                     globals);
         goto error;
     }
     if (!PyMapping_Check(locals)) {
diff --git a/Python/ceval.c b/Python/ceval.c
index 3ad46cf1ec85ff..1e5142f4b456a1 100644
--- a/Python/ceval.c
+++ b/Python/ceval.c
@@ -2718,7 +2718,7 @@ static PyObject *
 get_globals_builtins(PyObject *globals)
 {
     PyObject *builtins = NULL;
-    if (PyDict_Check(globals)) {
+    if (PyAnyDict_Check(globals)) {
         if (PyDict_GetItemRef(globals, &_Py_ID(__builtins__), &builtins) < 0) {
             return NULL;
         }
@@ -2743,6 +2743,10 @@ set_globals_builtins(PyObject *globals, PyObject 
*builtins)
     }
     else {
         if (PyObject_SetItem(globals, &_Py_ID(__builtins__), builtins) < 0) {
+            if (PyFrozenDict_Check(globals)) {
+                PyErr_SetString(PyExc_TypeError,
+                                "cannot assign __builtins__ to frozendict 
globals");
+            }
             return -1;
         }
     }
@@ -3584,7 +3588,7 @@ _PyEval_GetANext(PyObject *aiter)
 void
 _PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject 
*name, _PyStackRef *writeto)
 {
-    if (PyDict_CheckExact(globals) && PyDict_CheckExact(builtins)) {
+    if (PyAnyDict_CheckExact(globals) && PyAnyDict_CheckExact(builtins)) {
         _PyDict_LoadGlobalStackRef((PyDictObject *)globals,
                                     (PyDictObject *)builtins,
                                     name, writeto);
diff --git a/Python/import.c b/Python/import.c
index 3ed808f67f4149..34224f4c6d6514 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -3728,8 +3728,9 @@ resolve_name(PyThreadState *tstate, PyObject *name, 
PyObject *globals, int level
         _PyErr_SetString(tstate, PyExc_KeyError, "'__name__' not in globals");
         goto error;
     }
-    if (!PyDict_Check(globals)) {
-        _PyErr_SetString(tstate, PyExc_TypeError, "globals must be a dict");
+    if (!PyAnyDict_Check(globals)) {
+        _PyErr_SetString(tstate, PyExc_TypeError,
+                         "globals must be a dict or a frozendict");
         goto error;
     }
     if (PyDict_GetItemRef(globals, &_Py_ID(__package__), &package) < 0) {
diff --git a/Python/pythonrun.c b/Python/pythonrun.c
index 043bdf3433ab57..a21f494dc69d82 100644
--- a/Python/pythonrun.c
+++ b/Python/pythonrun.c
@@ -1348,8 +1348,9 @@ static PyObject *
 run_eval_code_obj(PyThreadState *tstate, PyCodeObject *co, PyObject *globals, 
PyObject *locals)
 {
     /* Set globals['__builtins__'] if it doesn't exist */
-    if (!globals || !PyDict_Check(globals)) {
-        PyErr_SetString(PyExc_SystemError, "globals must be a real dict");
+    if (!globals || !PyAnyDict_Check(globals)) {
+        PyErr_SetString(PyExc_SystemError,
+                        "globals must be a real dict or a real frozendict");
         return NULL;
     }
     int has_builtins = PyDict_ContainsString(globals, "__builtins__");

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to