mirror of
https://github.com/python/cpython.git
synced 2025-08-27 20:25:18 +00:00
gh-94808: Cover LOAD_GLOBAL
for custom dict subtypes (GH-96767)
This commit is contained in:
parent
016c7d37b6
commit
044bcc1771
1 changed files with 31 additions and 0 deletions
|
@ -736,6 +736,7 @@ class BuiltinTest(unittest.TestCase):
|
||||||
self.assertRaises(TypeError,
|
self.assertRaises(TypeError,
|
||||||
exec, code, {'__builtins__': 123})
|
exec, code, {'__builtins__': 123})
|
||||||
|
|
||||||
|
def test_exec_globals_frozen(self):
|
||||||
class frozendict_error(Exception):
|
class frozendict_error(Exception):
|
||||||
pass
|
pass
|
||||||
|
|
||||||
|
@ -767,6 +768,36 @@ class BuiltinTest(unittest.TestCase):
|
||||||
self.assertRaises(frozendict_error,
|
self.assertRaises(frozendict_error,
|
||||||
exec, code, namespace)
|
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):
|
def test_exec_redirected(self):
|
||||||
savestdout = sys.stdout
|
savestdout = sys.stdout
|
||||||
sys.stdout = None # Whatever that cannot flush()
|
sys.stdout = None # Whatever that cannot flush()
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue