mirror of
https://github.com/python/cpython.git
synced 2026-03-21 20:26:14 +00:00
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.
This commit is contained in:
parent
0f4a14b56f
commit
3ccec68a05
16 changed files with 730 additions and 120 deletions
|
|
@ -149,15 +149,41 @@ def __init__(self, string=""):
|
|||
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):
|
||||
|
|
|
|||
|
|
@ -179,8 +179,10 @@ def test_setslice(self):
|
|||
self.assertEqual(a, self.type2test(range(10)))
|
||||
|
||||
self.assertRaises(TypeError, a.__setslice__, 0, 1, 5)
|
||||
self.assertRaises(TypeError, a.__setitem__, slice(0, 1, 5))
|
||||
|
||||
self.assertRaises(TypeError, a.__setslice__)
|
||||
self.assertRaises(TypeError, a.__setitem__)
|
||||
|
||||
def test_delslice(self):
|
||||
a = self.type2test([0, 1])
|
||||
|
|
|
|||
|
|
@ -912,7 +912,6 @@ def test_subscript(self):
|
|||
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')
|
||||
|
||||
|
|
@ -926,10 +925,21 @@ def test_slice(self):
|
|||
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)
|
||||
|
|
|
|||
|
|
@ -474,6 +474,18 @@ def test_getslice(self):
|
|||
array.array(self.typecode)
|
||||
)
|
||||
|
||||
def test_extended_getslice(self):
|
||||
# Test extended slicing by comparing with list slicing
|
||||
# (Assumes list conversion works correctly, too)
|
||||
a = array.array(self.typecode, self.example)
|
||||
indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
|
||||
for start in indices:
|
||||
for stop in indices:
|
||||
# Everything except the initial 0 (invalid step)
|
||||
for step in indices[1:]:
|
||||
self.assertEqual(list(a[start:stop:step]),
|
||||
list(a)[start:stop:step])
|
||||
|
||||
def test_setslice(self):
|
||||
a = array.array(self.typecode, self.example)
|
||||
a[:1] = a
|
||||
|
|
@ -557,12 +569,34 @@ def test_setslice(self):
|
|||
|
||||
a = array.array(self.typecode, self.example)
|
||||
self.assertRaises(TypeError, a.__setslice__, 0, 0, None)
|
||||
self.assertRaises(TypeError, a.__setitem__, slice(0, 0), None)
|
||||
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), None)
|
||||
|
||||
b = array.array(self.badtypecode())
|
||||
self.assertRaises(TypeError, a.__setslice__, 0, 0, b)
|
||||
self.assertRaises(TypeError, a.__setitem__, slice(0, 0), b)
|
||||
self.assertRaises(TypeError, a.__setitem__, slice(0, 1), b)
|
||||
|
||||
def test_extended_set_del_slice(self):
|
||||
indices = (0, None, 1, 3, 19, 100, -1, -2, -31, -100)
|
||||
for start in indices:
|
||||
for stop in indices:
|
||||
# Everything except the initial 0 (invalid step)
|
||||
for step in indices[1:]:
|
||||
a = array.array(self.typecode, self.example)
|
||||
L = list(a)
|
||||
# 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
|
||||
a[start:stop:step] = array.array(self.typecode, data)
|
||||
self.assertEquals(a, array.array(self.typecode, L))
|
||||
|
||||
del L[start:stop:step]
|
||||
del a[start:stop:step]
|
||||
self.assertEquals(a, array.array(self.typecode, L))
|
||||
|
||||
def test_index(self):
|
||||
example = 2*self.example
|
||||
a = array.array(self.typecode, example)
|
||||
|
|
|
|||
29
Lib/test/test_buffer.py
Normal file
29
Lib/test/test_buffer.py
Normal file
|
|
@ -0,0 +1,29 @@
|
|||
"""Unit tests for buffer objects.
|
||||
|
||||
For now, tests just new or changed functionality.
|
||||
|
||||
"""
|
||||
|
||||
import unittest
|
||||
from test import test_support
|
||||
|
||||
class BufferTests(unittest.TestCase):
|
||||
|
||||
def test_extended_getslice(self):
|
||||
# Test extended slicing by comparing with list slicing.
|
||||
s = "".join(chr(c) for c in list(range(255, -1, -1)))
|
||||
b = buffer(s)
|
||||
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
|
||||
for start in indices:
|
||||
for stop in indices:
|
||||
# Skip step 0 (invalid)
|
||||
for step in indices[1:]:
|
||||
self.assertEqual(b[start:stop:step],
|
||||
s[start:stop:step])
|
||||
|
||||
|
||||
def test_main():
|
||||
test_support.run_unittest(BufferTests)
|
||||
|
||||
if __name__ == "__main__":
|
||||
test_main()
|
||||
|
|
@ -306,6 +306,40 @@ def test_anonymous(self):
|
|||
m[x] = ch = chr(x & 255)
|
||||
self.assertEqual(m[x], ch)
|
||||
|
||||
def test_extended_getslice(self):
|
||||
# Test extended slicing by comparing with list slicing.
|
||||
s = "".join(chr(c) for c in reversed(range(256)))
|
||||
m = mmap.mmap(-1, len(s))
|
||||
m[:] = s
|
||||
self.assertEqual(m[:], s)
|
||||
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
|
||||
for start in indices:
|
||||
for stop in indices:
|
||||
# Skip step 0 (invalid)
|
||||
for step in indices[1:]:
|
||||
self.assertEqual(m[start:stop:step],
|
||||
s[start:stop:step])
|
||||
|
||||
def test_extended_set_del_slice(self):
|
||||
# Test extended slicing by comparing with list slicing.
|
||||
s = "".join(chr(c) for c in reversed(range(256)))
|
||||
m = mmap.mmap(-1, len(s))
|
||||
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
|
||||
for start in indices:
|
||||
for stop in indices:
|
||||
# Skip invalid step 0
|
||||
for step in indices[1:]:
|
||||
m[:] = s
|
||||
self.assertEqual(m[:], s)
|
||||
L = list(s)
|
||||
# Make sure we have a slice of exactly the right length,
|
||||
# but with different data.
|
||||
data = L[start:stop:step]
|
||||
data = "".join(reversed(data))
|
||||
L[start:stop:step] = data
|
||||
m[start:stop:step] = data
|
||||
self.assertEquals(m[:], "".join(L))
|
||||
|
||||
def test_main():
|
||||
run_unittest(MmapTests)
|
||||
|
||||
|
|
|
|||
|
|
@ -97,6 +97,18 @@ def test_reduce(self):
|
|||
t = time.gmtime()
|
||||
x = t.__reduce__()
|
||||
|
||||
def test_extended_getslice(self):
|
||||
# Test extended slicing by comparing with list slicing.
|
||||
t = time.gmtime()
|
||||
L = list(t)
|
||||
indices = (0, None, 1, 3, 19, 300, -1, -2, -31, -300)
|
||||
for start in indices:
|
||||
for stop in indices:
|
||||
# Skip step 0 (invalid)
|
||||
for step in indices[1:]:
|
||||
self.assertEqual(list(t[start:stop:step]),
|
||||
L[start:stop:step])
|
||||
|
||||
def test_main():
|
||||
test_support.run_unittest(StructSeqTest)
|
||||
|
||||
|
|
|
|||
|
|
@ -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 @@ def test_delslice(self):
|
|||
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()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue