I'm running a C executable from Python and this executable sometimes segfaults. When it segfaults the subprocess module does not return anything in stdout or stderr.
Sample code:
import subprocess
proc = subprocess.Popen(['./a.out'], stdin=subprocess.PIPE,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = proc.communicate()
print 'out: "%s"' % out
print 'err: "%s"' % err
print proc.returncode
The source for a.out
is:
int main() {
printf("hello world\n");
int x = 1 / 0;
}
The output of ./a.out
is:
hello world
Floating point exception
The output of the Python code is (in Linux, python 2.7):
out: ""
err: ""
-8
Is there a way to get the output of the executable even if it crashes?
A generic method to translate returncode to a string message, would also be nice.