Currently I have several Cython extension types which wrap different c++ classes by creating an attribute that has an instance of that class, like in the wrapping C++ Cython guide.
However, I'm running into problems when trying to use that attribute in other parts of my code. Despite being defined with cdef and a specific C++ class, Cython still considers the attribute a python object. While it's possible to copy the data from the attribute and create a new C++ class to pass in, that involves a lot of overhead and is not very good for what I'm working on.
A minimum reproducible example:
test.h
namespace test {
class IntClass
{
public:
IntClass() {};
IntClass(int attribute) : attribute(attribute) {};
int getAttribute() {return this->attribute;}
private:
int attribute;
};
}
test.pyx
cdef extern from "test.h" namespace "test":
cdef cppclass IntClass:
IntClass()
IntClass(int attribute)
int getAttribute()
cdef class PyIntClass:
cdef IntClass intclass
def __cinit__(self):
intclass = IntClass(9)
And the error I'm getting:
Error compiling Cython file:
------------------------------------------------------------
...
cdef class PyIntClass:
cdef IntClass intclass
def __cinit__(self):
intclass = IntClass(9) ^
------------------------------------------------------------
test.pyx:11:27: Cannot convert 'IntClass' to Python object
Is there a way to tell Cython to consider that attribute as a C++ object or otherwise avoid having to copy all the data?
Read more here: https://stackoverflow.com/questions/66342348/is-there-a-way-to-define-cython-extension-type-class-attributes-as-c-classes
Content Attribution
This content was originally published by JamesxGames at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.