0

I have a C(on windows) program which has a main function which returns a value.

#include <stdio.h>    
int testData()
{
  int testErr = 0;    
  // ....

  return(testErr);    
}

int main(void) {
  int mainErr = 0;

  mainErr = testData();  
  printf("mainerr = %d", mainErr);

  return mainErr;
}

This C code executable myTest.exe is run through python.

self.run_cmd = "myTest.exe "+cmd           # pass agruments to the C exe

    self.testRun = subprocess.run(self.run_cmd,
                                   stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE,
                                   bufsize=0, universal_newlines=True, timeout=100)
    print(">>>>", self.testRun.stdout)
    print(">>>>", self.testRun.stderr)

Using "print(">>>>", self.testRun.stdout)" I am not getting the C code's return value in python. In python how to get the return value returned by C main.

RKum
  • 758
  • 2
  • 12
  • 33
  • Does this answer your question? [How to get exit code when using Python subprocess communicate method?](https://stackoverflow.com/questions/5631624/how-to-get-exit-code-when-using-python-subprocess-communicate-method) – ChatterOne Jan 03 '20 at 07:25
  • Try checking testRun.returncode – Steve Jan 03 '20 at 07:25
  • Thanks ChatterOne for the response. But it did not help. Trying other suggestions. – RKum Jan 03 '20 at 07:31

3 Answers3

2

The run function returns a CompletedProcess object, which have a returncode property.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
0

In python subprocess module monitors the return value of the command (i.e. 0 for success and 1 for failure). Since you are returning some value from your C program the program successfully exited with return code 0. What you can do here is you can use exit(1) in your C program to terminate the program with exit code 1 where you want to return 1. The exit code of the C program can be monitored using,

subproc = subprocess.run(<command to execute>, stdout=subprocess.PIPE, stderr=subprocess.PIPE,)
if (subproc.returncode == 0):
    logger.info('Exited successfully')

Other way is to write your desired output in some files or DB and read that from your python code.

Soumyajit
  • 329
  • 1
  • 5
  • 16
  • 2
    The return code will be whatever is `return`'ed from `main`, calling `exit` when you're terminating the program by `return`ing from `main` is unnecessary – nickelpro Jan 03 '20 at 07:27
  • 1
    I agree, but for both ```return 0``` and ```return 1``` the ```subproc.returncode``` will always be 0 for a successful termination of the program. – Soumyajit Jan 03 '20 at 07:32
0

The return value obtained from using subprocess will be bytes. After running :

self.testRun = subprocess.run(self.run_cmd, stdout=subprocess.PIPE, stdin=None, stderr=subprocess.PIPE,  bufsize=0, universal_newlines=True, timeout=100)

You can capture the output as :

result = self.testRun.stdout.decode('utf-8')

This result is a string and can be used further.

Parv Sachdeva
  • 31
  • 1
  • 3
  • Thanks Parv for the suggestion. Since stdout is already in utf format so no need of decoding it. stdout does give only the return value of the main but entire printf logging done by the C program and the python script. – RKum Jan 03 '20 at 09:04