[pygtk] Wrapping plain structs

Gian Mario Tagliaretti gianmt at gnome.org
Fri Nov 2 04:45:32 WST 2007


2007/9/19, BJörn Lindqvist <bjourne a gmail.com>:

Hey BJörn!

I wanted to answer to this post for a loooong time but I didn't get
the chance until now, sorry for the delay, here we go :)

> How do you wrap naked typedef structs? I tried with define-boxed, like this:
>
> (define-boxed MyStruct
>   (in-module "mod")
>   (c-name "BlaHa")
>   (fields
>     ...
>   )
> )

That's a way to do it, but then you miss the GType declaration,

> But codegen.py seem to require a "gtype-id" field which requires the
> struct to be registered using glib's registering system. But what if
> my struct is not registered?

which is why codegen is complaining, you can define your own boxed
stuff in a private header, something like:

#define PY_TYPE_FOOBAR (py_foobar_get_type())

gpointer
py_foobar_copy (gpointer foo)
{
  g_warning ("you should not copy foobar");
  return NULL;
}

void
py_foobar_free (gpointer foo)
{
  g_foobar_destroy (foo);
}

GType py_foobar_get_type (void)
{
  static GType type;
  if (G_UNLIKELY (!type))
    type = g_boxed_type_register_static ("PyFoobar",
                                                       py_foobar_copy,
                                                       py_foobar_free);
  return type;
}

This way codegen should not complain anymore. (muntyan docet)

The other way is to manually define the python object for your struct,
we have done that in pygoocanvas (thanks to Gustavo), for an example:

http://svn.berlios.de/viewcvs/pygoocanvas/trunk/goocanvas.override?rev=198&view=markup

look at PyGooCanvasBounds_Type.

If you have quite a lot of methods to wrap for your naked struct the
first option should be the preferred since codegen will do a lot of
work for you through the defs.

I hope it helps and it's not too late.

cheers
-- 
Gian Mario Tagliaretti


More information about the pygtk mailing list