简体   繁体   English

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

[英]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.

相关问题 如何使用cffi嵌入在C语言中返回字符串的Python函数? - How can I embed a Python function that returns a string in C using cffi? 使用 CFFI 在 Fortran 中使用 Python 函数。 cffi 构建中的警告。 我无法通过控制台或文件中的打印获得结果,但在运行时不会出错 - Use CFFI for using Python function inside Fortran. Warnings in cffi build. I can't get results from print in console or file, but not error in running 如何使用Python的CFFI将指针传递给C函数? - How do I pass a pointer to a C function with Python's CFFI? 如何从Python的CFFI访问errno? - How can I access errno from Python's CFFI? 如何在开发期间构建Python CFFI模块? - How can I build Python CFFI modules during development? 我们如何在 Java(JNA) 中获取由 Python function 使用 CFFI 返回的字符串 - How do we get String in Java(JNA), that returned by Python function using CFFI 我如何指定 python cffi,将我的导入模块添加到生成的 DLL 中? - How can i specify to python cffi , to add my import modules into the generated DLL? cffi:如何将字符串字符的地址发送给C函数? - cffi: How do i send the address of a character of a string to a C function? 如何修复 CFFI 创建的共享库中的“未定义符号”? - How can I fix 'undefined symbol' in a shared library created by CFFI? 如何在python cffi中实例化一个结构? - How to instantiate a struct in python cffi?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM