[Python-Dev] a quit that actually quits

Fredrik Lundh fredrik at pythonware.com
Tue Dec 27 12:40:50 CET 2005


sourceforge just went off the air, so I'm posting this patch here, in order
to distract you all from Christian's deque thread.

this silly little patch changes the behaviour of the interpreter so that "quit"
and "exit" actually exits the interpreter.  it does this by installing a custom
excepthook that looks for NameErrors at the top level, in interactive mode
only.

whaddya think?

</F>

Index: Lib/site.py
===================================================================
--- Lib/site.py (revision 41831)
+++ Lib/site.py (working copy)
@@ -60,6 +60,7 @@
 import sys
 import os
 import __builtin__
+import traceback


 def makepath(*paths):
@@ -222,19 +223,20 @@


 def setquit():
-    """Define new built-ins 'quit' and 'exit'.
-    These are simply strings that display a hint on how to exit.
+    """Set default exception handler for the interactive mode."""
+    def defaultexcepthook(exc_type, exc_value, exc_info):
+        if hasattr(sys, "ps1"):
+            # interactive mode
+            if isinstance(exc_value, NameError) and not exc_info.tb_next:
+                text = exc_value[0]
+                if (text == "name 'exit' is not defined" or
+                    text == "name 'quit' is not defined"):
+                    # XXX: print helpful "Use control-D etc" message here?
+                    raise SystemExit
+                # XXX: add if text == "help" ?
+        traceback.print_exception(exc_type, exc_value, exc_info)
+    sys.excepthook = defaultexcepthook

-    """
-    if os.sep == ':':
-        exit = 'Use Cmd-Q to quit.'
-    elif os.sep == '\\':
-        exit = 'Use Ctrl-Z plus Return to exit.'
-    else:
-        exit = 'Use Ctrl-D (i.e. EOF) to exit.'
-    __builtin__.quit = __builtin__.exit = exit
-
-
 class _Printer(object):
     """interactive prompt objects for printing the license text, a list of
     contributors and the copyright notice."""





More information about the Python-Dev mailing list