1

im using cython to import c enums from .h file to my python code (the goal is to have one place that will include the enums for both of my programs). so i have my enums.h file:

typedef enum Numbers{
    ONE=1, TWO, THREE
} Numbers;

and i have my enums.pyx

cdef extern from "enums.h":
    cpdef enum Numbers:
        ONE,
        TWO,
        THREE

and this code works in the sense that it imports the enum to be accessible from the python code with the right values. i want to take it a step further and want to automatically identify the members of the c enums, so the pyx file would update automaticly, when i do:

cdef extern from "enums.h":
    cpdef enum Numbers:
        pass

i get a Numbers enum in the python but without members (so even if I do Numbers(1) ill get an error).

is there a way to identify the c enum members automatically?

Finci
  • 108
  • 1
  • 10
  • 1
    If with plenty of time, could try [swig](http://www.swig.org/tutorial.html). Cython is a powerful but low level tool, while swig can do something automatically. – heLomaN Sep 27 '20 at 09:53
  • [This answer](https://stackoverflow.com/a/66037988/208880) shows how to automatically create a Python `Enum` from a `.h` file. – Ethan Furman Sep 20 '21 at 19:50

1 Answers1

1

Cython does not have any ability to scan C files and generate declarations so the direct answer to your question is "no" and is very likely to remain "no".

Other tools do have greater introspection ability. CFFI for example. I'm sure there are others.

You could obviously write a short script using one of these tools to try to generate (parts of) your automatically Cython files, but it's definitely outside the scope of Cython on its own.

DavidW
  • 29,336
  • 6
  • 55
  • 86