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 all 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
18 changes: 13 additions & 5 deletions Doc/library/functions.rst
Original file line number Diff line number Diff line change
Expand Up @@ -524,11 +524,11 @@ are always available. They are listed here in alphabetical order.

.. _func-eval:

.. function:: eval(expression, globals=None, locals=None)
.. function:: eval(source, /, globals=None, locals=None)

:param expression:
:param source:
A Python expression.
:type expression: :class:`str` | :ref:`code object <code-objects>`
:type source: :class:`str` | :ref:`code object <code-objects>`

:param globals:
The global namespace (default: ``None``).
Expand Down Expand Up @@ -583,11 +583,15 @@ are always available. They are listed here in alphabetical order.
Raises an :ref:`auditing event <auditing>` ``exec`` with the code object
as the argument. Code compilation events may also be raised.

.. versionchanged:: 3.13

The *globals* and *locals* arguments can now be passed as keywords.

.. index:: pair: built-in function; exec

.. function:: exec(object, globals=None, locals=None, /, *, closure=None)
.. function:: exec(source, /, globals=None, locals=None, *, closure=None)

This function supports dynamic execution of Python code. *object* must be
This function supports dynamic execution of Python code. *source* must be
either a string or a code object. If it is a string, the string is parsed as
a suite of Python statements which is then executed (unless a syntax error
occurs). [#]_ If it is a code object, it is simply executed. In all cases,
Expand Down Expand Up @@ -640,6 +644,10 @@ are always available. They are listed here in alphabetical order.
.. versionchanged:: 3.11
Added the *closure* parameter.

.. versionchanged:: 3.13

The *globals* and *locals* arguments can now be passed as keywords.


.. function:: filter(function, iterable)

Expand Down
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 @@ -684,6 +686,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 @@ -777,6 +784,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,2 @@
Allow the *globals* and *locals* arguments to :func:`exec`
and :func:`eval` to be passed as keywords.
8 changes: 4 additions & 4 deletions Python/bltinmodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -925,9 +925,9 @@ builtin_divmod_impl(PyObject *module, PyObject *x, PyObject *y)
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 @@ -941,7 +941,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=7c7bce5299a89062]*/
{
PyObject *result = NULL, *source_copy;
const char *str;
Expand Down Expand Up @@ -1024,9 +1024,9 @@ builtin_eval_impl(PyObject *module, PyObject *source, PyObject *globals,
exec as builtin_exec

source: object
/
globals: object = None
locals: object = None
/
*
closure: object(c_default="NULL") = None

Expand All @@ -1044,7 +1044,7 @@ 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=25e989b6d87a3a21]*/
{
PyObject *v;

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.

Loading