[pyatspi2] Move IndexException logic to __getitem__



commit 66029ccaf90c52abcceb534b9406daf7258e5800
Author: Mike Gorse <mgorse novell com>
Date:   Mon May 31 13:56:47 2010 -0400

    Move IndexException logic to __getitem__
    
    Have __getitem__ check count and throw IndexException when out of range,
    and remove range check from getChildAtIndex, as in the original pyatspi.
    This also eliminates repeated ChildCount calls when iterating through
    children using getChildAtIndex.

 pyatspi/accessible.py |   20 ++++++++++++++++----
 1 files changed, 16 insertions(+), 4 deletions(-)
---
diff --git a/pyatspi/accessible.py b/pyatspi/accessible.py
index 2a04fde..05c12a4 100644
--- a/pyatspi/accessible.py
+++ b/pyatspi/accessible.py
@@ -241,8 +241,23 @@ class Accessible(BaseProxy):
                 return self.getChildCount()
 
         def __getitem__(self, index):
+                """
+                Thin wrapper around getChildAtIndex.
+                
+                @param index: Index of desired child
+                @type index: integer
+                @return: Accessible child
+                @rtype: Accessibility.Accessible
+                """
+                n = self.childCount
+                if index >= n:
+                        raise IndexError
+                elif index < -n:
+                        raise IndexError
+                elif index < 0:
+                        index += n
                 return self.getChildAtIndex(index)
-
+            
         # Bonobo interface --------------------------------------------------------------
 
         def queryInterface(self, interface):
@@ -336,9 +351,6 @@ class Accessible(BaseProxy):
                 if self.cached and not(self._cached_data.state[0] & (1 << STATE_MANAGES_DESCENDANTS)):
                         (name, path) = self._cached_data.children[index]
                 else:
-                        count = Int32(self._pgetter(ATSPI_ACCESSIBLE, "ChildCount"))
-                        if index >= count:
-                                raise IndexError
                         func = self.get_dbus_method("GetChildAtIndex", dbus_interface=ATSPI_ACCESSIBLE)
                         (name, path) = func (index)
 



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