[pygobject] Fix bool() operations on GLib.Variant objects
- From: Sebastian Polsterl <sebp src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Fix bool() operations on GLib.Variant objects
- Date: Sat, 4 Feb 2012 14:59:57 +0000 (UTC)
commit 534ec71c575a279ff1c05da20a8858bb1145b4d0
Author: Nirbheek Chauhan <nirbheek gentoo org>
Date: Sat Feb 4 15:42:36 2012 +0100
Fix bool() operations on GLib.Variant objects
Defines __nonzero__ (python2) and __bool__ (python3) for GLib.Variant
Also adds some tests for boolean comparisons.
https://bugzilla.gnome.org/show_bug.cgi?id=647723
gi/overrides/GLib.py | 18 ++++++++++++++++++
tests/test_overrides.py | 45 +++++++++++++++++++++++++++++++++++++++++++++
2 files changed, 63 insertions(+), 0 deletions(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index a8f8e34..27fe017 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -172,6 +172,24 @@ class Variant(GLib.Variant):
def __repr__(self):
return '<GLib.Variant(%s)>' % getattr(self, 'print')(True)
+ def __eq__(self, other):
+ try:
+ return self.equal(other)
+ except TypeError:
+ return False
+
+ def __ne__(self, other):
+ try:
+ return not self.equal(other)
+ except TypeError:
+ return True
+
+ def __hash__(self):
+ # We're not using just hash(self.unpack()) because otherwise we'll have
+ # hash collisions between the same content in different variant types,
+ # which will cause a performance issue in set/dict/etc.
+ return hash((self.get_type_string(), self.unpack()))
+
def unpack(self):
'''Decompose a GVariant into a native Python object.'''
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 5a4d8fd..bcb11b1 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -348,6 +348,51 @@ class TestGLib(unittest.TestCase):
self.assertEqual(GLib.Variant.split_signature('(a{iv}(ii)((ss)a{s(ss)}))'),
['a{iv}', '(ii)', '((ss)a{s(ss)})'])
+ def test_variant_hash(self):
+ v1 = GLib.Variant('s', 'somestring')
+ v2 = GLib.Variant('s', 'somestring')
+ v3 = GLib.Variant('s', 'somestring2')
+
+ self.assertTrue(v2 in set([v1, v3]))
+ self.assertTrue(v2 in frozenset([v1, v3]))
+ self.assertTrue(v2 in {v1: '1', v3:'2' })
+
+ def test_variant_compare(self):
+ # Check if identical GVariant are equal
+
+ def assert_equal(vtype, value):
+ self.assertEqual(GLib.Variant(vtype, value), GLib.Variant(vtype, value))
+
+ def assert_not_equal(vtype1, value1, vtype2, value2):
+ self.assertNotEqual(GLib.Variant(vtype1, value1), GLib.Variant(vtype2, value2))
+
+ numbers = ['y', 'n', 'q', 'i', 'u', 'x', 't', 'h', 'd']
+ for num in numbers:
+ assert_equal(num, 42)
+ assert_not_equal(num, 42, num, 41)
+ assert_not_equal(num, 42, 's', '42')
+
+ assert_equal('s', 'something')
+ assert_not_equal('s', 'something', 's', 'somethingelse')
+ assert_not_equal('s', 'something', 'i', 1234)
+
+ assert_equal('g', 'dustybinqhogx')
+ assert_not_equal('g', 'dustybinqhogx', 'g', 'dustybin')
+ assert_not_equal('g', 'dustybinqhogx', 'i', 1234)
+
+ assert_equal('o', '/dev/null')
+ assert_not_equal('o', '/dev/null', 'o', '/dev/zero')
+ assert_not_equal('o', '/dev/null', 'i', 1234)
+
+ assert_equal('(s)', ('strtuple',))
+ assert_not_equal('(s)', ('strtuple',), '(s)', ('strtuple2',))
+
+ assert_equal('a{si}', {'str': 42})
+ assert_not_equal('a{si}', {'str': 42}, 'a{si}', {'str': 43})
+
+ assert_equal('v', GLib.Variant('i', 42))
+ assert_not_equal('v', GLib.Variant('i', 42), 'v', GLib.Variant('i', 43))
+
def test_variant_bool(self):
# Check if the GVariant bool matches the unpacked Pythonic bool
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]