diff --git a/IPython/core/autocall.py b/IPython/core/autocall.py index af3b532..11c3c56 100644 --- a/IPython/core/autocall.py +++ b/IPython/core/autocall.py @@ -56,3 +56,16 @@ class ExitAutocall(IPyAutocall): def __call__(self): self._ip.ask_exit() + +class ZMQExitAutocall(ExitAutocall): + """Exit IPython. Autocallable, so it needn't be explicitly called. + + Parameters + ---------- + keep_kernel : bool + If True, leave the kernel alive. Otherwise, tell the kernel to exit too + (default). + """ + def __call__(self, keep_kernel=False): + self._ip.keepkernel_on_exit = keep_kernel + self._ip.ask_exit() diff --git a/IPython/core/interactiveshell.py b/IPython/core/interactiveshell.py index 17998fa..8c61dc2 100644 --- a/IPython/core/interactiveshell.py +++ b/IPython/core/interactiveshell.py @@ -199,6 +199,9 @@ class InteractiveShell(Configurable, Magic): display_pub_class = Type(DisplayPublisher) exit_now = CBool(False) + exiter = Instance(ExitAutocall) + def _exiter_default(self): + return ExitAutocall(self) # Monotonically increasing execution counter execution_count = Int(1) filename = Unicode("") @@ -1025,9 +1028,8 @@ class InteractiveShell(Configurable, Magic): # Store myself as the public api!!! ns['get_ipython'] = self.get_ipython - exiter = ExitAutocall(self) for n in ['exit', 'Exit', 'quit', 'Quit']: - ns[n] = exiter + ns[n] = self.exiter # Sync what we've added so far to user_ns_hidden so these aren't seen # by %who diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py index 412077b..8793441 100644 --- a/IPython/zmq/zmqshell.py +++ b/IPython/zmq/zmqshell.py @@ -24,6 +24,7 @@ from IPython.core.interactiveshell import ( InteractiveShell, InteractiveShellABC ) from IPython.core import page +from IPython.core.autocall import ZMQExitAutocall from IPython.core.displayhook import DisplayHook from IPython.core.displaypub import DisplayPublisher from IPython.core.macro import Macro @@ -104,6 +105,10 @@ class ZMQInteractiveShell(InteractiveShell): displayhook_class = Type(ZMQDisplayHook) display_pub_class = Type(ZMQDisplayPublisher) + + exiter = Instance(ZMQExitAutocall) + def _exiter_default(self): + return ZMQExitAutocall(self) keepkernel_on_exit = None @@ -592,16 +597,5 @@ class ZMQInteractiveShell(InteractiveShell): text=content ) self.payload_manager.write_payload(payload) - - def magic_Exit(self, parameter_s=''): - """Exit IPython. If the -k option is provided, the kernel will be left - running. Otherwise, it will shutdown without prompting. - """ - opts,args = self.parse_options(parameter_s,'k') - self.shell.keepkernel_on_exit = opts.has_key('k') - self.shell.ask_exit() - - # Add aliases as magics so all common forms work: exit, quit, Exit, Quit. - magic_exit = magic_quit = magic_Quit = magic_Exit InteractiveShellABC.register(ZMQInteractiveShell)