Back to index

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

List of all members.

Public Member Functions

def __init__
def runsource
def runcode
def showsyntaxerror
def showtraceback
def write

Public Attributes

 locals
 compile

Detailed Description

Base class for InteractiveConsole.

This class deals with parsing and interpreter state (the user's
namespace); it doesn't deal with input buffering or prompting or
input file naming (the filename is always passed in explicitly).

Definition at line 15 of file code.py.


Constructor & Destructor Documentation

def code.InteractiveInterpreter.__init__ (   self,
  locals = None 
)
Constructor.

The optional 'locals' argument specifies the dictionary in
which code will be executed; it defaults to a newly created
dictionary with key "__name__" set to "__console__" and key
"__doc__" set to None.

Reimplemented in idlelib.PyShell.ModifiedInterpreter.

Definition at line 24 of file code.py.

00024 
00025     def __init__(self, locals=None):
00026         """Constructor.
00027 
00028         The optional 'locals' argument specifies the dictionary in
00029         which code will be executed; it defaults to a newly created
00030         dictionary with key "__name__" set to "__console__" and key
00031         "__doc__" set to None.
00032 
00033         """
00034         if locals is None:
00035             locals = {"__name__": "__console__", "__doc__": None}
00036         self.locals = locals
00037         self.compile = CommandCompiler()

Here is the call graph for this function:

Here is the caller graph for this function:


Member Function Documentation

def code.InteractiveInterpreter.runcode (   self,
  code 
)
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" 
)
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 
)
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)
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 
)
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

code.InteractiveInterpreter.compile

Definition at line 36 of file code.py.

Definition at line 35 of file code.py.


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