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

carl.dhalluin at gmail.com carl.dhalluin at gmail.com
Thu Jul 12 05:30:45 EDT 2007


On 12 jul, 01:32, Wojciech Mu a
<wojciech_m... at poczta.null.onet.pl.invalid> wrote:
> carl.dhall... at 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(.)




More information about the Python-list mailing list