【问题标题】:SWIG how to make a typedef function pointer in a struct callable from PythonSWIG 如何在 Python 可调用的结构中创建 typedef 函数指针
【发布时间】:2019-04-24 04:35:56
【问题描述】:

TL;DR 有人知道如何指示 SWIG 将 C-struct 的这些成员视为函数指针并使其可从 Python 调用吗?

全文 我有 C 结构,其中包含指向函数的指针。这些函数都是类型定义的。我有一个 C 函数,它将为此 C 结构分配内存,并将函数指针设置为指向有效的 C 函数。 我的简化头文件是这样的

// simplified api.h
typedef void *handle_t;
typedef void sample_t;
typedef error_t comp_close_t(handle_t *h);
typedef error_t comp_process_t(handle_t h,
                               sample_t *in_ptr,
                               sample_t *out_ptr,
                               size_t *nr_samples);
typedef struct
{
    comp_close_t *close;
    comp_process_t *process;
} audio_comp_t;

// prototype for init
error_t comp_init(handle_t *h, int size);

以及对应的简化源文件:

// simplified api.c
static comp_close_t my_close;
static comp_process_t my_process;

audio_comp_t comp = {
    my_close,
    my_process
};

error_t comp_init(audio_comp_t **handle) {
    *handle = ∁
    return 0;
}

error_t my_close(handle_t *h) {
    // stuff
    *h = NULL;
    return 0;
}

error_t my_process(handle_t h,
                   sample_t *in_ptr,
                   sample_t *out_ptr,
                   size_t *nr_samples) {
    audio_comp_t *c = (audio_comp_t*) h;
    // stuff 
    printf("doing something useful\n");
}

还有我的接口文件的最新版本:

%module comp_wrapper
%{
#include "api.h"
%}

%include "api.h"

// Take care of the double pointer in comp_init
%ignore comp_init;
%rename(comp_init) comp_init_overload;
%newobject comp_init;

%inline %{
audio_comp_t* comp_init_overload(int size) {
    audio_comp_t *result = NULL;
    error_t err = comp_init(&result, size);

    if (SSS_NO_ERROR == err) {
        ...
    }

    return result;
}
%}

// wrap the process call to verify the process_t * function pointer    
%inline %{
sss_error_t call_process(   audio_comp_t *h, 
                            sample_t *in, 
                            sample_t *out, 
                            size_t nr_samples)
{
    return h->process(h, in, out, &nr_samples);
}
%}

我想使用 SWIG 创建语言绑定,这样我就可以用最少的 Python 样板代码调用这些类似对象的结构。最终我想这样使用:

h = comp_init(50)
h.process(h, input_data, output_data, block_size)
h.close(h)

然而,SWIG 将这些结构中的这些函数指针视为对象,所以每当我想调用它们时,我都会得到

>>> h = comp_init(50)
>>> h.api.process()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: 'SwigPyObject' object is not callable

我可以通过你可以在接口文件中找到的“call_process”函数来解决它:

call_process(h, in, out, 32) 

但这需要我为所有结构成员函数添加一个额外的包装器,虽然这不是必需的,因为 [SWIG 文档声明完全支持函数指针][1]

我假设我应该在接口文件中编写一些代码,以便 SWIG 知道它处理的是函数而不是 SwigPyObject

有一些关于如何处理(python)回调的信息,但在这种情况下似乎没有一个特别有效: SWIG call function pointers stored within struct

或者不将头文件中的所有信息或多或少地复制到接口文件中: Using SWIG with pointer to function in C struct

最后但并非最不重要的是,当您将函数指针包装在结构中时似乎有所不同,因此解决方案 5 不起作用: How to wrap a c++ function which takes in a function pointer in python using SWIG

有人知道如何指示 SWIG 将 C-struct 的这些成员视为函数指针并使其可从 Python 调用吗?

【问题讨论】:

  • 您自己引用的示例stackoverflow.com/questions/1583293/… 考虑了一个带有函数指针成员的结构,并展示了如何从 Python 调用它。这与您尝试做的有什么不同
  • 情况也一样。然而,解决方案并不是我希望的那样。必须在接口文件中重复每个与函数指针相关的结构和函数声明并不是最好的解决方案。在这种情况下,我不妨编写包装器来手动从 python 调用代码。我特别希望能够告诉 SWIG 使函数指针可调用。
  • SWIG 的功能远不止于此,您无需在如此低的级别上工作并使用裸指针进行强制转换。所有函数都可以被包装,它甚至可以生成一个运行时,可用于查询内容。我的建议是跳过typedefed 函数原型并简单地包装实际函数。将被附在图书馆中。如果需要初始化,请在 *nix 或 Windows 中使用 attribute(constructor) DLL_PROCESS_ATTACH
  • 如果你愿意的话,我这里有一个小项目,它做了很多工作,github.com/JensMunkHansen/sofus
  • 我目前正在考虑解决方案,但现在我有一个快速的澄清点:comp_process_t 采用 handle_t,但 comp_close_t 采用handle_t*?另外你愿意稍微编辑一下 api.h,而不是更改 C API,而是使用一些宏来简化 SWIG 界面吗?

