Created
July 10, 2013 04:45
-
-
Save dagbrown/5963542 to your computer and use it in GitHub Desktop.
If a computer knows what a person is trying to accomplish, it's not the computer's job to actually do what the person was asking to do. It's the computer's job to tell them the correct way to do what they're obviously trying to do.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def setquit(): | |
"""Define new builtins 'quit' and 'exit'. | |
These are objects which make the interpreter exit when called. | |
The repr of each object contains a hint at how it works. | |
""" | |
if os.sep == ':': | |
eof = 'Cmd-Q' | |
elif os.sep == '\\': | |
eof = 'Ctrl-Z plus Return' | |
else: | |
eof = 'Ctrl-D (i.e. EOF)' | |
class Quitter(object): | |
def __init__(self, name): | |
self.name = name | |
def __repr__(self): | |
return 'Use %s() or %s to exit' % (self.name, eof) | |
def __call__(self, code=None): | |
# Shells like IDLE catch the SystemExit, but listen when their | |
# stdin wrapper is closed. | |
try: | |
sys.stdin.close() | |
except: | |
pass | |
raise SystemExit(code) | |
__builtin__.quit = Quitter('quit') | |
__builtin__.exit = Quitter('exit') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment