[pygobject] GLib overrides: Support unpacking 'maybe' variants



commit 3a6a4a7a21a0f5e851518b7912d8ff455aa3ede4
Author: Paolo Borelli <pborelli gnome org>
Date:   Fri Feb 1 21:09:26 2013 +0100

    GLib overrides: Support unpacking 'maybe' variants
    
    Automatically unpack 'maybe' variants to None or to their actual value
    as we do for other kind of variants
    
    https://bugzilla.gnome.org/show_bug.cgi?id=693032

 gi/overrides/GLib.py         |    5 +++++
 tests/test_overrides_glib.py |    8 ++++++++
 2 files changed, 13 insertions(+), 0 deletions(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index 6f116a3..b3f1563 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -292,6 +292,11 @@ class Variant(GLib.Variant):
         if self.get_type_string().startswith('v'):
             return self.get_variant().unpack()
 
+        # maybe
+        if self.get_type_string().startswith('m'):
+            m = self.get_maybe()
+            return m.unpack() if m else None
+
         raise NotImplementedError('unsupported GVariant type ' + self.get_type_string())
 
     @classmethod
diff --git a/tests/test_overrides_glib.py b/tests/test_overrides_glib.py
index 6bad3e0..4d7e63a 100644
--- a/tests/test_overrides_glib.py
+++ b/tests/test_overrides_glib.py
@@ -272,6 +272,14 @@ class TestGVariant(unittest.TestCase):
         res = GLib.Variant('a{si}', {'key1': 1, 'key2': 2}).unpack()
         self.assertEqual(res, {'key1': 1, 'key2': 2})
 
+        # maybe
+        v = GLib.Variant.new_maybe(GLib.VariantType.new('i'), GLib.Variant('i', 1))
+        res = v.unpack()
+        self.assertEqual(res, 1)
+        v = GLib.Variant.new_maybe(GLib.VariantType.new('i'), None)
+        res = v.unpack()
+        self.assertEqual(res, None)
+
     def test_iteration(self):
         # array index access
         vb = GLib.VariantBuilder.new(gi._gi.variant_type_from_string('ai'))



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]