1 """Utilities needed to emulate Python's interactive interpreter.
5 # Inspired by similar code by Jeff Epler and Fredrik Lundh.
10 from codeop
import CommandCompiler
, compile_command
12 __all__
= ["InteractiveInterpreter", "InteractiveConsole", "interact",
15 def softspace(file, newvalue
):
18 oldvalue
= file.softspace
19 except AttributeError:
22 file.softspace
= newvalue
23 except (AttributeError, TypeError):
24 # "attribute-less object" or "read-only attributes"
28 class InteractiveInterpreter
:
29 """Base class for InteractiveConsole.
31 This class deals with parsing and interpreter state (the user's
32 namespace); it doesn't deal with input buffering or prompting or
33 input file naming (the filename is always passed in explicitly).
37 def __init__(self
, locals=None):
40 The optional 'locals' argument specifies the dictionary in
41 which code will be executed; it defaults to a newly created
42 dictionary with key "__name__" set to "__console__" and key
43 "__doc__" set to None.
47 locals = {"__name__": "__console__", "__doc__": None}
49 self
.compile = CommandCompiler()
51 def runsource(self
, source
, filename
="<input>", symbol
="single"):
52 """Compile and run some source in the interpreter.
54 Arguments are as for compile_command().
56 One several things can happen:
58 1) The input is incorrect; compile_command() raised an
59 exception (SyntaxError or OverflowError). A syntax traceback
60 will be printed by calling the showsyntaxerror() method.
62 2) The input is incomplete, and more input is required;
63 compile_command() returned None. Nothing happens.
65 3) The input is complete; compile_command() returned a code
66 object. The code is executed by calling self.runcode() (which
67 also handles run-time exceptions, except for SystemExit).
69 The return value is True in case 2, False in the other cases (unless
70 an exception is raised). The return value can be used to
71 decide whether to use sys.ps1 or sys.ps2 to prompt the next
76 code
= self
.compile(source
, filename
, symbol
)
77 except (OverflowError, SyntaxError, ValueError):
79 self
.showsyntaxerror(filename
)
90 def runcode(self
, code
):
91 """Execute a code object.
93 When an exception occurs, self.showtraceback() is called to
94 display a traceback. All exceptions are caught except
95 SystemExit, which is reraised.
97 A note about KeyboardInterrupt: this exception may occur
98 elsewhere in this code, and may not always be caught. The
99 caller should be prepared to deal with it.
103 exec code
in self
.locals
109 if softspace(sys
.stdout
, 0):
112 def showsyntaxerror(self
, filename
=None):
113 """Display the syntax error that just occurred.
115 This doesn't display a stack trace because there isn't one.
117 If a filename is given, it is stuffed in the exception instead
118 of what was there before (because Python's parser always uses
119 "<string>" when reading from a string).
121 The output is written by self.write(), below.
124 type, value
, sys
.last_traceback
= sys
.exc_info()
126 sys
.last_value
= value
127 if filename
and type is SyntaxError:
128 # Work hard to stuff the correct filename in the exception
130 msg
, (dummy_filename
, lineno
, offset
, line
) = value
132 # Not the format we expect; leave it alone
135 # Stuff in the right filename
136 value
= SyntaxError(msg
, (filename
, lineno
, offset
, line
))
137 sys
.last_value
= value
138 list = traceback
.format_exception_only(type, value
)
139 map(self
.write
, list)
141 def showtraceback(self
):
142 """Display the exception that just occurred.
144 We remove the first stack item because it is our own code.
146 The output is written by self.write(), below.
150 type, value
, tb
= sys
.exc_info()
152 sys
.last_value
= value
153 sys
.last_traceback
= tb
154 tblist
= traceback
.extract_tb(tb
)
156 list = traceback
.format_list(tblist
)
158 list.insert(0, "Traceback (most recent call last):\n")
159 list[len(list):] = traceback
.format_exception_only(type, value
)
162 map(self
.write
, list)
164 def write(self
, data
):
167 The base implementation writes to sys.stderr; a subclass may
168 replace this with a different implementation.
171 sys
.stderr
.write(data
)
174 class InteractiveConsole(InteractiveInterpreter
):
175 """Closely emulate the behavior of the interactive Python interpreter.
177 This class builds on InteractiveInterpreter and adds prompting
178 using the familiar sys.ps1 and sys.ps2, and input buffering.
182 def __init__(self
, locals=None, filename
="<console>"):
185 The optional locals argument will be passed to the
186 InteractiveInterpreter base class.
188 The optional filename argument should specify the (file)name
189 of the input stream; it will show up in tracebacks.
192 InteractiveInterpreter
.__init
__(self
, locals)
193 self
.filename
= filename
196 def resetbuffer(self
):
197 """Reset the input buffer."""
200 def interact(self
, banner
=None):
201 """Closely emulate the interactive Python console.
203 The optional banner argument specify the banner to print
204 before the first interaction; by default it prints a banner
205 similar to the one printed by the real Python interpreter,
206 followed by the current class name in parentheses (so as not
207 to confuse this with the real interpreter -- since it's so
213 except AttributeError:
217 except AttributeError:
219 cprt
= 'Type "help", "copyright", "credits" or "license" for more information.'
221 self
.write("Python %s on %s\n%s\n(%s)\n" %
222 (sys
.version
, sys
.platform
, cprt
,
223 self
.__class
__.__name
__))
225 self
.write("%s\n" % str(banner
))
234 line
= self
.raw_input(prompt
)
235 # Can be None if sys.stdin was redefined
236 encoding
= getattr(sys
.stdin
, "encoding", None)
237 if encoding
and not isinstance(line
, unicode):
238 line
= line
.decode(encoding
)
243 more
= self
.push(line
)
244 except KeyboardInterrupt:
245 self
.write("\nKeyboardInterrupt\n")
249 def push(self
, line
):
250 """Push a line to the interpreter.
252 The line should not have a trailing newline; it may have
253 internal newlines. The line is appended to a buffer and the
254 interpreter's runsource() method is called with the
255 concatenated contents of the buffer as source. If this
256 indicates that the command was executed or invalid, the buffer
257 is reset; otherwise, the command is incomplete, and the buffer
258 is left as it was after the line was appended. The return
259 value is 1 if more input is required, 0 if the line was dealt
260 with in some way (this is the same as runsource()).
263 self
.buffer.append(line
)
264 source
= "\n".join(self
.buffer)
265 more
= self
.runsource(source
, self
.filename
)
270 def raw_input(self
, prompt
=""):
271 """Write a prompt and read a line.
273 The returned line does not include the trailing newline.
274 When the user enters the EOF key sequence, EOFError is raised.
276 The base implementation uses the built-in function
277 raw_input(); a subclass may replace this with a different
281 return raw_input(prompt
)
284 def interact(banner
=None, readfunc
=None, local
=None):
285 """Closely emulate the interactive Python interpreter.
287 This is a backwards compatible interface to the InteractiveConsole
288 class. When readfunc is not specified, it attempts to import the
289 readline module to enable GNU readline if it is available.
291 Arguments (all optional, all default to None):
293 banner -- passed to InteractiveConsole.interact()
294 readfunc -- if not None, replaces InteractiveConsole.raw_input()
295 local -- passed to InteractiveInterpreter.__init__()
298 console
= InteractiveConsole(local
)
299 if readfunc
is not None:
300 console
.raw_input = readfunc
306 console
.interact(banner
)
309 if __name__
== '__main__':
311 pdb
.run("interact()\n")