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

匿名 (未验证) 提交于 2019-12-03 09:18:39

问题:

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 http://docs.python.org/3/c-api/module.html#PyModule_AddIntMacro



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