Model/Marshaling
This page is to document my experiments with marshaling objects between C and Python.
Background
Model is an activity for system dynamics modeling. It consists of a small number of gobject classes written in C, and a GTK ui written in Python. Of interest is the OpensimSimulator class. With this class you can add variables, simulate models, and save them. Currently, I expose a signal, 'saving', which is emitted when we are in the middle of saving our XML-based model file (here). The general idea is that we write the header, the model, then emit the saving signal so that users of the simulator class (like our UI) can add their own metadata, then write the footer (which is little more than '</opensim>').
Implementation
The question is how to best pass information on an open file from C to Python. Currently I use a dirty hack, implemented here to take that FILE * (which pygobject wraps nicely into a gobject.GPointer), get the pointer from that wrapper, and return a PyFile from the pointer (and some info from the simulator object, like file name). I thought about GIOStream, but looking through the source I couldn't find any way to let the marshaler know about it. I also thought about using a custom marshaler for the python bindings, but it looked (literally) impossible from the python source. Maybe someone with more experience would be able to give a definitive answer.
GIO?
perhaps GIO would work here, that will be the next area I investigate. Although I hesitate to pull in another library (even though its included with glib).