Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes
code.InteractiveConsole Class Reference
Inheritance diagram for code.InteractiveConsole:
Inheritance graph
[legend]
Collaboration diagram for code.InteractiveConsole:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def __init__
def resetbuffer
def interact
def push
def raw_input
def runsource
def runcode
def showsyntaxerror
def showtraceback
def write

Public Attributes

 filename
 buffer
 locals
 compile

Detailed Description

Closely emulate the behavior of the interactive Python interpreter.

This class builds on InteractiveInterpreter and adds prompting
using the familiar sys.ps1 and sys.ps2, and input buffering.

Definition at line 158 of file code.py.


Constructor & Destructor Documentation

def code.InteractiveConsole.__init__ (   self,
  locals = None,
  filename = "<console>" 
)
Constructor.

The optional locals argument will be passed to the
InteractiveInterpreter base class.

The optional filename argument should specify the (file)name
of the input stream; it will show up in tracebacks.

Definition at line 166 of file code.py.

00166 
00167     def __init__(self, locals=None, filename="<console>"):
00168         """Constructor.
00169 
00170         The optional locals argument will be passed to the
00171         InteractiveInterpreter base class.
00172 
00173         The optional filename argument should specify the (file)name
00174         of the input stream; it will show up in tracebacks.
00175 
00176         """
00177         InteractiveInterpreter.__init__(self, locals)
00178         self.filename = filename
00179         self.resetbuffer()

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def code.InteractiveConsole.interact (   self,
  banner = None 
)
Closely emulate the interactive Python console.

The optional banner argument specifies the banner to print
before the first interaction; by default it prints a banner
similar to the one printed by the real Python interpreter,
followed by the current class name in parentheses (so as not
to confuse this with the real interpreter -- since it's so
close!).

Definition at line 184 of file code.py.

00184 
00185     def interact(self, banner=None):
00186         """Closely emulate the interactive Python console.
00187 
00188         The optional banner argument specifies the banner to print
00189         before the first interaction; by default it prints a banner
00190         similar to the one printed by the real Python interpreter,
00191         followed by the current class name in parentheses (so as not
00192         to confuse this with the real interpreter -- since it's so
00193         close!).
00194 
00195         """
00196         try:
00197             sys.ps1
00198         except AttributeError:
00199             sys.ps1 = ">>> "
00200         try:
00201             sys.ps2
00202         except AttributeError:
00203             sys.ps2 = "... "
00204         cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
00205         if banner is None:
00206             self.write("Python %s on %s\n%s\n(%s)\n" %
00207                        (sys.version, sys.platform, cprt,
00208                         self.__class__.__name__))
00209         else:
00210             self.write("%s\n" % str(banner))
00211         more = 0
00212         while 1:
00213             try:
00214                 if more:
00215                     prompt = sys.ps2
00216                 else:
00217                     prompt = sys.ps1
00218                 try:
00219                     line = self.raw_input(prompt)
00220                 except EOFError:
00221                     self.write("\n")
00222                     break
00223                 else:
00224                     more = self.push(line)
00225             except KeyboardInterrupt:
00226                 self.write("\nKeyboardInterrupt\n")
00227                 self.resetbuffer()
00228                 more = 0

Here is the call graph for this function:

def code.InteractiveConsole.push (   self,
  line 
)
Push a line to the interpreter.

The line should not have a trailing newline; it may have
internal newlines.  The line is appended to a buffer and the
interpreter's runsource() method is called with the
concatenated contents of the buffer as source.  If this
indicates that the command was executed or invalid, the buffer
is reset; otherwise, the command is incomplete, and the buffer
is left as it was after the line was appended.  The return
value is 1 if more input is required, 0 if the line was dealt
with in some way (this is the same as runsource()).

Definition at line 229 of file code.py.

