如何在Cython中返回新的c++对象
How to return new C++ objects in Cython?
我怀疑有一个简单的答案,但是我需要一些帮助来开始使用Cython。我有一个现有的c++代码库,我想通过Cython暴露给Python。对于我想公开的每个类,我创建了一个Python cpp类_ClassName
和Python包装器类ClassName
。
一个最小的例子:
Object.h
CythonMinimal.pyx
setup.py
Object.h
含量:
class Object {
public:
Object clone() {
Object o;
return o;
}
};
CythonMinimal.pyx
含量:
cdef extern from "Object.h":
cdef cppclass _Object "Object":
_Object() except +
_Object clone()
cdef class Object:
cdef _Object *thisptr
def __cinit__(self):
self.thisptr = new _Object()
def __dealloc__(self):
del self.thisptr
def clone(self):
return self.thisptr.clone()
setup.py
的内容
from distutils.core import setup
from distutils.extension import Extension
from Cython.Build import cythonize
from Cython.Distutils import build_ext
import os
os.environ["CC"] = "g++-4.7"
os.environ["CXX"] = "g++-4.7"
modules = [Extension("CythonMinimal",
["CythonMinimal.pyx"],
language = "c++",
extra_compile_args=["-std=c++11"],
extra_link_args=["-std=c++11"])]
for e in modules:
e.cython_directives = {"embedsignature" : True}
setup(name="CythonMinimal",
cmdclass={"build_ext": build_ext},
ext_modules=modules)
这是我在编译时得到的错误:
cls ~/workspace/CythonMinimal $ python3 setup.py build
running build
running build_ext
cythoning CythonMinimal.pyx to CythonMinimal.cpp
Error compiling Cython file:
------------------------------------------------------------
...
def __dealloc__(self):
del self.thisptr
def clone(self):
return self.thisptr.clone()
^
------------------------------------------------------------
CythonMinimal.pyx:18:27: Cannot convert '_Object' to Python object
building 'CythonMinimal' extension
creating build
creating build/temp.macosx-10.8-x86_64-3.3
g++-4.7 -Wno-unused-result -fno-common -dynamic -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -I/usr/local/include -I/usr/local/opt/sqlite/include -I/usr/local/Cellar/python3/3.3.0/Frameworks/Python.framework/Versions/3.3/include/python3.3m -c CythonMinimal.cpp -o build/temp.macosx-10.8-x86_64-3.3/CythonMinimal.o -std=c++11
cc1plus: warning: command line option '-Wstrict-prototypes' is valid for C/ObjC but not for C++ [enabled by default]
CythonMinimal.cpp:1:2: error: #error Do not use this file, it is the result of a failed Cython compilation.
error: command 'g++-4.7' failed with exit status 1
我假设_Object.clone
需要返回_Object
(cppclass类型),但Objet.clone
应该返回Object
(Python类型)。但如何?
你在一个python函数中返回一个c++对象,该函数只允许返回python对象:
def clone(self):
return self.thisptr.clone()
这样写:
cdef _Object clone(self) except *:
return self.thisptr.clone()
但这取决于你想要做什么,你可能想要返回Object而不是_Object,所以我会这样修改它:
cdef class Object:
cdef _Object thisobj
cdef _Object *thisptr
def __cinit__(self, Object obj=None):
if obj:
self.thisobj = obj.thisobj.clone()
self.thisptr = &self.thisobj
def __dealloc__(self):
pass
def clone(self):
return Object(self)
相关文章:
- 什么时候调用组成单元对象的析构函数
- 对RValue对象调用的LValue ref限定成员函数
- CMake-按正确顺序将项目与C运行时对象文件链接
- 空基优化子对象的地址
- 将对象数组的引用传递给函数
- 你能重载对象变量名本身返回的内容吗
- C++使用整数的压缩数组初始化对象
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 将对象移动到std::shared_ptr
- 代理对象的常量正确性
- 提升 ASIO 无法识别计时器对象
- 将Ref对象作为类成员
- 将包含C样式数组的对象初始化为成员变量(C++)
- 如何返回一个类的两个对象相加的结果
- 使用std::函数映射对象方法
- 是否需要删除包含对象的"pair"?
- 如何在自删除后将对象设置为nullptr
- 迭代时从向量和内存中删除对象
- 构造对象的歧义
- 使用"std::unordereded_map"映射到"std::list"对象