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 679939 - GObject properties won't accept a Python3 int when bound to GSettings schema
GObject properties won't accept a Python3 int when bound to GSettings schema
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: introspection
3.2.x
Other Linux
: Normal blocker
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2012-07-14 19:47 UTC by Robert Bruce Park
Modified: 2012-07-24 13:15 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Fixed property type mapping from int to TYPE_INT for python3 (6.05 KB, patch)
2012-07-21 04:42 UTC, Simon Feltman
none Details | Review
Addition re-factoring (11.34 KB, patch)
2012-07-22 06:53 UTC, Simon Feltman
none Details | Review

Description Robert Bruce Park 2012-07-14 19:47:07 UTC
I have the following Python code, which worked fine in Python2, but is failing in Python3:


    class Camera(GObject.GObject):
        offset = GObject.property(type=int, minimum=-3600, maximum=3600)
        utc_offset = GObject.property(type=int, minimum=-24, maximum=24)


Here is the associated GSettings schema that I am trying to bind it to:


      <schema id="ca.exolucere.gottengeography.camera">
        <key type="x" name="offset">
          <range min="-3600" max="3600"/>
          <default>0</default>
        </key>
        <key type="x" name="utc-offset">
          <range min="-24" max="24"/>
          <default>0</default>
        </key>
      </schema>


And this is the error message that I get:


(gottengeography:18327): GLib-GIO-CRITICAL **: g_settings_bind: property 'offset' on class 'gg+camera+Camera' has type 'glong' which is not compatible with type 'x' of key 'offset' on schema 'ca.exolucere.gottengeography.camera'

(gottengeography:18327): GLib-GIO-CRITICAL **: g_settings_bind: property 'utc-offset' on class 'gg+camera+Camera' has type 'glong' which is not compatible with type 'x' of key 'utc-offset' on schema 'ca.exolucere.gottengeography.camera'


Originally I was using type 'i' in Python2, but that was giving the same error message, so I tried 'x' (among others) to see if anything would work, but nothing does. 

Other properties (like strings and floats) are working just fine, it's only ints that are the problem. I realize that Python3 has eliminated the concept of 'int' and replaced it with only 'long' type (but called 'int' still...), which is why it's using C type 'glong' internally, but there needs to be some way for me to have a GObject property that's an int, so I'm not sure what the correct solution to this is.
Comment 1 Simon Feltman 2012-07-21 03:55:31 UTC
It looks to be a potentially fixable problem in gi/_gobject/propertyhelper.py.
This defines _long as int for python3, conversion from py types to gobject type for long happens first which is why properties are being set up as glong in python3. This could be fixed by first checking for int type in "_type_from_python" instead of long.

You should be able to explicitly set your property types to something like "GObject.TYPE_INT" and continue using "i" as a workaround.
Comment 2 Simon Feltman 2012-07-21 04:42:04 UTC
Created attachment 219367 [details] [review]
Fixed property type mapping from int to TYPE_INT for python3

Python3 does not have a long type, however, propertyhelper.py was using long_ = int; to get things working. Type mapping code was then checking for long_ first and always returning TYPE_LONG.

This is needed because GSettings uses GVariant which doesn't seem to support glong types. Causing settings/object bindings to error.
Comment 3 Robert Bruce Park 2012-07-21 16:36:02 UTC
I haven't tested your patch, but the workaround you mentioned is working beautifully. Thanks!
Comment 4 Simon Feltman 2012-07-22 06:53:18 UTC
Created attachment 219407 [details] [review]
Addition re-factoring

Latest patch with additional re-factoring which moves large if/elif statements into dictionary lookups and usage of tuples instead of lists for simple 'in' list of items tests.
Comment 5 Robert Bruce Park 2012-07-22 07:24:24 UTC
Oh, that looks excellent! Thanks so much for this work!
Comment 6 Martin Pitt 2012-07-24 13:15:49 UTC
Thank you! Applied with some PEP-8 whitespace fixes.