Consider a python script error.py
import sys
sys.exit(3)
Invoking
python error.py; echo $?
yields the expected "3". However, consider runner.py
import os
result = os.system("python error.py")
print result
yields 768. It seems that somehow the result of python code has been leftshifted by 8, but how these two situations are different is not clear. What's going on?
This is occurring in python 2.5 and 2.6.
From the docs:
On Unix, the return value is the exit status of the process encoded inthe format specified for wait(). Note that POSIX does not specify themeaning of the return value of the C system() function, so the returnvalue of the Python function is system-dependent.
os.wait()
Wait for completion of a child process, and return a tuple containingits pid and exit status indication: a 16-bit number, whose low byte isthe signal number that killed the process, and whose high byte is theexit status (if the signal number is zero); the high bit of the lowbyte is set if a core file was produced.
In your case, the return value 768 in binary is 00000011 00000000
. The high byte is 3.