How can a #defined C value be exposed to Python in a Cython module?

前端 未结 2 1023
小鲜肉
小鲜肉 2020-12-24 12:28

I\'d like to make the integer constants (ACTIVE_TAG, etc) defined here:

//island management, m_activationState1
#define ACTIVE_TAG 1
#define ISLAND_SLEEPING          


        
2条回答
  •  春和景丽
    2020-12-24 13:05

    Here's one way, which, while seemingly tedious, could feasibly be automated for any given .h file as input:

    Step 1. Get all the constants you want into a file, say bulletdefs.h, which has the #defines but with leading underscores, e.g:

    #define _ACTIVE_TAG 1
    #define _ISLAND_SLEEPING 2
    #define _WANTS_DEACTIVATION 3
    #define _DISABLE_DEACTIVATION 4
    #define _DISABLE_SIMULATION 5
    

    Step 2. Insert a section into the pyx file for your module, say bullet.pyx:

    cdef extern from "bulletdefs.h":
        cdef int _ACTIVE_TAG
        cdef int _ISLAND_SLEEPING
        cdef int _WANTS_DEACTIVATION
        cdef int _DISABLE_DEACTIVATION
        cdef int _DISABLE_SIMULATION
    
    ACTIVE_TAG = _ACTIVE_TAG
    ISLAND_SLEEPING = _ISLAND_SLEEPING
    WANTS_DEACTIVATION = _WANTS_DEACTIVATION
    DISABLE_DEACTIVATION = _DISABLE_DEACTIVATION
    DISABLE_SIMULATION = _DISABLE_SIMULATION
    

    Then, when you compile your module, you should get the intended effect:

    Python 2.7.1+ (r271:86832, Apr 11 2011, 18:05:24) 
    [GCC 4.5.2] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import bullet
    >>> bullet.ACTIVE_TAG
    1
    >>> bullet.DISABLE_SIMULATION
    5
    >>>
    

提交回复
热议问题