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.

3
  • Does this answer your question? How to get exit code when using Python subprocess communicate method? Commented Jan 3, 2020 at 7:25
  • Try checking testRun.returncode Commented Jan 3, 2020 at 7:25
  • Thanks ChatterOne for the response. But it did not help. Trying other suggestions. Commented Jan 3, 2020 at 7:31

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

The return code will be whatever is return'ed from main, calling exit when you're terminating the program by returning from main is unnecessary
I agree, but for both return 0 and return 1 the subproc.returncode will always be 0 for a successful termination of the program.
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.

1 Comment

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.