我使用 Cython 0.19.2(和 Python 2.7.1)将 C++ 类公开给 Python。
作为第一次尝试,我使用文档的“Rectangle”类示例进行了测试。
http://docs.cython.org/src/userguide/wrapping_CPlusPlus.html
我有一个我不明白的崩溃。
我已经尽量简化了代码。但我仍然有问题。
这是我的 pyx 文件,C++ 源代码只是从 python/cython 的文档中剪切和粘贴。
# distutils: language = c++
# distutils: sources = Rectangle.cpp
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
pass
cdef class PyRectangle:
cdef Rectangle* thisptr
我只想用 thisptr 声明一个类,它指向 C++ Rectangle 类实例。
当我尝试编译程序时:
cython -a --cplus rect.pyx
我有以下崩溃:
Error compiling Cython file:
------------------------------------------------------------
...
cdef extern from "Rectangle.h" namespace "shapes":
cdef cppclass Rectangle:
pass
cdef class PyRectangle:
cdef Rectangle* thisptr
^
------------------------------------------------------------
rect.pyx:9:7: Compiler crash in AnalyseDeclarationsTransform
File 'ModuleNode.py', line 101, in analyse_declarations: ModuleNode(rect.pyx:1:0,
full_module_name = 'rect')
File 'Nodes.py', line 382, in analyse_declarations: StatListNode(rect.pyx:4:0)
File 'Nodes.py', line 4251, in analyse_declarations: CClassDefNode(rect.pyx:8:5,
as_name = u'PyRectangle',
class_name = u'PyRectangle',
module_name = u'',
visibility = u'private')
File 'Nodes.py', line 382, in analyse_declarations: StatListNode(rect.pyx:9:7)
File 'Nodes.py', line 1208, in analyse_declarations: CVarDefNode(rect.pyx:9:7,
modifiers = [...]/0,
visibility = u'private')
Compiler crash traceback from this point on:
File "/home/xxx/local/python2.7.1/site-packages/Cython/Compiler/Nodes.py", line 1208, in analyse_declarations
self.entry.doc = embed_position(self.pos, self.doc)
AttributeError: 'CVarDefNode' object has no attribute 'doc'
我曾尝试使用 pyrex、setup.py 等进行编译。但是我仍然有同样的错误。
有什么我想念的吗?
谢谢
最佳答案
好吧,我终于修好了。我的 Python 2.7.1 版本根本无法与最新的 Cython 0.19.2 一起使用。我升级到 2.7.6 并且可以正常工作。
关于c++ - 文档示例中的 Cython 崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20425936/