os.system() << 8 ?

Andrew Wilkinson ajw126 at NOSPAMyork.ac.uk
Wed May 28 11:49:24 EDT 2003


This is dealt with in the documentation...
http://www.python.org/doc/current/lib/os-process.html

system(command) 
... The return value is the exit status of the process encoded in the format
specified for wait()...

and the wait command...

wait() 
Wait for completion of a child process, and return a tuple containing its 
pid and exit status indication: a 16-bit number, whose low byte is the 
signal number that killed the process, and whose high byte is the exit 
status (if the signal number is zero); the high bit of the low byte is set
if a core file was produced. Availability: Unix. 

So the return code is in the high byte, and hence the need to shift it by
eight to get the correct return value.

HTH,
Andrew Wilkinson

Jacek Generowicz wrote:

> #include <cstdlib>
> 
> int main(int argc, char* argv[]) {
>   return atoi(argv[1]);
> }
> 
> Compiling the above to ./a.out, and calling it with the following
> Python script
> 
> import os
> for i in range(256):
>     print i, os.system("./a.out " + `i`)
> 
> 
> gives results that look like this:
> 
> 0 0
> 1 256
> 2 512
> 3 768
> 4 1024
> 5 1280
> 6 1536
> 7 1792
> 8 2048
> 
> ... and so on.
> 
> Why does os.system seem to return a value which is 256 times the actual
> status code returned by its argument ?





More information about the Python-list mailing list