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

This commit is contained in:
Victor Stinner 2026-03-05 12:35:43 +01:00 committed by GitHub
parent c8aa8de9a5
commit c0ecf211b2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
12 changed files with 66 additions and 22 deletions

View file

@ -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;
}

View file

@ -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)) {

View file

@ -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);

View file

@ -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) {

View file

@ -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__");