<!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-codeop.html" />
<link rel=
"prev" href=
"module-modulefinder.html" />
<link rel=
"parent" href=
"python.html" />
<link rel=
"next" href=
"interpreter-objects.html" />
<meta name='aesop' content='information'
/>
<title>3.25 code -- Interpreter base classes
</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.24 modulefinder "
href=
"module-modulefinder.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.25.1 Interactive Interpreter Objects"
href=
"interpreter-objects.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=
"module-modulefinder.html">3.24 modulefinder
</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=
"interpreter-objects.html">3.25.1 Interactive Interpreter Objects
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION0052500000000000000000">
3.25 <tt class=
"module">code
</tt> --
Interpreter base classes
</A>
<A NAME=
"module-code"></A>
The
<code>code
</code> module provides facilities to implement
read-eval-print loops in Python. Two classes and convenience
functions are included which can be used to build applications which
provide an interactive interpreter prompt.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
744' xml:id='l2h-
744'
class=
"class">InteractiveInterpreter
</tt></b>(
</nobr></td>
<td><var></var><big>[
</big><var>locals
</var><big>]
</big><var></var>)
</td></tr></table></dt>
This class deals with parsing and interpreter state (the user's
namespace); it does not deal with input buffering or prompting or
input file naming (the filename is always passed in explicitly).
The optional
<var>locals
</var> argument specifies the dictionary in
which code will be executed; it defaults to a newly created
dictionary with key
<code>'__name__'
</code> set to
<code>'__console__'
</code>
and key
<code>'__doc__'
</code> set to
<code>None
</code>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
745' xml:id='l2h-
745'
class=
"class">InteractiveConsole
</tt></b>(
</nobr></td>
<td><var></var><big>[
</big><var>locals
</var><big>[
</big><var>, filename
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Closely emulate the behavior of the interactive Python interpreter.
This class builds on
<tt class=
"class">InteractiveInterpreter
</tt> and adds
prompting using the familiar
<code>sys.ps1
</code> and
<code>sys.ps2
</code>, and
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
746' xml:id='l2h-
746'
class=
"function">interact
</tt></b>(
</nobr></td>
<td><var></var><big>[
</big><var>banner
</var><big>[
</big><var>,
readfunc
</var><big>[
</big><var>, local
</var><big>]
</big><var></var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Convenience function to run a read-eval-print loop. This creates a
new instance of
<tt class=
"class">InteractiveConsole
</tt> and sets
<var>readfunc
</var>
to be used as the
<tt class=
"method">raw_input()
</tt> method, if provided. If
<var>local
</var> is provided, it is passed to the
<tt class=
"class">InteractiveConsole
</tt> constructor for use as the default
namespace for the interpreter loop. The
<tt class=
"method">interact()
</tt> method
of the instance is then run with
<var>banner
</var> passed as the banner
to use, if provided. The console object is discarded after use.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
747' xml:id='l2h-
747'
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>
This function is useful for programs that want to emulate Python's
interpreter main loop (a.k.a. the read-eval-print loop). The tricky
part is to determine when the user has entered an incomplete command
that can be completed by entering more text (as opposed to a
complete command or a syntax error). This function
<em>almost
</em> always makes the same decision as the real interpreter
<var>source
</var> is the source string;
<var>filename
</var> is the optional
filename from which source was read, defaulting to
<code>'
<input
>'
</code>;
and
<var>symbol
</var> is the optional grammar start symbol, which should
be either
<code>'single'
</code> (the default) or
<code>'eval'
</code>.
Returns a code object (the same as
<code>compile(
<var>source
</var>,
<var>filename
</var>,
<var>symbol
</var>)
</code>) if the command is complete and
valid;
<code>None
</code> if the command is incomplete; raises
<tt class=
"exception">SyntaxError
</tt> if the command is complete and contains a
syntax error, or raises
<tt class=
"exception">OverflowError
</tt> or
<tt class=
"exception">ValueError
</tt> if the command contains an invalid literal.
<p><br /></p><hr class='online-navigation'
/>
<div class='online-navigation'
>
<!--Table of Child-Links-->
<A NAME=
"CHILD_LINKS"><STRONG>Subsections
</STRONG></a>
<LI><A href=
"interpreter-objects.html">3.25.1 Interactive Interpreter Objects
</a>
<LI><A href=
"console-objects.html">3.25.2 Interactive Console Objects
</a>
<!--End of Table of Child-Links-->
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"3.24 modulefinder "
href=
"module-modulefinder.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.25.1 Interactive Interpreter Objects"
href=
"interpreter-objects.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=
"module-modulefinder.html">3.24 modulefinder
</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=
"interpreter-objects.html">3.25.1 Interactive Interpreter Objects
</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.