[C++-sig] What if a Python override returns several values?

Alex Mohr amohr at pixar.com
Tue Sep 5 18:53:54 CEST 2006


> But if I have another virtual member function "void g(int& a, int &b)" 
> which returns two integers, then I'd like the Python version to take no 
> arguments and return a tuple (a,b). But how does the wrapper look like 
> in this case? I would have expected something like:
> 
>      void g(int& a, int& b)
>      {
>          boost::python::tuple tup = this->get_override("g")();
> 
> 	a = ...extract an int from tup[0]...
>          b = ...extract an int from tup[1]...
>      }
> 
> But when I run this code I get the following error at runtime:
> 
> TypeError: No registered converter was able to extract a C++ reference 
> to type boost::python::tuple from this Python object of type tuple

Not sure exactly what's going on here, but you might be able to find out 
by looking at override.hpp.  Look at method_result -- that's what you're 
getting back from get_override.

Off the top of my head, I think this would probably work for you:

tuple tup = call<tuple>(this->get_override("g").ptr());
a = ...extract an int from tup[0]
b = ...extract an int from tup[1]

It's certainly not as nice as what you wrote, but I have a feeling it 
might make the conversions work right.  The idea here is that the 
boost::python::override object manages the python callable object, so 
you can get it (using ptr()) and call it any way you like.

Alex



More information about the Cplusplus-sig mailing list