mirror of
https://github.com/python/cpython.git
synced 2025-07-28 21:55:21 +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
|
@ -474,6 +474,18 @@ class BaseTest(unittest.TestCase):
|
|||
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 @@ class BaseTest(unittest.TestCase):
|
|||
|
||||
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)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue