diff options
author | Guido van Rossum <guido@python.org> | 1997-10-07 14:47:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 1997-10-07 14:47:24 (GMT) |
commit | 90981e0e70450bfb58f0d83d4e4d00a4a7d4d5d9 (patch) | |
tree | 66499c9bbb7d8878a9f6d1650850e7d074fa9708 /Lib/code.py | |
parent | d5484fb75c6b2b43867662c60f0ef59283d8468a (diff) | |
download | cpython-90981e0e70450bfb58f0d83d4e4d00a4a7d4d5d9.zip cpython-90981e0e70450bfb58f0d83d4e4d00a4a7d4d5d9.tar.gz cpython-90981e0e70450bfb58f0d83d4e4d00a4a7d4d5d9.tar.bz2 |
Add Jeff Epler's interact() function. Note that it is broken.
(It should probably be withdrawn :-( )
Diffstat (limited to 'Lib/code.py')
-rw-r--r-- | Lib/code.py | 52 |
1 files changed, 52 insertions, 0 deletions
diff --git a/Lib/code.py b/Lib/code.py index 49fe541..e01051e 100644 --- a/Lib/code.py +++ b/Lib/code.py @@ -1,3 +1,5 @@ +# XXX This is broken with class exceptions + """Utilities dealing with code objects.""" def compile_command(source, filename="<input>", symbol="single"): @@ -50,3 +52,53 @@ def compile_command(source, filename="<input>", symbol="single"): return code if not code1 and err1 == err2: raise SyntaxError, err1 + + +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: + print banner + else: + print "Python Interactive Console", sys.version + print sys.copyright + buf = [] + while 1: + 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 = [] + +if __name__ == '__main__': + interact() |