summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_builtin.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_builtin.py')
-rw-r--r--Lib/test/test_builtin.py31
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()