Skip to content

Commit 0168802

Browse files
committed
Kill execfile(), use exec() instead
1 parent 41eaedd commit 0168802

Some content is hidden

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

98 files changed

+179
-341
lines changed

Demo/scripts/newslist.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
rcfile = os.path.join(dir, '.newslistrc.py')
100100
if os.path.exists(rcfile):
101101
print(rcfile)
102-
execfile(rcfile)
102+
exec(open(rcfile).read())
103103
break
104104

105105
from nntplib import NNTP

Demo/scripts/pp.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,9 @@
123123
fp = tempfile.NamedTemporaryFile()
124124
fp.write(program)
125125
fp.flush()
126+
script = open(tfn).read()
126127
if DFLAG:
127128
import pdb
128-
pdb.run('execfile(%r)' % (tfn,))
129+
pdb.run(script)
129130
else:
130-
execfile(tfn)
131+
exec(script)

Doc/dist/dist.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2290,7 +2290,7 @@ \section{\module{distutils.core} --- Core Distutils functionality}
22902290
(passed as keyword args from \var{script} to \function{setup()}), or
22912291
the contents of the config files or command-line.
22922292

2293-
\var{script_name} is a file that will be run with \function{execfile()}
2293+
\var{script_name} is a file that will be read and run with \function{exec()}
22942294
\code{sys.argv[0]} will be replaced with \var{script} for the duration of the
22952295
call. \var{script_args} is a list of strings; if supplied,
22962296
\code{sys.argv[1:]} will be replaced by \var{script_args} for the duration

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 \function{exec}, \function{execfile} and friends}
84+
\subsection{Unadorned \function{exec} 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.
@@ -97,7 +97,7 @@ \subsection{Unadorned \function{exec}, \function{execfile} and friends}
9797
>>> def func(s, **kw):
9898
>>> for var, val in kw.items():
9999
>>> exec("s.%s=val" % var) # invalid!
100-
>>> execfile("handler.py")
100+
>>> exec(open("handler.py").read())
101101
>>> handle()
102102
\end{verbatim}
103103

@@ -111,7 +111,7 @@ \subsection{Unadorned \function{exec}, \function{execfile} and friends}
111111
>>> for var, val in kw.items():
112112
>>> setattr(s, var, val)
113113
>>> d={}
114-
>>> execfile("handle.py", d, d)
114+
>>> exec(open("handler.py").read(), d, d)
115115
>>> handle = d['handle']
116116
>>> handle()
117117
\end{verbatim}

Doc/lib/libdoctest.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1828,7 +1828,7 @@ \subsection{Debugging\label{doctest-debugging}}
18281828
via \function{\refmodule{pdb}.post_mortem()}, passing the traceback object
18291829
from the unhandled exception. If \var{pm} is not specified, or is false,
18301830
the script is run under the debugger from the start, via passing an
1831-
appropriate \function{execfile()} call to \function{\refmodule{pdb}.run()}.
1831+
appropriate \function{exec()} call to \function{\refmodule{pdb}.run()}.
18321832

18331833
\versionadded{2.3}
18341834

Doc/lib/libexcs.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -260,7 +260,7 @@ \section{Built-in Exceptions}
260260
% XXXJH xref to these functions?
261261
Raised when the parser encounters a syntax error. This may occur in
262262
an \keyword{import} statement, in a call to the built-in functions
263-
\function{exec()}, \function{execfile()}, \function{eval()} or
263+
\function{exec()}, \function{eval()} or
264264
\function{input()}, or when reading the initial script or standard
265265
input (also interactively).
266266

Doc/lib/libfuncs.tex

Lines changed: 2 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -382,15 +382,13 @@ \section{Built-in Functions \label{built-in-funcs}}
382382
compiled passing \code{'eval'} as the \var{kind} argument.
383383

384384
Hints: dynamic execution of statements is supported by the
385-
\function{exec()} function. Execution of statements from a file is
386-
supported by the \function{execfile()} function. The
385+
\function{exec()} function. The
387386
\function{globals()} and \function{locals()} functions returns the
388387
current global and local dictionary, respectively, which may be
389388
useful to pass around for use by \function{eval()} or
390-
\function{execfile()}.
389+
\function{exec()}.
391390
\end{funcdesc}
392391

393-
394392
\begin{funcdesc}{exec}{object\optional{, globals\optional{, locals}}}
395393
This function supports dynamic execution of Python code.
396394
\var{object} must be either a string, an open file object, or
@@ -425,31 +423,6 @@ \section{Built-in Functions \label{built-in-funcs}}
425423
argument to \function{exec()}.}
426424
\end{funcdesc}
427425

