Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v9 / html / python / lib / module-code.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-codeop.html" />
13<link rel="prev" href="module-modulefinder.html" />
14<link rel="parent" href="python.html" />
15<link rel="next" href="interpreter-objects.html" />
16<meta name='aesop' content='information' />
17<title>3.25 code -- Interpreter base classes</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="3.24 modulefinder "
25 href="module-modulefinder.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
28 href="python.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="3.25.1 Interactive Interpreter Objects"
31 href="interpreter-objects.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="module-modulefinder.html">3.24 modulefinder </A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="interpreter-objects.html">3.25.1 Interactive Interpreter Objects</A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION0052500000000000000000">
563.25 <tt class="module">code</tt> --
57 Interpreter base classes</A>
58</H1>
59<A NAME="module-code"></A>
60<P>
61
62<P>
63The <code>code</code> module provides facilities to implement
64read-eval-print loops in Python. Two classes and convenience
65functions are included which can be used to build applications which
66provide an interactive interpreter prompt.
67
68<P>
69<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
70 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-744' xml:id='l2h-744' class="class">InteractiveInterpreter</tt></b>(</nobr></td>
71 <td><var></var><big>[</big><var>locals</var><big>]</big><var></var>)</td></tr></table></dt>
72<dd>
73This class deals with parsing and interpreter state (the user's
74namespace); it does not deal with input buffering or prompting or
75input file naming (the filename is always passed in explicitly).
76The optional <var>locals</var> argument specifies the dictionary in
77which code will be executed; it defaults to a newly created
78dictionary with key <code>'__name__'</code> set to <code>'__console__'</code>
79and key <code>'__doc__'</code> set to <code>None</code>.
80</dl>
81
82<P>
83<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
84 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-745' xml:id='l2h-745' class="class">InteractiveConsole</tt></b>(</nobr></td>
85 <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>
86<dd>
87Closely emulate the behavior of the interactive Python interpreter.
88This class builds on <tt class="class">InteractiveInterpreter</tt> and adds
89prompting using the familiar <code>sys.ps1</code> and <code>sys.ps2</code>, and
90input buffering.
91</dl>
92
93<P>
94<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
95 <td><nobr><b><tt id='l2h-746' xml:id='l2h-746' class="function">interact</tt></b>(</nobr></td>
96 <td><var></var><big>[</big><var>banner</var><big>[</big><var>,
97 readfunc</var><big>[</big><var>, local</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
98<dd>
99Convenience function to run a read-eval-print loop. This creates a
100new instance of <tt class="class">InteractiveConsole</tt> and sets <var>readfunc</var>
101to be used as the <tt class="method">raw_input()</tt> method, if provided. If
102<var>local</var> is provided, it is passed to the
103<tt class="class">InteractiveConsole</tt> constructor for use as the default
104namespace for the interpreter loop. The <tt class="method">interact()</tt> method
105of the instance is then run with <var>banner</var> passed as the banner
106to use, if provided. The console object is discarded after use.
107</dl>
108
109<P>
110<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
111 <td><nobr><b><tt id='l2h-747' xml:id='l2h-747' class="function">compile_command</tt></b>(</nobr></td>
112 <td><var>source</var><big>[</big><var>,
113 filename</var><big>[</big><var>, symbol</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
114<dd>
115This function is useful for programs that want to emulate Python's
116interpreter main loop (a.k.a. the read-eval-print loop). The tricky
117part is to determine when the user has entered an incomplete command
118that can be completed by entering more text (as opposed to a
119complete command or a syntax error). This function
120<em>almost</em> always makes the same decision as the real interpreter
121main loop.
122
123<P>
124<var>source</var> is the source string; <var>filename</var> is the optional
125filename from which source was read, defaulting to <code>'&lt;input&gt;'</code>;
126and <var>symbol</var> is the optional grammar start symbol, which should
127be either <code>'single'</code> (the default) or <code>'eval'</code>.
128
129<P>
130Returns a code object (the same as <code>compile(<var>source</var>,
131<var>filename</var>, <var>symbol</var>)</code>) if the command is complete and
132valid; <code>None</code> if the command is incomplete; raises
133<tt class="exception">SyntaxError</tt> if the command is complete and contains a
134syntax error, or raises <tt class="exception">OverflowError</tt> or
135<tt class="exception">ValueError</tt> if the command contains an invalid literal.
136</dl>
137
138<P>
139
140<p><br /></p><hr class='online-navigation' />
141<div class='online-navigation'>
142<!--Table of Child-Links-->
143<A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a>
144
145<UL CLASS="ChildLinks">
146<LI><A href="interpreter-objects.html">3.25.1 Interactive Interpreter Objects</a>
147<LI><A href="console-objects.html">3.25.2 Interactive Console Objects</a>
148</ul>
149<!--End of Table of Child-Links-->
150</div>
151
152<DIV CLASS="navigation">
153<div class='online-navigation'>
154<p></p><hr />
155<table align="center" width="100%" cellpadding="0" cellspacing="2">
156<tr>
157<td class='online-navigation'><a rel="prev" title="3.24 modulefinder "
158 href="module-modulefinder.html"><img src='../icons/previous.png'
159 border='0' height='32' alt='Previous Page' width='32' /></A></td>
160<td class='online-navigation'><a rel="parent" title="3. Python Runtime Services"
161 href="python.html"><img src='../icons/up.png'
162 border='0' height='32' alt='Up One Level' width='32' /></A></td>
163<td class='online-navigation'><a rel="next" title="3.25.1 Interactive Interpreter Objects"
164 href="interpreter-objects.html"><img src='../icons/next.png'
165 border='0' height='32' alt='Next Page' width='32' /></A></td>
166<td align="center" width="100%">Python Library Reference</td>
167<td class='online-navigation'><a rel="contents" title="Table of Contents"
168 href="contents.html"><img src='../icons/contents.png'
169 border='0' height='32' alt='Contents' width='32' /></A></td>
170<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
171 border='0' height='32' alt='Module Index' width='32' /></a></td>
172<td class='online-navigation'><a rel="index" title="Index"
173 href="genindex.html"><img src='../icons/index.png'
174 border='0' height='32' alt='Index' width='32' /></A></td>
175</tr></table>
176<div class='online-navigation'>
177<b class="navlabel">Previous:</b>
178<a class="sectref" rel="prev" href="module-modulefinder.html">3.24 modulefinder </A>
179<b class="navlabel">Up:</b>
180<a class="sectref" rel="parent" href="python.html">3. Python Runtime Services</A>
181<b class="navlabel">Next:</b>
182<a class="sectref" rel="next" href="interpreter-objects.html">3.25.1 Interactive Interpreter Objects</A>
183</div>
184</div>
185<hr />
186<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
187</DIV>
188<!--End of Navigation Panel-->
189<ADDRESS>
190See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
191</ADDRESS>
192</BODY>
193</HTML>