Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions Lib/collections/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1072,7 +1072,7 @@ def __init__(self, initlist=None):
if type(initlist) == type(self.data):
self.data[:] = initlist
elif isinstance(initlist, UserList):
self.data[:] = initlist.data[:]
self.data[:] = initlist.data
else:
self.data = list(initlist)
def __repr__(self): return repr(self.data)
Expand All @@ -1085,8 +1085,16 @@ def __cast(self, other):
return other.data if isinstance(other, UserList) else other
def __contains__(self, item): return item in self.data
def __len__(self): return len(self.data)
def __getitem__(self, i): return self.data[i]
def __setitem__(self, i, item): self.data[i] = item
def __getitem__(self, i):
if isinstance(i, slice):
return self.__class__(self.data[i])
else:
return self.data[i]
def __setitem__(self, i, item):
if isinstance(i, slice) and isinstance(item, UserList):
self.data[i] = item.data
else:
self.data[i] = item
def __delitem__(self, i): del self.data[i]
def __add__(self, other):
if isinstance(other, UserList):
Expand Down
8 changes: 8 additions & 0 deletions Lib/test/test_userlist.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,14 @@ def test_getslice(self):
for j in range(-3, 6):
self.assertEqual(u[i:j], l[i:j])

def test_slice_type(self):
l = [0, 1, 2, 3, 4]
u = self.type2test(l)
self.assertIsInstance(u[:], self.type2test)
self.assertEqual(u[:], l)
self.assertIsInstance(u[::-1], self.type2test)
Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added also the test for extended slices (this does not work in Python 2).

self.assertEqual(u[::-1], l[::-1])

def test_add_specials(self):
u = UserList("spam")
u2 = u + "eggs"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Slices of :class:`collections.UserList` objects are now instances of ``UserList``
or its subclasses. Patch by Dmitry Kazakov.