[英]How can I get the `const` modifier of a CFFI function in Python?
I am writing a Python wrapper for generated C code, which contains functions that use pointer arguments as outputs. 我正在为生成的C代码编写Python包装程序,其中包含使用指针参数作为输出的函数。 I want to introspect those functions, generated using CFFI, to determine if they are input our output arguments.
我想反省使用CFFI生成的那些函数,以确定它们是否输入了我们的输出参数。 The input arguments are marked
const
, but it seems CFFI discards this information. 输入参数标记为
const
,但CFFI似乎丢弃了此信息。
Consider the following example 考虑以下示例
from cffi import FFI
ffibuilder = FFI()
ffibuilder.set_source("_example",
r"""
int add(const int a, const int b) {
return a+b;
}
""")
ffibuilder.cdef("""
int add(const int a, const int b);
""")
if __name__ == "__main__":
ffibuilder.compile(verbose=True)
import _example
ff = _example.lib.add
ff_t = _example.ffi.typeof(ff)
print(ff_t)
Which prints <ctype 'int(*)(int, int)'>
, while the inputs are defined const
in the code. 当输入在代码中定义为
const
,它会打印<ctype 'int(*)(int, int)'>
。
Sorry, not possible. 抱歉,不可能。 This info is discarded already when translating the parsed code to CFFI's internal ctype.
将解析的代码转换为CFFI的内部ctype时,此信息已被丢弃。
Look at the pycparser
module (which is the one that CFFI uses internally); 查看
pycparser
模块(这是CFFI内部使用的模块); it transforms C source code into a parse tree. 它将C源代码转换为解析树。 From there you can get all this info again more directly.
从那里,您可以再次直接获得所有这些信息。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.