Skip to content

Commit 9a1e389

Browse files
committed
Patch #1550800: make exec a function.
1 parent 4b60658 commit 9a1e389

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

105 files changed

+1221
-1558
lines changed

Demo/parser/unparse.py

-10
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,6 @@ def _Assert(self, t):
123123
self.write(", ")
124124
self.dispatch(t.msg)
125125

126-
def _Exec(self, t):
127-
self.fill("exec ")
128-
self.dispatch(t.body)
129-
if t.globals:
130-
self.write(" in ")
131-
self.dispatch(t.globals)
132-
if t.locals:
133-
self.write(", ")
134-
self.dispatch(t.locals)
135-
136126
def _Print(self, t):
137127
self.fill("print ")
138128
do_comma = False

Demo/pysvr/pysvr.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ def run_command(code, stdin, stdout, globals):
108108
sys.stdout = sys.stderr = stdout
109109
sys.stdin = stdin
110110
try:
111-
exec code in globals
111+
exec(code, globals)
112112
except SystemExit, how:
113113
raise SystemExit, how, sys.exc_info()[2]
114114
except:

Demo/sockets/rpythond.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ def execute(request):
4040
sys.stdout = sys.stderr = fakefile = StringIO.StringIO()
4141
try:
4242
try:
43-
exec request in {}, {}
43+
exec(request, {}, {})
4444
except:
4545
print
4646
traceback.print_exc(100)

Doc/howto/doanddont.tex

+3-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ \subsubsection{When It Is Just Fine}
8181

8282
\end{itemize}
8383

84-
\subsection{Unadorned \keyword{exec}, \function{execfile} and friends}
84+
\subsection{Unadorned \function{exec}, \function{execfile} and friends}
8585

8686
The word ``unadorned'' refers to the use without an explicit dictionary,
8787
in which case those constructs evaluate code in the {\em current} environment.
@@ -93,10 +93,10 @@ \subsection{Unadorned \keyword{exec}, \function{execfile} and friends}
9393

9494
\begin{verbatim}
9595
>>> for name in sys.argv[1:]:
96-
>>> exec "%s=1" % name
96+
>>> exec("%s=1" % name)
9797
>>> def func(s, **kw):
9898
>>> for var, val in kw.items():
99-
>>> exec "s.%s=val" % var # invalid!
99+
>>> exec("s.%s=val" % var) # invalid!
100100
>>> execfile("handler.py")
101101
>>> handle()
102102
\end{verbatim}

Doc/lib/libdis.tex

-5
Original file line numberDiff line numberDiff line change
@@ -408,11 +408,6 @@ \subsection{Python Byte Code Instructions}
408408
This opcode implements \code{from module import *}.
409409
\end{opcodedesc}
410410

411-
\begin{opcodedesc}{EXEC_STMT}{}
412-
Implements \code{exec TOS2,TOS1,TOS}. The compiler fills
413-
missing optional parameters with \code{None}.
414-
\end{opcodedesc}
415-
416411
\begin{opcodedesc}{POP_BLOCK}{}
417412
Removes one block from the block stack. Per frame, there is a
418413
stack of blocks, denoting nested loops, try statements, and such.

Doc/lib/libexcs.tex

+4-4
Original file line numberDiff line numberDiff line change
@@ -293,10 +293,10 @@ \section{Built-in Exceptions}
293293
\begin{excdesc}{SyntaxError}
294294
% XXXJH xref to these functions?
295295
Raised when the parser encounters a syntax error. This may occur in
296-
an \keyword{import} statement, in an \keyword{exec} statement, in a call
297-
to the built-in function \function{eval()} or \function{input()}, or
298-
when reading the initial script or standard input (also
299-
interactively).
296+
an \keyword{import} statement, in a call to the built-in functions
297+
\function{exec()}, \function{execfile()}, \function{eval()} or
298+
\function{input()}, or when reading the initial script or standard
299+
input (also interactively).
300300

301301
Instances of this class have attributes \member{filename},
302302
\member{lineno}, \member{offset} and \member{text} for easier access

