[pygobject] Support defining GStrv properties from Python



commit 8321af2c7df499291e664c676376f149a0c3dcac
Author: Martin Pitt <martinpitt gnome org>
Date:   Sat May 5 13:58:29 2012 -0700

    Support defining GStrv properties from Python

 gi/_gobject/propertyhelper.py |   10 +++++-
 tests/test_properties.py      |   58 +++++++++++++++++++++++++++++++++++++++-
 2 files changed, 64 insertions(+), 4 deletions(-)
---
diff --git a/gi/_gobject/propertyhelper.py b/gi/_gobject/propertyhelper.py
index dff1d79..673b364 100644
--- a/gi/_gobject/propertyhelper.py
+++ b/gi/_gobject/propertyhelper.py
@@ -29,7 +29,7 @@ from .constants import \
      TYPE_ULONG, TYPE_INT64, TYPE_UINT64, TYPE_ENUM, TYPE_FLAGS, \
      TYPE_FLOAT, TYPE_DOUBLE, TYPE_STRING, \
      TYPE_POINTER, TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, \
-     TYPE_PYOBJECT, TYPE_GTYPE
+     TYPE_PYOBJECT, TYPE_GTYPE, TYPE_STRV
 from .constants import \
      G_MAXFLOAT, G_MAXDOUBLE, \
      G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
@@ -226,7 +226,7 @@ class Property(object):
                        TYPE_ULONG, TYPE_INT64, TYPE_UINT64,
                        TYPE_FLOAT, TYPE_DOUBLE, TYPE_POINTER,
                        TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, TYPE_STRING,
-                       TYPE_PYOBJECT, TYPE_GTYPE]:
+                       TYPE_PYOBJECT, TYPE_GTYPE, TYPE_STRV]:
             return type_
         else:
             raise TypeError("Unsupported type: %r" % (type_,))
@@ -268,6 +268,12 @@ class Property(object):
             if not _gobject.type_is_a(default, ptype):
                 raise TypeError("flags value %s must be an instance of %r" %
                                 (default, ptype))
+        elif _gobject.type_is_a(ptype, TYPE_STRV) and default is not None:
+            if not isinstance(default, list):
+                raise TypeError("Strv value %s must be a list" % repr(default))
+            for val in default:
+                if type(val) not in (str, bytes):
+                    raise TypeError("Strv value %s must contain only strings" % str(default))
 
     def _get_minimum(self):
         ptype = self.type
diff --git a/tests/test_properties.py b/tests/test_properties.py
index 5ae2b47..6df7f4d 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -9,7 +9,7 @@ from  gi.repository.GObject import GType, new, PARAM_READWRITE, \
      PARAM_CONSTRUCT, PARAM_READABLE, PARAM_WRITABLE, PARAM_CONSTRUCT_ONLY
 from gi.repository.GObject import \
      TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG, TYPE_INT64, \
-     TYPE_UINT64, TYPE_GTYPE, TYPE_INVALID, TYPE_NONE
+     TYPE_UINT64, TYPE_GTYPE, TYPE_INVALID, TYPE_NONE, TYPE_STRV
 from gi.repository.GObject import \
      G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
      G_MAXULONG
@@ -52,6 +52,9 @@ class PropertyObject(GObject.GObject):
     gtype = GObject.Property(
         type=TYPE_GTYPE, flags=PARAM_READWRITE | PARAM_CONSTRUCT)
 
+    strings = GObject.Property(
+        type=TYPE_STRV, flags=PARAM_READWRITE | PARAM_CONSTRUCT)
+
 
 class TestProperties(unittest.TestCase):
     def testGetSet(self):
@@ -84,8 +87,9 @@ class TestProperties(unittest.TestCase):
                                                'enum',
                                                'flags',
                                                'gtype',
+                                               'strings',
                                                'boxed'])
-            self.assertEqual(len(obj), 8)
+            self.assertEqual(len(obj), 9)
 
     def testNormal(self):
         obj = new(PropertyObject, normal="123")
@@ -241,6 +245,56 @@ class TestProperties(unittest.TestCase):
         self.assertRaises(TypeError, setattr, obj, 'boxed', 'foo')
         self.assertRaises(TypeError, setattr, obj, 'boxed', object())
 
+    def testStrings(self):
+        obj = new(PropertyObject)
+
+        # Should work with actual GStrv objects as well as
+        # Python string lists
+        class GStrv(list):
+            __gtype__ = GObject.TYPE_STRV
+
+        self.assertEqual(obj.props.strings, GStrv([]))
+        self.assertEqual(obj.strings, GStrv([]))
+        self.assertEqual(obj.props.strings, [])
+        self.assertEqual(obj.strings, [])
+
+        obj.strings = ['hello', 'world']
+        self.assertEqual(obj.props.strings, ['hello', 'world'])
+        self.assertEqual(obj.strings, ['hello', 'world'])
+
+        obj.strings = GStrv(['hello', 'world'])
+        self.assertEqual(obj.props.strings, GStrv(['hello', 'world']))
+        self.assertEqual(obj.strings, GStrv(['hello', 'world']))
+
+        obj.strings = []
+        self.assertEqual(obj.strings, [])
+        obj.strings = GStrv([])
+        self.assertEqual(obj.strings, GStrv([]))
+
+        p = GObject.Property(type=TYPE_STRV, default=['hello', '1'])
+        self.assertEqual(p.default, ['hello', '1'])
+        self.assertEqual(p.type, TYPE_STRV)
+        p = GObject.Property(type=TYPE_STRV, default=GStrv(['hello', '1']))
+        self.assertEqual(p.default, ['hello', '1'])
+        self.assertEqual(p.type, TYPE_STRV)
+
+        # set in constructor
+        obj = new(PropertyObject, strings=['hello', 'world'])
+        self.assertEqual(obj.props.strings, ['hello', 'world'])
+        self.assertEqual(obj.strings, ['hello', 'world'])
+
+        # wrong types
+        self.assertRaises(TypeError, setattr, obj, 'strings', 1)
+        self.assertRaises(TypeError, setattr, obj, 'strings', 'foo')
+        self.assertRaises(TypeError, setattr, obj, 'strings', ['foo', 1])
+
+        self.assertRaises(TypeError, GObject.Property, type=TYPE_STRV,
+                default=1)
+        self.assertRaises(TypeError, GObject.Property, type=TYPE_STRV,
+                default='foo')
+        self.assertRaises(TypeError, GObject.Property, type=TYPE_STRV,
+                default=['hello', 1])
+
     def testRange(self):
         # kiwi code
         def max(c):



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