Index: Lib/UserString.py =================================================================== --- Lib/UserString.py (revision 53047) +++ Lib/UserString.py (working copy) @@ -149,15 +149,41 @@ def __hash__(self): raise TypeError, "unhashable type (it is mutable)" def __setitem__(self, index, sub): - if index < 0: - index += len(self.data) - if index < 0 or index >= len(self.data): raise IndexError - self.data = self.data[:index] + sub + self.data[index+1:] + if isinstance(index, slice): + if isinstance(sub, UserString): + sub = sub.data + elif not isinstance(sub, basestring): + sub = str(sub) + start, stop, step = index.indices(len(self.data)) + if step == -1: + start, stop = stop+1, start+1 + sub = sub[::-1] + elif step != 1: + # XXX(twouters): I guess we should be reimplementing + # the extended slice assignment/deletion algorithm here... + raise TypeError, "invalid step in slicing assignment" + start = min(start, stop) + self.data = self.data[:start] + sub + self.data[stop:] + else: + if index < 0: + index += len(self.data) + if index < 0 or index >= len(self.data): raise IndexError + self.data = self.data[:index] + sub + self.data[index+1:] def __delitem__(self, index): - if index < 0: - index += len(self.data) - if index < 0 or index >= len(self.data): raise IndexError - self.data = self.data[:index] + self.data[index+1:] + if isinstance(index, slice): + start, stop, step = index.indices(len(self.data)) + if step == -1: + start, stop = stop+1, start+1 + elif step != 1: + # XXX(twouters): see same block in __setitem__ + raise TypeError, "invalid step in slicing deletion" + start = min(start, stop) + self.data = self.data[:start] + self.data[stop:] + else: + if index < 0: + index += len(self.data) + if index < 0 or index >= len(self.data): raise IndexError + self.data = self.data[:index] + self.data[index+1:] def __setslice__(self, start, end, sub): start = max(start, 0); end = max(end, 0) if isinstance(sub, UserString): Index: Lib/test/string_tests.py =================================================================== --- Lib/test/string_tests.py (.../p3yk) (revision 53047) +++ Lib/test/string_tests.py (.../p3yk-noslice) (revision 53047) @@ -907,7 +907,6 @@ self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000)) self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1)) self.checkequal(u'', 'abc', '__getitem__', slice(0, 0)) - # FIXME What about negative indices? This is handled differently by [] and __getitem__(slice) self.checkraises(TypeError, 'abc', '__getitem__', 'def') @@ -921,10 +920,21 @@ self.checkequal('', 'abc', '__getslice__', 1000, 1000) self.checkequal('', 'abc', '__getslice__', 2000, 1000) self.checkequal('', 'abc', '__getslice__', 2, 1) - # FIXME What about negative indizes? This is handled differently by [] and __getslice__ self.checkraises(TypeError, 'abc', '__getslice__', 'def') + def test_extended_getslice(self): + # Test extended slicing by comparing with list slicing. + s = string.ascii_letters + string.digits + indices = (0, None, 1, 3, 41, -1, -2, -37) + for start in indices: + for stop in indices: + # Skip step 0 (invalid) + for step in indices[1:]: + L = list(s)[start:stop:step] + self.checkequal(u"".join(L), s, '__getitem__', + slice(start, stop, step)) + def test_mul(self): self.checkequal('', 'abc', '__mul__', -1) self.checkequal('', 'abc', '__mul__', 0) Index: Lib/test/test_userstring.py =================================================================== --- Lib/test/test_userstring.py (revision 53047) +++ Lib/test/test_userstring.py (working copy) @@ -3,6 +3,7 @@ # UserString instances should behave similar to builtin string objects. import unittest +import string from test import test_support, string_tests from UserString import UserString, MutableString @@ -88,6 +89,28 @@ del s[-1:10] self.assertEqual(s, "fo") + def test_extended_set_del_slice(self): + indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100) + orig = string.ascii_letters + string.digits + for start in indices: + for stop in indices: + # Use indices[1:] when MutableString can handle real + # extended slices + for step in (None, 1, -1): + s = self.type2test(orig) + L = list(orig) + # Make sure we have a slice of exactly the right length, + # but with (hopefully) different data. + data = L[start:stop:step] + data.reverse() + L[start:stop:step] = data + s[start:stop:step] = "".join(data) + self.assertEquals(s, "".join(L)) + + del L[start:stop:step] + del s[start:stop:step] + self.assertEquals(s, "".join(L)) + def test_immutable(self): s = self.type2test("foobar") s2 = s.immutable()