I'm trying to run a cython example which is a bit more complex than the one which can be found in many tutorials (e.g. this guide ).
Here are a minimal example (please don't mind it doesn't have much functionality) and steps to reproduce my problem:
There are c++-classesRectangle and Group2 (I put here everything into the .h file to make it shorter):
// Rectangle.h
namespace shapes {
class Rectangle {
public:
Rectangle() {}
};
class Group2 {
public:
Group2(Rectangle rect0, Rectangle rect1) {}
};
}
Then I create a grp2.pyx file (in the same folder as the above header), with wrappers for Rectangle and Group2:
# RECTANGLE
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
Rectangle() except +
cdef class PyRectangle:
cdef Rectangle c_rect
def __cinit__(self):
self.c_rect = Rectangle()
# GROUP2
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Group2:
Group2(Rectangle rect0, Rectangle rect1) except +
cdef class PyGroup2:
cdef Group2 c_group2
def __cinit__(self, Rectangle rect0, Rectangle rect1):
self.c_group2 = Group2(rect0, rect1)
The extension is built via a setup.py file that I call from command line (python setup.py build_ext -i):
from distutils.core import setup, Extension
from Cython.Build import cythonize
setup(ext_modules = cythonize(Extension(
name="grp2", # the extension name
sources=["grp2.pyx"], # the Cython source
language="c++", # generate and compile C++ code
)))
At this point I have the error in the _cinint_ of PyGroup2:
Cannot convert Python object argument to type 'Rectangle'
I suppose there is some mistake in my pyx file, but I cannot tell what.