【发布时间】:2018-09-10 17:23:18
【问题描述】:
我正在尝试运行一个 cython 示例,它比许多教程中的示例(例如 this guide )要复杂一些。
这是一个最小的示例(请不要介意它没有太多功能)和重现我的问题的步骤:
有 c++-classesRectangle 和 Group2(我把这里的所有东西都放到了 .h 文件中以使其更短):
// Rectangle.h
namespace shapes {
class Rectangle {
public:
Rectangle() {}
};
class Group2 {
public:
Group2(Rectangle rect0, Rectangle rect1) {}
};
}
然后我创建一个 grp2.pyx 文件(在与上述标题相同的文件夹中),其中包含 Rectangle 和 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)
扩展是通过我从命令行 (python setup.py build_ext -i) 调用的setup.py 文件构建的:
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
)))
此时我在PyGroup2 的_cinint_ 中有错误:
无法将 Python 对象参数转换为类型“矩形”
我想我的 pyx 文件中有一些错误,但我不知道是什么。
【问题讨论】: