Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #20478 > unrolled thread

Re: atexit.register in case of errors

Started byTerry Reedy <tjreedy@udel.edu>
First post2012-02-15 16:31 -0500
Last post2012-02-15 16:31 -0500
Articles 1 — 1 participant

Back to article view | Back to comp.lang.python

This discussion starts older than the indexed window; earlier articles aren't shown. The article labeled Started by below is the oldest one visible, not the original post.


Contents

  Re: atexit.register in case of errors Terry Reedy <tjreedy@udel.edu> - 2012-02-15 16:31 -0500

#20478 — Re: atexit.register in case of errors

FromTerry Reedy <tjreedy@udel.edu>
Date2012-02-15 16:31 -0500
SubjectRe: atexit.register in case of errors
Message-ID<mailman.5856.1329341500.27778.python-list@python.org>
On 2/15/2012 8:12 AM, Andrea Crotti wrote:
> I have the following very simplified situation
>
> from atexit import register
>
> def goodbye(): print("saying goodbye")
>
> def main():
 >   while True: var = raw_input("read something")
>
> if __name__ == '__main__':
 >   register(goodbye)
 >   main()
>
> But in my case the "goodbye" function is deleting the logging file
> which was created during the application execution. Now the problem
> is that it *always* executes, even when the applications quits for
> some bad errors.
>
> Is there a way to have an exit hook, which doesn't execute in case of
> errors?

Have a single no-error normal exit point.

if __name__ == '__main__':
    main()
    cleanup()

if you really want to exit by exceptions rather than by returns,

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

-- 
Terry Jan Reedy

[toc] | [standalone]


Back to top | Article view | comp.lang.python


csiph-web