简体   繁体   English

如何获得 C object 并将其传递给 cython 中的 function 参数

[英]How to get a C object and pass it in argument of a function in cython

I am trying to use a C library "myClib" from python.我正在尝试使用 python 中的 C 库“myClib”。

This library has a function "myCfunction" that return a pointer struct "myCStruct" and then had other functions that take a pointer of this struct in argument.这个库有一个 function “myCfunction”,它返回一个指针结构“myCStruct”,然后有其他函数在参数中采用这个结构的指针。

I don't have the definition of this struct I only get this in the.h file:我没有这个结构的定义,我只在 .h 文件中得到这个:

typedef struct myCStruct myCStruct

The library is provided with as a static library with aa file.该库作为带有 a 文件的 static 库提供。 Some.so files are also provided they contains some dependencies used by the library.还提供了一些.so 文件,它们包含库使用的一些依赖项。 The.h files are also provided but the full definition of "myCStruct" seems not provided in any of the.h.还提供了 .h 文件,但似乎没有在任何 .h 文件中提供“myCStruct”的完整定义。

I don't have to read or process any information that would be contained in this object but I need to pass it in argument of an other C functions of the library.我不必阅读或处理此 object 中包含的任何信息,但我需要将其传递到库的其他 C 函数的参数中。

Right now to execute the C code I am using cython to generate a.so file that I can import in my python code.现在要执行 C 代码,我正在使用 cython 生成一个 .so 文件,我可以将其导入到我的 python 代码中。

The definition of the function in my pyx file looks like this:我的 pyx 文件中 function 的定义如下所示:

cdef extern from "myClib.h":
  void myCfunction()

def py_myCfunction():
  return myCfunction()

I didn't declare the return type of py_myCfunction as I didn't define any class matching the myCStruct since I don't have any idea about the members.我没有声明 py_myCfunction 的返回类型,因为我没有定义任何与 myCStruct 匹配的 class,因为我对成员一无所知。

My problem is that when I call py_myCfunction in my python program I am getting an object of type NoneType.我的问题是,当我在我的 python 程序中调用 py_myCfunction 时,我得到一个 NoneType 类型的 object。

So 2 questions:所以2个问题:

  • Could I get a N.netype object because I miss the declaration in the pyx?我可以得到 N.netype object 因为我错过了 pyx 中的声明吗? Or does it necessary means that myCfunction return nothing (it's supposed to return something).或者它是否有必要意味着 myCfunction 什么都不返回(它应该返回一些东西)。

  • If the problem is there because I miss the declaration, which return type could my py_myCfunction has since I don't know the member of the C struct equivalent?如果问题是因为我错过了声明,我的 py_myCfunction 可能有哪种返回类型,因为我不知道 C 结构等效的成员?

  • You need to declare the C API so cython knows what it is and how to call it您需要声明 C API 以便 cython 知道它是什么以及如何调用它

  • You should wrap the C struct * in a Python class, and add methods to the class that call the other C API functions that reference the struct您应该将 C struct * 包装在 Python class 中,并向 class 添加方法以调用其他 C API 引用该结构的函数

Here's example cython code based on your question:这是基于您的问题的示例 cython 代码:


# declare external stuff for cython

cdef extern from "myClib.h":

    struct myCStruct:
        pass

    struct myCStruct *myCFunction();
    int otherFunction(struct myCStruct *cstruct, int parameter);

# define python glue for python

cdef class MyPythonClass:
    cdef myCStruct *cstruct

    def __cinit__(self):
        self.cstruct = myCFunction()
    
    # plus methods that call other functions in myClib using the self.cstruct
    def other(self, parameter):
        return otherFunction(self.cstruct, parameter)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM