How can I reference #defines in a C file from python?

前端 未结 5 1690
星月不相逢
星月不相逢 2020-12-30 10:45

I have a C file that has a bunch of #defines for bits that I\'d like to reference from python. There\'s enough of them that I\'d rather not copy them into my python code, i

5条回答
  •  有刺的猬
    2020-12-30 11:19

    You might have some luck with the h2py.py script found in the Tools/scripts directory of the Python source tarball. While it can't handle complex preprocessor macros, it might be sufficient for your needs.

    Here is a description of the functionality from the top of the script:

    Read #define's and translate to Python code. Handle #include statements. Handle #define macros with one argument. Anything that isn't recognized or doesn't translate into valid Python is ignored.

    Without filename arguments, acts as a filter. If one or more filenames are given, output is written to corresponding filenames in the local directory, translated to all uppercase, with the extension replaced by ".py".

    By passing one or more options of the form "-i regular_expression" you can specify additional strings to be ignored. This is useful e.g. to ignore casts to u_long: simply specify "-i '(u_long)'".

提交回复
热议问题