[C++-sig] boost.python: problem exposing static attribute with specific to_python converter

Baptiste Lepilleur gaiacrtn at free.fr
Fri Jan 7 18:24:49 CET 2005


    I'm trying to expose 'staticVar' in the following class:

struct TestString {
   static CEGUI::String staticVar;
   CEGUI::String memberFn() const {
      return CEGUI::String( "member" );
   }
};

    CEGUI::String has no relation with std::string, except that you can
construct CEGUI::String implicitly with an std::string. A specific to_python
converter is used to expose it (see below).

    CEGUI::String and TestString are exposed as follow:

struct CEGUIStringToPython {
   static PyObject *convert( const CEGUI::String &s ) {
      return PyString_FromString( s.c_str() );
   }
};

void registerCEGUIStringConversion()
{
   boost::python::to_python_converter<CEGUI::String,
py::CEGUIStringToPython>();
   boost::python::implicitly_convertible<std::string, CEGUI::String>();

   boost::python::class_<TestString>( "TestString",
boost::python::init<>() )
      .def_readonly( "staticVar", &TestString::staticVar )
      .def( "memberFn", &TestString::memberFn )
      ;
}

    But when running the test below, calling memberFn() works, but staticVar
fails.

Python 2.3.3 (#51, Dec 18 2003, 20:22:39) [MSC v.1200 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import localcegui as cegui
>>> print "Member:", cegui.TestString().memberFn()
Member: member
>>> print "StaticVar:", cegui.TestString.staticVar
StaticVar:
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
TypeError: No Python class registered for C++ class class CEGUI::String

    Changing 'staticVar' type from CEGUI::String to std::string remove this
error message, so something is really going on with the to_python
conversion. Why isn't the to_python_converter found ?

    Any help is welcome,
    Baptiste.




More information about the Cplusplus-sig mailing list