0

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.

1 Answer 1

1

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for reply. Ye, I edited my answer cause I forgot to use self word, but it was not my problem. My problem is that i can't get access to pointer from python method.
...which is what the cdef SomeClass o = ... should do. It then knows that o.cpp_impl is a CPPClass pointer, and so should work

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.