GNOME Bugzilla – Bug 678401
unable to set utf8 fields in structs
Last modified: 2013-01-11 14:45:24 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
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.
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
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.
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
Created attachment 233220 [details] [review] g-i: patch for utf8 field in boxed struct
Created attachment 233221 [details] [review] Tests for utf8 set/get in structs
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 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!