Doc/lib/libfuncs.tex

+40-6
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ \section{Built-in Functions \label{built-in-funcs}}
178178
\begin{funcdesc}{compile}{string, filename, kind\optional{,
179179
flags\optional{, dont_inherit}}}
180180
Compile the \var{string} into a code object. Code objects can be
181-
executed by an \keyword{exec} statement or evaluated by a call to
181+
executed by a call to \function{exec()} or evaluated by a call to
182182
\function{eval()}. The \var{filename} argument should
183183
give the file from which the code was read; pass some recognizable value
184184
if it wasn't read from a file (\code{'<string>'} is commonly used).
@@ -366,21 +366,55 @@ \section{Built-in Functions \label{built-in-funcs}}
366366
compiled passing \code{'eval'} as the \var{kind} argument.
367367

368368
Hints: dynamic execution of statements is supported by the
369-
\keyword{exec} statement. Execution of statements from a file is
369+
\function{exec()} function. Execution of statements from a file is
370370
supported by the \function{execfile()} function. The
371371
\function{globals()} and \function{locals()} functions returns the
372372
current global and local dictionary, respectively, which may be
373373
useful to pass around for use by \function{eval()} or
374374
\function{execfile()}.
375375
\end{funcdesc}
376376

377+
378+
\begin{funcdesc}{exec}{object\optional{, globals\optional{, locals}}}
379+
This function supports dynamic execution of Python code.
380+
\var{object} must be either a string, an open file object, or
381+
a code object. If it is a string, the string is parsed as a suite of
382+
Python statements which is then executed (unless a syntax error
383+
occurs). If it is an open file, the file is parsed until \EOF{} and
384+
executed. If it is a code object, it is simply executed. In all
385+
cases, the code that's executed is expected to be valid as file
386+
input (see the section ``File input'' in the Reference Manual).
387+
Be aware that the \keyword{return} and \keyword{yield} statements may
388+
not be used outside of function definitions even within the context of
389+
code passed to the \function{exec()} function.
390+
The return value is \code{None}.
391+
392+
In all cases, if the optional parts are omitted, the code is executed
393+
in the current scope. If only \var{globals} is provided, it must be
394+
a dictionary, which will be used for both the global and the local
395+
variables. If \var{globals} and \var{locals} are given, they are used
396+
for the global and local variables, respectively. If provided,
397+
\var{locals} can be any mapping object.
398+
399+
If the \var{globals} dictionary does not contain a value for the
400+
key \code{__builtins__}, a reference to the dictionary of the built-in
401+
module \module{__builtin__} is inserted under that key. That way you
402+
can control what builtins are available to the executed code by
403+
inserting your own \code{__builtins__} dictionary into \var{globals}
404+
before passing it to \function{exec()}.
405+
406+
\note{The built-in functions \function{globals()} and \function{locals()}
407+
return the current global and local dictionary, respectively, which
408+
may be useful to pass around for use as the second and third
409+
argument to \function{exec()}.}
410+
\end{funcdesc}
411+
377412
\begin{funcdesc}{execfile}{filename\optional{, globals\optional{, locals}}}
378-
This function is similar to the
379-
\keyword{exec} statement, but parses a file instead of a string. It
413+
This function is similar to the \function{exec()} function, but parses a
414+
file given by the file name instead of a string. It
380415
is different from the \keyword{import} statement in that it does not
381416
use the module administration --- it reads the file unconditionally
382-
and does not create a new module.\footnote{It is used relatively
383-
rarely so does not warrant being made into a statement.}
417+
and does not create a new module.
384418