428-
\begin{funcdesc}{execfile}{filename\optional{, globals\optional{, locals}}}
429-
This function is similar to the \function{exec()} function, but parses a
430-
file given by the file name instead of a string. It
431-
is different from the \keyword{import} statement in that it does not
432-
use the module administration --- it reads the file unconditionally
433-
and does not create a new module.
434-
435-
The arguments are a file name and two optional dictionaries. The file is
436-
parsed and evaluated as a sequence of Python statements (similarly to a
437-
module) using the \var{globals} and \var{locals} dictionaries as global and
438-
local namespace. If provided, \var{locals} can be any mapping object.
439-
\versionchanged[formerly \var{locals} was required to be a dictionary]{2.4}
440-
If the \var{locals} dictionary is omitted it defaults to the \var{globals}
441-
dictionary. If both dictionaries are omitted, the expression is executed in
442-
the environment where \function{execfile()} is called. The return value is
443-
\code{None}.
444-
445-
\warning{The default \var{locals} act as described for function
446-
\function{locals()} below: modifications to the default \var{locals}
447-
dictionary should not be attempted. Pass an explicit \var{locals}
448-
dictionary if you need to see effects of the code on \var{locals} after
449-
function \function{execfile()} returns. \function{execfile()} cannot
450-
be used reliably to modify a function's locals.}
451-
\end{funcdesc}
452-
453426
\begin{funcdesc}{file}{filename\optional{, mode\optional{, bufsize}}}
454427
Constructor function for the \class{file} type, described further
455428
in section~\ref{bltin-file-objects}, ``\ulink{File

Doc/lib/libuser.tex

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ \section{\module{user} ---
2424

2525
The \module{user} module looks for a file \file{.pythonrc.py} in the user's
2626
home directory and if it can be opened, executes it (using
27-
\function{execfile()}\bifuncindex{execfile}) in its own (the
27+
\function{exec()}\bifuncindex{exec}) in its own (the
2828
module \module{user}'s) global namespace. Errors during this phase
2929
are not caught; that's up to the program that imports the
3030
\module{user} module, if it wishes. The home directory is assumed to

Doc/ref/ref4.tex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,7 @@ \section{Naming and binding \label{naming}}
1919
interpreter or specified on the interpreter command line the first
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
22-
block. The file read by the built-in function \function{execfile()}
23-
is a code block. The string argument passed to the built-in functions
22+
block. The string argument passed to the built-in functions
2423
\function{eval()} and \function{exec()} is a code block.
2524
The expression read and evaluated by the built-in function
2625
\function{input()} is a code block.
@@ -139,15 +138,15 @@ \subsection{Interaction with dynamic features \label{dynamic-features}}
139138
function and the function contains or is a nested block with free
140139
variables, the compiler will raise a \exception{SyntaxError}.
141140

142-
The \function{eval()}, \function{exec()}, \function{execfile()},
141+
The \function{eval()}, \function{exec()},
143142
and \function{input()} functions do not have access to the
144143
full environment for resolving names. Names may be resolved in the
145144
local and global namespaces of the caller. Free variables are not
146145
resolved in the nearest enclosing namespace, but in the global
147146
namespace.\footnote{This limitation occurs because the code that is
148147
executed by these operations is not available at the time the
149148
module is compiled.}
150-
The \function{exec()}, \function{eval()} and \function{execfile()}
149+
The \function{exec()} and \function{eval()}
151150
functions have optional arguments to override
152151
the global and local namespace. If only one namespace is specified,
153152
it is used for both.

Doc/ref/ref6.tex

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,8 +760,8 @@ \subsection{Future statements \label{future}}
760760
That is not a future statement; it's an ordinary import statement with
761761
no special semantics or syntax restrictions.
762762

763-
Code compiled by calls to the builtin functions \function{exec()},
764-
\function{compile()} and \function{execfile()} that occur in a module
763+
Code compiled by calls to the builtin functions \function{exec()} and
764+
\function{compile()} that occur in a module
765765
\module{M} containing a future statement will, by default, use the new
766766
syntax or semantics associated with the future statement. This can,
767767
starting with Python 2.2 be controlled by optional arguments to
@@ -811,9 +811,8 @@ \section{The \keyword{global} statement \label{global}}
811811
does not affect the code block \emph{containing} the function call,
812812
and code contained in such a string is unaffected by \keyword{global}
813813
statements in the code containing the function call. The same applies to the
814-
\function{eval()}, \function{execfile()} and \function{compile()} functions.
814+
\function{eval()} and \function{compile()} functions.
815815
\bifuncindex{exec}
816816
\bifuncindex{eval}
817-
\bifuncindex{execfile}
818817
\bifuncindex{compile}
819818

0 commit comments

Comments
 (0)