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 647725 - hash() and cmp() on GLib.Variant objects are broken
hash() and cmp() on GLib.Variant objects are broken
Status: RESOLVED FIXED
Product: pygobject
Classification: Bindings
Component: general
2.28.x
Other Linux
: Normal normal
: ---
Assigned To: Nobody's working on this now (help wanted and appreciated)
Python bindings maintainers
Depends on:
Blocks:
 
 
Reported: 2011-04-13 22:23 UTC by Nirbheek Chauhan
Modified: 2012-02-04 14:45 UTC
See Also:
GNOME target: ---
GNOME version: ---


Attachments
Implement __hash__, __eq__, __ne__ (3.73 KB, patch)
2012-01-28 07:58 UTC, Nirbheek Chauhan
committed Details | Review

Description Nirbheek Chauhan 2011-04-13 22:23:17 UTC
GLib.Variant.__hash__ does not return the same number for identical data

>>> a = GLib.Variant("(b)", (False,))
>>> b = GLib.Variant("(b)", (False,))
>>> hash(a)
38947056
>>> hash(b)
38947344
>>> a == b
False

This behaviour is completely wrong, as it breaks dicts, sets and other datastructures because __eq__ now always returns False.

In addition, this also breaks cmp() which relies on __hash__ for comparisons when __cmp__ is not defined.

>>> a = GLib.Variant("i", 1)
>>> b = GLib.Variant("i", 1)
>>> a > b
False
>>> a < b
True
>>> a == b
False
>>> hash(a)
20014848
>>> hash(b)
20014944
Comment 1 Nirbheek Chauhan 2012-01-28 07:58:03 UTC
Created attachment 206311 [details] [review]
Implement __hash__, __eq__, __ne__

This allows Variant types to be used in comparisons (==, !=) and in sets/frozensets/dicts.

This patch needs the patch from bug 647723 before it can be applied cleanly.
Comment 2 Sebastian Pölsterl 2012-01-28 10:42:10 UTC
Review of attachment 206311 [details] [review]:

Thanks for your work, looks good.
Comment 3 Sebastian Pölsterl 2012-02-04 14:45:14 UTC
This problem has been fixed in the development version. The fix will be available in the next major software release. Thank you for your bug report.