Is it possible to reliably convert C preprocessor macros to python code?

倖福魔咒の 提交于 2019-12-08 02:32:25

问题


I have a bunch of C macros the operation of which I need to simulate in python. I saw some pointers to pygccxml or ctypeslib etc. Are these the ways to go? Or is there something out there that is better ?

The C macros if and when they change, I would like the python implementation to be auto generated rather than having to make manual modifications. Hence the question.

my_c_header.h:

#ifdef OS

#define NUM_FLAGS   (uint16_t)(3)
#define NUM_BITS    (uint16_t)(8)

#else

#define NUM_FLAGS   (uint16_t)(6)
#define NUM_BITS    (uint16_t)(16)
#endif

#define MAKE_SUB_FLAGS    (uint16_t)((1<<NUMFLAGS) -1)

#define MAKE_TOTAL_FLAGS(x)  (uint16_t)((x & MAKE_SUB_FLAGS) >> NUM_BITS)

/* #defines type 2 */

#ifdef OS
#DO_SOMETHING(X)  os_specifc_process(x)
#else
#DO_SOMETHING(x)  
#endif

/* #defines type 3 */

enum
{
CASE0,
CASE1,
CASE2
}

#define MY_CASE_0     ((uint16_t)CASE0)
#define MY_CASE_1     ((uint16_t)CASE1)
#define MY_CASE_2     ((uint16_t)CASE2)


/*End of file <my_c_header.h> */

回答1:


If you are writing an extension module, use https://docs.python.org/3/c-api/module.html#c.PyModule_AddIntMacro



来源:https://stackoverflow.com/questions/2654329/is-it-possible-to-reliably-convert-c-preprocessor-macros-to-python-code

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!