GNOME Bugzilla – Bug 679939
GObject properties won't accept a Python3 int when bound to GSettings schema
Last modified: 2012-07-24 13:15:49 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.
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.
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.
I haven't tested your patch, but the workaround you mentioned is working beautifully. Thanks!
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.
Oh, that looks excellent! Thanks so much for this work!
Thank you! Applied with some PEP-8 whitespace fixes.