Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

os.system() << 8 ?

33 views
Skip to first unread message

Jacek Generowicz

unread,
May 28, 2003, 2:53:31 PM5/28/03
to
#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 ?

P...@draigbrady.com

unread,
May 28, 2003, 3:37:31 PM5/28/03
to
Jacek Generowicz wrote:
> Why does os.system seem to return a value which is 256 times the actual
> status code returned by its argument ?

os.WEXITSTATUS

man waitpid for more info

Pádraig.

Andrew Wilkinson

unread,
May 28, 2003, 3:49:24 PM5/28/03
to
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

Andreas Jung

unread,
May 28, 2003, 3:26:47 PM5/28/03
to
os.system() return the *exit* code from an application but not the value
returned
by return.

-aj

--On Mittwoch, 28. Mai 2003 16:53 Uhr +0200 Jacek Generowicz
<jacek.ge...@cern.ch> wrote:

># include <cstdlib>
>
> int main(int ar

Donn Cave

unread,
May 28, 2003, 4:43:58 PM5/28/03
to
| os.system() return the *exit* code from an application but not the value
| returned
| by return.

On the contrary, "return n" from main() is the same as "exit(n)",
at least in common C implementations on UNIX. The exit status
provided to the parent fork contains this return value and a couple
other status values pertaining to the cause of death. On UNIX
platforms the integer exit value is shifted up 8 bits, and the
low 8 bits contains information about signals if one occurred, but
I expect POSIX requires only that WEXITSTATUS yield the correct
value - better to use that to do whatever shifting is necessary.

Donn Cave, do...@u.washington.edu

Erik Max Francis

unread,
May 28, 2003, 7:29:50 PM5/28/03
to
Jacek Generowicz wrote:

> Why does os.system seem to return a value which is 256 times the
> actual
> status code returned by its argument ?

Others have pointed out where in the documentation the behavior is
documented, and how to get the exit value (os.WEXITSTATUS). Note that
Python is just following POSIX's lead; the C system call `system'
behaves the same way (man 3 system):

RETURN VALUE
The value returned is -1 on error (e.g. fork failed), and
the return status of the command otherwise. This latter
return status is in the format specified in wait(2).
Thus, the exit code of the command will be WEXITSTA-
TUS(status). In case /bin/sh could not be executed, the
exit status will be that of a command that does exit(127).

--
Erik Max Francis && m...@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Your theory is not right; it is not even wrong.
\__/ Wolfgang Pauli

0 new messages