Don’t trust python’s os.execv

Python is nice and all, but its low-level functions have real disruptive discrepancies between platforms.

Case at point:

import os
os.execvp("sh", ["sh", "-c", "exit 1"])

As a UNIXy person, I’d expect running the above script to return an error code of 1. And I would be perfectly right… on UNIX systems.

On Windows, it returns 0.

You’d think such a difference in behavior would be documented? It’s not.

Thank you python.

2013-11-23 01:24:26+0900

p.d.o, p.m.o

You can leave a response, or trackback from your own site.

8 Responses to “Don’t trust python’s os.execv”

  1. Erik Says:

    Is this a Python issue, or a Windows issue? If Windows is setting the return code to 0, then what is Python to do about it?

  2. Miek Gieben Says:

    Sounds silly to blame this on Python…

    But, why make stuff that works on Windows anyway??

  3. Leo Antunes Says:

    Wild guess, but if you’re using cygwin, could this have something to do with this?

  4. Matěj Cepl Says:

    @Erik … What is Python to do about it? What about documenting it?

  5. Erik Says:

    @Matěj – What is there to document? execv() has the same behavior on both Windows and Unix, which is to fire off a new process and let it run. Then you start up sh, presumably a cygwin binary. sh exits with a return code of zero on Windows. At the point at which you see the behavior, Python isn’t even running, as you’ve exec’ed into sh. That may be weird, but if it’s weird, it is either Windows or your sh binary. Python is having identical behavior across platforms.

  6. @Erik Says:

    @Erik execv() does not create a new process. It replaces the existing process.

  7. chaica Says:

    Found a bug in the Python documentation? Please submit a bug report/patch to the Python project =>

  8. Kay Hayen Says:

    Hit me too. I now use on Windows in Nuitka as well. Which is a pity, because it really would want to stop the original process right then and now it lives on just to eait for exit code to then exit with the same.


Leave a Reply