Defining enums in Cython code that will be used in the C part of code

风流意气都作罢 提交于 2019-12-12 17:50:19

问题


I have defined and enum in cython header file api.pxd:

ctypedef enum InstructionType:
    default = 0
    end_if = 1
    end_loop = 2
    backward_jump_here = 4

I also have checked if turning ctypedef to cdef would work (and it didn't).

And I want to use value from this enum in __cinit__ method fo some class:

from api cimport Instruction, CLinVM, InstructionType

# (...) some other classes

cdef class EndIf(Noop):
   def __cinit__(self):
      self.type = InstructionType.end_if

And I get compilation error:

    self.type = InstructionType.end_if
                              ^
------------------------------------------------------------
 /home/(...)/instructions.pyx:149:35: 'InstructionType' is not a constant, 

Any way to define and use enum in such way?


回答1:


You do not access enumerated constants through their typename they belong to, neither in C, nor in C++, nor in Cython. You'd need to create a wrapper .pxd for it.



来源:https://stackoverflow.com/questions/9341511/defining-enums-in-cython-code-that-will-be-used-in-the-c-part-of-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!