简体   繁体   中英

Python subprocess Popen returns with non zero error code but no error

I am trying to execute an executable binary that produces an error. My code for the execution of binary using Popen:

p = Popen(cmd, stdout=PIPE, stderr=PIPE, shell=True)
output, error = p.communicate()
return_code = p.returncode
if error:
   raise SomeLocallyDefinedError

This is supposed to raise error when the script execution results in an error. The cmd in this particular case is the path to the concerned executable binary.

Now when I execute this very binary on my terminal ./binary_file it gives me an error msg

"Floating point exception: 8".

But when the script is executed by Popen , there is no error as well as output. But the returncode is -8. I believe that a non zero return code implies an error and if so why was the message not captured by Popen.communicate()?

In case you guys are wondering what was the executable binary file. The binary file was generated by a compiling ac file that results in run time error. Here is the content of c file

# include<stdio.h>
int main(){
   int a = 18;
   int b = 0;
   int c = a/b;
   printf("%d", c);
}

The output is generated by the shell, while catching an SIGFPE signal. You have to catch this signal in your program by yourself and handle it as you wish.

This is not just an ordinary runtime error, it is a process crash raised by kernel (via a signal).

If you are running on UNIX//Linux/OS X you should get a core dump ( ulimit -c unlimited in the shell). Kernel does not write to a process's stderr - it can't because the process has crashed.

This is not an issue with Python.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM