如何在Python中获取CFFI函数的`const`修饰符?

时间:2018-07-03 19:40:41

标签: python python-cffi

我正在为生成的C代码编写Python包装程序,其中包含使用指针参数作为输出的函数。我想反省使用CFFI生成的那些函数,以确定它们是否输入了我们的输出参数。输入参数标记为const,但CFFI似乎会丢弃此信息。

考虑以下示例

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)

在代码中定义<ctype 'int(*)(int, int)'>的同时输入const

1 个答案:

答案 0 :(得分:0)

抱歉,不可能。将解析的代码转换为CFFI的内部ctype时,此信息已被丢弃。

查看pycparser模块(这是CFFI内部使用的模块);它将C源代码转换为解析树。从那里,您可以更直接地再次获得所有这些信息。