Is there a way how to preserve stdout and stderr order when subprocess module is used?
hello.c
#include <stdio.h>
void main()
{
printf("o1 ");
fprintf(stderr, "e1 ");
printf("o2 ");
fprintf(stderr, "e2 ");
printf("o3 ");
fprintf(stderr, "e3 ");
}
test.py
import subprocess
print ("Test 1")
sp = subprocess.Popen("hello", stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
output = str(sp.communicate()[0])
print(output)
print ("Test 2")
sp = subprocess.Popen("hello", stdout = subprocess.PIPE, stderr = subprocess.PIPE)
stdout, stderr = sp.communicate()
print(stdout + stderr)
print ("Test 3")
print(subprocess.check_output("hello", stderr=subprocess.STDOUT))
Output:
hello
o1 e1 o2 e2 o3 e3
Test 1
o1 o2 o3 e1 e2 e3
Test 2
o1 o2 o3 e1 e2 e3
Test 3
o1 o2 o3 e1 e2 e3