GNOME Bugzilla – Bug 677473
Add override for GValue
Last modified: 2013-01-14 09:45:39 UTC
An improved handling of GObject.Value would be very useful when you have to deal with GValue arguments. For example, in the Clutter override I need this a lot [1] so I have a custom _gvalue_from_python() for this. Other libraries may need this too. So instead of having code duplication in every override file, the conversion code should live in GObject.Value [1] http://git.gnome.org/browse/pyclutter/tree/introspection/Clutter.py?h=wip/introspection#n1161
Created attachment 215647 [details] [review] Add override for GValue Override GValue with a custom constructor and set_value() get_value() functions. This allows calling >>> GObject.Value(GObject.TYPE_FLOAT, 42.23) instead of >>> value = GObject.Value() >>> value.init(GObject.TYPE_FLOAT) >>> value.set_float(42.23) This is especially useful for overrides that need to convert a python value to a expected type like G_TYPE_FLOAT
I am a bit hesitant about that. PyGObject is already supposed to transparently convert Python values to GValue and back. For example, for GValue* arguments: >>> from gi.repository import GLib, Gio >>> Gio.dbus_gvalue_to_gvariant('foo', GLib.VariantType.new('s')) <GLib.Variant('foo')> >>> Gio.dbus_gvalue_to_gvariant(3, GLib.VariantType.new('i')) <GLib.Variant(3)> >>> Gio.dbus_gvalue_to_gvariant(3, GLib.VariantType.new('s')) /usr/lib/python3/dist-packages/gi/types.py:47: Warning: g_value_get_string: assertion `G_VALUE_HOLDS_STRING (value)' failed return info.invoke(*args, **kwargs) <GLib.Variant('')> And for GValue out arguments: >>> Gio.dbus_gvariant_to_gvalue(GLib.Variant('i', 3)) 3 >>> type(Gio.dbus_gvariant_to_gvalue(GLib.Variant('i', 3))) <class 'int'> >>> Gio.dbus_gvariant_to_gvalue(GLib.Variant('s', 'hello')) 'hello' >>> type(Gio.dbus_gvariant_to_gvalue(GLib.Variant('s', 'hello'))) <class 'str'> That seems to work fine. Do you have a particular case where Python objects are not transparently converted? I'd much rather fix that instead. Thanks!
One example would be this one: >>> from gi.repository import GObject >>> from gi.repository import Clutter >>> >>> Clutter.init([]) >>> >>> model = Clutter.ListModel() >>> model.set_types([GObject.TYPE_DOUBLE, GObject.TYPE_FLOAT]) >>> model.set_names(['double-column', 'float-column']) >>> >>> model.insert_value(0, 0, 123.4) # (row, column, value) >>> model.insert_value(0, 1, 123.4) The first insert_value() call works, the second doesn't because pygobject converts a python 'float' type to gdouble. (python:17972): Clutter-WARNING **: ./clutter-list-model.c:188: Unable to convert from gint to gfloat So, to call insert_value() with the correct type you have to use a GValue directly: >>> value = GObject.Value() >>> value.init(GObject.TYPE_FLOAT) >>> value.set_float(123.4) >>> model.insert_value(0, 1, value) And that's why Gtk.TreeModel does a conversion here: http://git.gnome.org/browse/pygobject/tree/gi/overrides/Gtk.py#n863 and pyclutter here: http://git.gnome.org/browse/pyclutter/tree/introspection/Clutter.py?h=wip/introspection#n47 Having this in GObject.Value would would help to get rid of custom conversion functions...
Comment on attachment 215647 [details] [review] Add override for GValue Thanks for pointing this out! I updated your patch for current master and fixed some fallout: http://git.gnome.org/browse/pygobject/commit/?id=f62b98398177991bfdbe0b6753342e79e6cf170a
This indeed simplifies the Gtk overrides considerably, so it is worth having this generic support in the GObject overrides: http://git.gnome.org/browse/pygobject/commit/?id=9cfba517e1a6dced5e66786b28ed5e101b7b4a29