Megaxela
Megaxela

Reputation: 97

Access to c pointer in python class from python method, cython

In the process of implementing my python integration I faced a problem. I have class that looks like this:

cdef class SomeClass:
    cdef CPPClass* cpp_impl

    def some_method(self):
        self.cpp_impl.cppMethod()

And I have cpp class that can return CPPClass* value. Smth like this:

class Creator
{
public:
    CPPClass* createClass();

}

So I'd like to create SomeClass instance like this:

cdef class PyCreator:
    cdef Creator* cpp_impl

    def getSomeClass(self):
        o = SomeClass()
        o.cpp_impl = self.cpp_impl.createClass()
        return o

But I'm getting error that cython can't convert CPPClass* to Python object. How can I solve my problem? Thank you.

Upvotes: 0

Views: 207

Answers (1)

DavidW
DavidW

Reputation: 30936

In getSomeClass it needs to know what type o is so that the assignment to cpp_impl makes sense:

def getSomeClass(self):
    cdef SomeClass o = SomeClass() # define the type
    o.cpp_impl = self.cpp_impl.createClass() # I think you missed a "self" here
    return o

Upvotes: 1

Related Questions