8 print("Python {} on {}".format(sys.version, sys.platform))
9 print('Type "help", "copyright", "credits" or "license" for more information.')
11 class InteractiveConsole(code.InteractiveConsole):
12 # code.InteractiveConsole without banner
14 # also more robust treating of sys.ps1, sys.ps2
15 # prints prompt into stderr rather than stdout
16 # flushes sys.stderr and sys.stdout
18 def __init__(self, locals=None, filename="<stdin>"):
20 super().__init__(locals, filename)
22 def raw_input(self, prompt=""):
23 sys.stderr.write(prompt)
26 def runcode(self, code):
37 except AttributeError:
42 except AttributeError:
51 except AttributeError:
56 except AttributeError:
60 line = self.raw_input(prompt)
64 more = self.push(line)
66 except KeyboardInterrupt:
67 self.write("\nKeyboardInterrupt\n")
74 raise SystemExit from None
77 running_console = None
80 global running_console
82 if running_console is not None:
83 raise RuntimeError("interactive console already running")
85 running_console = InteractiveConsole(__main__.__dict__)
86 running_console.interact()
89 global running_console
91 if running_console is None:
92 raise RuntimeError("interactive console is not running")
94 running_console.done = True
95 running_console = None