@@ -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
0 commit comments