3

I have a simple C function.

typedef struct {
    long unsigned int First;
    long unsigned int Second;
    int c;
} FRAGMENTS;

struct out {
    long unsigned int Four;
    FRAGMENTS fragments[10000];
};
struct out test() {
    struct out *out = (struct out *)malloc(sizeof(struct out));
    ...
    return *out
}

How to use this function in Python ? Any example for transform this structure to python object ( using python wrapper ) ?

1
  • 1
    Why are you returning an int from the C function? The right return type here is an out*. Commented Aug 16, 2011 at 22:49

2 Answers 2

1

Do you have the data already "in" Python (i.e. from the network or a binary file)? Than you an use the struct module.

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

Comments

1

The easiest way is to use SWIG to generate a Python wrapper around your C code. You can also use it to generate bindings/wrappers for lots of other scripting languages to.

1 Comment

For calling the function, and translating input and output to/from Python objects, use SWIG. Using the memory structure directly in Python is not supported because there is no way it could be portable. (Different CPU architectures and compilers may cause sizeof(FRAGMENTS) and the arrangement of its fields to vary.)

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.