6 class SimpleREPL(code.InteractiveConsole):
7 def __init__(self, prompt, dict):
8 code.InteractiveConsole.__init__(self,dict)
10 self.loop_exit = False
16 except AttributeError:
20 except AttributeError:
23 while not self.loop_exit:
25 self.read_py_command()
26 except (SystemExit, EOFError):
27 # EOF while in Python just breaks out to top level.
31 except KeyboardInterrupt:
32 self.write("\nKeyboardInterrupt\n")
38 def process_input (self, in_str):
39 # Canonicalize the format of the input string
42 words = temp_str.split()
43 temp_str = ('').join(words)
45 # Check the input string to see if it was the quit
46 # command. If so, intercept it, so that it doesn't
48 if (temp_str.lower() == "quit()" or temp_str.lower() == "exit()"):
50 in_str = "raise SystemExit "
53 def my_raw_input (self, prompt):
58 line = sys.stdin.readline()
59 except KeyboardInterrupt:
61 except (SystemExit, EOFError):
69 def read_py_command(self):
70 # Read off a complete Python command.
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)
87 def one_line (self, input):
88 line = self.process_input (input)
89 more = self.push(line)
91 self.write ("Input not a complete line.")
95 def run_python_interpreter (dict):
96 # Pass in the dictionary, for continuity from one session to the next.
97 repl = SimpleREPL('>>> ', dict)
100 def run_one_line (dict, input_string):
101 repl = SimpleREPL ('', dict)
102 repl.one_line (input_string)