如何将使用def
定义的Cython方法中的Python对象参数转换为C ++类型?我正在尝试为C ++库提供一个Cython包装类,如Cython文档的Using C++ in Cython部分所述。
这是一个演示我的问题的例子。
档案foo.h
:
namespace ns {
class Foo:
public:
Foo();
dosomething(std::shared_ptr<Bar>);
}
档案bar.h
:
namespace ns {
class Bar:
public:
Bar();
}
档案foo.pyx
:
from libcpp.memory cimport shared_ptr
cdef extern from 'bar.h' namespace 'ns':
cdef cppclass Bar:
Bar() except +
cdef extern from 'foo.h' namespace 'ns':
cdef cppclass Foo:
Foo() except +
void dosomething(shared_ptr[Bar])
cdef class PyBar:
cdef Bar* thisptr
def __cinit__(self):
self.thisptr = new Bar()
def __dealloc__(self):
del self.thisptr
cdef class PyFoo:
cdef Foo* thisptr
def __cinit__(self):
self.thisptr = new Foo()
def __dealloc__(self):
del self.thisptr
def dosomething(self, bar):
self.thisptr.dosomething(bar)
档案setup.py
:
from setuptools import Extension
from setuptools import setup
from Cython.Build import cythonize
extensions = [
Extension(
'foo',
sources=[
'foo.pyx',
'foo.cpp',
'bar.cpp',
],
language='c++',
include_dirs=['.'],
extra_compile_args=['-std=c++11'],
),
]
setup(
ext_modules=cythonize(extensions),
)
尝试编译时发生的错误:
$ python setup.py build_ext
Compiling foo.pyx because it changed.
[1/1] Cythonizing foo.pyx
Error compiling Cython file:
------------------------------------------------------------
...
def __dealloc__(self):
del self.thisptr
def dosomething(self, bar):
self.thisptr.dosomething(bar)
^
------------------------------------------------------------
foo.pyx:38:33: Cannot convert Python object to 'shared_ptr[Bar]'
答案 0 :(得分:0)
恐怕我不得不说,foo and bar
实际上是一个愚蠢的例子,不知道C ++代码想要完成什么,没有逻辑和没有实现。但是我也根据你的例子制作了这样一个愚蠢的例子,并使之成功。它很棘手,有点儿,希望能帮到你一点点。
我使用jupyter笔记本来简化编译:
%%file foo.h
#include <memory>
namespace ns {
class Bar{
public:
Bar(){};
};
class Foo{
public:
Foo(): bar_addr(0) {}
void dosomething(std::shared_ptr<Bar> sp){bar_addr = (size_t)sp.get();}
size_t get_bar_addr(){return bar_addr;}
private:
size_t bar_addr;
};
}
Cython的部分:
%%cython -f -+ -a
# distutils: include_dirs = .
from libcpp.memory cimport shared_ptr
cdef extern from 'foo.h' namespace 'ns':
cdef cppclass Foo:
Foo() except +
void dosomething(shared_ptr[Bar])
size_t get_bar_addr()
cdef cppclass Bar:
Bar() except +
cdef class PyBar:
cdef Bar* thisptr
def __cinit__(self):
self.thisptr = new Bar()
def __dealloc__(self):
del self.thisptr
def addr(self):
return <size_t><void*>self.thisptr
cdef class PyFoo:
cdef Foo* thisptr
# There is a bug here, see the comments
cdef PyBar owned_bar # keep this bar to prevent the underlying pointer losing.
cdef shared_ptr[Bar] owned_bar_sp # keep this bar shared_ptr to prevent the underlying pointer from deleting.
def __cinit__(self):
self.thisptr = new Foo()
def __dealloc__(self):
del self.thisptr
def dosomething(self, PyBar bar):
self.owned_bar = bar
print("Address passed in: ", self.owned_bar.addr())
self.owned_bar_sp = shared_ptr[Bar](<Bar*><size_t>self.owned_bar.addr())
self.thisptr.dosomething(self.owned_bar_sp)
print("Address got : ", self.thisptr.get_bar_addr())
试验:
bar = PyBar()
foo = PyFoo()
foo.dosomething(bar)
关于您的问题&#34;如何在Cython中将Python对象转换为C ++类型 &#34;,我认为答案是&#34; Can&#34;或&#34;不能&#34;或&#34;可能&#34;,取决于上下文。在这个特定的例子中,&#34;不能&#34;。你必须在Cython中做一些构造或转换,它可以作为Python和C / C ++之间的坚实桥梁