SWIG 将参数传递给 python 回调函数

发布于 2024-09-25 18:22:00 字数 1800 浏览 5 评论 0原文

所以我快完成了。现在我有调用 python 回调函数的工作代码。

我现在唯一需要的是如何将参数传递给 python 回调函数。

我的回调.c 是:

#include <stdio.h>

typedef void (*CALLBACK)(void);
CALLBACK my_callback = 0;

void set_callback(CALLBACK c);
void test(void);

void set_callback(CALLBACK c) {
  my_callback = c;
}

void test(void) {
  printf("Testing the callback function\n");
  if (my_callback) (*my_callback)();
  else printf("No callback registered\n");
}

我的回调.i 是:

// An entirely different mechanism for handling a callback

%module callback
%{
typedef void (*CALLBACK)(void);
extern CALLBACK my_callback;

extern void set_callback(CALLBACK c);
extern void my_set_callback(PyObject *PyFunc);

extern void test(void);
%}

extern CALLBACK my_callback;

extern void set_callback(CALLBACK c);
extern void my_set_callback(PyObject *PyFunc);

extern void test(void);

%{
static PyObject *my_pycallback = NULL;
static void PythonCallBack(void)
{
   PyObject *func, *arglist;
   PyObject *result;

   func = my_pycallback;     /* This is the function .... */
   arglist = Py_BuildValue("()");  /* No arguments needed */
   result =  PyEval_CallObject(func, arglist);
   Py_DECREF(arglist);
   Py_XDECREF(result);
   return /*void*/;
}

void my_set_callback(PyObject *PyFunc)
{
    Py_XDECREF(my_pycallback);          /* Dispose of previous callback */
    Py_XINCREF(PyFunc);         /* Add a reference to new callback */
    my_pycallback = PyFunc;         /* Remember new callback */
    set_callback(PythonCallBack);
}

%}

%typemap(python, in) PyObject *PyFunc {
  if (!PyCallable_Check($input)) {
      PyErr_SetString(PyExc_TypeError, "Need a callable object!");
      return NULL;
  }
  $1 = $input;
}

效果很好。我应该怎么做才能将参数传递给 my_callback? 任何帮助将不胜感激!

So I'm almost done. Now I have working code which calls python callback function.

Only thing I need now is how to pass argument to the python callback function.

My callback.c is:

#include <stdio.h>

typedef void (*CALLBACK)(void);
CALLBACK my_callback = 0;

void set_callback(CALLBACK c);
void test(void);

void set_callback(CALLBACK c) {
  my_callback = c;
}

void test(void) {
  printf("Testing the callback function\n");
  if (my_callback) (*my_callback)();
  else printf("No callback registered\n");
}

My callback.i is:

// An entirely different mechanism for handling a callback

%module callback
%{
typedef void (*CALLBACK)(void);
extern CALLBACK my_callback;

extern void set_callback(CALLBACK c);
extern void my_set_callback(PyObject *PyFunc);

extern void test(void);
%}

extern CALLBACK my_callback;

extern void set_callback(CALLBACK c);
extern void my_set_callback(PyObject *PyFunc);

extern void test(void);

%{
static PyObject *my_pycallback = NULL;
static void PythonCallBack(void)
{
   PyObject *func, *arglist;
   PyObject *result;

   func = my_pycallback;     /* This is the function .... */
   arglist = Py_BuildValue("()");  /* No arguments needed */
   result =  PyEval_CallObject(func, arglist);
   Py_DECREF(arglist);
   Py_XDECREF(result);
   return /*void*/;
}

void my_set_callback(PyObject *PyFunc)
{
    Py_XDECREF(my_pycallback);          /* Dispose of previous callback */
    Py_XINCREF(PyFunc);         /* Add a reference to new callback */
    my_pycallback = PyFunc;         /* Remember new callback */
    set_callback(PythonCallBack);
}

%}

%typemap(python, in) PyObject *PyFunc {
  if (!PyCallable_Check($input)) {
      PyErr_SetString(PyExc_TypeError, "Need a callable object!");
      return NULL;
  }
  $1 = $input;
}

It works well. What should I do so I can pass argument to my_callback?
Any help will be greatly appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无人问我粥可暖 2024-10-02 18:22:00

回调的参数是 PyEval_CallObject() 的第二个参数。现在您正在构建一个空元组,这意味着“没有参数”。所以,改变这一点。您现在所做的:

arglist = Py_BuildValue("()");  /* No arguments needed */

您可以传递 Py_BuildValue 您希望 Python 函数接收的任何参数。例如,如果您想向回调传递从某处获得的整数、字符串和 Python 对象,您可以这样做:

arglist = Py_BuildValue("(isO)", the_int, the_str, the_pyobject);

The arguments to the callback are the second argument to PyEval_CallObject(). Right now you're building an empty tuple, which means "no arguments". So, change that. Where you now do:

arglist = Py_BuildValue("()");  /* No arguments needed */

you instead pass Py_BuildValue whatever arguments you want the Python function to receive. For example, if you want to pass the callback an integer, a string and a Python object you got from somewhere, you would do:

arglist = Py_BuildValue("(isO)", the_int, the_str, the_pyobject);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文