是否可以可靠地将 C 预处理器宏转换为 python 代码?
我有一堆 C 宏,我需要在 python 中模拟它们的操作。我看到一些指向 pygccxml 或 ctypeslib 等的指针。这些是要走的路吗?或者有更好的东西吗?
C 宏如果发生变化,我希望自动生成 python 实现,而不必进行手动修改。因此就有了这个问题。
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> */
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> */
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您正在编写扩展模块,请使用 https://docs .python.org/3/c-api/module.html#c.PyModule_AddIntMacro
If you are writing an extension module, use https://docs.python.org/3/c-api/module.html#c.PyModule_AddIntMacro