After an evaluation, GNOME has moved from Bugzilla to GitLab. Learn more about GitLab.
No new issues can be reported in GNOME Bugzilla anymore.
To report an issue in a GNOME project, go to GNOME GitLab.
Do not go to GNOME Gitlab for: Bluefish, Doxygen, GnuCash, GStreamer, java-gnome, LDTP, NetworkManager, Tomboy.
Bug 677473 - Add override for GValue
Add override for GValue
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: introspection
unspecified
Other All
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2012-06-05 14:49 UTC by Bastian Winkler
Modified: 2013-01-14 09:45 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Add override for GValue (9.62 KB, patch)
2012-06-05 14:49 UTC, Bastian Winkler
committed Details | Review

Description Bastian Winkler 2012-06-05 14:49:38 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
Comment 1 Bastian Winkler 2012-06-05 14:49:40 UTC
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
Comment 2 Martin Pitt 2012-06-05 16:50:15 UTC
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!
Comment 3 Bastian Winkler 2012-06-05 17:26:34 UTC
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 4 Martin Pitt 2013-01-14 09:44:51 UTC
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
Comment 5 Martin Pitt 2013-01-14 09:45:39 UTC
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