Skip to content

Commit 3ccec68

Browse files
committed
Improve extended slicing support in builtin types and classes. Specifically:
- Specialcase extended slices that amount to a shallow copy the same way as is done for simple slices, in the tuple, string and unicode case. - Specialcase step-1 extended slices to optimize the common case for all involved types. - For lists, allow extended slice assignment of differing lengths as long as the step is 1. (Previously, 'l[:2:1] = []' failed even though 'l[:2] = []' and 'l[:2:None] = []' do not.) - Implement extended slicing for buffer, array, structseq, mmap and UserString.UserString. - Implement slice-object support (but not non-step-1 slice assignment) for UserString.MutableString. - Add tests for all new functionality.
1 parent 0f4a14b commit 3ccec68

16 files changed

+728
-118
lines changed

Lib/UserString.py

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -149,15 +149,41 @@ def __init__(self, string=""):
149149
def __hash__(self):
150150
raise TypeError, "unhashable type (it is mutable)"
151151
def __setitem__(self, index, sub):
152-
if index < 0:
153-
index += len(self.data)
154-
if index < 0 or index >= len(self.data): raise IndexError
155-
self.data = self.data[:index] + sub + self.data[index+1:]
152+
if isinstance(index, slice):
153+
if isinstance(sub, UserString):
154+
sub = sub.data
155+
elif not isinstance(sub, basestring):
156+
sub = str(sub)
157+
start, stop, step = index.indices(len(self.data))
158+
if step == -1:
159+
start, stop = stop+1, start+1
160+
sub = sub[::-1]
161+
elif step != 1:
162+
# XXX(twouters): I guess we should be reimplementing
163+
# the extended slice assignment/deletion algorithm here...
164+
raise TypeError, "invalid step in slicing assignment"
165+
start = min(start, stop)
166+
self.data = self.data[:start] + sub + self.data[stop:]
167+
else:
168+
if index < 0:
169+
index += len(self.data)
170+
if index < 0 or index >= len(self.data): raise IndexError
171+
self.data = self.data[:index] + sub + self.data[index+1:]
156172
def __delitem__(self, index):
157-
if index < 0:
158-
index += len(self.data)
159-
if index < 0 or index >= len(self.data): raise IndexError
160-
self.data = self.data[:index] + self.data[index+1:]
173+
if isinstance(index, slice):
174+
start, stop, step = index.indices(len(self.data))
175+
if step == -1:
176+
start, stop = stop+1, start+1
177+
elif step != 1:
178+
# XXX(twouters): see same block in __setitem__
179+
raise TypeError, "invalid step in slicing deletion"
180+
start = min(start, stop)
181+
self.data = self.data[:start] + self.data[stop:]
182+
else:
183+
if index < 0:
184+
index += len(self.data)
185+
if index < 0 or index >= len(self.data): raise IndexError
186+
self.data = self.data[:index] + self.data[index+1:]
161187
def __setslice__(self, start, end, sub):
162188
start = max(start, 0); end = max(end, 0)
163189
if isinstance(sub, UserString):

Lib/test/list_tests.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -179,8 +179,10 @@ def test_setslice(self):
179179
self.assertEqual(a, self.type2test(range(10)))
180180

181181
self.assertRaises(TypeError, a.__setslice__, 0, 1, 5)
182+
self.assertRaises(TypeError, a.__setitem__, slice(0, 1, 5))
182183

183184
self.assertRaises(TypeError, a.__setslice__)
185+
self.assertRaises(TypeError, a.__setitem__)
184186

185187
def test_delslice(self):
186188
a = self.type2test([0, 1])

Lib/test/string_tests.py

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,6 @@ def test_subscript(self):
912912
self.checkequal(u'abc', 'abc', '__getitem__', slice(0, 1000))
913913
self.checkequal(u'a', 'abc', '__getitem__', slice(0, 1))
914914
self.checkequal(u'', 'abc', '__getitem__', slice(0, 0))
915-
# FIXME What about negative indices? This is handled differently by [] and __getitem__(slice)
916915

917916
self.checkraises(TypeError, 'abc', '__getitem__', 'def')
918917

@@ -926,10 +925,21 @@ def test_slice(self):
926925
self.checkequal('', 'abc', '__getslice__', 1000, 1000)
927926
self.checkequal('', 'abc', '__getslice__', 2000, 1000)
928927
self.checkequal('', 'abc', '__getslice__', 2, 1)
929-
# FIXME What about negative indizes? This is handled differently by [] and __getslice__
930928

931929
self.checkraises(TypeError, 'abc', '__getslice__', 'def')
932930

931+
def test_extended_getslice(self):
932+
# Test extended slicing by comparing with list slicing.
933+
s = string.ascii_letters + string.digits
934+
indices = (0, None, 1, 3, 41, -1, -2, -37)
935+
for start in indices:
936+
for stop in indices:
937+
# Skip step 0 (invalid)
938+
for step in indices[1:]:
939+
L = list(s)[start:stop:step]
940+
self.checkequal(u"".join(L), s, '__getitem__',
941+
slice(start, stop, step))
942+
933943
def test_mul(self):
934944
self.checkequal('', 'abc', '__mul__', -1)
935945
self.checkequal('', 'abc', '__mul__', 0)

Lib/test/test_array.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -474,6 +474,18 @@ def test_getslice(self):
474474
array.array(self.typecode)
475475
)
476476

477+
def test_extended_getslice(self):
478+
# Test extended slicing by comparing with list slicing
479+
# (Assumes list conversion works correctly, too)
480+
a = array.array(self.typecode, self.example)
481+
indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
482+
for start in indices:
483+
for stop in indices:
484+
# Everything except the initial 0 (invalid step)
485+
for step in indices[1:]:
486+
self.assertEqual(list(a[start:stop:step]),
487+
list(a)[start:stop:step])
488+
477489
def test_setslice(self):
478490
a = array.array(self.typecode, self.example)
479491
a[:1] = a
@@ -557,12 +569,34 @@ def test_setslice(self):
557569