385419
The arguments are a file name and two optional dictionaries. The file is
386420
parsed and evaluated as a sequence of Python statements (similarly to a

Doc/lib/libhotshot.tex

+2-2
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ \subsection{Profile Objects \label{hotshot-objects}}
6161
\end{methoddesc}
6262

6363
\begin{methoddesc}{run}{cmd}
64-
Profile an \keyword{exec}-compatible string in the script environment.
64+
Profile an \function{exec()}-compatible string in the script environment.
6565
The globals from the \refmodule[main]{__main__} module are used as
6666
both the globals and locals for the script.
6767
\end{methoddesc}
@@ -76,7 +76,7 @@ \subsection{Profile Objects \label{hotshot-objects}}
7676

7777

7878
\begin{methoddesc}{runctx}{cmd, globals, locals}
79-
Evaluate an \keyword{exec}-compatible string in a specific environment.
79+
Profile an \function{exec()}-compatible string in a specific environment.
8080
The string is compiled before profiling begins.
8181
\end{methoddesc}
8282

Doc/lib/libparser.tex

+3-2
Original file line numberDiff line numberDiff line change
@@ -193,8 +193,9 @@ \subsection{Converting AST Objects \label{Converting ASTs}}
193193

194194
\begin{funcdesc}{compileast}{ast\optional{, filename\code{ = '<ast>'}}}
195195
The Python byte compiler can be invoked on an AST object to produce
196-
code objects which can be used as part of an \keyword{exec} statement or
197-
a call to the built-in \function{eval()}\bifuncindex{eval} function.
196+
code objects which can be used as part of a call to the built-in
197+
\function{exec()}\bifuncindex{exec} or \function{eval()}
198+
\bifuncindex{eval} functions.
198199
This function provides the interface to the compiler, passing the
199200
internal parse tree from \var{ast} to the parser, using the
200201
source file name specified by the \var{filename} parameter.

Doc/lib/libpdb.tex

+2-2
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,8 @@ \chapter{The Python Debugger \label{debugger}}
7979
explained below). The optional \var{globals} and \var{locals}
8080
arguments specify the environment in which the code is executed; by
8181
default the dictionary of the module \refmodule[main]{__main__} is
82-
used. (See the explanation of the \keyword{exec} statement or the
83-
\function{eval()} built-in function.)
82+
used. (See the explanation of the built-in \function{exec()} or
83+
\function{eval()} functions.)
8484
\end{funcdesc}
8585

8686
\begin{funcdesc}{runeval}{expression\optional{, globals\optional{, locals}}}

Doc/lib/libprofile.tex

+3-3
Original file line numberDiff line numberDiff line change
@@ -319,9 +319,9 @@ \section{Reference Manual -- \module{profile} and \module{cProfile}}
319319

320320
\begin{funcdesc}{run}{command\optional{, filename}}
321321

322-
This function takes a single argument that has can be passed to the
323-
\keyword{exec} statement, and an optional file name. In all cases this
324-
routine attempts to \keyword{exec} its first argument, and gather profiling
322+
This function takes a single argument that can be passed to the
323+
\function{exec()} function, and an optional file name. In all cases this
324+
routine attempts to \function{exec()} its first argument, and gather profiling
325325
statistics from the execution. If no file name is present, then this
326326
function automatically prints a simple profiling report, sorted by the
327327
standard name string (file/line/function-name) that is presented in

Doc/lib/librexec.tex

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ \section{\module{rexec} ---
1111
\end{notice}
1212

1313
This module contains the \class{RExec} class, which supports
14-
\method{r_eval()}, \method{r_execfile()}, \method{r_exec()}, and
14+
\method{r_exec()}, \method{r_eval()}, \method{r_execfile()}, and
1515
\method{r_import()} methods, which are restricted versions of the standard
16-
Python functions \method{eval()}, \method{execfile()} and
17-
the \keyword{exec} and \keyword{import} statements.
16+
Python functions \method{exec()}, \method{eval()}, \method{execfile()} and
17+
the \keyword{import} statement.
1818
Code executed in this restricted environment will
1919
only have access to modules and functions that are deemed safe; you
2020
can subclass \class{RExec} to add or remove capabilities as desired.

Doc/lib/libstdtypes.tex

+3-3
Original file line numberDiff line numberDiff line change
@@ -1972,9 +1972,9 @@ \subsection{Code Objects \label{bltin-code-objects}}
19721972
\withsubitem{(function object attribute)}{\ttindex{func_code}}
19731973

19741974
A code object can be executed or evaluated by passing it (instead of a
1975-
source string) to the \keyword{exec} statement or the built-in
1976-
\function{eval()} function.
1977-
\stindex{exec}
1975+
source string) to the \function{exec()} or \function{eval()}
1976+
built-in functions.
1977+
\bifuncindex{exec}
19781978
\bifuncindex{eval}
19791979

