1

I am writing a python script which compiles and runs a C/C++ code. I want to capture runtime errors. Eg: Nzecv, Segmentation fault, Fragmentation Error etc

I am using following command to execute the compiled file:

process = Popen( exec_path , stdin=PIPE, stdout=PIPE, stderr=PIPE, shell= False) 
(output, error) = process.communicate(testCase)

I have tried few programs with runtime errors, but the errors arent reflected in stderr. After some research i found that stderr reads from return value of program. And in C and mostly all other programs, main returns custom ("return 0;") value not the error signal.

Is it possible to catch runtime error signal? How should i go about implementing it?

EDIT: Can runtime error signal be captured by Terminal? HOW? If yes, ill run the corresponding shell script through python.

1 Answers1

1

Is it possible to catch runtime error signal? How should i go about implementing it?

To find out whether the child process is killed by a signal, you could inspect process.returncode:

A negative value -N indicates that the child was terminated by signal N (Unix only).

Note: if you want to provide a custom handler for a signal in the child process, you could call signal.signal() in preexec_fn, see code example that shows how to restore default signal handler using prexec_fn

Community
  • 1
  • 1
jfs
  • 399,953
  • 195
  • 994
  • 1,670