source: vendor/python/2.5/Doc/lib/libcodeop.tex

Last change on this file was 3225, checked in by bird, 18 years ago

Python 2.5

File size: 3.9 KB
Line 
1\section{\module{codeop} ---
2 Compile Python code}
3
4% LaTeXed from excellent doc-string.
5
6\declaremodule{standard}{codeop}
7\sectionauthor{Moshe Zadka}{moshez@zadka.site.co.il}
8\sectionauthor{Michael Hudson}{mwh@python.net}
9\modulesynopsis{Compile (possibly incomplete) Python code.}
10
11The \module{codeop} module provides utilities upon which the Python
12read-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.
16
17There are two parts to this job:
18
19\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{>>>~}' 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}
26
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:
31
32\begin{funcdesc}{compile_command}
33 {source\optional{, filename\optional{, symbol}}}
34Tries to compile \var{source}, which should be a string of Python
35code and return a code object if \var{source} is valid
36Python code. In that case, the filename attribute of the code object
37will be \var{filename}, which defaults to \code{'<input>'}.
38Returns \code{None} if \var{source} is \emph{not} valid Python
39code, but is a prefix of valid Python code.
40
41If there is a problem with \var{source}, an exception will be raised.
42\exception{SyntaxError} is raised if there is invalid Python syntax,
43and \exception{OverflowError} or \exception{ValueError} if there is an
44invalid literal.
45
46The \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.
50
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}
59
60\begin{classdesc}{Compile}{}
61Instances of this class have \method{__call__()} methods identical in
62signature to the built-in function \function{compile()}, but with the
63difference 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}{}
69Instances 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__}
72statement, 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_command
100\end{verbatim}
101
102and then call \code{CommandCompiler} every time you need a fresh
103compiler object.
Note: See TracBrowser for help on using the repository browser.