# HG changeset patch # User catherine@dellzilla # Date 1237915794 14400 # Node ID ff99b39259d57312f2a90067a799c3f114f6c81a # Parent d2cc9a090b7f8998a7b36c5a960edd35c5e5c201 maybe quit and exit work now? diff -r d2cc9a090b7f -r ff99b39259d5 cmd2.py --- a/cmd2.py Tue Mar 24 12:13:27 2009 -0400 +++ b/cmd2.py Tue Mar 24 13:29:54 2009 -0400 @@ -222,6 +222,34 @@ result = getPasteBuffer() return result +class EmbeddedConsoleExit(Exception): + pass + +class InteractiveConsole(code.InteractiveConsole): + def runcode(self, code): + """Execute a code object. + + When an exception occurs, self.showtraceback() is called to + display a traceback. All exceptions are caught except + SystemExit, which is reraised. + + A note about KeyboardInterrupt: this exception may occur + elsewhere in this code, and may not always be caught. The + caller should be prepared to deal with it. + + Copied directly from code.InteractiveInterpreter, except for + EmbeddedConsoleExit exceptions. + """ + try: + exec code in self.locals + except (SystemExit, EmbeddedConsoleExit): + raise + except: + self.showtraceback() + else: + if code.softspace(sys.stdout, 0): + print + class Cmd(cmd.Cmd): echo = False case_insensitive = True @@ -763,7 +791,7 @@ def do_py(self, arg): ''' py : Executes a Python command. - py: Enters interactive Python mode; end with `Ctrl-D`, `quit()`, or `exit()`. + py: Enters interactive Python mode; end with `Ctrl-D`. Do not end with Ctrl-Z, or it will end your entire cmd2 session! Non-python commands can be issued with cmd('your non-python command here'). ''' @@ -771,13 +799,15 @@ interp = code.InteractiveInterpreter(locals=self.pystate) interp.runcode(arg) else: - interp = code.InteractiveConsole(locals=self.pystate) + interp = InteractiveConsole(locals=self.pystate) def quit(): - interp.push(chr(4)) + raise EmbeddedConsoleExit self.pystate['quit'] = quit self.pystate['exit'] = quit - self.pystate[self.nonpythoncommand] = self.onecmd - interp.interact() + try: + interp.interact() + except (EmbeddedConsoleExit, SystemExit): + return def do_history(self, arg): """history [arg]: lists past commands issued