[C++-sig] Boost.Python embedding fails on MacOS X

Marco Biasini mbiasini at bluewin.ch
Mon Jun 30 19:25:57 CEST 2008


Hi!

I posted this mail on the boost users mailing list and was then  
pointed to this mailing-list. I've got a problem when using  
boost.python on MacOS X. The program exits with

TypeError: attribute name must be string

   This happens both in boost 1.34 and 1.35. I was however able to  
run attached snippet without problems with boost 1.33.

Code:

#include <boost/python.hpp>

using namespace boost::python;

class CppClass {
public:
   int getNum() {
     return 7;
   }
};

int main( int argc, char ** argv ) {
   try {
     Py_Initialize();

     object main_module((
       handle<>(borrowed(PyImport_AddModule("__main__")))));

     object main_namespace = main_module.attr("__dict__");
	main_namespace["CppClass"] = class_<CppClass>("CppClass")
	                               .def("getNum",&CppClass::getNum);
	handle<> ignored(( PyRun_String( "cpp = CppClass();print cpp.getNum() 
\n",
                                      Py_file_input,
                                      main_namespace.ptr(),
                                      main_namespace.ptr() ) ));
   } catch( error_already_set ) {
     PyErr_Print();
   }
}


Specs, if it should matter:

   * MacOS 10.4.11
   * Compiler gcc 4.0.1 (Apple, Inc.), Python 2.4 (MacPorts)

Any suggestions/directions? Please let me know if you need additional  
information.


Best,
Marco






More information about the Cplusplus-sig mailing list