python - 声明 cpdef 枚举时出现此 Cython 编译错误的原因是什么?

标签 python python-3.x enums compiler-errors cython

我正在使用带有 Cython 0.24 和 GCC 4.9.1 的 Python 3.4.2 开发 Raspberry PI。
我想使用 cpdef enum这会创建一个 PEP 435样式 Python 枚举(自 Python 3.4 起可用)。此功能在 Cython 0.21 中引入.

我正在使用以下源代码:

#lib.h file
typedef enum { A, B, C, D } test;

#lib.pyx file
cdef extern from "lib.h":
    cpdef enum test:
        A, B, C, D

def t1():
    for t in test: print(t.value)

但是,几个编译错误说几次或多或少相同,例如:
- lib.c:4664:20: error: invalid application of 'sizeof' to incomplete type 'enum test' - lib.c:2599:45: error: type of formal parameter 1 is incomplete __pyx_t_4 = __Pyx_PyInt_From_enum__test(C); if (unlikely(!__pyx_t_4)) __PYX_ERR(1, 56, __pyx_L1_error)
在交互式外壳中,我运行:
>>> from enum import Enum
>>> Enum
<enum 'Enum'>

显然,该模块似乎存在并且正在工作。

我的问题是:这些错误的原因可能是什么?

最佳答案

可以使用 ctypedef 声明枚举。或 cdef.尝试定义您的枚举,例如:

cdef enum Test:
    A, B, C, D

或者
ctypedef enum Test:
    A, B, C, D

关于python - 声明 cpdef 枚举时出现此 Cython 编译错误的原因是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38049841/

相关文章:

python - 处理python脚本的绝对/相对路径时的异常处理

python - 如何在 Python 中检查回文?

python-3.x - PyQt,QThread : GUI freeze with large amount of SIGNAL from another thread (QListWidget massive update)

python - 名称错误 : name 'add' is not defined

c++ - C++ 中的命名枚举

enums - Rust-将特征包装在枚举中以用于内存布局和更简单的泛型?

python - 当键对应于列表时仅对字典中的第一个值求和 (Python)

python - 使用 scrapy 的项目加载器填充数据可以在 shell 中工作,但不能在 Spider 中工作

java - 如何将枚举序列化为对象形状和默认字符串?

python - 如何在企业网络内部署Python应用程序