Wrap C struct with array member for access in python: SWIG? cython? ctypes?

后端 未结 5 1111
囚心锁ツ
囚心锁ツ 2021-02-03 10:19

I want to access a C function that returns a struct containing double arrays (where the lengths of these arrays is given by other int members of the struct) from python. The dec

5条回答
  •  一生所求
    2021-02-03 10:55

    Check out SWIG's typemaps. They let you write your own code for handling specific types, specific instances (type+name) or even groups of arguments. I haven't done it for structures, but to specially handle a case where the C function takes an array and its size:

    %typemap(in) (int argc, Descriptor* argv) {
        /* Check if is a list */
        if (PyList_Check($input)) {
            int size = PyList_Size($input);
            $1 = size;
            ...
            $2 = ...;
        }
    }
    

    That will take the pair of arguments int argc, Descriptor* argv (since the names are provided they have to match as well) and pass you the PyObject used and you write whatever C code you need to do the conversion. You could do a typemap for double *dn that would use the NumPy C API to do the conversion.

提交回复
热议问题