[pygobject/pygobject-2-28] use GValue support to marshal GtkTreeModel values correctly



commit 6fe7783b96366f380f9168909932a87e35d994b3
Author: John (J5) Palmieri <johnp redhat com>
Date:   Mon Feb 21 17:20:57 2011 -0500

    use GValue support to marshal GtkTreeModel values correctly
    
    * needs patch from https://bugzilla.gnome.org/show_bug.cgi?id=642914
    
    https://bugzilla.gnome.org/show_bug.cgi?id=642921

 gi/overrides/Gtk.py     |   37 ++++++++++++++++++--
 tests/test_overrides.py |   87 ++++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 107 insertions(+), 17 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index bbca61d..37f4e98 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -728,9 +728,9 @@ class TreeModel(Gtk.TreeModel):
 
             # we may need to convert to a basic type
             type_ = self.get_column_type(column)
-            if type_ == gobject.TYPE_PYOBJECT:
+            if type_ == GObject.TYPE_PYOBJECT:
                 pass # short-circut branching
-            elif type_ == gobject.TYPE_STRING:
+            elif type_ == GObject.TYPE_STRING:
                 if isinstance(value, str):
                     value = str(value)
                 elif sys.version_info < (3, 0):
@@ -740,12 +740,12 @@ class TreeModel(Gtk.TreeModel):
                         raise ValueError('Expected string or unicode for column %i but got %s%s' % (column, value, type(value)))
                 else:
                     raise ValueError('Expected a string for column %i but got %s' % (column, type(value)))
-            elif type_ == gobject.TYPE_FLOAT or type_ == gobject.TYPE_DOUBLE:
+            elif type_ == GObject.TYPE_FLOAT or type_ == GObject.TYPE_DOUBLE:
                 if isinstance(value, float):
                     value = float(value)
                 else:
                     raise ValueError('Expected a float for column %i but got %s' % (column, type(value)))
-            elif type_ == gobject.TYPE_LONG or type_ == gobject.TYPE_INT:
+            elif type_ == GObject.TYPE_LONG or type_ == GObject.TYPE_INT:
                 if isinstance(value, int):
                     value = int(value)
                 elif sys.version_info < (3, 0):
@@ -755,6 +755,35 @@ class TreeModel(Gtk.TreeModel):
                         raise ValueError('Expected an long for column %i but got %s' % (column, type(value)))
                 else:
                     raise ValueError('Expected an integer for column %i but got %s' % (column, type(value)))
+            elif type_ == GObject.TYPE_BOOLEAN:
+                if isinstance(value, (int, long)):
+                    value = bool(value)
+                else:
+                    raise ValueError('Expected a bool for column %i but got %s' % (column, type(value)))
+            else:
+                # use GValues directly to marshal to the correct type
+                # standard object checks should take care of validation
+                # so we don't have to do it here
+                value_container = GObject.Value()
+                value_container.init(type_)
+                if type_ == GObject.TYPE_CHAR:
+                    value_container.set_char(value)
+                    value = value_container
+                elif type_ == GObject.TYPE_UCHAR:
+                    value_container.set_uchar(value)
+                    value = value_container
+                elif type_ == GObject.TYPE_UINT:
+                    value_container.set_uint(value)
+                    value = value_container
+                elif type_ == GObject.TYPE_ULONG:
+                    value_container.set_ulong(value)
+                    value = value_container
+                elif type_ == GObject.TYPE_INT64:
+                    value_container.set_int64(value)
+                    value = value_container
+                elif type_ == GObject.TYPE_UINT64:
+                    value_container.set_uint64(value)
+                    value = value_container
 
             return value
 
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index 0ab1968..36b9f59 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -655,7 +655,20 @@ class TestGtk(unittest.TestCase):
         test_pyobj = TestPyObject()
         test_pydict = {1:1, "2":2, "3":"3"}
         test_pylist = [1,"2", "3"]
-        tree_store = Gtk.TreeStore(int, 'gchararray', TestGtk.TestClass, object, object, object)
+        tree_store = Gtk.TreeStore(int,
+                                   'gchararray',
+                                   TestGtk.TestClass,
+                                   object,
+                                   object,
+                                   object,
+                                   bool,
+                                   bool,
+                                   GObject.TYPE_UINT,
+                                   GObject.TYPE_ULONG,
+                                   GObject.TYPE_INT64,
+                                   GObject.TYPE_UINT64,
+                                   GObject.TYPE_UCHAR,
+                                   GObject.TYPE_CHAR)
 
         parent = None
         for i in range(100):
@@ -666,7 +679,16 @@ class TestGtk(unittest.TestCase):
                                                 testobj,
                                                 test_pyobj,
                                                 test_pydict,
-                                                test_pylist))
+                                                test_pylist,
+                                                i % 2,
+                                                bool(i % 2),
+                                                i,
+                                                sys.maxint + 1,
+                                                -9223372036854775808,
+                                                0xffffffffffffffff,
+                                                254,
+                                                'a'
+                                                ))
 
         # len gets the number of children in the root node
         # since we kept appending to the previous node
@@ -682,9 +704,6 @@ class TestGtk(unittest.TestCase):
            i = tree_store.get_value(treeiter, 0)
            s = tree_store.get_value(treeiter, 1)
            obj = tree_store.get_value(treeiter, 2)
