[pygobject] Allow setting TreeModel values to None



commit 0a5587b6a56d417a6703e342f153596f08cd5889
Author: Simon Feltman <sfeltman src gnome org>
Date:   Sun Jan 13 18:19:51 2013 -0800

    Allow setting TreeModel values to None
    
    Change TreeModel.set_value to use an empty but initialized GValue when
    None is used as the value argument. This allows clearing of cell data
    which was not accessible due to auto-coercion.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=684094

 gi/overrides/Gtk.py         |   11 ++++++++---
 tests/test_overrides_gtk.py |    8 ++++++++
 2 files changed, 16 insertions(+), 3 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index 223d8f4..281a680 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -872,11 +872,16 @@ class TreeModel(Gtk.TreeModel):
             self.set_value(treeiter, column, value)
 
     def _convert_value(self, column, value):
-        if value is None:
-            return None
-
         # we may need to convert to a basic type
         type_ = self.get_column_type(column)
+
+        # Allow None to be used as an initialized but empty value.
+        # https://bugzilla.gnome.org/show_bug.cgi?id=684094
+        if value is None:
+            value_container = GObject.Value()
+            value_container.init(type_)
+            return value_container
+
         if type_ == GObject.TYPE_STRING:
             if isinstance(value, str):
                 value = str(value)
diff --git a/tests/test_overrides_gtk.py b/tests/test_overrides_gtk.py
index 3578ef0..ddea148 100644
--- a/tests/test_overrides_gtk.py
+++ b/tests/test_overrides_gtk.py
@@ -1309,6 +1309,14 @@ class TestTreeModel(unittest.TestCase):
 
         self.assertRaises(ValueError, set_row3)
 
+    def test_tree_model_set_value_to_none(self):
+        # Tests allowing the usage of None to set an empty value on a model.
+        store = Gtk.ListStore(str)
+        row = store.append(['test'])
+        self.assertSequenceEqual(store[0][:], ['test'])
+        store.set_value(row, 0, None)
+        self.assertSequenceEqual(store[0][:], [None])
+
 
 @unittest.skipUnless(Gtk, 'Gtk not available')
 class TestTreeView(unittest.TestCase):



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