[C++-sig] Exposing C Array members to python using boost

Carpman, David dcarpman at ll.mit.edu
Thu Dec 20 00:41:34 CET 2007


Hello,

I've been reading the archives on the topic of how to expose read/write access of C-arrays to python using boost::python, but what I could find was several years old. Some of the examples reference files that no longer exist, particularly in the indexing/container suite area. I would very much appreciate some wisdom on this subject from this list, if folks would be so kind. :)

>From what I've read, Raoul Gough's indexing suite is needed to convert the arrays to python lists. I see the vector and map examples in the boost distribution (I have tried 1.35-PR1 and svn rev 42152) but nothing having to do with arrays. http://osdir.com/ml/python.c++/2003-11/msg00185.html seems to indicate Raoul had a version of the indexing suite that supported arrays in 2003, but I can't find anything about it. Has anything changed or been done since 2003 for this?

Here is my situation: I'm trying to wrap some legacy C-structures that define a network packet structure that will be written by python and read by some embedded computer. I have no control over the definition of these structures, nor can I add to or change the header files. My hang-up is that some of the members in these structures are defined as C-arrays, for example, int numsamples[32]. I would like to have access in python to the elements of these arrays for reading and writing. Scalar primitive members work fine.

Example:

#include <boost/python.hpp>
#include <boost/python/class.hpp>
#include <boost/python/module.hpp>

//The Foo struct definition would be in another header that cannot be altered
struct Foo
{
    int a[8];
};

BOOST_PYTHON_MODULE(TestCArrays)
{
    boost::python::class_< Foo >("Foo", boost::python::init<>() )
        .def_readwrite("a", &Foo::a)
    ;
}

Gives me: (sorry for the long lines)
/usr/include/boost/python/data_members.hpp: In member function 'void boost::python::detail::member<Data, Class>::operator()(Class&, typename boost::python::detail::value_arg<T>::type) const [with Data = int [8], Class = Foo]':
/usr/include/boost/python/detail/invoke.hpp:81:   instantiated from 'PyObject* boost::python::detail::invoke(boost::python::detail::invoke_tag_<true, false>, const RC&, F&, AC0&, AC1&) [with RC = int, F = boost::python::detail::member<int [8], Foo>, AC0 = boost::python::arg_from_python<Foo&>, AC1 = boost::python::arg_from_python<const int (&)[8]>]'
/usr/include/boost/python/detail/caller.hpp:199:   instantiated from 'PyObject* boost::python::detail::caller_arity<2u>::impl<F, Policies, Sig>::operator()(PyObject*, PyObject*) [with F = boost::python::detail::member<int [8], Foo>, Policies = boost::python::default_call_policies, Sig = boost::mpl::vector3<void, Foo&, const int (&)[8]>]'
/usr/include/boost/python/object/py_function.hpp:38:   instantiated from 'PyObject* boost::python::objects::caller_py_function_impl<Caller>::operator()(PyObject*, PyObject*) [with Caller = boost::python::detail::caller<boost::python::detail::member<int [8], Foo>, boost::python::default_call_policies, boost::mpl::vector3<void, Foo&, const int (&)[8]> >]'
testcarray.cpp:34:   instantiated from here
/usr/include/boost/python/data_members.hpp:64: error: ISO C++ forbids assignment of arrays

How would I define Foo's python wrapper class_ so that I would have the following kind of access in python (perhaps using the indexing suite)?

Myfoo = Foo()
Myfoo.a[0] = 1
Myfoo.a[1] = 2

I would like to avoid having to write C++ wrapper classes for the C-structs containing the C-arrays, as there are many dozens of them.
I'd be very thankful if someone could help!

Regards,
David Carpman





More information about the Cplusplus-sig mailing list