-           i = tree_store.get_value(treeiter, 0)
-           s = tree_store.get_value(treeiter, 1)
-           obj = tree_store.get_value(treeiter, 2)
            obj.check(i, s)
 
            pyobj = tree_store.get_value(treeiter, 3)
@@ -694,6 +713,25 @@ class TestGtk(unittest.TestCase):
            pylist = tree_store.get_value(treeiter, 5)
            self.assertEquals(pylist, test_pylist)
 
+           bool_1 = tree_store.get_value(treeiter, 6)
+           bool_2 = tree_store.get_value(treeiter, 7)
+           self.assertEquals(bool_1, bool_2)
+           self.assertTrue(isinstance(bool_1, bool))
+           self.assertTrue(isinstance(bool_2, bool))
+
+           uint_ = tree_store.get_value(treeiter, 8)
+           self.assertEquals(uint_, i)
+           ulong_ = tree_store.get_value(treeiter, 9)
+           self.assertEquals(ulong_, sys.maxint + 1)
+           int64_ = tree_store.get_value(treeiter, 10)
+           self.assertEquals(int64_, -9223372036854775808)
+           uint64_ = tree_store.get_value(treeiter, 11)
+           self.assertEquals(uint64_, 0xffffffffffffffff)
+           uchar_ = tree_store.get_value(treeiter, 12)
+           self.assertEquals(ord(uchar_), 254)
+           char_ = tree_store.get_value(treeiter, 13)
+           self.assertEquals(char_, 'a')
+
            parent = treeiter
            treeiter = tree_store.iter_children(parent)
 
@@ -707,7 +745,7 @@ class TestGtk(unittest.TestCase):
         test_pydict = {1:1, "2":2, "3":"3"}
         test_pylist = [1,"2", "3"]
 
-        list_store = Gtk.ListStore(int, str, 'GIOverrideTreeAPITest', object, object, object)
+        list_store = Gtk.ListStore(int, str, 'GIOverrideTreeAPITest', object, object, object, bool, bool)
         for i in range(93):
             label = 'this is row #%d' % i
             testobj = TestGtk.TestClass(self, i, label)
@@ -716,7 +754,9 @@ class TestGtk(unittest.TestCase):
                                         testobj,
                                         test_pyobj,
                                         test_pydict,
-                                        test_pylist))
+                                        test_pylist,
+                                        i % 2,
+                                        bool(i % 2)))
 
         i = 93
         label = _unicode('this is row #93')
@@ -727,6 +767,8 @@ class TestGtk(unittest.TestCase):
         list_store.set_value(treeiter, 3, test_pyobj)
         list_store.set_value(treeiter, 4, test_pydict)
         list_store.set_value(treeiter, 5, test_pylist)
+        list_store.set_value(treeiter, 6, 1)
+        list_store.set_value(treeiter, 7, True)
 
         # test automatic unicode->str conversion
         i = 94
@@ -736,7 +778,9 @@ class TestGtk(unittest.TestCase):
                                       TestGtk.TestClass(self, i, label),
                                       test_pyobj,
                                       test_pydict,
-                                      test_pylist))
+                                      test_pylist,
+                                      0,
+                                      False))
 
         # add sorted items out of order to test insert* apis
         i = 97
@@ -746,7 +790,9 @@ class TestGtk(unittest.TestCase):
                                       TestGtk.TestClass(self, i, label),
                                       test_pyobj,
                                       test_pydict,
-                                      test_pylist))
+                                      test_pylist,
+                                      1,
+                                      True))
         # this should append
         i = 99
         label = 'this is row #99'
@@ -755,7 +801,9 @@ class TestGtk(unittest.TestCase):
                                  TestGtk.TestClass(self, i, label),
                                  test_pyobj,
                                  test_pydict,
-                                 test_pylist))
+                                 test_pylist,
+                                 1,
+                                 True))
 
         i = 96
         label = 'this is row #96'
@@ -764,7 +812,9 @@ class TestGtk(unittest.TestCase):
                                             TestGtk.TestClass(self, i, label),
                                             test_pyobj,
                                             test_pydict,
-                                            test_pylist))
+                                            test_pylist,
+                                            0,
+                                            False))
 
         i = 98
         label = 'this is row #98'
@@ -773,7 +823,9 @@ class TestGtk(unittest.TestCase):
                                            TestGtk.TestClass(self, i, label),
                                            test_pyobj,
                                            test_pydict,
-                                           test_pylist))
+                                           test_pylist,
+                                           0,
+                                           False))
 
 
         i = 95
@@ -783,7 +835,9 @@ class TestGtk(unittest.TestCase):
                                TestGtk.TestClass(self, i, label),
                                test_pyobj,
                                test_pydict,
-                               test_pylist))
+                               test_pylist,
+                               1,
+                               True))
 
         self.assertEquals(len(list_store), 100)
 
@@ -805,6 +859,13 @@ class TestGtk(unittest.TestCase):
             self.assertEquals(pydict, test_pydict)
             pylist = list_store.get_value(treeiter, 5)
             self.assertEquals(pylist, test_pylist)
+
+            bool_1 = list_store.get_value(treeiter, 6)
+            bool_2 = list_store.get_value(treeiter, 7)
+            self.assertEquals(bool_1, bool_2)
+            self.assertTrue(isinstance(bool_1, bool))
+            self.assertTrue(isinstance(bool_2, bool))
+
             treeiter = list_store.iter_next(treeiter)
 
             counter += 1



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