Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 1 | import readline |
| 2 | import code |
| 3 | import sys |
| 4 | import traceback |
| 5 | |
| 6 | class SimpleREPL(code.InteractiveConsole): |
| 7 | def __init__(self, prompt, dict): |
| 8 | code.InteractiveConsole.__init__(self,dict) |
| 9 | self.prompt = prompt |
| 10 | self.loop_exit = False |
| 11 | self.dict = dict |
| 12 | |
| 13 | def interact(self): |
| 14 | try: |
| 15 | sys.ps1 |
| 16 | except AttributeError: |
| 17 | sys.ps1 = ">>> " |
| 18 | try: |
| 19 | sys.ps2 |
| 20 | except AttributeError: |
| 21 | sys.ps2 = "... " |
| 22 | |
| 23 | while not self.loop_exit: |
| 24 | try: |
| 25 | self.read_py_command() |
| 26 | except (SystemExit, EOFError): |
| 27 | # EOF while in Python just breaks out to top level. |
| 28 | self.write('\n') |
| 29 | self.loop_exit = True |
| 30 | break |
| 31 | except KeyboardInterrupt: |
| 32 | self.write("\nKeyboardInterrupt\n") |
| 33 | self.resetbuffer() |
| 34 | more = 0 |
| 35 | except: |
| 36 | traceback.print_exc() |
| 37 | |
| 38 | def process_input (self, in_str): |
| 39 | # Canonicalize the format of the input string |
| 40 | temp_str = in_str |
| 41 | temp_str.strip(' \t') |
| 42 | words = temp_str.split() |
| 43 | temp_str = ('').join(words) |
| 44 | |
| 45 | # Check the input string to see if it was the quit |
| 46 | # command. If so, intercept it, so that it doesn't |
| 47 | # close stdin on us! |
Jason Molenda | a8a5e56 | 2010-06-09 21:56:00 +0000 | [diff] [blame] | 48 | if (temp_str.lower() == "quit()" or temp_str.lower() == "exit()"): |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 49 | self.loop_exit = True |
| 50 | in_str = "raise SystemExit " |
| 51 | return in_str |
| 52 | |
| 53 | def my_raw_input (self, prompt): |
| 54 | stream = sys.stdout |
| 55 | stream.write (prompt) |
| 56 | stream.flush () |
| 57 | try: |
| 58 | line = sys.stdin.readline() |
| 59 | except KeyboardInterrupt: |
| 60 | line = " \n" |
| 61 | except (SystemExit, EOFError): |
| 62 | line = "quit()\n" |
| 63 | if not line: |
| 64 | raise EOFError |
| 65 | if line[-1] == '\n': |
| 66 | line = line[:-1] |
| 67 | return line |
| 68 | |
| 69 | def read_py_command(self): |
| 70 | # Read off a complete Python command. |
| 71 | more = 0 |
| 72 | while 1: |
| 73 | if more: |
| 74 | prompt = sys.ps2 |
| 75 | else: |
| 76 | prompt = sys.ps1 |
| 77 | line = self.my_raw_input(prompt) |
| 78 | # Can be None if sys.stdin was redefined |
| 79 | encoding = getattr(sys.stdin, "encoding", None) |
| 80 | if encoding and not isinstance(line, unicode): |
| 81 | line = line.decode(encoding) |
| 82 | line = self.process_input (line) |
| 83 | more = self.push(line) |
| 84 | if not more: |
| 85 | break |
| 86 | |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 87 | def one_line (self, input): |
| 88 | line = self.process_input (input) |
| 89 | more = self.push(line) |
| 90 | if more: |
| 91 | self.write ("Input not a complete line.") |
| 92 | self.resetbuffer() |
| 93 | more = 0 |
| 94 | |
Chris Lattner | 24943d2 | 2010-06-08 16:52:24 +0000 | [diff] [blame] | 95 | def run_python_interpreter (dict): |
| 96 | # Pass in the dictionary, for continuity from one session to the next. |
| 97 | repl = SimpleREPL('>>> ', dict) |
| 98 | repl.interact() |
Caroline Tice | 0aa2e55 | 2011-01-14 00:29:16 +0000 | [diff] [blame] | 99 | |
| 100 | def run_one_line (dict, input_string): |
| 101 | repl = SimpleREPL ('', dict) |
| 102 | repl.one_line (input_string) |
| 103 | |