gh-132386: Fix a crash when passing a dict subclass to exec (GH-132412)

* Fix crash when passing a dict subclass to exec

* Add news entry
This commit is contained in:
Tomas R. 2025-04-11 23:05:03 +02:00 committed by GitHub
parent deda47d6e1
commit e6ef47ac22
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 14 additions and 0 deletions

View 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):

View file

@ -0,0 +1,2 @@
Fix crash when passing a dict subclass as the ``globals`` parameter to
:func:`exec`.

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