Commit e97e2804 authored by Martin Pitt's avatar Martin Pitt

[gi] Add Variant construction/unpack support for boxed Variants

Construction uses a GVariantBuilder for now, as the new_variant() constructor
currently does not work (see https://bugzilla.gnome.org/show_bug.cgi?id=639952)
parent 71dd0326
......@@ -25,6 +25,17 @@ GLib = modules['GLib']._introspection_module
__all__ = []
def _create_variant(value):
'''Create a variant containing the variant "value".
This is usually done with the GLib.Variant.new_variant() leaf
constructor, but this is currently broken, see GNOME#639952.
'''
builder = GLib.VariantBuilder()
builder.init(variant_type_from_string('v'))
builder.add_value(value)
return builder.end()
class _VariantCreator(object):
_LEAF_CONSTRUCTORS = {
......@@ -41,7 +52,8 @@ class _VariantCreator(object):
's': GLib.Variant.new_string,
'o': GLib.Variant.new_object_path,
'g': GLib.Variant.new_signature,
'v': GLib.Variant.new_variant,
#'v': GLib.Variant.new_variant,
'v': _create_variant,
}
def _create(self, format, args):
......@@ -211,6 +223,10 @@ class Variant(GLib.Variant):
return [self.get_child_value(i).unpack()
for i in xrange(self.n_children())]
# variant (just unbox transparently)
if self.get_type_string().startswith('v'):
return self.get_variant().unpack()
raise NotImplementedError('unsupported GVariant type ' + self.get_type_string())
#
......
......@@ -32,6 +32,20 @@ class TestGLib(unittest.TestCase):
self.assertTrue(isinstance(variant, GLib.Variant))
self.assertEquals(variant.get_string(), 'hello')
# boxed variant
variant = GLib.Variant('v', GLib.Variant('i', 42))
self.assertTrue(isinstance(variant, GLib.Variant))
self.assertTrue(isinstance(variant.get_variant(), GLib.Variant))
self.assertEqual(variant.get_type_string(), 'v')
self.assertEqual(variant.get_variant().get_type_string(), 'i')
self.assertEquals(variant.get_variant().get_int32(), 42)
variant = GLib.Variant('v', GLib.Variant('v', GLib.Variant('i', 42)))
self.assertEqual(variant.get_type_string(), 'v')
self.assertEqual(variant.get_variant().get_type_string(), 'v')
self.assertEqual(variant.get_variant().get_variant().get_type_string(), 'i')
self.assertEquals(variant.get_variant().get_variant().get_int32(), 42)
# tuples
variant = GLib.Variant('()')
......@@ -166,6 +180,11 @@ class TestGLib(unittest.TestCase):
self.assertEqual(variant.get_type_string(), 'a{s(ib)}')
self.assertEqual(variant.unpack(), obj)
obj = {'a1': (1, GLib.Variant('b', True)), 'a2': (2, GLib.Variant('y', 255))}
variant = GLib.Variant('a{s(iv)}', obj)
self.assertEqual(variant.get_type_string(), 'a{s(iv)}')
self.assertEqual(variant.unpack(), {'a1': (1, True), 'a2': (2, 255)})
obj = (1, {'a': {'a1': True, 'a2': False},
'b': {'b1': False},
'c': {}
......@@ -203,6 +222,13 @@ class TestGLib(unittest.TestCase):
res = GLib.Variant.new_object_path('/foo/Bar').unpack()
self.assertEqual(res, '/foo/Bar')
# variant
res = GLib.Variant('v', GLib.Variant.new_int32(-42)).unpack()
self.assertEqual(res, -42)
variant = GLib.Variant('v', GLib.Variant('v', GLib.Variant('i', 42)))
self.assertEqual(res, -42)
# tuple
res = GLib.Variant.new_tuple(GLib.Variant.new_int32(-1),
GLib.Variant.new_string('hello')).unpack()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment