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 678401 - unable to set utf8 fields in structs
unable to set utf8 fields in structs
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: gobject
3.3.x
Other Linux
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2012-06-19 12:37 UTC by Benny Malengier
Modified: 2013-01-11 14:45 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Proposed patch (980 bytes, patch)
2012-12-14 10:12 UTC, Vadim Rutkovsky
committed Details | Review
g-i: patch for utf8 field in boxed struct (1.31 KB, patch)
2013-01-11 12:00 UTC, Vadim Rutkovsky
none Details | Review
Tests for utf8 set/get in structs (3.24 KB, patch)
2013-01-11 12:01 UTC, Vadim Rutkovsky
none Details | Review
gimarshallingtests: Add string_ to boxed structure (2.53 KB, patch)
2013-01-11 14:38 UTC, Martin Pitt
committed Details | Review

Description Benny Malengier 2012-06-19 12:37:04 UTC
I need to set StockItem and WindowAttr. These are struct, and contain gchar fields. The gchar fields I cannot set. Eg:

http://developer.gnome.org/gdk3/stable/gdk3-Windows.html#GdkWindowAttr

>>> a = Gdk.WindowAttr()
>>> a.x = 3
>>> a.title = 'a'
RuntimeError: unable to set value for field

For stockitem: http://developer.gnome.org/gtk3/stable/gtk3-Stock-Items.html#GtkStockItem

So here it is not possible to call Gtk.stock_add. In pygtk this worked, as pygtk converted a list to stockitem in the wrapping layer

Is there a workaround? Can this be made to work?
Closest via google: https://mail.gnome.org/archives/gtk-list/2011-October/msg00049.html
Comment 1 Martin Pitt 2012-06-25 10:35:13 UTC
title is not a gchar, it's utf8 (in C terms, a gchar*). Indeed it is not currently possible to set pointer values in structs.
Comment 2 Benny Malengier 2012-06-25 11:33:10 UTC
Would it not be possible to write a C function with arguments struct, string of the attribute name, and string value, and add that to the wrapping layer?

Then we could do in python something like

wattr = Gdk.WindowAttr()
GObject.set_struct(wattr, 'title', mytitle)

So a workaround of some kind? Alternative is that I add some C to my python application.
Don't know if in C doing passing a string 'title' can be converted to the attribute name x.title like you can in python. 
Of course, stupid way with 
if name == 'title':
  attr.title = mytitle
would be possible also in C
Comment 3 Martin Pitt 2012-06-25 13:23:11 UTC
It would certainly be possible to add a constructor for this struct GTK, e. g. gdk_window_attr_new() which takes all the struct elements as arguments and g_strdup()'s them accordingly. However, that's only a workaround.
Comment 4 Vadim Rutkovsky 2012-12-14 10:12:00 UTC
Created attachment 231559 [details] [review]
Proposed patch

Here is a working version for this. 

Not sure if this is an optimal route - as other pointer types can be used, but this patch solves this particular problem
Comment 5 Vadim Rutkovsky 2013-01-11 12:00:41 UTC
Created attachment 233220 [details] [review]
g-i: patch for utf8 field in boxed struct
Comment 6 Vadim Rutkovsky 2013-01-11 12:01:12 UTC
Created attachment 233221 [details] [review]
Tests for utf8 set/get in structs
Comment 7 Martin Pitt 2013-01-11 14:38:32 UTC
Created attachment 233229 [details] [review]
gimarshallingtests: Add string_ to boxed structure

Updated g-i test:
 - Fix data type to "gchar*"; gchar** is a string array.
 - Handle the string_ field in the boxing _copy() and _free()
 - Set the string_ field in gi_marshalling_tests_boxed_struct_returnv(), to also test a pure C → Python direction

I verified that this does not break the gjs and pygobject testsuite.

Thanks!
Comment 8 Martin Pitt 2013-01-11 14:45:00 UTC
Comment on attachment 231559 [details] [review]
Proposed patch

Committed with a slight parentheses rearrangement to fix the compiler warning:

http://git.gnome.org/browse/pygobject/commit/?id=58bd307c57d542a8f69867dea2d0a0eb51230c7b

This also includes test cases, which I based on your original test case patch.

Thank you!