annotate Lib/UserList.py @ 25838:8540887bcec6 2.2

Backport of 2.183: Fix memory leak in add_subclass() found by NealN with valgrind.
author Guido van Rossum <guido@python.org>
date Fri, 18 Oct 2002 13:42:21 +0000
parents bbac2a6a5d16
children 14f65d661084
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
11691
e34991022571 Mass patch by Ka-Ping Yee:
Guido van Rossum <guido@python.org>
parents: 10481
diff changeset
1 """A more or less complete user-defined wrapper around list objects."""
1416
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
d6f6a1cb7d79 * test_*.py: new lambda syntax (also affects tests for filter, map,
Guido van Rossum <guido@python.org>
parents:
diff changeset
3 class UserList:
12134
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
4 def __init__(self, initlist=None):
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
5 self.data = []
12134
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
6 if initlist is not None:
13620
5b727ab37092 Spelling fixes supplied by Rob W. W. Hooft. All these are fixes in either
Thomas Wouters <thomas@python.org>
parents: 12134
diff changeset
7 # XXX should this accept an arbitrary sequence?
12134
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
8 if type(initlist) == type(self.data):
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
9 self.data[:] = initlist
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
10 elif isinstance(initlist, UserList):
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
11 self.data[:] = initlist.data[:]
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
12 else:
12134
c160749acff8 robustify UserList constructor -- will now accept any sequence
Jeremy Hylton <jeremy@alum.mit.edu>
parents: 11691
diff changeset
13 self.data = list(initlist)
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
14 def __repr__(self): return repr(self.data)
15990
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
15 def __lt__(self, other): return self.data < self.__cast(other)
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
16 def __le__(self, other): return self.data <= self.__cast(other)
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
17 def __eq__(self, other): return self.data == self.__cast(other)
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
18 def __ne__(self, other): return self.data != self.__cast(other)
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
19 def __gt__(self, other): return self.data > self.__cast(other)
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
20 def __ge__(self, other): return self.data >= self.__cast(other)
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
21 def __cast(self, other):
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
22 if isinstance(other, UserList): return other.data
3f4b6002c07d Bite the bullet: use rich comparisons here, too.
Guido van Rossum <guido@python.org>
parents: 15060
diff changeset
23 else: return other
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
24 def __cmp__(self, other):
18063
bbac2a6a5d16 Patch #424475: Speed-up tp_compare usage, by special-casing the common
Martin v. Löwis <martin@v.loewis.de>
parents: 16657
diff changeset
25 return cmp(self.data, self.__cast(other))
14692
c6f43c6036ba SF patch 101391: implemented UserList.__contains__.
Tim Peters <tim.peters@gmail.com>
parents: 14164
diff changeset
26 def __contains__(self, item): return item in self.data
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
27 def __len__(self): return len(self.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
28 def __getitem__(self, i): return self.data[i]
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
29 def __setitem__(self, i, item): self.data[i] = item
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
30 def __delitem__(self, i): del self.data[i]
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
31 def __getslice__(self, i, j):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
32 i = max(i, 0); j = max(j, 0)
15060
177ae1c52037 __getslice__(): Make this use the constructor form that gets a sequence
Fred Drake <fdrake@acm.org>
parents: 14692
diff changeset
33 return self.__class__(self.data[i:j])
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
34 def __setslice__(self, i, j, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
35 i = max(i, 0); j = max(j, 0)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
36 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
37 self.data[i:j] = other.data
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
38 elif isinstance(other, type(self.data)):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
39 self.data[i:j] = other
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
40 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
41 self.data[i:j] = list(other)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
42 def __delslice__(self, i, j):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
43 i = max(i, 0); j = max(j, 0)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
44 del self.data[i:j]
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
45 def __add__(self, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
46 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
47 return self.__class__(self.data + other.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
48 elif isinstance(other, type(self.data)):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
49 return self.__class__(self.data + other)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
50 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
51 return self.__class__(self.data + list(other))
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
52 def __radd__(self, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
53 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
54 return self.__class__(other.data + self.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
55 elif isinstance(other, type(self.data)):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
56 return self.__class__(other + self.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
57 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
58 return self.__class__(list(other) + self.data)
14164
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
59 def __iadd__(self, other):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
60 if isinstance(other, UserList):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
61 self.data += other.data
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
62 elif isinstance(other, type(self.data)):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
63 self.data += other
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
64 else:
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
65 self.data += list(other)
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
66 return self
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
67 def __mul__(self, n):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
68 return self.__class__(self.data*n)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
69 __rmul__ = __mul__
14164
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
70 def __imul__(self, n):
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
71 self.data *= n
dba4aec970e6 Support for augmented assignment in the UserList, UserDict, UserString and
Thomas Wouters <thomas@python.org>
parents: 13620
diff changeset
72 return self
10481
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
73 def append(self, item): self.data.append(item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
74 def insert(self, i, item): self.data.insert(i, item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
75 def pop(self, i=-1): return self.data.pop(i)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
76 def remove(self, item): self.data.remove(item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
77 def count(self, item): return self.data.count(item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
78 def index(self, item): return self.data.index(item)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
79 def reverse(self): self.data.reverse()
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
80 def sort(self, *args): apply(self.data.sort, args)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
81 def extend(self, other):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
82 if isinstance(other, UserList):
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
83 self.data.extend(other.data)
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
84 else:
bae650f57c41 Use isinstance() where appropriate.
Guido van Rossum <guido@python.org>
parents: 9778
diff changeset
85 self.data.extend(other)