[pygobject] Add support for GFlags properties



commit c0922589964c1d8bffe5a56d2f56df96eedfac10
Author: Martin Pitt <martinpitt gnome org>
Date:   Wed Apr 4 19:08:54 2012 +0200

    Add support for GFlags properties
    
    https://bugzilla.gnome.org/show_bug.cgi?id=620943

 gi/_gobject/propertyhelper.py |    9 +++++++--
 tests/test_properties.py      |   28 +++++++++++++++++++++++++++-
 2 files changed, 34 insertions(+), 3 deletions(-)
---
diff --git a/gi/_gobject/propertyhelper.py b/gi/_gobject/propertyhelper.py
index da748b8..ceb6570 100644
--- a/gi/_gobject/propertyhelper.py
+++ b/gi/_gobject/propertyhelper.py
@@ -26,7 +26,7 @@ from . import _gobject
 from .constants import \
      TYPE_NONE, TYPE_INTERFACE, TYPE_CHAR, TYPE_UCHAR, \
      TYPE_BOOLEAN, TYPE_INT, TYPE_UINT, TYPE_LONG, \
-     TYPE_ULONG, TYPE_INT64, TYPE_UINT64, TYPE_ENUM, \
+     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
@@ -218,6 +218,7 @@ class Property(object):
         elif (isinstance(type_, type) and
               issubclass(type_, (_gobject.GObject,
                                  _gobject.GEnum,
+                                 _gobject.GFlags,
                                  _gobject.GBoxed))):
             return type_.__gtype__
         elif type_ in [TYPE_NONE, TYPE_INTERFACE, TYPE_CHAR, TYPE_UCHAR,
@@ -260,6 +261,10 @@ class Property(object):
             elif not _gobject.type_is_a(default, ptype):
                 raise TypeError("enum value %s must be an instance of %r" %
                                 (default, ptype))
+        elif _gobject.type_is_a(ptype, TYPE_FLAGS):
+            if not _gobject.type_is_a(default, ptype):
+                raise TypeError("flags value %s must be an instance of %r" %
+                                (default, ptype))
 
     def _get_minimum(self):
         ptype = self.type
@@ -328,7 +333,7 @@ class Property(object):
                      TYPE_INT64, TYPE_UINT64, TYPE_FLOAT, TYPE_DOUBLE]:
             args = self.minimum, self.maximum, self.default
         elif (ptype == TYPE_STRING or ptype == TYPE_BOOLEAN or
-              ptype.is_a(TYPE_ENUM)):
+              ptype.is_a(TYPE_ENUM) or ptype.is_a(TYPE_FLAGS)):
             args = (self.default,)
         elif ptype == TYPE_PYOBJECT:
             args = ()
diff --git a/tests/test_properties.py b/tests/test_properties.py
index 01ec20d..fc2a497 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -16,6 +16,7 @@ from gi.repository.GObject import \
 
 from gi.repository import Gio
 from gi.repository import GLib
+from gi.repository import GIMarshallingTests
 
 if sys.version_info < (3, 0):
     TEST_UTF8 = "\xe2\x99\xa5"
@@ -44,6 +45,10 @@ class PropertyObject(GObject.GObject):
     boxed = GObject.Property(
         type=GLib.Regex, flags=PARAM_READWRITE | PARAM_CONSTRUCT)
 
+    flags = GObject.Property(
+        type=GIMarshallingTests.Flags, flags=PARAM_READWRITE | PARAM_CONSTRUCT,
+        default=GIMarshallingTests.Flags.VALUE1)
+
 
 class TestProperties(unittest.TestCase):
     def testGetSet(self):
@@ -74,8 +79,9 @@ class TestProperties(unittest.TestCase):
                                                'construct-only',
                                                'uint64',
                                                'enum',
+                                               'flags',
                                                'boxed'])
-            self.assertEqual(len(obj), 6)
+            self.assertEqual(len(obj), 7)
 
     def testNormal(self):
         obj = new(PropertyObject, normal="123")
@@ -168,6 +174,26 @@ class TestProperties(unittest.TestCase):
         self.assertRaises(TypeError, GObject.Property, type=Gio.SocketType,
                           default=1)
 
+    def testFlags(self):
+        obj = new(PropertyObject)
+        self.assertEqual(obj.props.flags, GIMarshallingTests.Flags.VALUE1)
+        self.assertEqual(obj.flags, GIMarshallingTests.Flags.VALUE1)
+
+        obj.flags = GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE3
+        self.assertEqual(obj.props.flags, GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE3)
+        self.assertEqual(obj.flags, GIMarshallingTests.Flags.VALUE2 | GIMarshallingTests.Flags.VALUE3)
+
+        self.assertRaises(TypeError, setattr, obj, 'flags', 'foo')
+        self.assertRaises(TypeError, setattr, obj, 'flags', object())
+        self.assertRaises(TypeError, setattr, obj, 'flags', None)
+
+        self.assertRaises(TypeError, GObject.Property,
+                type=GIMarshallingTests.Flags, default='foo')
+        self.assertRaises(TypeError, GObject.Property,
+                type=GIMarshallingTests.Flags, default=object())
+        self.assertRaises(TypeError, GObject.Property,
+                type=GIMarshallingTests.Flags, default=None)
+
     def textBoxed(self):
         obj = new(PropertyObject)
 



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