diff options
author | Mark Dickinson <mdickinson@enthought.com> | 2012-11-25 13:25:57 (GMT) |
---|---|---|
committer | Mark Dickinson <mdickinson@enthought.com> | 2012-11-25 13:25:57 (GMT) |
commit | 1658797a9d27e5307be1c0c9ab653ebf1d8c80d7 (patch) | |
tree | a594225a8250af628189adcb71df0b954f412463 /Doc | |
parent | 508d7d356e5c50eece60b379e6b5b98760fc28fe (diff) | |
download | cpython-1658797a9d27e5307be1c0c9ab653ebf1d8c80d7.zip cpython-1658797a9d27e5307be1c0c9ab653ebf1d8c80d7.tar.gz cpython-1658797a9d27e5307be1c0c9ab653ebf1d8c80d7.tar.bz2 |
Issue #16339: Document and test exec(stmt, globals, locals) form in Python 2.7.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/reference/simple_stmts.rst | 27 |
1 files changed, 17 insertions, 10 deletions
diff --git a/Doc/reference/simple_stmts.rst b/Doc/reference/simple_stmts.rst index a9766a4..843ce17 100644 --- a/Doc/reference/simple_stmts.rst +++ b/Doc/reference/simple_stmts.rst @@ -978,18 +978,18 @@ The :keyword:`exec` statement exec_stmt: "exec" `or_expr` ["in" `expression` ["," `expression`]] This statement supports dynamic execution of Python code. The first expression -should evaluate to either a string, an open file object, 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 an open file, the file -is parsed until EOF and executed. If it is a code object, it is simply -executed. In all cases, the code that's executed is expected to be valid as -file input (see section :ref:`file-input`). Be aware that the -:keyword:`return` and :keyword:`yield` statements may not be used outside of -function definitions even within the context of code passed to the -:keyword:`exec` statement. +should evaluate to either a string, an open file object, a code object, or a +tuple. 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 an open +file, the file is parsed until EOF and executed. If it is a code object, it is +simply executed. For the interpretation of a tuple, see below. In all cases, +the code that's executed is expected to be valid as file input (see section +:ref:`file-input`). Be aware that the :keyword:`return` and :keyword:`yield` +statements may not be used outside of function definitions even within the +context of code passed to the :keyword:`exec` statement. In all cases, if the optional parts are omitted, the code is executed in the -current scope. If only the first expression after :keyword:`in` is specified, +current scope. If only the first expression after ``in`` is specified, it should be a dictionary, which will be used for both the global and the local variables. If two expressions are given, they are used for the global and local variables, respectively. If provided, *locals* can be any mapping object. @@ -997,6 +997,13 @@ Remember that at module level, globals and locals are the same dictionary. If two separate objects are given as *globals* and *locals*, the code will be executed as if it were embedded in a class definition. +The first expression may also be a tuple of length 2 or 3. In this case, the +optional parts must be omitted. The form ``exec(expr, globals)`` is equivalent +to ``exec expr in globals``, while the form ``exec(expr, globals, locals)`` is +equivalent to ``exec expr in globals, locals``. The tuple form of ``exec`` +provides compatibility with Python 3, where ``exec`` is a function rather than +a statement. + .. versionchanged:: 2.4 Formerly, *locals* was required to be a dictionary. |