[Python-Dev] a quit that actually quits

Nick Coghlan ncoghlan at gmail.com
Thu Dec 29 04:18:59 CET 2005


Reinhold Birkenfeld wrote:
> Fredrik Lundh wrote:
>> Walter Dörwald wrote:
>>
>>> We have sys.displayhook and sys.excepthook. Why not add a sys.inputhook?
>>> sys.inputhook gets passed each line entered and may return True if it has
>>> processed the line inself and False if normal handling of the input should be
>>> done. This allows special treatment of "quit", "exit", "help" /.../
>> so how would such a hook deal with the
>>
>>     >>> def exit():
>>     ...     pass
>>     >>> exit
>>
>> case ?
> 
> In the inputhook one would have to check for "exit" being defined at
> interpreter level.

Which is fairly trivial given a slight change to my proposed default input hook:

    def default_inputhook(statement):
        if statement in vars(sys.modules["__main__"]):
            return False
        try:
            aliased = sys.alias[statement]
        except KeyError:
            return False
        else:
            aliased()
            return True

That is, a real variable will always shadow an alias - you need to get rid of 
the real variable before the alias will start working again (or else change 
the name of the alias).

Or you can give the alias a different name via:

   sys.alias["exit_"] = sys.alias["exit"]

Cheers,
Nick.

-- 
Nick Coghlan   |   ncoghlan at gmail.com   |   Brisbane, Australia
---------------------------------------------------------------
             http://www.boredomandlaziness.org


More information about the Python-Dev mailing list