Skip to content

Instantly share code, notes, and snippets.

@leorochael
Last active September 15, 2016 17:07
Show Gist options
  • Save leorochael/db1874a5e400a188876291e10e919f84 to your computer and use it in GitHub Desktop.
Save leorochael/db1874a5e400a188876291e10e919f84 to your computer and use it in GitHub Desktop.
"""
This file can be used to demonstrate that the exit status of the process
that is run by a os.exec*() call will be taken as the exit status of the
original process, as can be expected from the fact that os.exec() completely
replaces the original process.
Use it like this on bash:
python test_exit.py die 2 ; echo $?
python test_exit.py sub 2 ; echo $?
python test_exit.py exe 2 ; echo $?
On Windows CMD, you need to `echo %errorcode%` instead.
Replace "2" with whatever exit status you want to test.
"""
import sys
import os
import subprocess
def sub(status):
args = [sys.executable, sys.argv[0], "die", status]
print "running: sys.exit(subprocess.call({})".format(args)
sys.exit(subprocess.call(args))
def exe(status):
args = [sys.executable, sys.argv[0], sys.argv[0], "die", status]
print "running: execl(*{})".format(args)
sys.stdout.flush(); sys.stderr.flush()
os.execl(*args)
def die(status):
print "dying with status:", status
sys.exit(int(status))
if __name__ == "__main__":
globals()[sys.argv[1]](sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment