atexit.register in case of errors

A

Andrea Crotti

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?
I've seen the code of atexit and it apparently doesn't know anything
about the current
status and why the application is actually quitting, is that correct?
 
M

Mel Wilson

Andrea said:
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?
I've seen the code of atexit and it apparently doesn't know anything
about the current
status and why the application is actually quitting, is that correct?

That's sort of the point: to do things that simply *have* to happen, even if
you've lost control of the program.

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.

Mel.
 
D

Devin Jeanpierre

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
 
A

Andrea Crotti

In such a case. one can do::

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

-- Devin

Makes perfect sense, I solved like this then, thanks
 
T

Thomas Rachel

Am 15.02.2012 14:52 schrieb Devin Jeanpierre:
In such a case. one can do::

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

-- Devin

Wouldn't

if __name__ == '__main__':
try:
main()
finally:
goodbye()

be even better? Or doesn't it work well together with SystemExit?


Thomas
 
A

Andrea Crotti

Wouldn't

if __name__ == '__main__':
try:
main()
finally:
goodbye()

be even better? Or doesn't it work well together with SystemExit?


Thomas

Well in that case goodbye is always called, even if I have some other
nasty exception,
which is not what I want.. (and is exactly what I had with atexit.register).
Isn't it?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top