diff options
author | Nikita Sobolev <mail@sobolevn.me> | 2022-11-04 09:58:38 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-11-04 09:58:38 (GMT) |
commit | 044bcc1771fe7e2f8eba21793a72ba15e75e6715 (patch) | |
tree | 1a7877559771efed4a886f7afe89fea55df81644 /Lib/test | |
parent | 016c7d37b6acfe2203542a2655080c6402b3be1f (diff) | |
download | cpython-044bcc1771fe7e2f8eba21793a72ba15e75e6715.zip cpython-044bcc1771fe7e2f8eba21793a72ba15e75e6715.tar.gz cpython-044bcc1771fe7e2f8eba21793a72ba15e75e6715.tar.bz2 |
gh-94808: Cover `LOAD_GLOBAL` for custom dict subtypes (GH-96767)
Diffstat (limited to 'Lib/test')
-rw-r--r-- | Lib/test/test_builtin.py | 31 |
1 files changed, 31 insertions, 0 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py index 31e5063..814ebe3 100644 --- a/Lib/test/test_builtin.py +++ b/Lib/test/test_builtin.py @@ -736,6 +736,7 @@ class BuiltinTest(unittest.TestCase): self.assertRaises(TypeError, exec, code, {'__builtins__': 123}) + def test_exec_globals_frozen(self): class frozendict_error(Exception): pass @@ -767,6 +768,36 @@ class BuiltinTest(unittest.TestCase): self.assertRaises(frozendict_error, exec, code, namespace) + def test_exec_globals_error_on_get(self): + # custom `globals` or `builtins` can raise errors on item access + class setonlyerror(Exception): + pass + + class setonlydict(dict): + def __getitem__(self, key): + raise setonlyerror + + # globals' `__getitem__` raises + code = compile("globalname", "test", "exec") + self.assertRaises(setonlyerror, + exec, code, setonlydict({'globalname': 1})) + + # builtins' `__getitem__` raises + code = compile("superglobal", "test", "exec") + self.assertRaises(setonlyerror, exec, code, + {'__builtins__': setonlydict({'superglobal': 1})}) + + def test_exec_globals_dict_subclass(self): + class customdict(dict): # this one should not do anything fancy + pass + + code = compile("superglobal", "test", "exec") + # works correctly + exec(code, {'__builtins__': customdict({'superglobal': 1})}) + # custom builtins dict subclass is missing key + self.assertRaisesRegex(NameError, "name 'superglobal' is not defined", + exec, code, {'__builtins__': customdict()}) + def test_exec_redirected(self): savestdout = sys.stdout sys.stdout = None # Whatever that cannot flush() |