rest25/library/code.rst => rest262/library/code.rst
f1
2:mod:`code` --- Interpreter base classes
3========================================
4
5.. module:: code
n6- 
n6+   :synopsis: Facilities to implement read-eval-print loops.
7
8
9
10The ``code`` module provides facilities to implement read-eval-print loops in
11Python.  Two classes and convenience functions are included which can be used to
12build applications which provide an interactive interpreter prompt.
13
14
62
63
64.. _interpreter-objects:
65
66Interactive Interpreter Objects
67-------------------------------
68
69
n70-.. method:: XXX Class.runsource(source[, filename[, symbol]])
n70+.. method:: InteractiveInterpreter.runsource(source[, filename[, symbol]])
71
72   Compile and run some source in the interpreter. Arguments are the same as for
73   :func:`compile_command`; the default for *filename* is ``'<input>'``, and for
74   *symbol* is ``'single'``.  One several things can happen:
75
n76-* The input is incorrect; :func:`compile_command` raised an exception
n76+   * The input is incorrect; :func:`compile_command` raised an exception
77     (:exc:`SyntaxError` or :exc:`OverflowError`).  A syntax traceback will be
78     printed by calling the :meth:`showsyntaxerror` method.  :meth:`runsource`
79     returns ``False``.
80
n81-* The input is incomplete, and more input is required; :func:`compile_command`
n81+   * The input is incomplete, and more input is required; :func:`compile_command`
82     returned ``None``. :meth:`runsource` returns ``True``.
83
n84-* The input is complete; :func:`compile_command` returned a code object.  The
n84+   * The input is complete; :func:`compile_command` returned a code object.  The
85     code is executed by calling the :meth:`runcode` (which also handles run-time
86     exceptions, except for :exc:`SystemExit`). :meth:`runsource` returns ``False``.
87
88   The return value can be used to decide whether to use ``sys.ps1`` or ``sys.ps2``
89   to prompt the next line.
90
91
n92-.. method:: XXX Class.runcode(code)
n92+.. method:: InteractiveInterpreter.runcode(code)
93
94   Execute a code object. When an exception occurs, :meth:`showtraceback` is called
95   to display a traceback.  All exceptions are caught except :exc:`SystemExit`,
96   which is allowed to propagate.
97
98   A note about :exc:`KeyboardInterrupt`: this exception may occur elsewhere in
99   this code, and may not always be caught.  The caller should be prepared to deal
100   with it.
101
102
n103-.. method:: XXX Class.showsyntaxerror([filename])
n103+.. method:: InteractiveInterpreter.showsyntaxerror([filename])
104
105   Display the syntax error that just occurred.  This does not display a stack
106   trace because there isn't one for syntax errors. If *filename* is given, it is
107   stuffed into the exception instead of the default filename provided by Python's
108   parser, because it always uses ``'<string>'`` when reading from a string. The
109   output is written by the :meth:`write` method.
110
111
n112-.. method:: XXX Class.showtraceback()
n112+.. method:: InteractiveInterpreter.showtraceback()
113
114   Display the exception that just occurred.  We remove the first stack item
115   because it is within the interpreter object implementation. The output is
116   written by the :meth:`write` method.
117
118
n119-.. method:: XXX Class.write(data)
n119+.. method:: InteractiveInterpreter.write(data)
120
121   Write a string to the standard error stream (``sys.stderr``). Derived classes
122   should override this to provide the appropriate output handling as needed.
123
124
125.. _console-objects:
126
127Interactive Console Objects
128---------------------------
129
130The :class:`InteractiveConsole` class is a subclass of
131:class:`InteractiveInterpreter`, and so offers all the methods of the
132interpreter objects as well as the following additions.
133
134
n135-.. method:: XXX Class.interact([banner])
n135+.. method:: InteractiveConsole.interact([banner])
136
137   Closely emulate the interactive Python console. The optional banner argument
138   specify the banner to print before the first interaction; by default it prints a
139   banner similar to the one printed by the standard Python interpreter, followed
140   by the class name of the console object in parentheses (so as not to confuse
141   this with the real interpreter -- since it's so close!).
142
143
n144-.. method:: XXX Class.push(line)
n144+.. method:: InteractiveConsole.push(line)
145
146   Push a line of source text to the interpreter. The line should not have a
147   trailing newline; it may have internal newlines.  The line is appended to a
148   buffer and the interpreter's :meth:`runsource` method is called with the
149   concatenated contents of the buffer as source.  If this indicates that the
150   command was executed or invalid, the buffer is reset; otherwise, the command is
151   incomplete, and the buffer is left as it was after the line was appended.  The
152   return value is ``True`` if more input is required, ``False`` if the line was
153   dealt with in some way (this is the same as :meth:`runsource`).
154
155
n156-.. method:: XXX Class.resetbuffer()
n156+.. method:: InteractiveConsole.resetbuffer()
157
158   Remove any unhandled source text from the input buffer.
159
160
t161-.. method:: XXX Class.raw_input([prompt])
t161+.. method:: InteractiveConsole.raw_input([prompt])
162
163   Write a prompt and read a line.  The returned line does not include the trailing
164   newline.  When the user enters the EOF key sequence, :exc:`EOFError` is raised.
165   The base implementation uses the built-in function :func:`raw_input`; a subclass
166   may replace this with a different implementation.
167
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op