|
rpm-build |
2bd099 |
"""Utilities needed to emulate Python's interactive interpreter.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
# Inspired by similar code by Jeff Epler and Fredrik Lundh.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
import sys
|
|
rpm-build |
2bd099 |
import traceback
|
|
rpm-build |
2bd099 |
import argparse
|
|
rpm-build |
2bd099 |
from codeop import CommandCompiler, compile_command
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
__all__ = ["InteractiveInterpreter", "InteractiveConsole", "interact",
|
|
rpm-build |
2bd099 |
"compile_command"]
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
class InteractiveInterpreter:
|
|
rpm-build |
2bd099 |
"""Base class for InteractiveConsole.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
This class deals with parsing and interpreter state (the user's
|
|
rpm-build |
2bd099 |
namespace); it doesn't deal with input buffering or prompting or
|
|
rpm-build |
2bd099 |
input file naming (the filename is always passed in explicitly).
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def __init__(self, locals=None):
|
|
rpm-build |
2bd099 |
"""Constructor.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The optional 'locals' argument specifies the dictionary in
|
|
rpm-build |
2bd099 |
which code will be executed; it defaults to a newly created
|
|
rpm-build |
2bd099 |
dictionary with key "__name__" set to "__console__" and key
|
|
rpm-build |
2bd099 |
"__doc__" set to None.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
if locals is None:
|
|
rpm-build |
2bd099 |
locals = {"__name__": "__console__", "__doc__": None}
|
|
rpm-build |
2bd099 |
self.locals = locals
|
|
rpm-build |
2bd099 |
self.compile = CommandCompiler()
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def runsource(self, source, filename="<input>", symbol="single"):
|
|
rpm-build |
2bd099 |
"""Compile and run some source in the interpreter.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
Arguments are as for compile_command().
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
One several things can happen:
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
1) The input is incorrect; compile_command() raised an
|
|
rpm-build |
2bd099 |
exception (SyntaxError or OverflowError). A syntax traceback
|
|
rpm-build |
2bd099 |
will be printed by calling the showsyntaxerror() method.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
2) The input is incomplete, and more input is required;
|
|
rpm-build |
2bd099 |
compile_command() returned None. Nothing happens.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
3) The input is complete; compile_command() returned a code
|
|
rpm-build |
2bd099 |
object. The code is executed by calling self.runcode() (which
|
|
rpm-build |
2bd099 |
also handles run-time exceptions, except for SystemExit).
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The return value is True in case 2, False in the other cases (unless
|
|
rpm-build |
2bd099 |
an exception is raised). The return value can be used to
|
|
rpm-build |
2bd099 |
decide whether to use sys.ps1 or sys.ps2 to prompt the next
|
|
rpm-build |
2bd099 |
line.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
code = self.compile(source, filename, symbol)
|
|
rpm-build |
2bd099 |
except (OverflowError, SyntaxError, ValueError):
|
|
rpm-build |
2bd099 |
# Case 1
|
|
rpm-build |
2bd099 |
self.showsyntaxerror(filename)
|
|
rpm-build |
2bd099 |
return False
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
if code is None:
|
|
rpm-build |
2bd099 |
# Case 2
|
|
rpm-build |
2bd099 |
return True
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
# Case 3
|
|
rpm-build |
2bd099 |
self.runcode(code)
|
|
rpm-build |
2bd099 |
return False
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def runcode(self, code):
|
|
rpm-build |
2bd099 |
"""Execute a code object.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
When an exception occurs, self.showtraceback() is called to
|
|
rpm-build |
2bd099 |
display a traceback. All exceptions are caught except
|
|
rpm-build |
2bd099 |
SystemExit, which is reraised.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
A note about KeyboardInterrupt: this exception may occur
|
|
rpm-build |
2bd099 |
elsewhere in this code, and may not always be caught. The
|
|
rpm-build |
2bd099 |
caller should be prepared to deal with it.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
exec(code, self.locals)
|
|
rpm-build |
2bd099 |
except SystemExit:
|
|
rpm-build |
2bd099 |
raise
|
|
rpm-build |
2bd099 |
except:
|
|
rpm-build |
2bd099 |
self.showtraceback()
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def showsyntaxerror(self, filename=None):
|
|
rpm-build |
2bd099 |
"""Display the syntax error that just occurred.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
This doesn't display a stack trace because there isn't one.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
If a filename is given, it is stuffed in the exception instead
|
|
rpm-build |
2bd099 |
of what was there before (because Python's parser always uses
|
|
rpm-build |
2bd099 |
"<string>" when reading from a string).
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The output is written by self.write(), below.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
type, value, tb = sys.exc_info()
|
|
rpm-build |
2bd099 |
sys.last_type = type
|
|
rpm-build |
2bd099 |
sys.last_value = value
|
|
rpm-build |
2bd099 |
sys.last_traceback = tb
|
|
rpm-build |
2bd099 |
if filename and type is SyntaxError:
|
|
rpm-build |
2bd099 |
# Work hard to stuff the correct filename in the exception
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
msg, (dummy_filename, lineno, offset, line) = value.args
|
|
rpm-build |
2bd099 |
except ValueError:
|
|
rpm-build |
2bd099 |
# Not the format we expect; leave it alone
|
|
rpm-build |
2bd099 |
pass
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
# Stuff in the right filename
|
|
rpm-build |
2bd099 |
value = SyntaxError(msg, (filename, lineno, offset, line))
|
|
rpm-build |
2bd099 |
sys.last_value = value
|
|
rpm-build |
2bd099 |
if sys.excepthook is sys.__excepthook__:
|
|
rpm-build |
2bd099 |
lines = traceback.format_exception_only(type, value)
|
|
rpm-build |
2bd099 |
self.write(''.join(lines))
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
# If someone has set sys.excepthook, we let that take precedence
|
|
rpm-build |
2bd099 |
# over self.write
|
|
rpm-build |
2bd099 |
sys.excepthook(type, value, tb)
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def showtraceback(self):
|
|
rpm-build |
2bd099 |
"""Display the exception that just occurred.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
We remove the first stack item because it is our own code.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The output is written by self.write(), below.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
sys.last_type, sys.last_value, last_tb = ei = sys.exc_info()
|
|
rpm-build |
2bd099 |
sys.last_traceback = last_tb
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
lines = traceback.format_exception(ei[0], ei[1], last_tb.tb_next)
|
|
rpm-build |
2bd099 |
if sys.excepthook is sys.__excepthook__:
|
|
rpm-build |
2bd099 |
self.write(''.join(lines))
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
# If someone has set sys.excepthook, we let that take precedence
|
|
rpm-build |
2bd099 |
# over self.write
|
|
rpm-build |
2bd099 |
sys.excepthook(ei[0], ei[1], last_tb)
|
|
rpm-build |
2bd099 |
finally:
|
|
rpm-build |
2bd099 |
last_tb = ei = None
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def write(self, data):
|
|
rpm-build |
2bd099 |
"""Write a string.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The base implementation writes to sys.stderr; a subclass may
|
|
rpm-build |
2bd099 |
replace this with a different implementation.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
sys.stderr.write(data)
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
class InteractiveConsole(InteractiveInterpreter):
|
|
rpm-build |
2bd099 |
"""Closely emulate the behavior of the interactive Python interpreter.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
This class builds on InteractiveInterpreter and adds prompting
|
|
rpm-build |
2bd099 |
using the familiar sys.ps1 and sys.ps2, and input buffering.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def __init__(self, locals=None, filename="<console>"):
|
|
rpm-build |
2bd099 |
"""Constructor.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The optional locals argument will be passed to the
|
|
rpm-build |
2bd099 |
InteractiveInterpreter base class.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The optional filename argument should specify the (file)name
|
|
rpm-build |
2bd099 |
of the input stream; it will show up in tracebacks.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
InteractiveInterpreter.__init__(self, locals)
|
|
rpm-build |
2bd099 |
self.filename = filename
|
|
rpm-build |
2bd099 |
self.resetbuffer()
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def resetbuffer(self):
|
|
rpm-build |
2bd099 |
"""Reset the input buffer."""
|
|
rpm-build |
2bd099 |
self.buffer = []
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def interact(self, banner=None, exitmsg=None):
|
|
rpm-build |
2bd099 |
"""Closely emulate the interactive Python console.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The optional banner argument specifies the banner to print
|
|
rpm-build |
2bd099 |
before the first interaction; by default it prints a banner
|
|
rpm-build |
2bd099 |
similar to the one printed by the real Python interpreter,
|
|
rpm-build |
2bd099 |
followed by the current class name in parentheses (so as not
|
|
rpm-build |
2bd099 |
to confuse this with the real interpreter -- since it's so
|
|
rpm-build |
2bd099 |
close!).
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The optional exitmsg argument specifies the exit message
|
|
rpm-build |
2bd099 |
printed when exiting. Pass the empty string to suppress
|
|
rpm-build |
2bd099 |
printing an exit message. If exitmsg is not given or None,
|
|
rpm-build |
2bd099 |
a default message is printed.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
sys.ps1
|
|
rpm-build |
2bd099 |
except AttributeError:
|
|
rpm-build |
2bd099 |
sys.ps1 = ">>> "
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
sys.ps2
|
|
rpm-build |
2bd099 |
except AttributeError:
|
|
rpm-build |
2bd099 |
sys.ps2 = "... "
|
|
rpm-build |
2bd099 |
cprt = 'Type "help", "copyright", "credits" or "license" for more information.'
|
|
rpm-build |
2bd099 |
if banner is None:
|
|
rpm-build |
2bd099 |
self.write("Python %s on %s\n%s\n(%s)\n" %
|
|
rpm-build |
2bd099 |
(sys.version, sys.platform, cprt,
|
|
rpm-build |
2bd099 |
self.__class__.__name__))
|
|
rpm-build |
2bd099 |
elif banner:
|
|
rpm-build |
2bd099 |
self.write("%s\n" % str(banner))
|
|
rpm-build |
2bd099 |
more = 0
|
|
rpm-build |
2bd099 |
while 1:
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
if more:
|
|
rpm-build |
2bd099 |
prompt = sys.ps2
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
prompt = sys.ps1
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
line = self.raw_input(prompt)
|
|
rpm-build |
2bd099 |
except EOFError:
|
|
rpm-build |
2bd099 |
self.write("\n")
|
|
rpm-build |
2bd099 |
break
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
more = self.push(line)
|
|
rpm-build |
2bd099 |
except KeyboardInterrupt:
|
|
rpm-build |
2bd099 |
self.write("\nKeyboardInterrupt\n")
|
|
rpm-build |
2bd099 |
self.resetbuffer()
|
|
rpm-build |
2bd099 |
more = 0
|
|
rpm-build |
2bd099 |
if exitmsg is None:
|
|
rpm-build |
2bd099 |
self.write('now exiting %s...\n' % self.__class__.__name__)
|
|
rpm-build |
2bd099 |
elif exitmsg != '':
|
|
rpm-build |
2bd099 |
self.write('%s\n' % exitmsg)
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def push(self, line):
|
|
rpm-build |
2bd099 |
"""Push a line to the interpreter.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The line should not have a trailing newline; it may have
|
|
rpm-build |
2bd099 |
internal newlines. The line is appended to a buffer and the
|
|
rpm-build |
2bd099 |
interpreter's runsource() method is called with the
|
|
rpm-build |
2bd099 |
concatenated contents of the buffer as source. If this
|
|
rpm-build |
2bd099 |
indicates that the command was executed or invalid, the buffer
|
|
rpm-build |
2bd099 |
is reset; otherwise, the command is incomplete, and the buffer
|
|
rpm-build |
2bd099 |
is left as it was after the line was appended. The return
|
|
rpm-build |
2bd099 |
value is 1 if more input is required, 0 if the line was dealt
|
|
rpm-build |
2bd099 |
with in some way (this is the same as runsource()).
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
self.buffer.append(line)
|
|
rpm-build |
2bd099 |
source = "\n".join(self.buffer)
|
|
rpm-build |
2bd099 |
more = self.runsource(source, self.filename)
|
|
rpm-build |
2bd099 |
if not more:
|
|
rpm-build |
2bd099 |
self.resetbuffer()
|
|
rpm-build |
2bd099 |
return more
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def raw_input(self, prompt=""):
|
|
rpm-build |
2bd099 |
"""Write a prompt and read a line.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The returned line does not include the trailing newline.
|
|
rpm-build |
2bd099 |
When the user enters the EOF key sequence, EOFError is raised.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
The base implementation uses the built-in function
|
|
rpm-build |
2bd099 |
input(); a subclass may replace this with a different
|
|
rpm-build |
2bd099 |
implementation.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
return input(prompt)
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
def interact(banner=None, readfunc=None, local=None, exitmsg=None):
|
|
rpm-build |
2bd099 |
"""Closely emulate the interactive Python interpreter.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
This is a backwards compatible interface to the InteractiveConsole
|
|
rpm-build |
2bd099 |
class. When readfunc is not specified, it attempts to import the
|
|
rpm-build |
2bd099 |
readline module to enable GNU readline if it is available.
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
Arguments (all optional, all default to None):
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
banner -- passed to InteractiveConsole.interact()
|
|
rpm-build |
2bd099 |
readfunc -- if not None, replaces InteractiveConsole.raw_input()
|
|
rpm-build |
2bd099 |
local -- passed to InteractiveInterpreter.__init__()
|
|
rpm-build |
2bd099 |
exitmsg -- passed to InteractiveConsole.interact()
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
"""
|
|
rpm-build |
2bd099 |
console = InteractiveConsole(local)
|
|
rpm-build |
2bd099 |
if readfunc is not None:
|
|
rpm-build |
2bd099 |
console.raw_input = readfunc
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
try:
|
|
rpm-build |
2bd099 |
import readline
|
|
rpm-build |
2bd099 |
except ImportError:
|
|
rpm-build |
2bd099 |
pass
|
|
rpm-build |
2bd099 |
console.interact(banner, exitmsg)
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
|
|
rpm-build |
2bd099 |
if __name__ == "__main__":
|
|
rpm-build |
2bd099 |
parser = argparse.ArgumentParser()
|
|
rpm-build |
2bd099 |
parser.add_argument('-q', action='store_true',
|
|
rpm-build |
2bd099 |
help="don't print version and copyright messages")
|
|
rpm-build |
2bd099 |
args = parser.parse_args()
|
|
rpm-build |
2bd099 |
if args.q or sys.flags.quiet:
|
|
rpm-build |
2bd099 |
banner = ''
|
|
rpm-build |
2bd099 |
else:
|
|
rpm-build |
2bd099 |
banner = None
|
|
rpm-build |
2bd099 |
interact(banner)
|