How to declare enum in Cython pxd file for interface with external C library?

Viewed 161

I'm trying to use Cython to call external C library from Python. The code looks like this:

header.h

typedef enum {
    A = 0,
    B = 1,
    C = 2,
    D = 3
} my_enum_type;

cheader.pxd

cdef extern from "header.h":
    ctypedef enum my_enum_type:
        A = 1,
        B = 2,
        C = 3,
        D = 4

code.pyx

cimport cheader

cdef cheader.my_enum_type test = A

if test == A:
    print("OK")
else:
    print("NOK")

With code above I get compiler error: undeclared name not builtin: A

With code bellow there are no compilation errors.

cimport cheader

cdef cheader.my_enum_type test = cheader.my_enum_type.A

if test == cheader.my_enum_type.A:
    print("OK")
else:
    print("NOK")

If enum is defined in pyx file than code bellow can be used:

ctypedef enum my_enum_type:
    A = 1,
    B = 2,
    C = 3,
    D = 4

cdef my_enum_type test = A

if test == A:
    print("OK")
else:
    print("NOK")

Is it possible to declare enum in pxd file so it's not necessary to reference imported module every time enum has to be used?

What is the proper way to declare enum in pxd file and use it in pyx files?

0 Answers
Related