ozindfw
ozindfw

Reputation: 21

Python os.system() returning bad value

Background: I'm using a Python script with os.system() to repeatedly call a command line program to run test cases. The program being tested returns zero for normal operation and a negative value for error conditions. I compare this value against what is expected (not always zero) as part of determining pass/fail.

The problem is that a normally exiting test case is returning zero, but os.system() is returning -1073740940. So far, other non-zero returns are working as expected, though the sample is small.

I'm fairly confident the called program is returning zero because I output the return value to std out just before I return. Also, if run the program with the same inputs from the command line or a batch file, the return value is zero.

Possibly related, the C++ program generates a log file using ofstream with the same content as the command line - the same buffer is sent to std::cout and the log file. When controlled by the os.system() call, the log file content stops about 80% of the way into the execution of the program even though the cout information continues normally. As with the return value if run with the same inputs from the command line or a batch file, the log file completes correctly and has the same content as the terminal.

Behavior is the same under Python 2.7 and 3.8 for both the return value and log file issues.

My question is: How do I diagnose this? I'm not sure what else to try.

Upvotes: 1

Views: 684

Answers (2)

gelonida
gelonida

Reputation: 5630

You are using os.system, which does not as you expect return the exit code, but something different.

in https://docs.python.org/3.8/library/os.html#os.system you can read

On Unix, the return value is the exit status of the process encoded in the format specified for wait(). Note that POSIX does not specify the meaning of the return value of the C system() function, so the return value of the Python function is system-dependent.

So either use subprocess.call or do something like

rslt = os.system("whatever")
exit_code = 0xff & rslt

Upvotes: 2

ozindfw
ozindfw

Reputation: 21

So the short answer is that the value I'm getting is the exit code from the program, unless Windows feels it has something important to say.

In this case a DLL I was calling was corrupting the heap and cause windows to abort the execution. To get the actual 32 bit Windows return code and not a sign extended version I needed to AND it with 0xffffffff. I don't know if I can answer my own question, but gelonida set me on the right trail the answer.

Upvotes: 1

Related Questions