[pygobject] Gtk overrides: Implement __delitem__ on TreeModel
- From: Martin Pitt <martinpitt src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [pygobject] Gtk overrides: Implement __delitem__ on TreeModel
- Date: Mon, 14 May 2012 11:23:50 +0000 (UTC)
commit 3f712b56397296bca2f5358cd52977b1a2011964
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 5ae61b2..f3db8b3 100644
--- a/gi/overrides/Gtk.py
+++ b/gi/overrides/Gtk.py
@@ -750,9 +750,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:
@@ -761,18 +761,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 35b7dfe..0131792 100644
--- a/tests/test_overrides.py
+++ b/tests/test_overrides.py
@@ -1516,6 +1516,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]