Merge ~cjwatson/launchpad:py3-limitedlist into launchpad:master

Proposed by Colin Watson
Status: Merged
Approved by: Colin Watson
Approved revision: 078fdd2a2b51feaaf252f9f34fdf76d4a97ff619
Merge reported by: Otto Co-Pilot
Merged at revision: not available
Proposed branch: ~cjwatson/launchpad:py3-limitedlist
Merge into: launchpad:master
Diff against target: 39 lines (+8/-2)
2 files modified
lib/lp/services/doc/limitedlist.txt (+1/-1)
lib/lp/services/limitedlist.py (+7/-1)
Reviewer Review Type Date Requested Status
Thiago F. Pappacena (community) Approve
Review via email: mp+387707@code.launchpad.net

Commit message

Fix LimitedList for Python 3

Description of the change

The slice protocol is a bit different, as is one exception message.

To post a comment you must log in.
Revision history for this message
Thiago F. Pappacena (pappacena) wrote :

LGTM

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1diff --git a/lib/lp/services/doc/limitedlist.txt b/lib/lp/services/doc/limitedlist.txt
2index 4abd0a0..10f1fed 100644
3--- a/lib/lp/services/doc/limitedlist.txt
4+++ b/lib/lp/services/doc/limitedlist.txt
5@@ -16,7 +16,7 @@ The property max_length stores the maximum allowed length of the list.
6 >>> LimitedList()
7 Traceback (most recent call last):
8 ...
9- TypeError: __new__() takes at least 2 arguments (1 given)
10+ TypeError: __new__() ...
11
12 We can optionally specify the initial content of the sequence. Note that
13 only the last N elements of the second parameter are stored, where N is
14diff --git a/lib/lp/services/limitedlist.py b/lib/lp/services/limitedlist.py
15index 5033df7..ea340b9 100644
16--- a/lib/lp/services/limitedlist.py
17+++ b/lib/lp/services/limitedlist.py
18@@ -30,7 +30,7 @@ class LimitedList(list):
19 """Ensure that the maximum length is not exceeded."""
20 elements_to_drop = self.__len__() - self.max_length
21 if elements_to_drop > 0:
22- self.__delslice__(0, elements_to_drop)
23+ del self[0:elements_to_drop]
24
25 def __add__(self, other):
26 return LimitedList(
27@@ -56,6 +56,12 @@ class LimitedList(list):
28 self._ensureLength()
29 return result
30
31+ def __setitem__(self, key, value):
32+ result = super(LimitedList, self).__setitem__(key, value)
33+ if isinstance(key, slice):
34+ self._ensureLength()
35+ return result
36+
37 def __setslice__(self, i, j, sequence):
38 result = super(LimitedList, self).__setslice__(i, j, sequence)
39 self._ensureLength()

Subscribers

People subscribed via source and target branches

to status/vote changes: