[pygobject] GLib.Variant: Fix creation of guchar array from bytes. Fixes #174



commit e1c00daff8c75f0a543c841d5a749b698f051a6f
Author: Christoph Reiter <reiter christoph gmail com>
Date:   Sat Mar 10 10:41:27 2018 +0100

    GLib.Variant: Fix creation of guchar array from bytes. Fixes #174
    
    This is some fallout from !4
    
    Instead of only allowing certain types just make sure that the passed
    value is iterable.

 gi/overrides/GLib.py         |  4 +++-
 tests/test_overrides_glib.py | 16 ++++++++++++++++
 2 files changed, 19 insertions(+), 1 deletion(-)
---
diff --git a/gi/overrides/GLib.py b/gi/overrides/GLib.py
index b1ff24f7..f00c7793 100644
--- a/gi/overrides/GLib.py
+++ b/gi/overrides/GLib.py
@@ -126,7 +126,9 @@ class _VariantCreator(object):
             builder.add_value(self._create(gvtype.element().dup_string(), value))
             return builder.end()
 
-        if not isinstance(value, (dict, tuple, list)):
+        try:
+            iter(value)
+        except TypeError:
             raise TypeError("Could not create array, tuple or dictionary entry from non iterable value %s 
%s" %
                             (format, value))
 
diff --git a/tests/test_overrides_glib.py b/tests/test_overrides_glib.py
index 6dcfd586..9130334f 100644
--- a/tests/test_overrides_glib.py
+++ b/tests/test_overrides_glib.py
@@ -142,6 +142,22 @@ class TestGVariant(unittest.TestCase):
         self.assertEqual(variant.get_type_string(), 'aai')
         self.assertEqual(variant.unpack(), [[1, 2], [3, 4, 5]])
 
+    def test_create_array_guchar(self):
+        variant = GLib.Variant('ay', [97, 97, 97])
+        assert variant.unpack() == [97, 97, 97]
+
+        variant = GLib.Variant('ay', b'aaa')
+        assert variant.unpack() == [97, 97, 97]
+
+        variant = GLib.Variant('ay', iter([1, 2, 3]))
+        assert variant.unpack() == [1, 2, 3]
+
+        with self.assertRaises(TypeError):
+            GLib.Variant('ay', u'aaa')
+
+        with self.assertRaises(TypeError):
+            GLib.Variant('ay', object())
+
     def test_create_maybe(self):
         variant = GLib.Variant('mai', None)
         self.assertEqual(variant.get_type_string(), 'mai')


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