How to return new C++ objects in Cython?

后端 未结 1 856
滥情空心
滥情空心 2021-02-09 05:10

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 ea

相关标签:
1条回答
  • 2021-02-09 05:55

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

    Make it this:

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

    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)
    
    0 讨论(0)
提交回复
热议问题