| 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="console-objects.html" /> |
| 13 | <link rel="prev" href="module-code.html" /> |
| 14 | <link rel="parent" href="module-code.html" /> |
| 15 | <link rel="next" href="console-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>3.25.1 Interactive Interpreter Objects </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.25 code " |
| 25 | href="module-code.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.25 code " |
| 28 | href="module-code.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.2 Interactive Console Objects" |
| 31 | href="console-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-code.html">3.25 code </A> |
| 46 | <b class="navlabel">Up:</b> |
| 47 | <a class="sectref" rel="parent" href="module-code.html">3.25 code </A> |
| 48 | <b class="navlabel">Next:</b> |
| 49 | <a class="sectref" rel="next" href="console-objects.html">3.25.2 Interactive Console Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0052510000000000000000"></A><A NAME="interpreter-objects"></A> |
| 56 | <BR> |
| 57 | 3.25.1 Interactive Interpreter Objects |
| 58 | |
| 59 | </H2> |
| 60 | |
| 61 | <P> |
| 62 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 63 | <td><nobr><b><tt id='l2h-748' xml:id='l2h-748' class="method">runsource</tt></b>(</nobr></td> |
| 64 | <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> |
| 65 | <dd> |
| 66 | Compile and run some source in the interpreter. |
| 67 | Arguments are the same as for <tt class="function">compile_command()</tt>; the |
| 68 | default for <var>filename</var> is <code>'<input>'</code>, and for |
| 69 | <var>symbol</var> is <code>'single'</code>. One several things can happen: |
| 70 | |
| 71 | <P> |
| 72 | |
| 73 | <UL> |
| 74 | <LI>The input is incorrect; <tt class="function">compile_command()</tt> raised an |
| 75 | exception (<tt class="exception">SyntaxError</tt> or <tt class="exception">OverflowError</tt>). A |
| 76 | syntax traceback will be printed by calling the |
| 77 | <tt class="method">showsyntaxerror()</tt> method. <tt class="method">runsource()</tt> returns |
| 78 | <code>False</code>. |
| 79 | |
| 80 | <P> |
| 81 | </LI> |
| 82 | <LI>The input is incomplete, and more input is required; |
| 83 | <tt class="function">compile_command()</tt> returned <code>None</code>. |
| 84 | <tt class="method">runsource()</tt> returns <code>True</code>. |
| 85 | |
| 86 | <P> |
| 87 | </LI> |
| 88 | <LI>The input is complete; <tt class="function">compile_command()</tt> returned a code |
| 89 | object. The code is executed by calling the <tt class="method">runcode()</tt> (which |
| 90 | also handles run-time exceptions, except for <tt class="exception">SystemExit</tt>). |
| 91 | <tt class="method">runsource()</tt> returns <code>False</code>. |
| 92 | </LI> |
| 93 | </UL> |
| 94 | |
| 95 | <P> |
| 96 | The return value can be used to decide whether to use |
| 97 | <code>sys.ps1</code> or <code>sys.ps2</code> to prompt the next line. |
| 98 | </dl> |
| 99 | |
| 100 | <P> |
| 101 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 102 | <td><nobr><b><tt id='l2h-749' xml:id='l2h-749' class="method">runcode</tt></b>(</nobr></td> |
| 103 | <td><var>code</var>)</td></tr></table></dt> |
| 104 | <dd> |
| 105 | Execute a code object. |
| 106 | When an exception occurs, <tt class="method">showtraceback()</tt> is called to |
| 107 | display a traceback. All exceptions are caught except |
| 108 | <tt class="exception">SystemExit</tt>, which is allowed to propagate. |
| 109 | |
| 110 | <P> |
| 111 | A note about <tt class="exception">KeyboardInterrupt</tt>: this exception may occur |
| 112 | elsewhere in this code, and may not always be caught. The caller |
| 113 | should be prepared to deal with it. |
| 114 | </dl> |
| 115 | |
| 116 | <P> |
| 117 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 118 | <td><nobr><b><tt id='l2h-750' xml:id='l2h-750' class="method">showsyntaxerror</tt></b>(</nobr></td> |
| 119 | <td><var></var><big>[</big><var>filename</var><big>]</big><var></var>)</td></tr></table></dt> |
| 120 | <dd> |
| 121 | Display the syntax error that just occurred. This does not display |
| 122 | a stack trace because there isn't one for syntax errors. |
| 123 | If <var>filename</var> is given, it is stuffed into the exception instead |
| 124 | of the default filename provided by Python's parser, because it |
| 125 | always uses <code>'<string>'</code> when reading from a string. |
| 126 | The output is written by the <tt class="method">write()</tt> method. |
| 127 | </dl> |
| 128 | |
| 129 | <P> |
| 130 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 131 | <td><nobr><b><tt id='l2h-751' xml:id='l2h-751' class="method">showtraceback</tt></b>(</nobr></td> |
| 132 | <td><var></var>)</td></tr></table></dt> |
| 133 | <dd> |
| 134 | Display the exception that just occurred. We remove the first stack |
| 135 | item because it is within the interpreter object implementation. |
| 136 | The output is written by the <tt class="method">write()</tt> method. |
| 137 | </dl> |
| 138 | |
| 139 | <P> |
| 140 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 141 | <td><nobr><b><tt id='l2h-752' xml:id='l2h-752' class="method">write</tt></b>(</nobr></td> |
| 142 | <td><var>data</var>)</td></tr></table></dt> |
| 143 | <dd> |
| 144 | Write a string to the standard error stream (<code>sys.stderr</code>). |
| 145 | Derived classes should override this to provide the appropriate output |
| 146 | handling as needed. |
| 147 | </dl> |
| 148 | |
| 149 | <P> |
| 150 | |
| 151 | <DIV CLASS="navigation"> |
| 152 | <div class='online-navigation'> |
| 153 | <p></p><hr /> |
| 154 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 155 | <tr> |
| 156 | <td class='online-navigation'><a rel="prev" title="3.25 code " |
| 157 | href="module-code.html"><img src='../icons/previous.png' |
| 158 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 159 | <td class='online-navigation'><a rel="parent" title="3.25 code " |
| 160 | href="module-code.html"><img src='../icons/up.png' |
| 161 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 162 | <td class='online-navigation'><a rel="next" title="3.25.2 Interactive Console Objects" |
| 163 | href="console-objects.html"><img src='../icons/next.png' |
| 164 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 165 | <td align="center" width="100%">Python Library Reference</td> |
| 166 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 167 | href="contents.html"><img src='../icons/contents.png' |
| 168 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 169 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 170 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 171 | <td class='online-navigation'><a rel="index" title="Index" |
| 172 | href="genindex.html"><img src='../icons/index.png' |
| 173 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 174 | </tr></table> |
| 175 | <div class='online-navigation'> |
| 176 | <b class="navlabel">Previous:</b> |
| 177 | <a class="sectref" rel="prev" href="module-code.html">3.25 code </A> |
| 178 | <b class="navlabel">Up:</b> |
| 179 | <a class="sectref" rel="parent" href="module-code.html">3.25 code </A> |
| 180 | <b class="navlabel">Next:</b> |
| 181 | <a class="sectref" rel="next" href="console-objects.html">3.25.2 Interactive Console Objects</A> |
| 182 | </div> |
| 183 | </div> |
| 184 | <hr /> |
| 185 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 186 | </DIV> |
| 187 | <!--End of Navigation Panel--> |
| 188 | <ADDRESS> |
| 189 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 190 | </ADDRESS> |
| 191 | </BODY> |
| 192 | </HTML> |