首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在Cython中返回新的C++对象?

如何在Cython中返回新的C++对象?
EN

Stack Overflow用户
提问于 2013-06-02 20:09:03
回答 1查看 3.6K关注 0票数 7

我想这个问题有一个简单的答案,但我需要一些帮助来开始使用Cython。我有一个现有的C++代码库,我想通过Cython将其公开给Python。对于我想公开的每个类,我创建了一个Cython cppclass _ClassName和Python包装器类ClassName

一个最小的例子:

代码语言:javascript
复制
Object.h
CythonMinimal.pyx
setup.py

Object.h的内容

代码语言:javascript
复制
class Object {

public:

    Object clone() {
        Object o;
        return o; 
    }

};

CythonMinimal.pyx的内容

代码语言:javascript
复制
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的内容

代码语言:javascript
复制
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)

这是我在编译时得到的错误:

代码语言:javascript
复制
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类型)。但是怎么做呢?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-06-02 23:06:07

您正在python函数中返回一个C++对象,该函数仅允许返回python对象:

代码语言:javascript
复制
def clone(self):
    return self.thisptr.clone()

让它变成这样:

代码语言:javascript
复制
cdef _Object clone(self) except *:
    return self.thisptr.clone()

但这取决于你要做什么,你可能想要返回Object而不是_Object,所以我会这样修改它:

代码语言:javascript
复制
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)
票数 7
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/16882625

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档