Skip to content

Commit 19ea083

Browse files
committed
Fix crash when passing a dict subclass to exec
1 parent 26ae05e commit 19ea083

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

Lib/test/test_compile.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,16 @@ async def name_4():
16361636
pass
16371637
[[]]
16381638

1639+
def test_globals_dict_subclass(self):
1640+
# gh-132386
1641+
class WeirdDict(dict):
1642+
pass
1643+
1644+
ns = {}
1645+
exec('def foo(): return a', WeirdDict(), ns)
1646+
1647+
self.assertRaises(NameError, ns['foo'])
1648+
16391649
class TestBooleanExpression(unittest.TestCase):
16401650
class Value:
16411651
def __init__(self):

Python/ceval.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3312,6 +3312,8 @@ _PyEval_LoadGlobalStackRef(PyObject *globals, PyObject *builtins, PyObject *name
33123312
_PyEval_FormatExcCheckArg(
33133313
PyThreadState_GET(), PyExc_NameError,
33143314
NAME_ERROR_MSG, name);
3315+
*writeto = PyStackRef_NULL;
3316+
return;
33153317
}
33163318
}
33173319
*writeto = PyStackRef_FromPyObjectSteal(res);

0 commit comments

Comments
 (0)