@@ -5,14 +5,29 @@ \section{\module{codeop} ---
55
66\declaremodule {standard}{codeop}
77\sectionauthor {Moshe Zadka}{
[email protected] }
8+ \sectionauthor {Michael Hudson}{
[email protected] }
89\modulesynopsis {Compile (possibly incomplete) Python code.}
910
10- The \module {codeop} module provides a function to compile Python code
11- with hints on whether it is certainly complete, possibly complete or
12- definitely incomplete. This is used by the \refmodule {code} module
13- and should not normally be used directly.
11+ The \module {codeop} module provides utilities upon which the Python
12+ read-eval-print loop can be emulated -- as in the \refmodule {code}
13+ module. As a result, you probably don't want to use the module
14+ directly -- if you want to include such a loop in your program you
15+ probably want to use the \refmodule {code} instead.
1416
15- The \module {codeop} module defines the following function:
17+ There are two parts to this job:
18+
19+ \begin {list }
20+ \listitem Being able to tell if a line of input completes a Python
21+ statement -- in short telling whether to print `` >>> '' or
22+ `` ... '' next.
23+ \listitem Remembering which future statements the user has entered, so
24+ subsequent input can be compiled wiht these in effect.
25+ \end {list }
26+
27+ The \module {codeop} module provides a way of doing each of these
28+ things, and a way of doing them both.
29+
30+ To do just the former:
1631
1732\begin {funcdesc }{compile_command}
1833 {source\optional {, filename\optional {, symbol}}}
@@ -25,8 +40,8 @@ \section{\module{codeop} ---
2540
2641If there is a problem with \var {source}, an exception will be raised.
2742\exception {SyntaxError} is raised if there is invalid Python syntax,
28- and \exception {OverflowError} if there is an invalid numeric
29- constant .
43+ and \exception {OverflowError} or \exception {ValueError} if there is an
44+ invalid literal .
3045
3146The \var {symbol} argument determines whether \var {source} is compiled
3247as a statement (\code {'single'}, the default) or as an expression
@@ -41,3 +56,48 @@ \section{\module{codeop} ---
4156followed by arbitrary garbage. This will be fixed once the API
4257for the parser is better.
4358\end {funcdesc }
59+
60+ \begin {classdesc }{Compile}{}
61+ Instances of this class have \method {__call__} methods indentical in
62+ signature to the built-in function \function {compile}, but with the
63+ difference that if the instance compiles program text containing a
64+ \module {__future__} statement, the instance 'remembers' and compiles
65+ all subsequent program texts with the statement in force.
66+ \end {classdesc }
67+
68+ \begin {classdesc }{CommandCompiler}{}
69+ Instances of this class have \method {__call__} methods identical in
70+ signature to \function {compile_command}; the difference is that if the
71+ instance compiles program text containing a \method {__future__}
72+ statement, the instance 'remembers' and compiles all subsequent
73+ program texts with the statement in force.
74+ \end {classdesc }
75+
76+ A note on version compatibility: the \class {Compile} and
77+ \class {CommandCompiler} are new in Python 2.2. If you want to enable
78+ the future-tracking features of 2.2 but also retain compatibility with
79+ 2.1 and earlier versions of Python you can either write
80+
81+ \begin {verbatim }
82+ try:
83+ from codeop import CommandCompiler
84+ compile_command = CommandCompiler()
85+ del CommandCompiler
86+ except ImportError:
87+ from codeop import compile_command
88+ \end {verbatim }
89+
90+ which is a low-impact change, but introduces possibly unwanted global
91+ state into your program, or you can write:
92+
93+ \begin {verbatim }
94+ try:
95+ from codeop import CommandCompiler
96+ except ImportError:
97+ def CommandCompiler():
98+ from codeop import compile_command
99+ return compile_comamnd
100+ \end {verbatim }
101+
102+ and then call \code {CommandCompiler} every time you need a fresh
103+ compiler object.
0 commit comments