简体   繁体   English

如何用 python-cffi 包装 C 函数以便 pythons 关键字参数起作用?

[英]How to wrap a C-function with python-cffi so that pythons keyword-arguments work?

Using the API mode, calling C sources instead of a compiled library in the python-cffi framework I want to call my c-function in python with keyword arguments.使用API 模式,调用 C 源代码而不是 python-cffi 框架中的已编译库我想在 Z23EEEB4347BDD26BFC6B7EE9A3B755DDBD89EDEFB7777777BDD26BFC6B7EE9A3B755BDD829EDFZ777777B755DDBDABD89 中调用我的 c 函数。 Is there an inbuilt feature in cffi for this? cffi 中是否有为此的内置功能? Otherwise I would have to write a python wrapper around my cffi-wrapped c-functions which I don't want to do since it seems like an ugly solution.否则,我将不得不围绕我的 cffi 包装的 c 函数编写一个 python 包装器,我不想这样做,因为它看起来像一个丑陋的解决方案。

( run both files with python, should work out-of-the-box if cffi and gcc are present: "python example_extension_build.py && python test_example.py" ) 使用 python 运行这两个文件,如果 cffi 和 gcc 存在,则应该开箱即用:“python example_extension_build.py && python test_example.py”

Note: That in this Example code I use the API level, out-of-line mode instead (for clearnes)注意:在此示例代码中,我使用API 级别,离线模式代替(用于清除)

# file: example_extension_build.py
from cffi import FFI
ffibuilder = FFI()

# objects shared between c and python
ffibuilder.cdef("""
    struct my_s{ int a; char * b; };
    int my_f(int, struct my_s);
""")

# definitions of the python-c shared objects
ffibuilder.set_source("_example",r"""

    struct my_s{ int a; char * b; };

    #include <stdio.h>
    int my_f(int arg_1, struct my_s arg_2) // some random example function
    {
        printf("%s\n", arg_2.b); 
        return arg_1 + arg_2.a;
    }

""", sources=[])

if __name__ == "__main__" :
    ffibuilder.compile(verbose=True)

And the python calls python 调用

# file: test_example.py
import _example as e

n = 21;

s = e.ffi.new("struct my_s *")
s.a = 21
s.b = e.ffi.new("char[]", b"Hello World!")

# e.lib.my_f(arg_2=s[0], arg_1=n); # <-- Here is what I want
e.lib.my_f(n, s[0]); # the standard way

As of today – May 18 2021 – it is not possible to wrap a c function with cffi as to expose keyword arguments to python. As of today – May 18 2021 – it is not possible to wrap a c function with cffi as to expose keyword arguments to python. This answer was provided by Armin Rigo on the cffi mailing list here (a patch to provide this feature will be accepted by Armin Rigo , if certain conditions are fulfilled).此答案由 Armin Rigo 在此处的 cffi 邮件列表中提供(如果满足某些条件, Armin Rigo将接受提供此功能的补丁)。 The only possible solution then is to wrap the cffi-wrapped c-function in python code (ugh).唯一可能的解决方案是将 cffi 包装的 c 函数包装在 python 代码中(呃)。

Note: Even uglier will it get, if one wanted to set a default argument value for arg_2注意:如果想为 arg_2 设置默认参数值,它会变得更丑陋

# file: test_wrap.py
import _example as e

def my_f(arg_1, arg_2) :
    return e.lib.my_f(arg_1, arg_2)

n = 21

s = e.ffi.new("struct my_s *")
s.a = 21
s.b = e.ffi.new("char[]", b"Hello World!")

sol = my_f(arg_2=s[0], arg_1=n) # <-- Here is what I want

print(sol)

On the command line在命令行上

$ python test_wrap.py
Hello World!
42

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM