简体   繁体   中英

import a full c enum using cython automatically

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?

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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