Last active
September 15, 2016 17:07
-
-
Save leorochael/db1874a5e400a188876291e10e919f84 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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