views:

164

answers:

1

Hello all,

I recently installed Boost using MacPorts, with the intent to do some Python embedding in C++. I then decided to check if I configured Xcode correctly with an example found on Python's website:

#include <boost/python.hpp>

using namespace boost::python;

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

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

        object main_namespace = main_module.attr("__dict__");

        handle<> ignored(PyRun_String("print \"Hello, World\"",
                                      Py_file_input,
                                      main_namespace.ptr(),
                                      main_namespace.ptr()));
    } 
    catch( error_already_set ) 
    {
        PyErr_Print();
    }
}

It compiles correctly, but when I launch it, the call to attr() throws an exception, and the resulting error message is "TypeError: attribute name must be string, not 'str'". Which suspiciously sounds like a placeholder.

I looked it up on Google, but no luck.

I use Boost v1.39, Python 2.5 and GCC 4.0, on Leopard.

+1  A: 

Your code worked for me with the following configuration:

  • Snow Leopard
  • gcc version 4.2.1 (AppleInc. build 5646)
  • Boost 1.41.0 installed to /usr/local/boost/1_41_0/
  • Stock OSX Python 2.5

Compiled using:

g++ -I/Developer/SDKs/MacOSX10.6.sdk/System/Library/Frameworks/Python.framework/Versions/2.6/include/python2.6/ -I/usr/local/boost/1_41_0/include -L/usr/local/boost/1_41_0/lib/ -boost_python -L/usr/lib/python2.6/config -lpython2.6 test.cpp

synthesizerpatel
I tried with Boost 1.42, GCC 4.2 and Python 2.5; still does not work. I have yet to test it with Snow Leopard...
Etienne de Martel