blob: cd9cb635fcbf6a06cfcc4e91434b398cbd733347 [file] [log] [blame]
Fred Drakeb742a421999-06-23 13:33:40 +00001\section{\module{codeop} ---
2 Compile Python code}
3
Fred Drake38e5d272000-04-03 20:13:55 +00004% LaTeXed from excellent doc-string.
5
Fred Drakeb742a421999-06-23 13:33:40 +00006\declaremodule{standard}{codeop}
7\sectionauthor{Moshe Zadka}{mzadka@geocities.com}
8\modulesynopsis{Compile (possibly incomplete) Python code.}
9
10The \module{codeop} module provides a function to compile Python code
Fred Drake38e5d272000-04-03 20:13:55 +000011with hints on whether it is certainly complete, possibly complete or
Fred Drakeb742a421999-06-23 13:33:40 +000012definitely incomplete. This is used by the \refmodule{code} module
13and should not normally be used directly.
14
15The \module{codeop} module defines the following function:
16
17\begin{funcdesc}{compile_command}
18 {source\optional{, filename\optional{, symbol}}}
Fred Drake38e5d272000-04-03 20:13:55 +000019Tries to compile \var{source}, which should be a string of Python
20code and return a code object if \var{source} is valid
Fred Drakeb742a421999-06-23 13:33:40 +000021Python code. In that case, the filename attribute of the code object
22will be \var{filename}, which defaults to \code{'<input>'}.
Fred Drake38e5d272000-04-03 20:13:55 +000023Returns \code{None} if \var{source} is \emph{not} valid Python
Fred Drakeb742a421999-06-23 13:33:40 +000024code, but is a prefix of valid Python code.
25
Fred Drake38e5d272000-04-03 20:13:55 +000026If there is a problem with \var{source}, an exception will be raised.
27\exception{SyntaxError} is raised if there is invalid Python syntax,
28and \exception{OverflowError} if there is an invalid numeric
29constant.
Fred Drakeb742a421999-06-23 13:33:40 +000030
Fred Drake38e5d272000-04-03 20:13:55 +000031The \var{symbol} argument determines whether \var{source} is compiled
32as a statement (\code{'single'}, the default) or as an expression
33(\code{'eval'}). Any other value will cause \exception{ValueError} to
34be raised.
Fred Drakeb742a421999-06-23 13:33:40 +000035
36\strong{Caveat:}
37It is possible (but not likely) that the parser stops parsing
38with a successful outcome before reaching the end of the source;
39in this case, trailing symbols may be ignored instead of causing an
40error. For example, a backslash followed by two newlines may be
41followed by arbitrary garbage. This will be fixed once the API
42for the parser is better.
43\end{funcdesc}