Can I get the exit code "n" passed to sys.exit(n) ?

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Tue Apr 10 16:35:22 EDT 2007


En Tue, 10 Apr 2007 15:15:57 -0300, Yujo <yujorodrigues at gmail.com>  
escribió:

> In the following code of the finish() function, is there any way to
> get the exit code passed to sys.exit() ?
>
> def finish() :
>    RETURN_CODE_FROM_SYS_EXIT = ????    # how can I get it ?
>    if RETURN_CODE_FROM_SYS_EXIT = 0 :
>      # process ended OK
>    else :
>      # process ended with some error
>      # execute something
>
> atexit.register(finish)
>
> # this is my main program....
>
> ERR_CODE=3
> sys.exit(ERR_CODE)

Uhm, I think you can't, unfortunately (at least on 2.4; I don't have the  
2.5 sources at hand).
But you can instead wrap your main function with a try/except SystemExit:

def main():
   return 3

try:
   sys.exit(main())
except SystemExit, exitcode:
   finish(exitcode)

-- 
Gabriel Genellina




More information about the Python-list mailing list