[pygobject] Handle GObject subclasses in the property helper.



commit 7cc8ac35bb0d8dbf7d66f014f8cd7ff070b3acb8
Author: Steve Frécinaux <code istique net>
Date:   Wed Aug 4 00:30:05 2010 +0200

    Handle GObject subclasses in the property helper.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=625982

 gobject/propertyhelper.py |   26 +++++++++++++-------------
 tests/test_properties.py  |    9 +++++++++
 2 files changed, 22 insertions(+), 13 deletions(-)
---
diff --git a/gobject/propertyhelper.py b/gobject/propertyhelper.py
index 2f77a4d..745a13c 100644
--- a/gobject/propertyhelper.py
+++ b/gobject/propertyhelper.py
@@ -176,30 +176,30 @@ class property(object):
             self._exc = None
             raise exc
 
-    def _type_from_python(self, type):
-        if type == _long:
+    def _type_from_python(self, type_):
+        if type_ == _long:
             return TYPE_LONG
-        elif type == int:
+        elif type_ == int:
             return TYPE_INT
-        elif type == bool:
+        elif type_ == bool:
             return TYPE_BOOLEAN
-        elif type == float:
+        elif type_ == float:
             return TYPE_DOUBLE
-        elif type == str:
+        elif type_ == str:
             return TYPE_STRING
-        elif type == object:
+        elif type_ == object:
             return TYPE_PYOBJECT
-        elif type == _gobject.GObject:
-            return TYPE_OBJECT
-        elif type in [TYPE_NONE, TYPE_INTERFACE, TYPE_CHAR, TYPE_UCHAR,
+        elif isinstance(type_, type) and issubclass(type_, _gobject.GObject):
+            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]:
-            return type
+            return type_
         else:
-            raise TypeError("Unsupported type: %r" % (type,))
+            raise TypeError("Unsupported type: %r" % (type_,))
 
     def _get_default(self, default):
         ptype = self.type
@@ -296,7 +296,7 @@ class property(object):
             args = (self.default,)
         elif ptype == TYPE_PYOBJECT:
             args = ()
-        elif ptype == TYPE_OBJECT:
+        elif ptype.is_a(TYPE_OBJECT):
             args = ()
         else:
             raise NotImplementedError(ptype)
diff --git a/tests/test_properties.py b/tests/test_properties.py
index d156199..90db3ac 100644
--- a/tests/test_properties.py
+++ b/tests/test_properties.py
@@ -336,6 +336,15 @@ class TestProperty(unittest.TestCase):
         pobj1 = pobj2.obj
         self.assertEqual(hash(pobj1), obj1_hash)
 
+    def testObjectSubclassProperty(self):
+        class ObjectSubclass(GObject):
+            __gtype_name__ = 'ObjectSubclass'
+
+        class PropertyObjectSubclass(GObject):
+            obj = gobject.property(type=ObjectSubclass)
+
+        obj1 = PropertyObjectSubclass(obj=ObjectSubclass())
+
     def testPropertySubclass(self):
         # test for #470718
         class A(GObject):



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