atexit.register in case of errors

Devin Jeanpierre jeanpierreda at gmail.com
Wed Feb 15 14:52:21 CET 2012


On Wed, Feb 15, 2012 at 8:33 AM, Mel Wilson <mwilson at the-wire.com> wrote:
> The usual way to do what you're asking is
>
> if __name__ == '__main__':
>    main()
>    goodbye()
>
> and write main so that it returns after it's done all the things it's
> supposed to do.  If you've sprinkled `sys.exit()` all over your code, then
> don't do that.  If you're forced to deal with a library that hides
> `sys.exit()` calls in the functions, then you have my sympathy.  Library
> authors should not do that, and there have been threads on c.l.p explaining
> why they shouldn't.

In such a case. one can do::

    if __name__ == '__main__':
        try:
            main()
        except SystemExit:
            pass
        goodbye()

-- Devin



More information about the Python-list mailing list