From d7099e08d83cdd70237a79854682f0fbcb960578 2010-10-26 07:16:36 From: Erik Tollerud Date: 2010-10-26 07:16:36 Subject: [PATCH] __builtin__.exit and quit are now hidden - exit magic is now the only exit command --- diff --git a/IPython/core/builtin_trap.py b/IPython/core/builtin_trap.py index 38a8097..c89bb64 100755 --- a/IPython/core/builtin_trap.py +++ b/IPython/core/builtin_trap.py @@ -32,6 +32,9 @@ from IPython.utils.traitlets import Instance class __BuiltinUndefined(object): pass BuiltinUndefined = __BuiltinUndefined() +class __HideBuiltin(object): pass +HideBuiltin = __HideBuiltin() + class BuiltinTrap(Configurable): @@ -44,9 +47,10 @@ class BuiltinTrap(Configurable): # Only turn off the trap when the outermost call to __exit__ is made. self._nested_level = 0 self.shell = shell - # builtins we always add - self.auto_builtins = {'exit': Quitter(self.shell, 'exit'), - 'quit': Quitter(self.shell, 'quit'), + # builtins we always add - if set to HideBuiltin, they will just + # be removed instead of being replaced by something else + self.auto_builtins = {'exit': HideBuiltin, + 'quit': HideBuiltin, 'get_ipython': self.shell.get_ipython, } # Recursive reload function @@ -78,7 +82,10 @@ class BuiltinTrap(Configurable): bdict = __builtin__.__dict__ orig = bdict.get(key, BuiltinUndefined) self._orig_builtins[key] = orig - bdict[key] = value + if value is HideBuiltin: + del bdict[key] + else: + bdict[key] = value def remove_builtin(self, key): """Remove an added builtin and re-set the original.""" diff --git a/IPython/frontend/qt/console/ipythonqt.py b/IPython/frontend/qt/console/ipythonqt.py index b759fd9..a243fbc 100644 --- a/IPython/frontend/qt/console/ipythonqt.py +++ b/IPython/frontend/qt/console/ipythonqt.py @@ -58,12 +58,16 @@ class MainWindow(QtGui.QMainWindow): #--------------------------------------------------------------------------- def closeEvent(self, event): - """ Reimplemented to prompt the user and close the kernel cleanly. + """ Reimplemented to prompt the user and close the kernel cleanly, or + close without prompt only if the exit magic is used. """ - keepkernel = self._frontend._keep_kernel_on_exit + keepkernel = None #Use the prompt by default + if hasattr(self._frontend,'_keep_kernel_on_exit'): #set by exit magic + keepkernel = self._frontend._keep_kernel_on_exit + kernel_manager = self._frontend.kernel_manager - if keepkernel is None: + if keepkernel is None: #show prompt if kernel_manager and kernel_manager.channels_running: title = self.window().windowTitle() cancel = QtGui.QMessageBox.Cancel @@ -106,13 +110,13 @@ class MainWindow(QtGui.QMainWindow): event.accept() else: event.ignore() - elif keepkernel: #close console but leave kernel running + elif keepkernel: #close console but leave kernel running (no prompt) if kernel_manager and kernel_manager.channels_running: if not self._existing: # I have the kernel: don't quit, just close the window self._app.setQuitOnLastWindowClosed(False) event.accept() - else: #close console and kernel + else: #close console and kernel (no prompt) if kernel_manager and kernel_manager.channels_running: kernel_manager.shutdown_kernel() event.accept() diff --git a/IPython/zmq/zmqshell.py b/IPython/zmq/zmqshell.py index 1805622..b418ec3 100644 --- a/IPython/zmq/zmqshell.py +++ b/IPython/zmq/zmqshell.py @@ -78,7 +78,7 @@ class ZMQInteractiveShell(InteractiveShell): """A subclass of InteractiveShell for ZMQ.""" displayhook_class = Type(ZMQDisplayHook) - keepkernel = None + keepkernel_on_exit = None def init_environment(self): """Configure the user's environment. @@ -112,7 +112,7 @@ class ZMQInteractiveShell(InteractiveShell): payload = dict( source='IPython.zmq.zmqshell.ZMQInteractiveShell.ask_exit', exit=True, - keepkernel=self.keepkernel, + keepkernel=self.keepkernel_on_exit, ) self.payload_manager.write_payload(payload) @@ -571,7 +571,7 @@ class ZMQInteractiveShell(InteractiveShell): running. Otherwise, it will shutdown without prompting. """ opts,args = self.parse_options(parameter_s,'k') - self.shell.keepkernel = opts.has_key('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.