标签: python c function struct swig


【解决方案1】:

最简单的解决方案是,如果我们向 SWIG 声称 function pointers are simply member functions 那么它将生成的包装器工作得非常好。

为了证明在这种情况下我们需要修复示例代码中的一些错误,所以我最终得到的 api.h 看起来像这样:

// simplified api.h
#include <stdint.h>
#include <stdlib.h>

typedef uint32_t api_error_t;

typedef void *handle_t;
typedef void sample_t;
typedef api_error_t comp_close_t(handle_t h);
typedef api_error_t comp_process_t(handle_t h,
                               sample_t *in_ptr,
                               sample_t *out_ptr,
                               size_t nr_samples);
typedef struct
{
    comp_close_t *close;
    comp_process_t *process;
} audio_comp_t;

// prototype for init
api_error_t comp_init(handle_t *new_h);

和 api.c 看起来像这样:

#include "api.h"
#include <stdio.h>

// simplified api.c
static comp_close_t my_close;
static comp_process_t my_process;

audio_comp_t comp = {
    my_close,
    my_process
};

api_error_t comp_init(handle_t *handle) {
    *handle = &comp;
    return 0;
}

api_error_t my_close(handle_t h) {
    (void)h; // stuff
    return 0;
}

api_error_t my_process(handle_t h,
                   sample_t *in_ptr,
                   sample_t *out_ptr,
                   size_t nr_samples) {
    audio_comp_t *c = (audio_comp_t*) h;
    (void)c;(void)in_ptr;(void)out_ptr;// stuff 
    printf("doing something useful\n");
    return 0;
}

有了它,我们可以编写如下的 api.i:

%module api

%{
#include "api.h"
%}

%include <stdint.i>

%typemap(in,numinputs=0) handle_t *new_h (handle_t tmp) %{
    $1 = &tmp;
%}

%typemap(argout) handle_t *new_h %{
    if (!result) {
        $result = SWIG_NewPointerObj(tmp$argnum, $descriptor(audio_comp_t *), 0 /*| SWIG_POINTER_OWN */);
    }
    else {
        // Do something to make the error a Python exception...
    }
%}

// From my earlier answer: https://stackoverflow.com/a/11029809/168175
%typemap(in,numinputs=0) handle_t self "$1=NULL;"
%typemap(check) handle_t self {
  $1 = arg1;
}

typedef struct {
  api_error_t close(handle_t self);
  api_error_t process(handle_t self,
                      sample_t *in_ptr,
                      sample_t *out_ptr,
                      size_t nr_samples);
} audio_comp_t;

%ignore audio_comp_t;
%include "api.h"

