Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you pass kwargs to a boost-python wrapped function?

I have a python function with this signature:

def post_message(self, message, *args, **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)

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

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

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

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

python_func(message, arguments, **options)

Unfortunately, this results in

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.

like image 361
David Avatar asked Jun 28 '11 01:06

David


People also ask

How do you pass Kwargs Python?

Use the Python **kwargs parameter to allow the function to accept a variable number of keyword arguments. Inside the function, the kwargs argument is a dictionary that contains all keyword arguments as its name-value pairs. Precede double stars ( ** ) to a dictionary argument to pass it to **kwargs parameter.

How do you pass multiple Kwargs Python?

Python 3.5+ allows passing multiple sets of keyword arguments ("kwargs") to a function within a single call, using the `"**"` syntax.

How does Python pass dictionary as Kwargs?

“ kwargs ” stands for keyword arguments. It is used for passing advanced data objects like dictionaries to a function because in such functions one doesn't have a clue about the number of arguments, hence data passed is be dealt properly by adding “**” to the passing type.

What is Kwargs in a built in function in Python?

**kwargs allows us to pass a variable number of keyword arguments to a Python function. In the function, we use the double-asterisk ( ** ) before the parameter name to denote this type of argument.


1 Answers

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_proxy and kwds_proxy are generated by the * overloads. This is really nice.

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

The resulting example works:

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...

like image 113
David Avatar answered Sep 22 '22 08:09

David