I'm having a conceptual issue with the way C++ interfaces with Python (boost::python). I'm trying to convert a C++ object to a Python object, but my attempt causes the program to crash. I think I'm doing a lot of things wrong. Ultimately I'd like to be able to pass C++ objects to a python function that modifies their values.

/**** In the lib ****/// I believe this bit is only used if you create the object in PythonBOOST_PYTHON_MODULE(game){ boost::python::class_<Unit>("Unit") .def_readwrite("health", &Unit::health) .def_readwrite("name", &Unit::name) .def_readwrite("type", &Unit::type) .def_readwrite("coord", &Unit::coord) ;}

Ok, I had some wonderful help over at the Cplusplus-sig mailing list and it turns out my problem was quite simple to solve(read type).

First, when you create a set of wrappers you need to append them to the init tab. The BOOST_PYTHON_MODULE macro will create you the function needed to append all of the wrappers to the init tab. The name of this function is init[module_name]. Thus invoking BOOST_PYTHON_MODULE(game) creates the routine initgame. Second, you need to import the module you created after you initialize the python runtime. Also of note, the wrappers created in BOOST_PYTHON_MODULE will generate to/from python converters for all primitive and stl types (I think) so there's no need to define them again like I did.

Here is the corrected code. Please note that the python object foo is a copy, not a reference:

Ok, that's great! We can convert a C++ object to a python object. However, this was not entirely what I wanted to do so anyone reading this gets some extra fun bonus information As stated in my first post I wanted to pass C++ objects to a python function that modifies their values. This is my python function:

Wait... The health should be 90 now. What happened?When you call the python function hit, it implicitly converts all of the arguments into python objects by copying the data. Thus unit1's data is copied into a boost::python object and this copy is used in the hit function. The hit function goes out of scope and the copy is garbage collected. The original is never modified.Ok, so not what I want. What I want is easily achieved using boost ref. So if we look at that try catch block again:

Hooray! You made it to the end. I just started using Boost::Python so there's probably a few errors in what I said above. Also, a note about compiling.Visual Studios: Don't forget to put -DBOOST_PYTHON_STATIC_LIB into your command line options (configuration properties)Linux: Compile with -I/usr/include/python2.6 -lpython2.6 -lboost_python

Also also, I never got it running on Windows, but the Py++ is an awesome code generator for boost::python. The boost-ogre guys used it in their project.

Thanks I was hoping it would be useful to someone other than myself. Maybe I'll write a more comprehensive tutorial after I'm more experienced with the library. I'd like to know a lot more about the call policies provided by boost::python.