我正在使用带有 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/