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

Alex Mohr amohr at pixar.com
Tue Sep 12 18:36:06 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]
> 
> 
> I don't know why people always use ugly low-level interfaces when they
> can use beautiful high-level ones:
> 
>     python::tuple tup = this->get_override("g")();
>     a = extract<t1>(tup[0]);
>     .
>     .
>     .

Isn't this what the original poster had, and claimed didn't work?  I 
agree that this is what should be written.  I was merely suggesting a 
workaround.  What am I missing?

> There's almost never a reason to use call<> anymore!  I should
> deprecate it.

Please don't -- I use it in some low-level code.

> I'm not trying to be critical, but this really concerns me.  I see
> this sort of thing all the time.  If you could help me understand
> why you were moved to suggest doing it the hard way, I would
> appreciate it.

Again, just because the original (which as far as I can see is the same 
as what you suggested) didn't work.  I was just suggesting a workaround. 
  I wouldn't suggest it as the "correct" solution.

Alex



More information about the Cplusplus-sig mailing list