[C++-sig] [boost.python] char arrays in structs

Markus Schöpflin markus.schoepflin at comsoft.de
Mon Jul 4 16:07:27 CEST 2005


Using gcc-3.4.3 and boost.python 1.32.0. Full example:

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

typedef char bar_t[4];

struct foo
{
   bar_t bar;
   foo() {
     bar[0] = 'b'; bar[1] = 'a';
     bar[2] = 'r'; bar[3] = 0;
   }
};

BOOST_PYTHON_MODULE(test)
{
   using namespace boost::python;

   class_<foo>("foo")
     .def_readonly("bar", &foo::bar)
     ;
}
--->%---

The compiles and links ok, but when using it from python I get the 
following error:

Python 2.3 (#1, Jul 30 2003, 14:14:00)
[GCC 3.3 20030226 (prerelease) (SuSE Linux)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
 >>> from test import *
 >>> print foo().bar
Traceback (most recent call last):
   File "<stdin>", line 1, in ?
TypeError: No to_python (by-value) converter found for C++ type: char [4]

So I went ahead and added a custom converter for bar_t like so:

struct bar_t_to_python_str
{
   static PyObject* convert(bar_t const &s)
   {
     return boost::python::incref(boost::python::object(s).ptr());
   }
};

and in BOOST_PYTHON_MODULE(test):

   to_python_converter<bar_t, bar_t_to_python_str>();

Is it to be expected that I do need a custom type converter for this? Or do 
I miss something?

TIA, Markus




More information about the Cplusplus-sig mailing list