本文介绍了如何在Cython中返回新的C ++对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我怀疑有一个简单的答案,但我需要一些帮助开始使用Cython。我有一个现有的C ++代码库,我想通过Cython暴露给Python。对于我想展示的每个类,我创建一个Cython cppclass _ClassName 和Python包装类 ClassName

I suspect there is an easy answer to this, but I need some help getting started with Cython. I have an existing C++ code base which I want to expose to Python via Cython. For each class I want to expose, I create a Cython cppclass _ClassName and the Python wrapper class ClassName.

最小范例:

Object.h
CythonMinimal.pyx
setup.py

内容 Object.h

class Object {

public:

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

};

内容 CythonMinimal.pyx



content of 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()

内容 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)

这是我得到的错误编译:

This is the error I get when compiling:

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

code> _Object.clone 需要返回 _Object (cppclass类型),但 Objet.clone 应该返回一个 Object (Python类型)。但是如何?

I assume that _Object.clone needs to return a _Object (cppclass type), but Objet.clone should return a Object (Python type). But how?

推荐答案

你在一个python函数中返回一个C ++对象,只允许返回python对象:

You are returning a C++ object in a python function that is allowed to return python objects only:

def clone(self):
    return self.thisptr.clone()

设为:

cdef _Object clone(self) except *:
    return self.thisptr.clone()

你可能想要返回Object而不是_Object,所以我会这样修改:

But it depends on what you're trying to do, you probably want to return Object and not _Object, so I would modify it this way:

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)

这篇关于如何在Cython中返回新的C ++对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-29 16:28