c++ - 文档示例中的 Cython 崩溃

标签 c++ python crash cython

我使用 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/

相关文章:

c++ - 模板 + Typedef C++

C++ 优雅的方式来标记不属于 vector 的索引

python - 创建一个包含 2 个 CSV 的 Zip

c++ - C++ 中的字符串类 e char

c++ - SDL2 尝试裁剪纹理或表面

python - 使用pandas从python中现有的csv文件生成指定数量的新csv文件数据帧的方法

python - 我正在尝试在 ubuntu 上的 sublime text 中的 python 上使用 tkinter,但我不断收到错误

android - 读取文本文件并将其存储在数组中(Android 开发)

assembly - MASM32循环

ios - 构建成功后将文本文件安装/导入到物理设备时 Xcode 崩溃 (RangeException)