← Back to team overview

launchpad-reviewers team mailing list archive

[Merge] ~cjwatson/launchpad:remove-getslice-setslice into launchpad:master

 

Colin Watson has proposed merging ~cjwatson/launchpad:remove-getslice-setslice into launchpad:master.

Commit message:
Remove obsolete __getslice__ and __setslice__ methods

Requested reviews:
  Launchpad code reviewers (launchpad-reviewers)

For more details, see:
https://code.launchpad.net/~cjwatson/launchpad/+git/launchpad/+merge/396692

__getitem__ and __setitem__ with slice arguments have been preferred since Python 2.0.

We do have to keep __getslice__ around in a few interfaces to avoid trouble with security proxies until we drop Python 2 support.
-- 
Your team Launchpad code reviewers is requested to review the proposed merge of ~cjwatson/launchpad:remove-getslice-setslice into launchpad:master.
diff --git a/lib/lp/archivepublisher/tests/test_ftparchive.py b/lib/lp/archivepublisher/tests/test_ftparchive.py
index a8a6727..52c41b9 100755
--- a/lib/lp/archivepublisher/tests/test_ftparchive.py
+++ b/lib/lp/archivepublisher/tests/test_ftparchive.py
@@ -78,8 +78,8 @@ class FakeSelectResult:
     def count(self):
         return len(self._result)
 
-    def __getslice__(self, i, j):
-        return self._result[i:j]
+    def __getitem__(self, key):
+        return self._result[key]
 
 
 class TestFTPArchive(TestCaseWithFactory):
diff --git a/lib/lp/services/limitedlist.py b/lib/lp/services/limitedlist.py
index ea340b9..9c9ca8b 100644
--- a/lib/lp/services/limitedlist.py
+++ b/lib/lp/services/limitedlist.py
@@ -6,6 +6,8 @@ __all__ = [
     'LimitedList',
     ]
 
+import sys
+
 
 class LimitedList(list):
     """A mutable sequence that takes a limited number of elements."""
@@ -62,10 +64,14 @@ class LimitedList(list):
             self._ensureLength()
         return result
 
-    def __setslice__(self, i, j, sequence):
-        result = super(LimitedList, self).__setslice__(i, j, sequence)
-        self._ensureLength()
-        return result
+    if sys.version_info[0] < 3:
+        # list.__setslice__ exists on Python 2, so we must override it in
+        # this subclass.  (If it didn't exist, as is the case on Python 3,
+        # then __setitem__ above would be good enough.)
+        def __setslice__(self, i, j, sequence):
+            result = super(LimitedList, self).__setslice__(i, j, sequence)
+            self._ensureLength()
+            return result
 
     def append(self, value):
         result = super(LimitedList, self).append(value)
diff --git a/lib/lp/services/webapp/batching.py b/lib/lp/services/webapp/batching.py
index 925515b..4ce5f0c 100644
--- a/lib/lp/services/webapp/batching.py
+++ b/lib/lp/services/webapp/batching.py
@@ -237,14 +237,12 @@ class ShadowedList:
         """See `list`."""
         return len(self.values)
 
-    def __getslice__(self, start, end):
+    def __getitem__(self, key):
         """See `list`."""
-        return ShadowedList(
-            self.values[start:end], self.shadow_values[start:end])
-
-    def __getitem__(self, index):
-        """See `list`."""
-        return self.values[index]
+        if isinstance(key, slice):
+            return ShadowedList(self.values[key], self.shadow_values[key])
+        else:
+            return self.values[key]
 
     def __add__(self, other):
         """See `list`."""