Re: exception KeyboardInterrupt and os.system command

Discussion in 'Python' started by jepler@unpythonic.net, Nov 27, 2005.

  1. Guest

    You can tell by the exit code from system() whether the subprocess
    exited due to a signal. Consider this code:
    import os
    while 1:
    print os.system("sleep 1")
    unless you happen to hit ctrl-c at the right time, you'll see it print
    "2" (and "0" when the sleep finishes). The exit code can be interpreted
    according to the waitpid manpage, though I am not sure where the Python
    equivalent of the WIFSIGNALED and WTERMSIG macros are.

    Jeff

    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.4.1 (GNU/Linux)

    iD8DBQFDij8sJd01MZaTXX0RAimpAJsGTXMcuMEGLtsfeRMrHYpQ79DfOwCfUyHQ
    i4Jt2ISfBDnRG3VODg/xc2w=
    =8z/z
    -----END PGP SIGNATURE-----
    , Nov 27, 2005
    #1
    1. Advertising

  2. wrote:
    > You can tell by the exit code from system() whether the subprocess
    > exited due to a signal. Consider this code:
    > import os
    > while 1:
    > print os.system("sleep 1")
    > unless you happen to hit ctrl-c at the right time, you'll see it print
    > "2" (and "0" when the sleep finishes). The exit code can be interpreted
    > according to the waitpid manpage, though I am not sure where the Python
    > equivalent of the WIFSIGNALED and WTERMSIG macros are.


    Boy, one never stops learning....

    But I have some trouble understanding that - when I write a test-script
    that returns 1 as exit-code, os.system returns 256. Inn fact, returnung
    n means that 256*n is returned from os.system.

    But 256 is _also_ returned when C-c is used. Any idea why that's the case?

    Regards,

    Diez
    Diez B. Roggisch, Nov 28, 2005
    #2
    1. Advertising

  3. malv Guest

    That's also kind of what I expected.
    However, I quickly tried:
    import os
    while 1:
    y = os.system("sleep 1")
    z = (y >> 8) & 0xFF
    print z

    I never get anything in return but 0, hitting c-C or not.
    I have uset the above code to get exit code returns in the past though.
    Would there be anything special with sleep?

    Regards,
    malv
    malv, Nov 28, 2005
    #3
  4. Donn Cave Guest

    In article <>,
    "malv" <> wrote:
    > That's also kind of what I expected.
    > However, I quickly tried:
    > import os
    > while 1:
    > y = os.system("sleep 1")
    > z = (y >> 8) & 0xFF
    > print z
    >
    > I never get anything in return but 0, hitting c-C or not.
    > I have uset the above code to get exit code returns in the past though.
    > Would there be anything special with sleep?


    That algorithm will give you the same thing as os.WEXITSTATUS(),
    on most platforms, though not necessarily all so it's better
    to use the function.

    On platforms where it works, exit status is of course stored in
    2nd byte from the low end, and signal status is stored separately,
    in the low byte. So naturally, your right shift discards the
    signal status and you're left with 0.

    On the other hand, if you use os.spawnv, signal status will
    be returned as a negative integer, instead of a positive
    integer exit status. spawnv() is safer than system() if the
    command is constructed from data, and it also doesn't block
    SIGINT in the caller like system does, so it would work for
    the problem posed in the original post.

    But it might be just as well to watch the process status for
    any non-zero value, and then call the graceful exit procedure.

    Donn Cave,
    Donn Cave, Nov 28, 2005
    #4
    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. PantherSE
    Replies:
    0
    Views:
    329
    PantherSE
    May 16, 2005
  2. darren kirby
    Replies:
    1
    Views:
    515
    Diez B. Roggisch
    Nov 27, 2005
  3. darren kirby
    Replies:
    1
    Views:
    327
  4. Tony Nelson

    Trace KeyboardInterrupt exception?

    Tony Nelson, Jun 13, 2006, in forum: Python
    Replies:
    4
    Views:
    495
    Tony Nelson
    Jun 15, 2006
  5. Michael Goerz
    Replies:
    3
    Views:
    406
    Gabriel Genellina
    Feb 4, 2008
Loading...

Share This Page