<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"module-pprint.html" />
<link rel=
"prev" href=
"module-code.html" />
<link rel=
"parent" href=
"python.html" />
<link rel=
"next" href=
"module-pprint.html" />
<meta name='aesop' content='information'
/>
<title>3.26 codeop -- Compile Python code
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.25.2 Interactive Console Objects"
href=
"console-objects.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"3. Python Runtime Services"
href=
"python.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"3.27 pprint "
href=
"module-pprint.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"console-objects.html">3.25.2 Interactive Console Objects
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"python.html">3. Python Runtime Services
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-pprint.html">3.27 pprint
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION0052600000000000000000">
3.26 <tt class=
"module">codeop
</tt> --
<A NAME=
"module-codeop"></A>
The
<tt class=
"module">codeop
</tt> module provides utilities upon which the Python
read-eval-print loop can be emulated, as is done in the
<tt class=
"module"><a href=
"module-code.html">code
</a></tt> module. As a result, you probably don't want to use
the module directly; if you want to include such a loop in your
program you probably want to use the
<tt class=
"module"><a href=
"module-code.html">code
</a></tt> module instead.
There are two parts to this job:
<LI>Being able to tell if a line of input completes a Python
statement: in short, telling whether to print
`
<code>><code>></code>> </code>' or `
<code>...
</code>' next.
<LI>Remembering which future statements the user has entered, so
subsequent input can be compiled with these in effect.
The
<tt class=
"module">codeop
</tt> module provides a way of doing each of these
things, and a way of doing them both.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
758' xml:id='l2h-
758'
class=
"function">compile_command
</tt></b>(
</nobr></td>
<td><var>source
</var><big>[
</big><var>, filename
</var><big>[
</big><var>, symbol
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Tries to compile
<var>source
</var>, which should be a string of Python
code and return a code object if
<var>source
</var> is valid
Python code. In that case, the filename attribute of the code object
will be
<var>filename
</var>, which defaults to
<code>'
<input
>'
</code>.
Returns
<code>None
</code> if
<var>source
</var> is
<em>not
</em> valid Python
code, but is a prefix of valid Python code.
If there is a problem with
<var>source
</var>, an exception will be raised.
<tt class=
"exception">SyntaxError
</tt> is raised if there is invalid Python syntax,
and
<tt class=
"exception">OverflowError
</tt> or
<tt class=
"exception">ValueError
</tt> if there is an
The
<var>symbol
</var> argument determines whether
<var>source
</var> is compiled
as a statement (
<code>'single'
</code>, the default) or as an expression
(
<code>'eval'
</code>). Any other value will cause
<tt class=
"exception">ValueError
</tt> to
It is possible (but not likely) that the parser stops parsing
with a successful outcome before reaching the end of the source;
in this case, trailing symbols may be ignored instead of causing an
error. For example, a backslash followed by two newlines may be
followed by arbitrary garbage. This will be fixed once the API
for the parser is better.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
759' xml:id='l2h-
759'
class=
"class">Compile
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Instances of this class have
<tt class=
"method">__call__()
</tt> methods identical in
signature to the built-in function
<tt class=
"function">compile()
</tt>, but with the
difference that if the instance compiles program text containing a
<tt class=
"module">__future__
</tt> statement, the instance 'remembers' and compiles
all subsequent program texts with the statement in force.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
760' xml:id='l2h-
760'
class=
"class">CommandCompiler
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Instances of this class have
<tt class=
"method">__call__()
</tt> methods identical in
signature to
<tt class=
"function">compile_command()
</tt>; the difference is that if
the instance compiles program text containing a
<code>__future__
</code>
statement, the instance 'remembers' and compiles all subsequent
program texts with the statement in force.
A note on version compatibility: the
<tt class=
"class">Compile
</tt> and
<tt class=
"class">CommandCompiler
</tt> are new in Python
2.2. If you want to enable
the future-tracking features of
2.2 but also retain compatibility with
2.1 and earlier versions of Python you can either write
<div class=
"verbatim"><pre>
from codeop import CommandCompiler
compile_command = CommandCompiler()
from codeop import compile_command
which is a low-impact change, but introduces possibly unwanted global
state into your program, or you can write:
<div class=
"verbatim"><pre>
from codeop import CommandCompiler
from codeop import compile_command
and then call
<code>CommandCompiler
</code> every time you need a fresh
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.25.2 Interactive Console Objects"
href=
"console-objects.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"3. Python Runtime Services"
href=
"python.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"3.27 pprint "
href=
"module-pprint.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"console-objects.html">3.25.2 Interactive Console Objects
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"python.html">3. Python Runtime Services
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-pprint.html">3.27 pprint
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.