blob: 90053371654eceb246c417fc7db7f3e02746a689 [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}
Fred Drake57657bc2000-12-01 15:25:23 +00007\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
Michael W. Hudson53da3172001-08-27 20:02:17 +00008\sectionauthor{Michael Hudson}{mwh@python.net}
Fred Drakeb742a421999-06-23 13:33:40 +00009\modulesynopsis{Compile (possibly incomplete) Python code.}
10
Michael W. Hudson53da3172001-08-27 20:02:17 +000011The \module{codeop} module provides utilities upon which the Python
Fred Drake5cb29a42001-08-28 14:25:03 +000012read-eval-print loop can be emulated, as is done in the
13\refmodule{code} module. As a result, you probably don't want to use
14the module directly; if you want to include such a loop in your
15program you probably want to use the \refmodule{code} module instead.
Fred Drakeb742a421999-06-23 13:33:40 +000016
Michael W. Hudson53da3172001-08-27 20:02:17 +000017There are two parts to this job:
18
Fred Drake5cb29a42001-08-28 14:25:03 +000019\begin{enumerate}
20 \item Being able to tell if a line of input completes a Python
21 statement: in short, telling whether to print
22 `\code{>\code{>}>~} or `\code{...~}' next.
23 \item Remembering which future statements the user has entered, so
24 subsequent input can be compiled with these in effect.
25\end{enumerate}
Michael W. Hudson53da3172001-08-27 20:02:17 +000026
27The \module{codeop} module provides a way of doing each of these
28things, and a way of doing them both.
29
30To do just the former:
Fred Drakeb742a421999-06-23 13:33:40 +000031
32\begin{funcdesc}{compile_command}
33 {source\optional{, filename\optional{, symbol}}}
Fred Drake38e5d272000-04-03 20:13:55 +000034Tries to compile \var{source}, which should be a string of Python
35code and return a code object if \var{source} is valid
Fred Drakeb742a421999-06-23 13:33:40 +000036Python code. In that case, the filename attribute of the code object
37will be \var{filename}, which defaults to \code{'<input>'}.
Fred Drake38e5d272000-04-03 20:13:55 +000038Returns \code{None} if \var{source} is \emph{not} valid Python
Fred Drakeb742a421999-06-23 13:33:40 +000039code, but is a prefix of valid Python code.
40
Fred Drake38e5d272000-04-03 20:13:55 +000041If there is a problem with \var{source}, an exception will be raised.
42\exception{SyntaxError} is raised if there is invalid Python syntax,
Michael W. Hudson53da3172001-08-27 20:02:17 +000043and \exception{OverflowError} or \exception{ValueError} if there is an
44invalid literal.
Fred Drakeb742a421999-06-23 13:33:40 +000045
Fred Drake38e5d272000-04-03 20:13:55 +000046The \var{symbol} argument determines whether \var{source} is compiled
47as a statement (\code{'single'}, the default) or as an expression
48(\code{'eval'}). Any other value will cause \exception{ValueError} to
49be raised.
Fred Drakeb742a421999-06-23 13:33:40 +000050
51\strong{Caveat:}
52It is possible (but not likely) that the parser stops parsing
53with a successful outcome before reaching the end of the source;
54in this case, trailing symbols may be ignored instead of causing an
55error. For example, a backslash followed by two newlines may be
56followed by arbitrary garbage. This will be fixed once the API
57for the parser is better.
58\end{funcdesc}
Michael W. Hudson53da3172001-08-27 20:02:17 +000059
60\begin{classdesc}{Compile}{}
Fred Drake5cb29a42001-08-28 14:25:03 +000061Instances of this class have \method{__call__()} methods indentical in
62signature to the built-in function \function{compile()}, but with the
Michael W. Hudson53da3172001-08-27 20:02:17 +000063difference that if the instance compiles program text containing a
64\module{__future__} statement, the instance 'remembers' and compiles
65all subsequent program texts with the statement in force.
66\end{classdesc}
67
68\begin{classdesc}{CommandCompiler}{}
Fred Drake5cb29a42001-08-28 14:25:03 +000069Instances of this class have \method{__call__()} methods identical in
70signature to \function{compile_command()}; the difference is that if
71the instance compiles program text containing a \code{__future__}
Michael W. Hudson53da3172001-08-27 20:02:17 +000072statement, the instance 'remembers' and compiles all subsequent
73program texts with the statement in force.
74\end{classdesc}
75
76A note on version compatibility: the \class{Compile} and
77\class{CommandCompiler} are new in Python 2.2. If you want to enable
78the future-tracking features of 2.2 but also retain compatibility with
792.1 and earlier versions of Python you can either write
80
81\begin{verbatim}
82try:
83 from codeop import CommandCompiler
84 compile_command = CommandCompiler()
85 del CommandCompiler
86except ImportError:
87 from codeop import compile_command
88\end{verbatim}
89
90which is a low-impact change, but introduces possibly unwanted global
91state into your program, or you can write:
92
93\begin{verbatim}
94try:
95 from codeop import CommandCompiler
96except ImportError:
97 def CommandCompiler():
98 from codeop import compile_command
99 return compile_comamnd
100\end{verbatim}
101
102and then call \code{CommandCompiler} every time you need a fresh
103compiler object.