[Python-Dev] Proposal - KeyboardInterrupt should inherit directly from Exception

Barry A. Warsaw barry@zope.com
Wed, 7 Nov 2001 22:17:10 -0500


>>>>> "SM" == Skip Montanaro <montanaro@tttech.com> writes:

    SM> I suggest that KeyboardInterrupt should also inherit from
    SM> Exception, and not StandardError.

It doesn't sound completely unreasonable, but I'd be -1 on it for
Python 2.2.

>>>>> "GW" == Greg Ward <gward@python.net> writes:

    GW> Hmmm... does anyone else habitually write

    |   if __name__ == "__main__":
    |       try:
    |           main()
    |       except KeyboardInterrupt:
    |           sys.exit("interrupted")

No, but I sometimes put "pass" in the except-suite of a KeyboardInterrupt.

    GW> And is anyone else sick of doing this?

Not really.  I only do it for one or two daemon main loops, generally
never for plain scripts.  Hitting C-c and seeing the traceback is
usually fine, and often exactly what I want!  E.g. I want to know that
I had to kill it in the take_yer_time_figgering_this_one_out() method.

>>>>> "Fred" == Fred L Drake, Jr <fdrake@acm.org> writes:

    Fred> import errno

    |     # output result to file...
    |     try:
    |         write_result()  # or whatever it really is...
    |     except IOError, e:
    |         if e.errno != errno.EPIPE:
    |             raise

I do stuff like this all the time, although it's usually OSError.  I
love that OSError and IOError have a common base class!  I've often
wanted all the errno's to be transformed into subclasses of
IOError/OSError, so I could just do something like:

    try:
	os.mkdir(...)
    except OSErrorEEXIST:
	pass
    # any other OSError propagates up

-Barry