00229 
00230     def push(self, line):
00231         """Push a line to the interpreter.
00232 
00233         The line should not have a trailing newline; it may have
00234         internal newlines.  The line is appended to a buffer and the
00235         interpreter's runsource() method is called with the
00236         concatenated contents of the buffer as source.  If this
00237         indicates that the command was executed or invalid, the buffer
00238         is reset; otherwise, the command is incomplete, and the buffer
00239         is left as it was after the line was appended.  The return
00240         value is 1 if more input is required, 0 if the line was dealt
00241         with in some way (this is the same as runsource()).
00242 
00243         """
00244         self.buffer.append(line)
00245         source = "\n".join(self.buffer)
00246         more = self.runsource(source, self.filename)
00247         if not more:
00248             self.resetbuffer()
00249         return more

Here is the call graph for this function:

Here is the caller graph for this function:

def code.InteractiveConsole.raw_input (   self,
  prompt = "" 
)
Write a prompt and read a line.

The returned line does not include the trailing newline.
When the user enters the EOF key sequence, EOFError is raised.

The base implementation uses the built-in function
input(); a subclass may replace this with a different
implementation.

Definition at line 250 of file code.py.

00250 
00251     def raw_input(self, prompt=""):
00252         """Write a prompt and read a line.
00253 
00254         The returned line does not include the trailing newline.
00255         When the user enters the EOF key sequence, EOFError is raised.
00256 
00257         The base implementation uses the built-in function
00258         input(); a subclass may replace this with a different
00259         implementation.
00260 
00261         """
00262         return input(prompt)
00263 
00264 

Here is the call graph for this function:

Here is the caller graph for this function:

def code.InteractiveConsole.resetbuffer (   self)
Reset the input buffer.

Definition at line 180 of file code.py.

00180 
00181     def resetbuffer(self):
00182         """Reset the input buffer."""
00183         self.buffer = []

Here is the caller graph for this function:

def code.InteractiveInterpreter.runcode (   self,
  code 
) [inherited]
Execute a code object.

When an exception occurs, self.showtraceback() is called to
display a traceback.  All exceptions are caught except
SystemExit, which is reraised.

A note about KeyboardInterrupt: this exception may occur
elsewhere in this code, and may not always be caught.  The
caller should be prepared to deal with it.

Reimplemented in idlelib.PyShell.ModifiedInterpreter.

Definition at line 77 of file code.py.

00077 
00078     def runcode(self, code):
00079         """Execute a code object.
00080 
00081         When an exception occurs, self.showtraceback() is called to
00082         display a traceback.  All exceptions are caught except
00083         SystemExit, which is reraised.
00084 
00085         A note about KeyboardInterrupt: this exception may occur
00086         elsewhere in this code, and may not always be caught.  The
00087         caller should be prepared to deal with it.
00088 
00089         """
00090         try:
00091             exec(code, self.locals)
00092         except SystemExit:
00093             raise
00094         except:
00095             self.showtraceback()

Here is the call graph for this function:

Here is the caller graph for this function:

def code.InteractiveInterpreter.runsource (   self,
  source,
  filename = "<input>",
  symbol = "single" 
) [inherited]
Compile and run some source in the interpreter.

Arguments are as for compile_command().

One several things can happen:

1) The input is incorrect; compile_command() raised an
exception (SyntaxError or OverflowError).  A syntax traceback
will be printed by calling the showsyntaxerror() method.

2) The input is incomplete, and more input is required;
compile_command() returned None.  Nothing happens.

3) The input is complete; compile_command() returned a code
object.  The code is executed by calling self.runcode() (which
also handles run-time exceptions, except for SystemExit).

The return value is True in case 2, False in the other cases (unless
an exception is raised).  The return value can be used to
decide whether to use sys.ps1 or sys.ps2 to prompt the next
line.

Definition at line 38 of file code.py.

00038 
00039     def runsource(self, source, filename="<input>", symbol="single"):
00040         """Compile and run some source in the interpreter.
00041 
00042         Arguments are as for compile_command().
00043 
00044         One several things can happen:
00045 
00046         1) The input is incorrect; compile_command() raised an
00047         exception (SyntaxError or OverflowError).  A syntax traceback
00048         will be printed by calling the showsyntaxerror() method.
00049 
00050         2) The input is incomplete, and more input is required;
00051         compile_command() returned None.  Nothing happens.
00052 
00053         3) The input is complete; compile_command() returned a code
00054         object.  The code is executed by calling self.runcode() (which
00055         also handles run-time exceptions, except for SystemExit).
00056 
00057         The return value is True in case 2, False in the other cases (unless
00058         an exception is raised).  The return value can be used to
00059         decide whether to use sys.ps1 or sys.ps2 to prompt the next
00060         line.
00061 
00062         """
00063         try:
00064             code = self.compile(source, filename, symbol)
00065         except (OverflowError, SyntaxError, ValueError):
00066             # Case 1
00067             self.showsyntaxerror(filename)
00068             return False
00069 
00070         if code is None:
00071             # Case 2
00072             return True
00073 
00074         # Case 3
00075         self.runcode(code)
00076         return False

