Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7cae87c

Browse files
committed
Patch #1550800: make exec a function.
1 parent 4e472e0 commit 7cae87c

105 files changed

Lines changed: 1221 additions & 1558 deletions

Some content is hidden

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

Demo/parser/unparse.py

Lines changed: 0 additions & 10 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 3 additions & 3 deletions
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

Lines changed: 0 additions & 5 deletions
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

Lines changed: 4 additions & 4 deletions
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

Lines changed: 40 additions & 6 deletions
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 3 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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}}}

0 commit comments

Comments
 (0)