[C++-sig] tuples::tuple -> python::tuple

Andreas Beyer mail at a-beyer.de
Tue May 2 14:43:17 CEST 2006


Hi,

I tried to implement an example given on this list earlier this year:
http://mail.python.org/pipermail/c++-sig/2006-January/010103.html

I ran into the following issues.

Suggested code:

>>/  namespace my
>/>/  {
>/>/    template <class H, class T>
>/>/    python::tuple tuple_to_python(tuples::cons<H,T> const& x)
>/>/    {
>/>/        return python::make_tuple(x) + my::tuple_to_python(x.tail);
>/>/    }
>/                                   ^^^
>
>I guess it should rather be:
>
>          return python::make_tuple(x.head) + my::tuple_to_python(x.tail);
> 
>
If I try to compile this example (with the changes from *François as 
shown above) *I get the following compiler message (which refers to that 
line of code):

tuple_test.cpp:13: error: 'const struct boost::tuples::cons<double,
   boost::tuples::null_type>' has no member named 'tail'


I think, the line should actually read:
        return python::make_tuple(x.get_head()) + 
my::tuple_to_python(x.get_tail());

because 'x' is tuples:cons, which provides those methods for accessing 
head and tail. (I don't understand why the compiler seems to accept 
x.head but not x.tail.)

When compiling the eample with this additional change, I get the 
following error message:

tuple_test.cpp:13: error: conversion from `boost::python::api::object' to
   non-scalar type `boost::python::tuple' requested

which refers to the above line of code. I don't understand the message, 
because both python::make_tuple() and my::tuple_to_python() return 
python::tuple and not python::object.

Thanks, Andreas

Here is the complete, minimal example:

--- begin tuple_test.cpp ---

#include <boost/python.hpp>
#include <boost/tuple/tuple.hpp>

using namespace boost;

namespace my
{
   template <class H, class T>
   python::tuple tuple_to_python(tuples::cons<H,T> const& x)
   {
        return python::make_tuple(x.get_head()) + 
my::tuple_to_python(x.get_tail());
   }

   python::tuple tuple_to_python(tuples::null_type)
   {
        return python::tuple();
   }

   template <class T>
   struct tupleconverter
   {
        static PyObject* convert(T const& x)
        {
            return python::incref(my::tuple_to_python(x).ptr());
        }
   };
 
}

typedef tuples::tuple<int, double> a_tuple;
 
BOOST_PYTHON_MODULE(tuple_tester) {  
   python::to_python_converter<a_tuple, my::tupleconverter<a_tuple> >();
}

--- end tuple_test.cpp ---

-- 
Dr. Andreas Beyer
mail at a-beyer.de
www.andreas-beyer.de




More information about the Cplusplus-sig mailing list