[pygobject] Add support for enums in gobject.property
- From: John Palmieri <johnp src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Add support for enums in gobject.property
- Date: Mon, 27 Jun 2011 16:37:03 +0000 (UTC)
commit 2aa12267bee91aa696633a0cea2a0accae09250a
Author: Johan Dahlin <jdahlin litl com>
Date: Mon Jun 27 10:56:20 2011 -0300
Add support for enums in gobject.property
https://bugzilla.gnome.org/show_bug.cgi?id=653488
gobject/propertyhelper.py | 23 ++++++++++++++++-------
tests/test_properties.py | 40 ++++++++++++++++++++++++++++++++++++----
2 files changed, 52 insertions(+), 11 deletions(-)
---
diff --git a/gobject/propertyhelper.py b/gobject/propertyhelper.py
index 9643c82..b9b587a 100644
--- a/gobject/propertyhelper.py
+++ b/gobject/propertyhelper.py
@@ -188,14 +188,16 @@ class property(object):
return TYPE_STRING
elif type_ == object:
return TYPE_PYOBJECT
- elif isinstance(type_, type) and issubclass(type_, _gobject.GObject):
+ elif (isinstance(type_, type) and
+ issubclass(type_, (_gobject.GObject,
+ _gobject.GEnum))):
return type_.__gtype__
elif type_ in [TYPE_NONE, TYPE_INTERFACE, TYPE_CHAR, TYPE_UCHAR,
- TYPE_INT, TYPE_UINT, TYPE_BOOLEAN, TYPE_LONG,
- TYPE_ULONG, TYPE_INT64, TYPE_UINT64, TYPE_ENUM,
- TYPE_FLAGS, TYPE_FLOAT, TYPE_DOUBLE, TYPE_POINTER,
- TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, TYPE_STRING,
- TYPE_PYOBJECT]:
+ TYPE_INT, TYPE_UINT, TYPE_BOOLEAN, TYPE_LONG,
+ TYPE_ULONG, TYPE_INT64, TYPE_UINT64,
+ TYPE_FLOAT, TYPE_DOUBLE, TYPE_POINTER,
+ TYPE_BOXED, TYPE_PARAM, TYPE_OBJECT, TYPE_STRING,
+ TYPE_PYOBJECT]:
return type_
else:
raise TypeError("Unsupported type: %r" % (type_,))
@@ -224,6 +226,12 @@ class property(object):
elif ptype == TYPE_PYOBJECT:
if default is not None:
raise TypeError("object types does not have default values")
+ elif gobject.type_is_a(ptype, TYPE_ENUM):
+ if default is None:
+ raise TypeError("enum properties needs a default value")
+ elif not gobject.type_is_a(default, ptype):
+ raise TypeError("enum value %s must be an instance of %r" %
+ (default, ptype))
def _get_minimum(self):
ptype = self.type
@@ -291,7 +299,8 @@ class property(object):
if ptype in [TYPE_INT, TYPE_UINT, TYPE_LONG, TYPE_ULONG,
TYPE_INT64, TYPE_UINT64, TYPE_FLOAT, TYPE_DOUBLE]:
args = self._get_minimum(), self._get_maximum(), self.default
- elif ptype == TYPE_STRING or ptype == TYPE_BOOLEAN:
+ elif (ptype == TYPE_STRING or ptype == TYPE_BOOLEAN or
+ ptype.is_a(TYPE_ENUM)):
args = (self.default,)
elif ptype == TYPE_PYOBJECT:
args = ()
diff --git a/tests/test_properties.py b/tests/test_properties.py
index 54afd11..74c1b38 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -14,6 +14,8 @@ from gobject.constants import \
G_MININT, G_MAXINT, G_MAXUINT, G_MINLONG, G_MAXLONG, \
G_MAXULONG
+import gio
+
if sys.version_info < (3, 0):
TEST_UTF8 = "\xe2\x99\xa5"
UNICODE_UTF8 = unicode(TEST_UTF8, 'UTF-8')
@@ -34,6 +36,9 @@ class PropertyObject(GObject):
uint64 = gobject.property(
type=TYPE_UINT64, flags=PARAM_READWRITE|PARAM_CONSTRUCT)
+ enum = gobject.property(
+ type=gio.SocketType, default=gio.SOCKET_TYPE_STREAM)
+
class TestProperties(unittest.TestCase):
def testGetSet(self):
obj = PropertyObject()
@@ -61,8 +66,9 @@ class TestProperties(unittest.TestCase):
self.failUnless(pspec.name in ['normal',
'construct',
'construct-only',
- 'uint64'])
- self.assertEqual(len(obj), 4)
+ 'uint64',
+ 'enum'])
+ self.assertEqual(len(obj), 5)
def testNormal(self):
obj = new(PropertyObject, normal="123")
@@ -127,6 +133,34 @@ class TestProperties(unittest.TestCase):
(etype, ex) = sys.exc_info()[2:]
self.fail(str(ex))
+ def testEnum(self):
+ obj = new(PropertyObject)
+ self.assertEqual(obj.props.enum, gio.SOCKET_TYPE_STREAM)
+ self.assertEqual(obj.enum, gio.SOCKET_TYPE_STREAM)
+ obj.enum = gio.SOCKET_TYPE_DATAGRAM
+ self.assertEqual(obj.props.enum, gio.SOCKET_TYPE_DATAGRAM)
+ self.assertEqual(obj.enum, gio.SOCKET_TYPE_DATAGRAM)
+ obj.props.enum = gio.SOCKET_TYPE_STREAM
+ self.assertEqual(obj.props.enum, gio.SOCKET_TYPE_STREAM)
+ self.assertEqual(obj.enum, gio.SOCKET_TYPE_STREAM)
+ obj.props.enum = 2
+ self.assertEqual(obj.props.enum, gio.SOCKET_TYPE_DATAGRAM)
+ self.assertEqual(obj.enum, gio.SOCKET_TYPE_DATAGRAM)
+ obj.enum = 1
+ self.assertEqual(obj.props.enum, gio.SOCKET_TYPE_STREAM)
+ self.assertEqual(obj.enum, gio.SOCKET_TYPE_STREAM)
+
+ self.assertRaises(TypeError, setattr, obj, 'enum', 'foo')
+ self.assertRaises(TypeError, setattr, obj, 'enum', object())
+
+ self.assertRaises(TypeError, gobject.property, type=gio.SocketType)
+ self.assertRaises(TypeError, gobject.property, type=gio.SocketType,
+ default=gio.SOCKET_PROTOCOL_TCP)
+ self.assertRaises(TypeError, gobject.property, type=gio.SocketType,
+ default=object())
+ self.assertRaises(TypeError, gobject.property, type=gio.SocketType,
+ default=1)
+
def testRange(self):
# kiwi code
def max(c):
@@ -270,8 +304,6 @@ class TestProperty(unittest.TestCase):
# self.assertRaises(TypeError, gobject.property, type=bool, default=0)
self.assertRaises(TypeError, gobject.property, type=bool, default='ciao mamma')
self.assertRaises(TypeError, gobject.property, type=bool)
- self.assertRaises(TypeError, gobject.property, type=GEnum)
- self.assertRaises(TypeError, gobject.property, type=GEnum, default=0)
self.assertRaises(TypeError, gobject.property, type=object, default=0)
self.assertRaises(TypeError, gobject.property, type=complex)
self.assertRaises(TypeError, gobject.property, flags=-10)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]