diff options
author | Georg Brandl <georg@python.org> | 2006-09-06 06:51:57 (GMT) |
---|---|---|
committer | Georg Brandl <georg@python.org> | 2006-09-06 06:51:57 (GMT) |
commit | 7cae87ca7b0a3a7ce497cbd335c8ec82fe680476 (patch) | |
tree | 612cc46e728bef49b19f3d4bc26fa4951b2c1c83 /Doc/ref | |
parent | 4e472e05bdddde72d91d6f25d6e048371cf3c9be (diff) | |
download | cpython-7cae87ca7b0a3a7ce497cbd335c8ec82fe680476.zip cpython-7cae87ca7b0a3a7ce497cbd335c8ec82fe680476.tar.gz cpython-7cae87ca7b0a3a7ce497cbd335c8ec82fe680476.tar.bz2 |
Patch #1550800: make exec a function.
Diffstat (limited to 'Doc/ref')
-rw-r--r-- | Doc/ref/ref2.tex | 13 | ||||
-rw-r--r-- | Doc/ref/ref4.tex | 18 | ||||
-rw-r--r-- | Doc/ref/ref6.tex | 65 | ||||
-rw-r--r-- | Doc/ref/ref8.tex | 2 | ||||
-rw-r--r-- | Doc/ref/reswords.py | 2 |
5 files changed, 21 insertions, 79 deletions
diff --git a/Doc/ref/ref2.tex b/Doc/ref/ref2.tex index f82d9ce..39b75a9 100644 --- a/Doc/ref/ref2.tex +++ b/Doc/ref/ref2.tex @@ -308,13 +308,12 @@ identifiers. They must be spelled exactly as written here:% \index{reserved word} \begin{verbatim} -and del from not while -as elif global or with -assert else if pass yield -break except import print -class exec in raise -continue finally is return -def for lambda try +and def for is raise +as del from lambda return +assert elif global not try +break else if or while +class except import pass with +continue finally in print yield \end{verbatim} % When adding keywords, use reswords.py for reformatting diff --git a/Doc/ref/ref4.tex b/Doc/ref/ref4.tex index 12a2b92..c8b536e 100644 --- a/Doc/ref/ref4.tex +++ b/Doc/ref/ref4.tex @@ -20,8 +20,8 @@ interpreter or specified on the interpreter command line the first argument) is a code block. A script command (a command specified on the interpreter command line with the `\strong{-c}' option) is a code block. The file read by the built-in function \function{execfile()} -is a code block. The string argument passed to the built-in function -\function{eval()} and to the \keyword{exec} statement is a code block. +is a code block. The string argument passed to the built-in functions +\function{eval()} and \function{exec()} is a code block. The expression read and evaluated by the built-in function \function{input()} is a code block. @@ -139,22 +139,16 @@ If the wild card form of import --- \samp{import *} --- is used in a function and the function contains or is a nested block with free variables, the compiler will raise a \exception{SyntaxError}. -If \keyword{exec} is used in a function and the function contains or -is a nested block with free variables, the compiler will raise a -\exception{SyntaxError} unless the exec explicitly specifies the local -namespace for the \keyword{exec}. (In other words, \samp{exec obj} -would be illegal, but \samp{exec obj in ns} would be legal.) - -The \function{eval()}, \function{execfile()}, and \function{input()} -functions and the \keyword{exec} statement do not have access to the +The \function{eval()}, \function{exec()}, \function{execfile()}, +and \function{input()} functions do not have access to the full environment for resolving names. Names may be resolved in the local and global namespaces of the caller. Free variables are not resolved in the nearest enclosing namespace, but in the global namespace.\footnote{This limitation occurs because the code that is executed by these operations is not available at the time the module is compiled.} -The \keyword{exec} statement and the \function{eval()} and -\function{execfile()} functions have optional arguments to override +The \function{exec()}, \function{eval()} and \function{execfile()} +functions have optional arguments to override the global and local namespace. If only one namespace is specified, it is used for both. diff --git a/Doc/ref/ref6.tex b/Doc/ref/ref6.tex index 04db013..c1bbd9b 100644 --- a/Doc/ref/ref6.tex +++ b/Doc/ref/ref6.tex @@ -20,7 +20,6 @@ by semicolons. The syntax for simple statements is: \productioncont{| \token{continue_stmt}} \productioncont{| \token{import_stmt}} \productioncont{| \token{global_stmt}} - \productioncont{| \token{exec_stmt}} \end{productionlist} @@ -809,7 +808,7 @@ import __future__ [as name] That is not a future statement; it's an ordinary import statement with no special semantics or syntax restrictions. -Code compiled by an \keyword{exec} statement or calls to the builtin functions +Code compiled by calls to the builtin functions \function{exec()}, \function{compile()} and \function{execfile()} that occur in a module \module{M} containing a future statement will, by default, use the new syntax or semantics associated with the future statement. This can, @@ -855,64 +854,14 @@ program.) \strong{Programmer's note:} the \keyword{global} is a directive to the parser. It applies only to code parsed at the same time as the \keyword{global} -statement. In particular, a \keyword{global} statement contained in an -\keyword{exec} statement does not affect the code block \emph{containing} -the \keyword{exec} statement, and code contained in an \keyword{exec} -statement is unaffected by \keyword{global} statements in the code -containing the \keyword{exec} statement. The same applies to the +statement. In particular, a \keyword{global} statement contained in a +string or code object supplied to the builtin \function{exec()} function +does not affect the code block \emph{containing} the function call, +and code contained in such a string is unaffected by \keyword{global} +statements in the code containing the function call. The same applies to the \function{eval()}, \function{execfile()} and \function{compile()} functions. -\stindex{exec} +\bifuncindex{exec} \bifuncindex{eval} \bifuncindex{execfile} \bifuncindex{compile} - -\section{The \keyword{exec} statement \label{exec}} -\stindex{exec} - -\begin{productionlist} - \production{exec_stmt} - {"exec" \token{expression} - ["in" \token{expression} ["," \token{expression}]]} -\end{productionlist} - -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}, ``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, 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, \var{locals} can be any mapping object. -\versionchanged[formerly \var{locals} was required to be a dictionary]{2.4} - -As a side effect, an implementation may insert additional keys into -the dictionaries given besides those corresponding to variable names -set by the executed code. For example, the current implementation -may add a reference to the dictionary of the built-in module -\module{__builtin__} under the key \code{__builtins__} (!). -\ttindex{__builtins__} -\refbimodindex{__builtin__} - -\strong{Programmer's hints:} -dynamic evaluation of expressions is supported by the built-in -function \function{eval()}. The built-in functions -\function{globals()} and \function{locals()} return the current global -and local dictionary, respectively, which may be useful to pass around -for use by \keyword{exec}. -\bifuncindex{eval} -\bifuncindex{globals} -\bifuncindex{locals} - - - diff --git a/Doc/ref/ref8.tex b/Doc/ref/ref8.tex index 45be71d..3fe4cc5 100644 --- a/Doc/ref/ref8.tex +++ b/Doc/ref/ref8.tex @@ -62,7 +62,7 @@ This syntax is used in the following situations: \item when parsing a module; -\item when parsing a string passed to the \keyword{exec} statement; +\item when parsing a string passed to the \function{exec()} function; \end{itemize} diff --git a/Doc/ref/reswords.py b/Doc/ref/reswords.py index 68862bb..53b8dc8 100644 --- a/Doc/ref/reswords.py +++ b/Doc/ref/reswords.py @@ -9,7 +9,7 @@ def main(): words.sort() colwidth = 1 + max(map(len, words)) nwords = len(words) - nrows = (nwords + ncols - 1) / ncols + nrows = (nwords + ncols - 1) // ncols for irow in range(nrows): for icol in range(ncols): i = irow + icol * nrows |