1997-07-18 16:57:52 +00:00
|
|
|
"""Utilities dealing with code objects."""
|
|
|
|
|
|
|
|
def compile_command(source, filename="<input>", symbol="single"):
|
|
|
|
r"""Compile a command and determine whether it is incomplete.
|
|
|
|
|
|
|
|
Arguments:
|
|
|
|
|
|
|
|
source -- the source string; may contain \n characters
|
|
|
|
filename -- optional filename from which source was read; default "<input>"
|
|
|
|
symbol -- optional grammar start symbol; "single" (default) or "eval"
|
|
|
|
|
|
|
|
Return value / exception raised:
|
|
|
|
|
|
|
|
- Return a code object if the command is complete and valid
|
|
|
|
- Return None if the command is incomplete
|
|
|
|
- Raise SyntaxError if the command is a syntax error
|
|
|
|
|
|
|
|
Approach:
|
|
|
|
|
|
|
|
Compile three times: as is, with \n, and with \n\n appended. If
|
|
|
|
it compiles as is, it's complete. If it compiles with one \n
|
|
|
|
appended, we expect more. If it doesn't compile either way, we
|
|
|
|
compare the error we get when compiling with \n or \n\n appended.
|
|
|
|
If the errors are the same, the code is broken. But if the errors
|
|
|
|
are different, we expect more. Not intuitive; not even guaranteed
|
|
|
|
to hold in future releases; but this matches the compiler's
|
|
|
|
behavior in Python 1.4 and 1.5.
|
|
|
|
|
|
|
|
"""
|
|
|
|
|
|
|
|
err = err1 = err2 = None
|
|
|
|
code = code1 = code2 = None
|
|
|
|
|
|
|
|
try:
|
1998-03-26 21:13:24 +00:00
|
|
|
code = compile(source, filename, symbol)
|
1997-07-18 16:57:52 +00:00
|
|
|
except SyntaxError, err:
|
1998-03-26 21:13:24 +00:00
|
|
|
pass
|
1997-07-18 16:57:52 +00:00
|
|
|
|
|
|
|
try:
|
1998-03-26 21:13:24 +00:00
|
|
|
code1 = compile(source + "\n", filename, symbol)
|
1997-07-18 16:57:52 +00:00
|
|
|
except SyntaxError, err1:
|
1998-03-26 21:13:24 +00:00
|
|
|
pass
|
1997-07-18 16:57:52 +00:00
|
|
|
|
|
|
|
try:
|
1998-03-26 21:13:24 +00:00
|
|
|
code2 = compile(source + "\n\n", filename, symbol)
|
1997-07-18 16:57:52 +00:00
|
|
|
except SyntaxError, err2:
|
1998-03-26 21:13:24 +00:00
|
|
|
pass
|
1997-07-18 16:57:52 +00:00
|
|
|
|
|
|
|
if code:
|
1998-03-26 21:13:24 +00:00
|
|
|
return code
|
1998-01-14 15:40:30 +00:00
|
|
|
try:
|
1998-03-26 21:13:24 +00:00
|
|
|
e1 = err1.__dict__
|
1998-01-14 15:40:30 +00:00
|
|
|
except AttributeError:
|
1998-03-26 21:13:24 +00:00
|
|
|
e1 = err1
|
1998-01-14 15:40:30 +00:00
|
|
|
try:
|
1998-03-26 21:13:24 +00:00
|
|
|
e2 = err2.__dict__
|
1998-01-14 15:40:30 +00:00
|
|
|
except AttributeError:
|
1998-03-26 21:13:24 +00:00
|
|
|
e2 = err2
|
1998-01-14 15:40:30 +00:00
|
|
|
if not code1 and e1 == e2:
|
1998-03-26 21:13:24 +00:00
|
|
|
raise SyntaxError, err1
|
1997-10-07 14:47:24 +00:00
|
|
|
|
|
|
|
|
|
|
|
def interact(banner=None, readfunc=raw_input, local=None):
|
|
|
|
# Due to Jeff Epler, with changes by Guido:
|
|
|
|
"""Closely emulate the interactive Python console."""
|
|
|
|
try: import readline # Enable GNU readline if available
|
|
|
|
except: pass
|
|
|
|
local = local or {}
|
|
|
|
import sys, string, traceback
|
|
|
|
sys.ps1 = '>>> '
|
|
|
|
sys.ps2 = '... '
|
|
|
|
if banner:
|
1998-03-26 21:13:24 +00:00
|
|
|
print banner
|
1997-10-07 14:47:24 +00:00
|
|
|
else:
|
1998-03-26 21:13:24 +00:00
|
|
|
print "Python Interactive Console", sys.version
|
|
|
|
print sys.copyright
|
1997-10-07 14:47:24 +00:00
|
|
|
buf = []
|
|
|
|
while 1:
|
1998-03-26 21:13:24 +00:00
|
|
|
if buf: prompt = sys.ps2
|
|
|
|
else: prompt = sys.ps1
|
|
|
|
try: line = readfunc(prompt)
|
|
|
|
except KeyboardInterrupt:
|
|
|
|
print "\nKeyboardInterrupt"
|
|
|
|
buf = []
|
|
|
|
continue
|
|
|
|
except EOFError: break
|
|
|
|
buf.append(line)
|
|
|
|
try: x = compile_command(string.join(buf, "\n"))
|
|
|
|
except SyntaxError:
|
|
|
|
traceback.print_exc(0)
|
|
|
|
buf = []
|
|
|
|
continue
|
|
|
|
if x == None: continue
|
|
|
|
else:
|
|
|
|
try: exec x in local
|
|
|
|
except:
|
|
|
|
exc_type, exc_value, exc_traceback = \
|
|
|
|
sys.exc_type, sys.exc_value, \
|
|
|
|
sys.exc_traceback
|
|
|
|
l = len(traceback.extract_tb(sys.exc_traceback))
|
|
|
|
try: 1/0
|
|
|
|
except:
|
|
|
|
m = len(traceback.extract_tb(
|
|
|
|
sys.exc_traceback))
|
|
|
|
traceback.print_exception(exc_type,
|
|
|
|
exc_value, exc_traceback, l-m)
|
|
|
|
buf = []
|
|
|
|
|
1997-10-07 14:47:24 +00:00
|
|
|
if __name__ == '__main__':
|
|
|
|
interact()
|