我正在尝试使用 PyBindGen 为我的具有回调的 C 库创建一个 python 扩展模块。尽管PyBindGen 的首页说回调是一个缺失的功能,但当前的源代码以及这个讨论和存档都有一些示例,说明如何设置 PyBindGen 以将回调函数从 python 传递到 C 库。
问题是该示例将回调方法定义为具有PyObject*
作为void *
参数来携带回调函数指针信息。由于我的 API 没有这样的上下文指针,我试图在 C 绑定代码中保留对回调的引用,类似于标准 python 示例。问题是我无法弄清楚如何让 PyBindGen 设置全局 PyObject * my_callback。我怎样才能做到这一点?这是我的代码
我的库头文件
typedef void (*CallbackType) (int value);
void register_cb(CallbackType cb);
void set_num(int n);
我的库源文件
#include "c.h"
static CallbackType cb_;
void register_cb(CallbackType cb)
{
cb_ = cb;
}
void set_num(int n)
{
cb_(n);
}
我的 PyBindGen modulegen.py带有注释部分,说明需要添加到自动生成的代码中才能正常工作
import sys
import pybindgen
from pybindgen import ReturnValue, Parameter, Module, Function, FileCodeSink
from pybindgen import CppMethod, CppConstructor, CppClass, Enum
from pybindgen.typehandlers.base import ForwardWrapperBase
class CallbackTypeParam(Parameter):
DIRECTIONS = [Parameter.DIRECTION_IN]
CTYPES = ['CallbackType']
def convert_python_to_c(self, wrapper):
assert isinstance(wrapper, ForwardWrapperBase)
py_cb = wrapper.declarations.declare_variable("PyObject*", self.name)
wrapper.parse_params.add_parameter('O', ['&'+py_cb], self.name)
wrapper.before_call.write_error_check("!PyCallable_Check(%s)" % py_cb, """PyErr_SetString(PyExc_TypeError, "CallbackType parameter must be callable");""")
#####
# NEED TO INSERT THE FOLLOWING TWO LINES INTO AUTOGENERATED OUTPUT HERE
# Py_XDECREF(my_callback); // NEEDS TO BE AUTO-GENERATED !!!
# my_callback = cb; // NEEDS TO BE AUTO-GENERATED !!!
#####
wrapper.call_params.append("_wrap_callback")
wrapper.before_call.write_code("Py_INCREF(%s);" % py_cb)
wrapper.before_call.add_cleanup_code("Py_DECREF(%s);" % py_cb)
def convert_c_to_python(self, wrapper):
raise NotImplementedError
def my_module_gen(out_file):
mod = Module('c')
mod.add_include('"c.h"')
mod.header.writeln("""
void _wrap_callback(int value);
static PyObject *my_callback = NULL;
""")
mod.body.writeln("""
void _wrap_callback(int value)
{
int arg;
PyObject *arglist;
arg = value;
printf("@@@@ Inside the binding: %d %p\\n", value, my_callback); fflush(NULL);
arglist = Py_BuildValue("(i)", arg);
PyObject_CallObject(my_callback, arglist);
Py_DECREF(arglist);
}
""")
mod.add_function("register_cb", None, [Parameter.new("CallbackType", "cb")])
mod.add_function("set_num", None, [Parameter.new("int", "n")])
mod.generate(FileCodeSink(out_file))
if __name__ == '__main__':
my_module_gen(sys.stdout)
我用于构建扩展模块的 setup.py 文件
#!/usr/bin/env python
import os
from distutils.core import setup, Extension
from modulegen import my_module_gen as generate
try:
os.mkdir("build")
except OSError:
pass
os.environ["CC"] = "g++"
module_fname = os.path.join("build", "autogen-binding.c")
with open(module_fname, "wt") as file_:
print("Generating file {}".format(module_fname))
generate(file_)
mymodule = Extension('c',
sources = [module_fname, 'c.cc'],
include_dirs=['.'])
setup(name='PyBindGen-example',
version="0.0",
description='PyBindGen example',
author='xxx',
author_email='yyy@zz',
ext_modules=[mymodule],
)
我的 python 脚本使用扩展模块
import c
def my_callback(value):
print("In Callback: " + str(value))
c.register_cb(my_callback)
c.set_num(10);
c.set_num(20);