mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
gh-105879: Add support for keyword arguments to eval and exec (#105885)
Co-authored-by: Jelle Zijlstra <jelle.zijlstra@gmail.com>
This commit is contained in:
parent
72867c962c
commit
2770d5caca
5 changed files with 98 additions and 33 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue