如何将kwargs传递给boost-python包装的函数? [英] How do you pass kwargs to a boost-python wrapped function?

查看:104
本文介绍了如何将kwargs传递给boost-python包装的函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有此签名的python函数:

I have a python function with this signature:

def post_message(self, message, *args, **kwargs):

我想从c ++调用该函数,并将其传递给一些kwargs.调用函数不是问题.知道如何通过矮人是.这是一个无效的措辞示例:

I would like to call the function from c++ and pass to it some kwargs. Calling the function is not the problem. Knowing how to pass the kwargs is. Here is a non-working paraphrased sample:

std::string message("aMessage");
boost::python::list arguments;
arguments.append("1");

boost::python::dict options;
options["source"] = "cpp";

boost::python::object python_func = get_python_func_of_wrapped_object()
python_func(message, arguments, options)

当我执行此代码时,在pdb中我得到了(这不是我想要的):

When I exercise this code, in pdb I get (which is not what I would like):

messsage = aMessage
args = (['1'], {'source': 'cpp'})
kwargs = {}

在我的示例中,如何在** kwargs词典中传递 options ?

How do you pass the options in my example in the **kwargs dictionary ?

我看过一个帖子建议使用** options语法(这太酷了!):

I have seen one post suggesting to use the **options syntax (how cool is this!):

python_func(message, arguments, **options)

不幸的是,这导致了

TypeError: No to_python (by-value) converter found for C++ type: class boost::python::detail::kwds_proxy

感谢您提供的任何帮助.

Thank you for any help you can give.

推荐答案

经过调查,结果发现对象函数调用运算符被 args_proxy kwds_proxy .因此,您必须使用具有两个参数的特定调用样式.

After some investigation, it turns out that the object function call operator is overridden for two arguments of type args_proxy and kwds_proxy. So you have to use this specific call style of two arguments.

args_proxykwds_proxy由*重载生成.真的很好

args_proxy and kwds_proxy are generated by the * overloads. This is really nice.

此外,第一个参数必须为元组类型,以便python解释器正确处理* args参数.

Additionally, the first argument must be a tuple type so that the python interpreter correctly handles the *args argument.

结果示例有效:

boost::python::list arguments;
arguments.append("aMessage");
arguments.append("1");

boost::python::dict options;
options["source"] = "cpp";

boost::python::object python_func = get_python_func_of_wrapped_object()
python_func(*boost::python::tuple(arguments), **options)

希望这对您有帮助...

Hope this helps...

这篇关于如何将kwargs传递给boost-python包装的函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