mirror of
https://github.com/python/cpython.git
synced 2025-08-01 07:33:08 +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
|
@ -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 @@ class MutableStringTest(UserStringTest):
|
|||
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