Here is the call graph for this function:

Here is the caller graph for this function:

def code.InteractiveInterpreter.showsyntaxerror (   self,
  filename = None 
) [inherited]
Display the syntax error that just occurred.

This doesn't display a stack trace because there isn't one.

If a filename is given, it is stuffed in the exception instead
of what was there before (because Python's parser always uses
"<string>" when reading from a string).

The output is written by self.write(), below.

Reimplemented in idlelib.PyShell.ModifiedInterpreter.

Definition at line 96 of file code.py.

00096 
00097     def showsyntaxerror(self, filename=None):
00098         """Display the syntax error that just occurred.
00099 
00100         This doesn't display a stack trace because there isn't one.
00101 
00102         If a filename is given, it is stuffed in the exception instead
00103         of what was there before (because Python's parser always uses
00104         "<string>" when reading from a string).
00105 
00106         The output is written by self.write(), below.
00107 
00108         """
00109         type, value, sys.last_traceback = sys.exc_info()
00110         sys.last_type = type
00111         sys.last_value = value
00112         if filename and type is SyntaxError:
00113             # Work hard to stuff the correct filename in the exception
00114             try:
00115                 msg, (dummy_filename, lineno, offset, line) = value.args
00116             except ValueError:
00117                 # Not the format we expect; leave it alone
00118                 pass
00119             else:
00120                 # Stuff in the right filename
00121                 value = SyntaxError(msg, (filename, lineno, offset, line))
00122                 sys.last_value = value
00123         lines = traceback.format_exception_only(type, value)
00124         self.write(''.join(lines))

Here is the call graph for this function:

Here is the caller graph for this function:

def code.InteractiveInterpreter.showtraceback (   self) [inherited]
Display the exception that just occurred.

We remove the first stack item because it is our own code.

The output is written by self.write(), below.

Reimplemented in idlelib.PyShell.ModifiedInterpreter.

Definition at line 125 of file code.py.

00125 
00126     def showtraceback(self):
00127         """Display the exception that just occurred.
00128 
00129         We remove the first stack item because it is our own code.
00130 
00131         The output is written by self.write(), below.
00132 
00133         """
00134         try:
00135             type, value, tb = sys.exc_info()
00136             sys.last_type = type
00137             sys.last_value = value
00138             sys.last_traceback = tb
00139             tblist = traceback.extract_tb(tb)
00140             del tblist[:1]
00141             lines = traceback.format_list(tblist)
00142             if lines:
00143                 lines.insert(0, "Traceback (most recent call last):\n")
00144             lines.extend(traceback.format_exception_only(type, value))
00145         finally:
00146             tblist = tb = None
00147         self.write(''.join(lines))

Here is the call graph for this function:

Here is the caller graph for this function:

def code.InteractiveInterpreter.write (   self,
  data 
) [inherited]
Write a string.

The base implementation writes to sys.stderr; a subclass may
replace this with a different implementation.

Reimplemented in idlelib.PyShell.ModifiedInterpreter.

Definition at line 148 of file code.py.

00148 
00149     def write(self, data):
00150         """Write a string.
00151 
00152         The base implementation writes to sys.stderr; a subclass may
00153         replace this with a different implementation.
00154 
00155         """
00156         sys.stderr.write(data)
00157 

Here is the call graph for this function:


Member Data Documentation

Definition at line 182 of file code.py.

code.InteractiveInterpreter.compile [inherited]

Definition at line 36 of file code.py.

Definition at line 177 of file code.py.

Definition at line 35 of file code.py.


The documentation for this class was generated from the following file: