471,342 Members | 1,962 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

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

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

Jul 11 '07 #1
3 3171
ca***********@gmail.com wrote:
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.
Jul 11 '07 #2
On 12 jul, 01:32, Wojciech Mu a
<wojciech_m...@poczta.null.onet.pl.invalidwrote:
carl.dhall...@gmail.com wrote:
I am playing with theatexitmodule 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
usingatexit.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.
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(.)

Jul 12 '07 #3
En Thu, 12 Jul 2007 06:30:45 -0300, ca***********@gmail.com
<ca***********@gmail.comescribió:
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:

pyhelp(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...)

--
Gabriel Genellina

Jul 13 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Hameed Khan | last post: by
3 posts views Thread by Serve Laurijssen | last post: by
8 posts views Thread by JKop | last post: by
2 posts views Thread by Steve Lambert | last post: by
7 posts views Thread by David Rushby | last post: by
16 posts views Thread by Laurent Deniau | last post: by
2 posts views Thread by Christopher Pisz | last post: by
reply views Thread by rosydwin | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.