除了隐藏原始结构并声称它充满了成员函数而不是成员指针之外,我们还做了一些事情:

  1. 制作 SWIG automatically pass the handle in as the 1st argument,而不是要求 Python 用户过于冗长。 (在 python 中它变成h.close() 而不是h.close(h)
  2. 使用 argout 类型映射来包装真正的 comp_init 函数,而不是仅仅替换它。这纯粹是一个偏好问题,我添加它是为了展示如何使用它。

这让我可以运行以下 Python:

import api

h=api.comp_init()
print(h)
h.process(None, None, 0)
h.close()

如果您愿意对 API 的标头进行一些外观更改以方便操作,我们可以做一些对 Python 和 C 都非常有效的事情。

我在 api.h 中引入了一个宏,MAKE_API_FUNC,它包装了你原来在其中的 typedef 语句。当使用 C 编译器编译时,它仍然会产生完全相同的结果,但是它让我们可以使用 SWIG 更好地操作。

所以 api.h 现在看起来像这样:

// simplified api.h
#include <stdint.h>
#include <stdlib.h>

typedef uint32_t api_error_t;

typedef void *handle_t;
typedef void sample_t;
#ifndef MAKE_API_FUNC
#define MAKE_API_FUNC(name, type, ...) typedef api_error_t comp_ ## name ## _t(__VA_ARGS__)
#endif

MAKE_API_FUNC(close, audio_comp_t, handle_t);
MAKE_API_FUNC(process, audio_comp_t, handle_t, sample_t *, sample_t *, size_t);

typedef struct
{
    comp_close_t *close;
    comp_process_t *process;
} audio_comp_t;

// prototype for init
api_error_t comp_init(handle_t *new_h);

所以在 api.i 中,我们现在用另一个宏替换该宏,它向 SWIG 声称函数指针 typedef 实际上是一个结构,具有专门提供的 __call__ 函数。通过创建这个额外的函数,我们可以将所有 Python 参数自动代理到对真实函数指针的调用中。

%module api

%{
#include "api.h"
%}

%include <stdint.i>

// From: https://stackoverflow.com/a/2653351
#define xstr(a) str(a)
#define str(a) #a

#define name_arg(num, type) arg_ ## num
#define param_arg(num, type) type name_arg(num, type)

#define FE_0(...)
#define FE_1(action,a1) action(0,a1)
#define FE_2(action,a1,a2) action(0,a1), action(1,a2)
#define FE_3(action,a1,a2,a3) action(0,a1), action(1,a2), action(2,a3)
#define FE_4(action,a1,a2,a3,a4) action(0,a1), action(1,a2), action(2,a3), action(3,a4)
#define FE_5(action,a1,a2,a3,a4,a5) action(0,a1), action(1,a2), action(2,a3), action(3,a4), action(4,a5)

#define GET_MACRO(_1,_2,_3,_4,_5,NAME,...) NAME
%define FOR_EACH(action,...)
  GET_MACRO(__VA_ARGS__, FE_5, FE_4, FE_3, FE_2, FE_1, FE_0)(action,__VA_ARGS__)
%enddef

%define MAKE_API_FUNC(name, api_type, ...)
%nodefaultctor comp_ ## name ## _t;
%nodefaultdtor comp_ ## name ## _t;
typedef struct {
    %extend {
        api_error_t __call__(FOR_EACH(param_arg, __VA_ARGS__)) {
            return $self(FOR_EACH(name_arg, __VA_ARGS__));
        }
    }
} comp_ ## name ## _t;
// Workaround from: https://github.com/swig/swig/issues/609
%rename("%s_fptr", "%$isvariable", "match$ismember"="1", "match$type"=xstr(comp_ ## name ## _t)) name;
%extend api_type {
    %pythoncode %{
        name = lambda self, *args: self.name ## _fptr(self, *args)
    %}
}
%enddef

%ignore comp_init;
%include "api.h"

%extend  audio_comp_t {
    audio_comp_t() {
        handle_t new_h = NULL;
        api_error_t err = comp_init(&new_h);
        if (err) {
            // throw or set Python error directly
        }
        return new_h;
    }

    ~audio_comp_t() {
        (void)$self;
        // Do whatever we need to cleanup properly here, could actually call close
    }
}

这是使用我在my answer on wrapping std::function objects 中使用的相同preprocessor mechanisms,但应用于此问题的函数指针。另外我从 Python 的角度使用了%extend to make a constructor/destructor,这使得 API 更好用。如果这是真实代码,我可能也会使用%rename

话虽如此,我们现在可以使用以下 Python 代码:

import api

h=api.audio_comp_t()
print(h)
print(h.process)
h.process(None, None, 0)

请参阅SWIG docs,了解如何将错误代码映射到 Python 的异常上。


我们可以通过一个简单的技巧消除对可变参数宏的参数进行迭代的需要,从而进一步简化这一点。如果我们将 api.h 宏更改为接受 3 个参数,其中第 3 个是函数指针的所有参数,如下所示:

// simplified api.h
#include <stdint.h>
#include <stdlib.h>

typedef uint32_t api_error_t;

typedef void *handle_t;
typedef void sample_t;
#ifndef MAKE_API_FUNC
#define MAKE_API_FUNC(name, type, args) typedef api_error_t comp_ ## name ## _t args
#endif

MAKE_API_FUNC(close, audio_comp_t, (handle_t self));
MAKE_API_FUNC(process, audio_comp_t, (handle_t self, sample_t *in_ptr, sample_t *out_ptr, size_t nr_samples));

typedef struct
{
    comp_close_t *close;
    comp_process_t *process;
} audio_comp_t;

// prototype for init
api_error_t comp_init(handle_t *new_h);

然后我们现在可以更改我们的 SWIG 接口,不提供我们通过 %extend 添加的 __call__ 函数的定义,而是编写一个宏来直接调用我们想要的函数指针:

%module api

%{
#include "api.h"
%}

%include <stdint.i>

// From: https://stackoverflow.com/a/2653351
#define xstr(a) str(a)
#define str(a) #a

%define MAKE_API_FUNC(name, api_type, arg_types)
%nodefaultctor comp_ ## name ## _t;
%nodefaultdtor comp_ ## name ## _t;
%{
#define comp_ ## name ## _t___call__(fptr, ...) fptr(__VA_ARGS__)
%}
typedef struct {
    %extend {
        api_error_t __call__ arg_types;
    }
} comp_ ## name ## _t;
// Workaround from: https://github.com/swig/swig/issues/609
%rename("%s_fptr", "%$isvariable", "match$ismember"="1", "match$type"=xstr(comp_ ## name ## _t)) name;
%extend api_type {
    %pythoncode %{
        name = lambda self, *args: self.name ## _fptr(self, *args)
    %}
}
%enddef

%ignore comp_init;
%include "api.h"

%extend  audio_comp_t {
    audio_comp_t() {
        handle_t new_h = NULL;
        api_error_t err = comp_init(&new_h);
        if (err) {
            // throw or set Python error directly
        }
        return new_h;
    }

    ~audio_comp_t() {
        (void)$self;
        // Do whatever we need to cleanup properly here, could actually call close
    }
}

这里的棘手之处在于,typedef struct {...} name; 成语的使用使得重命名或隐藏结构内的函数指针变得更加困难。 (但这只是为了保持自动添加 handle_t 参数)。

【讨论】:

  • 好帖子!非常感谢!我已经实现了第一部分,并将将第二部分(看起来很有趣)添加到 backlog 中。
猜你喜欢
  • 1970-01-01
  • 2015-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多