[pygtk] Wrapping GList

Nicholas Piper nick-pygtk at nickpiper.co.uk
Tue Oct 23 18:38:46 WST 2007


Dear List,

I regularly find myself wrapping libraries which use GList structures.

So far I've had to do this as I did for libgpod:
http://gtkpod.svn.sourceforge.net/viewvc/gtkpod/libgpod/trunk/bindings/python/gpod.i.in?view=markup

   92 PyObject* sw_get_tracks(Itdb_iTunesDB *itdb) {
   93   PyObject    *list;
   94   gint        i;
   95   GList       *l;
   96   list = PyList_New(g_list_length(itdb->tracks));
   97   for (l = itdb->tracks, i = 0; l; l = l->next, ++i) {
   98     PyList_SET_ITEM(list, i,  SWIG_NewPointerObj((void*)(l->data), SWIGTYPE_p__Itdb_Track, 0));
   99   }
  100   return list;
  101  }

I think I'd much prefer to return the GList more directly, without
creating a whole new Python list. In the above case, it's fairly
cheap... but in my other examples, the inner loop has to marshal more
expensive C structures into Python objects. I'd prefer to only do that
if they are actually accessed.

I suppose it is a tradeoff though, because if I wrapped the GList in
such a way that Python objects were created when list data is actually
accessed, each item might be wrapped more often that once.

However, it would be nice if _changes_ to the wrapped List (removing
records, extending, ...) could adjust the 'real' GList.

Does anyone have an example of a different way to wrap GList than I've
shown here? Maybe wrapping it into a caching proxy for the real GList.

Much thanks.

 Nick


More information about the pygtk mailing list