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

Discussion in 'Python' started by Yujo, Apr 10, 2007.

  1. Yujo

    Yujo Guest

    Hello everybody,

    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)


    Regards,

    Yujo
     
    Yujo, Apr 10, 2007
    #1
    1. Advertising

  2. Yujo

    Guest

    On Apr 10, 1:15 pm, "Yujo" <> wrote:
    > Hello everybody,
    >
    > 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)
    >
    > Regards,
    >
    > Yujo


    I'm not sure if this will help or not, but this article is pretty
    enlightening and is written by a well-known Pythonista:

    http://www.artima.com/weblogs/viewpost.jsp?thread=4829

    Here is another older post with helpful information:
    http://www.daniweb.com/techtalkforums/thread31226.html

    And here's a hack that might work: http://www.daniweb.com/techtalkforums/thread31166.html

    Mike
     
    , Apr 10, 2007
    #2
    1. Advertising

  3. En Tue, 10 Apr 2007 15:15:57 -0300, Yujo <>
    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
     
    Gabriel Genellina, Apr 10, 2007
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. GinTon

    sys.stderr.write and sys.exit

    GinTon, Nov 23, 2006, in forum: Python
    Replies:
    5
    Views:
    597
    Dennis Lee Bieber
    Nov 24, 2006
  2. Replies:
    3
    Views:
    308
    Gabriel Genellina
    Jul 13, 2007
  3. Harlin Seritt

    Sys.exit() does not fully exit

    Harlin Seritt, Feb 17, 2008, in forum: Python
    Replies:
    3
    Views:
    298
    Christian Heimes
    Feb 17, 2008
  4. Brendan

    exit() or sys.exit()

    Brendan, Jun 17, 2009, in forum: Python
    Replies:
    2
    Views:
    412
    Brendan
    Jun 17, 2009
  5. chad
    Replies:
    14
    Views:
    605
    Arnaud Delobelle
    Oct 13, 2010
Loading...

Share This Page