19801980
See the \citetitle[../ref/ref.html]{Python Reference Manual} for more

Doc/lib/libtraceback.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ \subsection{Traceback Example \label{traceback-example}}
139139
def run_user_code(envdir):
140140
source = raw_input(">>> ")
141141
try:
142-
exec source in envdir
142+
exec(source, envdir)
143143
except:
144144
print "Exception in user code:"
145145
print '-'*60

Doc/ref/ref2.tex

+6-7
Original file line numberDiff line numberDiff line change
@@ -308,13 +308,12 @@ \subsection{Keywords\label{keywords}}
308308
\index{reserved word}
309309

310310
\begin{verbatim}
311-
and del from not while
312-
as elif global or with
313-
assert else if pass yield
314-
break except import print
315-
class exec in raise
316-
continue finally is return
317-
def for lambda try
311+
and def for is raise
312+
as del from lambda return
313+
assert elif global not try
314+
break else if or while
315+
class except import pass with
316+
continue finally in print yield
318317
\end{verbatim}
319318

320319
% When adding keywords, use reswords.py for reformatting

Doc/ref/ref4.tex

+6-12
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@ \section{Naming and binding \label{naming}}
2020
argument) is a code block. A script command (a command specified on
2121
the interpreter command line with the `\strong{-c}' option) is a code
2222
block. The file read by the built-in function \function{execfile()}
23-
is a code block. The string argument passed to the built-in function
24-
\function{eval()} and to the \keyword{exec} statement is a code block.
23+
is a code block. The string argument passed to the built-in functions
24+
\function{eval()} and \function{exec()} is a code block.
2525
The expression read and evaluated by the built-in function
2626
\function{input()} is a code block.
2727

@@ -139,22 +139,16 @@ \subsection{Interaction with dynamic features \label{dynamic-features}}
139139
function and the function contains or is a nested block with free
140140
variables, the compiler will raise a \exception{SyntaxError}.
141141

142-
If \keyword{exec} is used in a function and the function contains or
143-
is a nested block with free variables, the compiler will raise a
144-
\exception{SyntaxError} unless the exec explicitly specifies the local
145-
namespace for the \keyword{exec}. (In other words, \samp{exec obj}
146-
would be illegal, but \samp{exec obj in ns} would be legal.)
147-
148-
The \function{eval()}, \function{execfile()}, and \function{input()}
149-
functions and the \keyword{exec} statement do not have access to the
142+
The \function{eval()}, \function{exec()}, \function{execfile()},
143+
and \function{input()} functions do not have access to the
150144
full environment for resolving names. Names may be resolved in the
151145
local and global namespaces of the caller. Free variables are not
152146
resolved in the nearest enclosing namespace, but in the global
153147
namespace.\footnote{This limitation occurs because the code that is
154148
executed by these operations is not available at the time the
155149
module is compiled.}
156-
The \keyword{exec} statement and the \function{eval()} and
157-
\function{execfile()} functions have optional arguments to override
150+
The \function{exec()}, \function{eval()} and \function{execfile()}
151+
functions have optional arguments to override
158152
the global and local namespace. If only one namespace is specified,
159153
it is used for both.
160154

Doc/ref/ref6.tex

+7-58
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ \chapter{Simple statements \label{simple}}
2020
\productioncont{| \token{continue_stmt}}
2121
\productioncont{| \token{import_stmt}}
2222
\productioncont{| \token{global_stmt}}
23-
\productioncont{| \token{exec_stmt}}
2423
\end{productionlist}
2524

2625

@@ -809,7 +808,7 @@ \subsection{Future statements \label{future}}
809808
That is not a future statement; it's an ordinary import statement with
810809
no special semantics or syntax restrictions.
811810
812-
Code compiled by an \keyword{exec} statement or calls to the builtin functions
811+
Code compiled by calls to the builtin functions \function{exec()},
813812
\function{compile()} and \function{execfile()} that occur in a module
814813
\module{M} containing a future statement will, by default, use the new
815814
syntax or semantics associated with the future statement. This can,
@@ -855,64 +854,14 @@ \section{The \keyword{global} statement \label{global}}
855854
\strong{Programmer's note:}
856855
the \keyword{global} is a directive to the parser. It
857856
applies only to code parsed at the same time as the \keyword{global}
858-
statement. In particular, a \keyword{global} statement contained in an
859-
\keyword{exec} statement does not affect the code block \emph{containing}
860-
the \keyword{exec} statement, and code contained in an \keyword{exec}
861-
statement is unaffected by \keyword{global} statements in the code
862-
containing the \keyword{exec} statement. The same applies to the
857+
statement. In particular, a \keyword{global} statement contained in a
858+
string or code object supplied to the builtin \function{exec()} function
859+
does not affect the code block \emph{containing} the function call,
860+
and code contained in such a string is unaffected by \keyword{global}
861+
statements in the code containing the function call. The same applies to the
863862
\function{eval()}, \function{execfile()} and \function{compile()} functions.
864-
\stindex{exec}
863+
\bifuncindex{exec}
865864
\bifuncindex{eval}
866865
\bifuncindex{execfile}
867866
\bifuncindex{compile}
868867
869-
870-
\section{The \keyword{exec} statement \label{exec}}
871-
\stindex{exec}
872-
873-
\begin{productionlist}
874-
\production{exec_stmt}
875-
{"exec" \token{expression}
876-
["in" \token{expression} ["," \token{expression}]]}
877-
\end{productionlist}
878-
879-
This statement supports dynamic execution of Python code. The first
880-
expression should evaluate to either a string, an open file object, or
881-
a code object. If it is a string, the string is parsed as a suite of
882-
Python statements which is then executed (unless a syntax error
883-
occurs). If it is an open file, the file is parsed until \EOF{} and
884-
executed. If it is a code object, it is simply executed. In all
885-
cases, the code that's executed is expected to be valid as file
886-
input (see section~\ref{file-input}, ``File input''). Be aware that
887-
the \keyword{return} and \keyword{yield} statements may not be used
888-
outside of function definitions even within the context of code passed
889-
to the \keyword{exec} statement.
890-
891-
In all cases, if the optional parts are omitted, the code is executed
892-
in the current scope. If only the first expression after \keyword{in}
893-
is specified, it should be a dictionary, which will be used for both
894-
the global and the local variables. If two expressions are given,
895-
they are used for the global and local variables, respectively.
896-
If provided, \var{locals} can be any mapping object.
897-
\versionchanged[formerly \var{locals} was required to be a dictionary]{2.4}
898-
899-
As a side effect, an implementation may insert additional keys into
900-
the dictionaries given besides those corresponding to variable names
901-
set by the executed code. For example, the current implementation
902-
may add a reference to the dictionary of the built-in module
903-
\module{__builtin__} under the key \code{__builtins__} (!).
904-
\ttindex{__builtins__}
905-
\refbimodindex{__builtin__}
906-
907-
\strong{Programmer's hints:}
908-
dynamic evaluation of expressions is supported by the built-in
909-
function \function{eval()}. The built-in functions
910-
\function{globals()} and \function{locals()} return the current global
911-
and local dictionary, respectively, which may be useful to pass around
912-
for use by \keyword{exec}.
913-
\bifuncindex{eval}
914-
\bifuncindex{globals}
915-
\bifuncindex{locals}
916-
917-
918-

Doc/ref/ref8.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ \section{File input\label{file-input}}
6262

6363
\item when parsing a module;
6464

65-
\item when parsing a string passed to the \keyword{exec} statement;
65+
\item when parsing a string passed to the \function{exec()} function;
6666

6767
\end{itemize}
6868

0 commit comments

Comments
 (0)