[pygobject/pygobject-3-2] Gtk overrides: Implement __delitem__ on TreeModel



commit 04c45e5c83701016d73a7ab2251bb5c7ce78c1a3
Author: Jose Rostagno <joserostagno vijona com ar>
Date:   Fri May 11 12:39:05 2012 -0300

    Gtk overrides: Implement __delitem__ on TreeModel
    
    https://bugzilla.gnome.org/show_bug.cgi?id=675892
    
    Signed-off-by: Martin Pitt <martinpitt gnome org>

 gi/overrides/Gtk.py     |   16 ++++++++++++----
 tests/test_overrides.py |    9 +++++++++
 2 files changed, 21 insertions(+), 4 deletions(-)
---
diff --git a/gi/overrides/Gtk.py b/gi/overrides/Gtk.py
index aba5a73..8a16f09 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -731,9 +731,9 @@ class TreeModel(Gtk.TreeModel):
     # alias for Python 2.x object protocol
     __nonzero__ = __bool__
 
-    def __getitem__(self, key):
+    def _getiter(self, key):
         if isinstance(key, Gtk.TreeIter):
-            return TreeModelRow(self, key)
+            return key
         elif isinstance(key, int) and key < 0:
             index = len(self) + key
             if index < 0:
@@ -742,18 +742,26 @@ class TreeModel(Gtk.TreeModel):
                 aiter = self.get_iter(index)
             except ValueError:
                 raise IndexError("could not find tree path '%s'" % key)
-            return TreeModelRow(self, aiter)
+            return aiter
         else:
             try:
                 aiter = self.get_iter(key)
             except ValueError:
                 raise IndexError("could not find tree path '%s'" % key)
-            return TreeModelRow(self, aiter)
+            return aiter
+
+    def __getitem__(self, key):
+        aiter = self._getiter(key)
+        return TreeModelRow(self, aiter)
 
     def __setitem__(self, key, value):
         row = self[key]
         self.set_row(row.iter, value)
 
+    def __delitem__(self, key):
+        aiter = self._getiter(key)
+        self.remove(aiter)
+
     def __iter__(self):
         return TreeModelRowIter(self, self.get_iter_first())
 
diff --git a/tests/test_overrides.py b/tests/test_overrides.py
index becb4cb..abe9379 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -1466,6 +1466,15 @@ class TestGtk(unittest.TestCase):
         self.assertRaises(ValueError, tree_store.get, aiter, 1, 100)
         self.assertEqual(tree_store.get(aiter, 0, 1), (10, 'this is row #10'))
 
+        # check __delitem__
+        self.assertEqual(len(tree_store), 100)
+        aiter = tree_store.get_iter(10)
+        del tree_store[aiter]
+        self.assertEqual(len(tree_store), 99)
+        self.assertRaises(TypeError, tree_store.__delitem__, None)
+        self.assertRaises(IndexError, tree_store.__delitem__, -101)
+        self.assertRaises(IndexError, tree_store.__delitem__, 101)
+
     def test_tree_model_edit(self):
         model = Gtk.ListStore(int, str, float)
         model.append([1, "one", -0.1])



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