summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorRaphael Gaschignard <r.gaschignard@gmail.com>2024-05-02 15:55:29 (GMT)
committerGitHub <noreply@github.com>2024-05-02 15:55:29 (GMT)
commit2770d5caca42d48102f8e18210132a964c34af7c (patch)
tree1f42b67312dc3082ae095fb9a7d1685009faec05 /Lib
parent72867c962cc59c6d56805f86530696bea6beb039 (diff)
downloadcpython-2770d5caca42d48102f8e18210132a964c34af7c.zip
cpython-2770d5caca42d48102f8e18210132a964c34af7c.tar.gz
cpython-2770d5caca42d48102f8e18210132a964c34af7c.tar.bz2
gh-105879: Add support for keyword arguments to eval and exec (#105885)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/test/test_builtin.py20
1 files changed, 20 insertions, 0 deletions
diff --git a/Lib/test/test_builtin.py b/Lib/test/test_builtin.py
index 9a0bf52..230789f 100644
--- a/Lib/test/test_builtin.py
+++ b/Lib/test/test_builtin.py
@@ -46,6 +46,8 @@ except ImportError:
x, y = 1e16, 2.9999 # use temporary values to defeat peephole optimizer
HAVE_DOUBLE_ROUNDING = (x + y == 1e16 + 4)
+# used as proof of globals being used
+A_GLOBAL_VALUE = 123
class Squares:
@@ -684,6 +686,11 @@ class BuiltinTest(unittest.TestCase):
raise ValueError
self.assertRaises(ValueError, eval, "foo", {}, X())
+ def test_eval_kwargs(self):
+ data = {"A_GLOBAL_VALUE": 456}
+ self.assertEqual(eval("globals()['A_GLOBAL_VALUE']", globals=data), 456)
+ self.assertEqual(eval("globals()['A_GLOBAL_VALUE']", locals=data), 123)
+
def test_general_eval(self):
# Tests that general mappings can be used for the locals argument
@@ -777,6 +784,19 @@ class BuiltinTest(unittest.TestCase):
del l['__builtins__']
self.assertEqual((g, l), ({'a': 1}, {'b': 2}))
+ def test_exec_kwargs(self):
+ g = {}
+ exec('global z\nz = 1', globals=g)
+ if '__builtins__' in g:
+ del g['__builtins__']
+ self.assertEqual(g, {'z': 1})
+
+ # if we only set locals, the global assignment will not
+ # reach this locals dictionary
+ g = {}
+ exec('global z\nz = 1', locals=g)
+ self.assertEqual(g, {})
+
def test_exec_globals(self):
code = compile("print('Hello World!')", "", "exec")
# no builtin function