atexit, sys.exit, sys.exitfunc, reaching end of source code

C

carl.dhalluin

Hi,

I am playing with the atexit module but I don't find a way to see the
difference
between a script calling sys.exit(<returncode>) and the interpreting
arriving at the end
of the source code file. This has a semantic difference for my
applications.
Is there a way to determine in an exithandler (that is registered
using atexit.register)
how I exited?

Second question: is there a way to determine in my exithandler what
the return code was.
atexit doesn't seem to support that

Third question: I can solve part of my problem by reassigning sys.exit
= myfunction
Is that a wise idea? It seems that an interpreter (python 2.4)
reaching end of source code, calls sys.exitfunc instead of sys.exit ?


Thanks

Carl D'Halluin

www.qlayer.com
 
W

Wojciech =?iso-8859-2?Q?Mu=B3a?=

I am playing with the atexit module but I don't find a way to see the
difference
between a script calling sys.exit(<returncode>) and the interpreting
arriving at the end
of the source code file. This has a semantic difference for my
applications.
Is there a way to determine in an exithandler (that is registered
using atexit.register)
how I exited?

Actually sys.exit raises exception SystemExit, but if interpreter
reaches end of script exception is not raised. Try something
like this:

if __name__ == '__main__':
exit_code_for_exithandler = None
try:
#...
sys.exit(5)
pass
#...
except SystemExit, e:
exit_code_for_exithandler = e.code
print "sys.exit called"
else:
print "end of script"

w.
 
C

carl.dhalluin

Actually sys.exit raises exception SystemExit, but if interpreter
reaches end of script exception is not raised. Try something
like this:

if __name__ == '__main__':
exit_code_for_exithandler = None
try:
#...
sys.exit(5)
pass
#...
except SystemExit, e:
exit_code_for_exithandler = e.code
print "sys.exit called"
else:
print "end of script"

w.

I am getting quite confused with this SystemExit.
If I register a custom sys.excepthook then it is never invoked
when I do sys.exit(.)
 
G

Gabriel Genellina

En Thu, 12 Jul 2007 06:30:45 -0300, (e-mail address removed)
I am getting quite confused with this SystemExit.
If I register a custom sys.excepthook then it is never invoked
when I do sys.exit(.)

No, sys.excepthook won't be called for SystemExit:

py> help(sys)
Help on built-in module sys:
[...]
excepthook -- called to handle any uncaught exception other than
SystemExit

Unfortunately help(sys.excepthook) does not provide the same information,
neither the docs for the sys module. (Should be corrected...)
 

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,982
Messages
2,570,185
Members
46,736
Latest member
AdolphBig6

Latest Threads

Top