2

I have got a large c++ code which I am wrapping in cython to call from python. I am not sure how something from enum should be called in cython. This is what I have already attempted. But this is not complete. Help!

Demonstration example

class Foo{
public:
   Foo1 bar(enum_is type = enum::any) const;
}
enum class enum_is:unsigned int{
 any = 0,
 one = 1,
 two = 2,
}; 

abc.pxd

cdef extern from "headerfile.hpp":
    cdef cpp class enum_is:
        pass

cdef extern from "headerfile.hpp" namespace "enum_is":
    cdef enum_is any
    cdef enum_is one
    cdef enum_is two

abc.pyx

cdef class Pyenum_is:
    cdef enum_is thisobj
            def __cinit__(self,unsigned int val):
                self.pin_isthisobj = <pin_is> val
            def get_pin_is_type(self)
                cdef r={<unsigned int> any:"any",
                        <unsigned int> one:"one",
                        <unsigned int> two:"two"
                       }
                return r[<unsigned int>self.thisobj]

I need help with the function on how to actually use the enum in python assuming that I have the enum class correctly wrapped

cdef class PyFoo1
    cdef Foo1 Foo1thisobj
    cdef Foo1* Foo1thisptr

cdef class PyFoo
    cdef Foo Foothisobj
    cdef Foo* Foothisptr
    def bar(self,#pass enum_is object here):
        cdef Foo tempv = self.thisobj.bar(#pass enum here)
        abc = PyFoo()
        abc.Foo1thisobj = tempv
        return abc

Could someone help me regarding how to go ahead and use this enum in cython

Chinmoy Kulkarni
  • 187
  • 1
  • 13

1 Answers1

-1

You can declare a enum in Cython like

ctypedef enum options: OPT1, OPT2, OPT3

Then for an example:

def main():
    cdef options test
    test = OPT2
    f(test)

cdef void f(options inp):
    if inp == OPT1:
        print('OPT1')
    elif inp == OPT2:
        print('OPT2')
    elif inp == OPT3:
        print('OPT3')
EthanBar
  • 667
  • 1
  • 9
  • 24