558570
a = array.array(self.typecode, self.example)
559571
self.assertRaises(TypeError, a.__setslice__, 0, 0, None)
572+
self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None)
560573
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None)
561574

562575
b = array.array(self.badtypecode())
563576
self.assertRaises(TypeError, a.__setslice__, 0, 0, b)
577+
self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b)
564578
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b)
565579

580+
def test_extended_set_del_slice(self):
581+
indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
582+
for start in indices:
583+
for stop in indices:
584+
# Everything except the initial 0 (invalid step)
585+
for step in indices[1:]:
586+
a = array.array(self.typecode, self.example)
587+
L = list(a)
588+
# Make sure we have a slice of exactly the right length,
589+
# but with (hopefully) different data.
590+
data = L[start:stop:step]
591+
data.reverse()
592+
L[start:stop:step] = data
593+
a[start:stop:step] = array.array(self.typecode, data)
594+
self.assertEquals(a, array.array(self.typecode, L))
595+
596+
del L[start:stop:step]
597+
del a[start:stop:step]
598+
self.assertEquals(a, array.array(self.typecode, L))
599+
566600
def test_index(self):
567601
example = 2*self.example
568602
a = array.array(self.typecode, example)

Lib/test/test_buffer.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
"""Unit tests for buffer objects.
2+
3+
For now, tests just new or changed functionality.
4+
5+
"""
6+
7+
import unittest
8+
from test import test_support
9+
10+
class BufferTests(unittest.TestCase):
11+
12+
def test_extended_getslice(self):
13+
# Test extended slicing by comparing with list slicing.
14+
s = "".join(chr(c) for c in list(range(255, -1, -1)))
15+
b = buffer(s)
16+
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
17+
for start in indices:
18+
for stop in indices:
19+
# Skip step 0 (invalid)
20+
for step in indices[1:]:
21+
self.assertEqual(b[start:stop:step],
22+
s[start:stop:step])
23+
24+
25+
def test_main():
26+
test_support.run_unittest(BufferTests)
27+
28+
if __name__ == "__main__":
29+
test_main()

Lib/test/test_mmap.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -306,6 +306,40 @@ def test_anonymous(self):
306306
m[x] = ch = chr(x & 255)
307307
self.assertEqual(m[x], ch)
308308

309+
def test_extended_getslice(self):
310+
# Test extended slicing by comparing with list slicing.
311+
s = "".join(chr(c) for c in reversed(range(256)))
312+
m = mmap.mmap(-1, len(s))
313+
m[:] = s
314+
self.assertEqual(m[:], s)
315+
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
316+
for start in indices:
317+
for stop in indices:
318+
# Skip step 0 (invalid)
319+
for step in indices[1:]:
320+
self.assertEqual(m[start:stop:step],
321+
s[start:stop:step])
322+
323+
def test_extended_set_del_slice(self):
324+
# Test extended slicing by comparing with list slicing.
325+
s = "".join(chr(c) for c in reversed(range(256)))
326+
m = mmap.mmap(-1, len(s))
327+
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
328+
for start in indices:
329+
for stop in indices:
330+
# Skip invalid step 0
331+
for step in indices[1:]:
332+
m[:] = s
333+
self.assertEqual(m[:], s)
334+
L = list(s)
335+
# Make sure we have a slice of exactly the right length,
336+
# but with different data.
337+
data = L[start:stop:step]
338+
data = "".join(reversed(data))
339+
L[start:stop:step] = data
340+
m[start:stop:step] = data
341+
self.assertEquals(m[:], "".join(L))
342+
309343
def test_main():
310344
run_unittest(MmapTests)
311345

Lib/test/test_structseq.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,18 @@ def test_reduce(self):
9797
t = time.gmtime()
9898
x = t.__reduce__()
9999

100+
def test_extended_getslice(self):
101+
# Test extended slicing by comparing with list slicing.
102+
t = time.gmtime()
103+
L = list(t)
104+
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
105+
for start in indices:
106+
for stop in indices:
107+
# Skip step 0 (invalid)
108+
for step in indices[1:]:
109+
self.assertEqual(list(t[start:stop:step]),
110+
L[start:stop:step])
111+
100112
def test_main():
101113
test_support.run_unittest(StructSeqTest)
102114

Lib/test/test_userstring.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
# UserString instances should behave similar to builtin string objects.
44

55
import unittest
6+
import string
67
from test import test_support, string_tests
78

89
from UserString import UserString, MutableString
@@ -88,6 +89,28 @@ def test_delslice(self):
8889
del s[-1:10]
8990
self.assertEqual(s, "fo")
9091

92+
def test_extended_set_del_slice(self):
93+
indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
94+
orig = string.ascii_letters + string.digits
95+
for start in indices:
96+
for stop in indices:
97+
# Use indices[1:] when MutableString can handle real
98+
# extended slices
99+
for step in (None, 1, -1):
100+
s = self.type2test(orig)
101+
L = list(orig)
102+
# Make sure we have a slice of exactly the right length,
103+
# but with (hopefully) different data.
104+
data = L[start:stop:step]
105+
data.reverse()
106+
L[start:stop:step] = data
107+
s[start:stop:step] = "".join(data)
108+
self.assertEquals(s, "".join(L))
109+
110+
del L[start:stop:step]
111+
del s[start:stop:step]
112+
self.assertEquals(s, "".join(L))
113+
91114
def test_immutable(self):
92115
s = self.type2test("foobar")
93116
s2 = s.immutable()

0 commit comments

Comments
 (0)