[pygobject] [gi] handle subtypes when inserting into tree models



commit 82689cbf53d92b1b951a459fe3de0e1d3a91791a
Author: John (J5) Palmieri <johnp redhat com>
Date:   Thu Dec 2 16:27:04 2010 -0500

    [gi] handle subtypes when inserting into tree models
    
    * Often modules will give back basic types wrapped in a subtype.
      This is the case with D-Bus where you may want to keep some of the
      metadata around.  More often than not, the developer is just looking
      to use the basetype.
    
    * This override checks the column type and handles basic types such as
      gchararrays, ints, longs, floats and doubles, converting them to their
      base types before sending them to the generic GI type marshaller.
    
    * More types may need to be supported but these are the common cases where
      apps break.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=635172

 gi/overrides/Gtk.py     |   38 ++++++++++++++++++++++++++++++++++++--
 tests/test_overrides.py |    8 +++++++-
 2 files changed, 43 insertions(+), 3 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index b088038..ca39682 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -664,8 +664,42 @@ class TreeModel(Gtk.TreeModel):
             raise ValueError('row sequence has the incorrect number of elements')
 
         for i in range(n_columns):
-            if row[i] is not None:
-                self.set_value(treeiter, i, row[i])
+            value = row[i]
+
+            if value is None:
+                continue
+            
+            # we may need to convert to a basic type
+            type_ = self.get_column_type(i)
+            if type_ == gobject.TYPE_PYOBJECT:
+                pass # short-circut branching
+            elif type_ == gobject.TYPE_STRING:
+                if isinstance(value, str):
+                    value = str(value)
+                elif sys.version_info < (3, 0):
+                    if isinstance(value, unicode):
+                        value = unicode(value)
+                    else:
+                        raise ValueError('Expected string or unicode for row %i but got %s' % i, type(value))
+                else:
+                    raise ValueError('Expected a string for row %i but got %s' % i, type(value))
+            elif type_ == gobject.TYPE_FLOAT or type_ == gobject.TYPE_DOUBLE:
+                if isinstance(value, float):
+                    value = float(value)
+                else:
+                    raise ValueError('Expected a float for row %i but got %s' % i, type(value))
+            elif type_ == gobject.TYPE_LONG or type_ == gobject.TYPE_INT:
+                if isinstance(value, int):
+                    value = int(value)
+                elif sys.version_info < (3, 0):
+                    if isinstance(value, long):
+                        value = long(value)
+                    else:
+                        raise ValueError('Expected an long for row %i but got %s' % i, type(value))
+                else:
+                    raise ValueError('Expected an interger for row %i but got %s' % i, type(value))
+
+            self.set_value(treeiter, i, value)
 
     def get(self, treeiter, *columns):
         n_columns = self.get_n_columns();
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 4afc921..7a0bfbd 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -482,9 +482,15 @@ class TestGtk(unittest.TestCase):
         for row in tree_store:
             self.fail("Should not be reached")
 
+        class DerivedIntType(int):
+            pass
+
+        class DerivedStrType(str):
+            pass
+
         for i in range(100):
             label = 'this is row #%d' % i
-            parent = tree_store.append(None, (i, label,))
+            parent = tree_store.append(None, (DerivedIntType(i), DerivedStrType(label),))
             self.assertNotEquals(parent, None)
             for j in range(20):
                 label = 'this is child #%d of node #%d' % (j, i)



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