【发布时间】:2012-11-07 06:24:55
【问题描述】:
我正在尝试将 cpp 库包装到 cython 中。以下是一些细节:
Handle.h:
class Handle {
public:
// accessors
// mutators
};
class Store {
public:
Handle* lookup(char* handleName);
int update(Handle*);
};
handle.pyx:
cdef extern from "Handle.h" namespace "xxx":
cdef cppclass Handle:
....
cdef extern from "Handle.h" namespace "xxx":
cdef cppclass Store:
Handle* lookup(char*)
int update(Handle*)
cdef class PyHandle:
cdef Handle* handle
....
cdef class PyStore:
cdef Store* store
def __cinit__(self):
store = ....
def lookup(self, name):
handle = self.store.lookup(name)
pHandle = PyHandle()
pHandle.handle = handle
return pHandle
def update(self, h):
self.store.update(h.handle)
最后一条语句给了我一个错误说Cannot convert Python object to 'Handle *'。我知道我错过了一些简单的东西。如何将嵌入在 Python 对象中的 Handle* 传递给调用?
【问题讨论】:
-
传递给 update(self, h) 的“h”是一个 Python 对象,而 store.update() 将 Handle* 作为参数。这就是cython所说的。您应该手动将 python 对象转换为 Handle* 或者 make 是 cdef 并输入 h 参数或者 make store.update() 将 python 对象作为参数。
-
我们如何使 python 对象成为 Handle*?谢谢。