Commit c0922589 authored by Martin Pitt's avatar Martin Pitt
Browse files

Add support for GFlags properties

https://bugzilla.gnome.org/show_bug.cgi?id=620943
parent d4054be9
......@@ -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 = ()
......
......@@ -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)
......
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