Search code examples
pythoncstructpopen

How to pass a C struct to Python to get data?


I have code for both Python and C that need to communicate to each other through a pipe created by Popen. I have a test struct in C that needs to be passed back to Python but I can't seem to reconstruct that struct on the Python side. This is a much more complicated project but the struct I created below is just an example to get the code to work, and I can try to figure out the more advanced things later. I am not an expert in C, pointers and piping, and I do not have a clear understanding of it. Most of the C code below is just from my readings.

Python:

testStruct = struct.Struct('< i') 
cProg = Popen("./cProg.out", stdin=PIPE, stdout=PIPE)
data = ""
dataRead = cProg.stdout.read(1)
while dataRead != "\n":
    data += dataRead
    dataRead = cProg.stdout.read(1)

myStruct = testStruct.unpack(data)
print myStruct.i

C:

typedef struct{
    int i;
} TestStruct;

int main(void)
{
    int wfd = fileno(stdout);
    TestStruct t;
    t.i = 5;
    char sendBack[sizeof(t)];
    memcpy(sendBack, &t, sizeof(t));
    write(wfd, sendBack, sizeof(sendBack));
    write(wfd, "\n", 1);

}

But when I run the Python code I get the error:

unpack requires a string argument of length 4

Like I said I do not understand how structs and C. If there's any suggestion on refining this code, or better yet another suggestion on passing a C struct back to Python to unpack and grab the data. I can read and write through the pipe, the code I have posted are just snippets from my actual code. I know that the issue has to do with sending of the struct back to Python through stdout.


Solution

  • Here's an example of reading data in Python from a C program through a pipe.

    C Program

    #include <stdio.h>
    
    typedef struct{
        int i;
        int j;
    } TestStruct;
    
    int main() {
        TestStruct ts = {11111, 22222};
        fwrite(&ts, sizeof ts, 1, stdout);
        return 0;
    }
    

    Python 2.7 Program

    from subprocess import Popen, PIPE
    from struct import calcsize, unpack
    
    cprog = Popen("cprog", stdout=PIPE)
    
    fmt = "@ii"
    str = cprog.stdout.read(calcsize(fmt))
    cprog.stdout.close()
    
    (i, j) = unpack(fmt, str)
    print i, j