0

I'm trying to write a Python program to mark/score a C program.

The C program to be tested is supposed to output something like

0102210303 Michael

Here is the code uploaded by a unqualified student.

#include <stdio.h>
#include <string.h>
int main(int argc, char **argv)
{
  if (argc == 1)
    printf("0102210303 Michael\n");
  printf("%d", strcmp(argv[1], "0102210303"));

  return 0;
}

which outputs

0102210303 Michael
Segmentation fault (core dumped)

I tried the following Python code to catch the output and error

result = subprocess.run(['./a.out'], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
print(result.stdout.decode("utf-8"))
print(result.stderr.decode("utf-8"))

neither stdout nor stderr catches the output, how do I capture something like

0102210303 Michael
Segmentation fault (core dumped)

with Python?

JJJohn
  • 915
  • 8
  • 26
  • 1
    Does this answer your question? [using "try" to avoiding a segmentation fault](https://stackoverflow.com/questions/27950296/using-try-to-avoiding-a-segmentation-fault) – mkrieger1 Dec 26 '21 at 16:09
  • What is `jcc`? Is that a compiler? If so, you're not passing it the name of the source file to be compiled, nor are you executing the produced binary... – John Gordon Dec 26 '21 at 16:13

1 Answers1

1

I may be wrong about this, but I think that the text "Segmentation fault (core dumped)" isn't produced by the crashing program, but by the shell that invoked it. The shell would gather this information by looking at the exit status of the process, probably using the WIFSIGNALED and WCOREDUMP macros (see the wait(2) manual page). You can get some of this info by looking at the return value of subprocess.run.

Ture Pålsson
  • 6,088
  • 2
  • 12
  • 15