Skip to content

gh-105879: Add support for keyword arguments to eval and exec #105885

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 2, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Lib/test/test_builtin.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@
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:

Expand Down Expand Up @@ -643,6 +645,11 @@ def __getitem__(self, key):
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

Expand Down Expand Up @@ -736,6 +743,19 @@ def test_exec(self):
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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Allow keyword arguments to be used when calling :meth:`~eval` and :meth:`~exec`.
7 changes: 3 additions & 4 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -891,7 +891,6 @@ eval as builtin_eval
source: object
globals: object = None
locals: object = None
/

Evaluate the given source in the context of globals and locals.

Expand All @@ -905,7 +904,7 @@ If only globals is given, locals defaults to it.
static PyObject *
builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
PyObject *locals)
/*[clinic end generated code: output=0a0824aa70093116 input=11ee718a8640e527]*/
/*[clinic end generated code: output=0a0824aa70093116 input=4cbfb23dd7cbe2a9]*/
{
PyObject *result, *source_copy;
const char *str;
Expand Down Expand Up @@ -980,7 +979,6 @@ exec as builtin_exec
source: object
globals: object = None
locals: object = None
/
*
closure: object(c_default="NULL") = None

Expand All @@ -998,11 +996,12 @@ when source is a code object requiring exactly that many cellvars.
static PyObject *
builtin_exec_impl(PyObject *module, PyObject *source, PyObject *globals,
PyObject *locals, PyObject *closure)
/*[clinic end generated code: output=7579eb4e7646743d input=f13a7e2b503d1d9a]*/
/*[clinic end generated code: output=7579eb4e7646743d input=99951d1832fece9e]*/
{
PyObject *v;

if (globals == Py_None) {

globals = PyEval_GetGlobals();
if (locals == Py_None) {
locals = PyEval_GetLocals();
Expand Down
83 changes: 59 additions & 24 deletions Python/clinic/bltinmodule.c.h

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.