繁体   English   中英

声明cpdef枚举时,此Cython编译错误的原因是什么?

[英]What is the reason for this Cython compile error when declaring a cpdef enum?

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

在交互式shell中,我运行了:

>>> from enum import Enum
>>> Enum
<enum 'Enum'>

显然,该模块似乎已经存在并且可以正常工作。

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

枚举可以用ctypedefcdef.声明cdef.
尝试像下面那样定义枚举:

cdef enum Test:
    A, B, C, D

要么

ctypedef enum Test:
    A, B, C, D

我认为您可能将分号放在标题的错误位置? 同样在标头中定义test ,但是在cython中,仅引用Test 根据实际的情况,下面的内容应该可以工作。 在运行Arch Linux的Raspberry Pi上进行了测试。

你好

typedef enum test { A, B, C, D } test;

你好

cpdef enum Test "test":意思是“在Python中使用Test并在C中进行test 。请参见解决命名冲突-C名称规范

cdef extern from "hello.h":
    cpdef enum Test "test":
        A, B, C, D

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

setup.py

from distutils.core import setup
from Cython.Build import cythonize

setup(name="hello", ext_modules=cythonize("hello.pyx"))

结果

Python 3.5.1 (default, Mar  6 2016, 12:32:57)
[GCC 5.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import hello
>>> hello.t1()
0
1
2
3

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM