diff options
author | Thomas Wouters <thomas@python.org> | 2007-08-30 22:57:53 (GMT) |
---|---|---|
committer | Thomas Wouters <thomas@python.org> | 2007-08-30 22:57:53 (GMT) |
commit | d2cf20eea2338a0369d4a5707adb01b201f7dfb2 (patch) | |
tree | 59fd4a094906997ae2b0cd520ff09010457da680 /Lib | |
parent | 582b5866174d20f7c027cbb6fb757fefb382f96f (diff) | |
download | cpython-d2cf20eea2338a0369d4a5707adb01b201f7dfb2.zip cpython-d2cf20eea2338a0369d4a5707adb01b201f7dfb2.tar.gz cpython-d2cf20eea2338a0369d4a5707adb01b201f7dfb2.tar.bz2 |
Remove the simple slicing API. All slicing is now done with slice objects.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/UserList.py | 14 | ||||
-rwxr-xr-x | Lib/UserString.py | 15 | ||||
-rw-r--r-- | Lib/opcode.py | 14 | ||||
-rw-r--r-- | Lib/sre_parse.py | 2 | ||||
-rw-r--r-- | Lib/test/list_tests.py | 2 | ||||
-rw-r--r-- | Lib/test/seq_tests.py | 2 | ||||
-rw-r--r-- | Lib/test/string_tests.py | 22 | ||||
-rwxr-xr-x | Lib/test/test_array.py | 2 | ||||
-rw-r--r-- | Lib/test/test_class.py | 41 | ||||
-rw-r--r-- | Lib/test/test_descr.py | 44 | ||||
-rw-r--r-- | Lib/test/test_descrtut.py | 3 | ||||
-rw-r--r-- | Lib/test/test_index.py | 17 | ||||
-rw-r--r-- | Lib/test/test_long.py | 11 | ||||
-rw-r--r-- | Lib/test/test_slice.py | 6 | ||||
-rw-r--r-- | Lib/test/test_support.py | 6 |
15 files changed, 42 insertions, 159 deletions
diff --git a/Lib/UserList.py b/Lib/UserList.py index 072f6a7..116122f 100644 --- a/Lib/UserList.py +++ b/Lib/UserList.py @@ -28,20 +28,6 @@ class UserList: def __getitem__(self, i): return self.data[i] def __setitem__(self, i, item): self.data[i] = item def __delitem__(self, i): del self.data[i] - def __getslice__(self, i, j): - i = max(i, 0); j = max(j, 0) - return self.__class__(self.data[i:j]) - def __setslice__(self, i, j, other): - i = max(i, 0); j = max(j, 0) - if isinstance(other, UserList): - self.data[i:j] = other.data - elif isinstance(other, type(self.data)): - self.data[i:j] = other - else: - self.data[i:j] = list(other) - def __delslice__(self, i, j): - i = max(i, 0); j = max(j, 0) - del self.data[i:j] def __add__(self, other): if isinstance(other, UserList): return self.__class__(self.data + other.data) diff --git a/Lib/UserString.py b/Lib/UserString.py index de50396..8417be7 100755 --- a/Lib/UserString.py +++ b/Lib/UserString.py @@ -63,10 +63,6 @@ class UserString: def __len__(self): return len(self.data) def __getitem__(self, index): return self.__class__(self.data[index]) - def __getslice__(self, start, end): - start = max(start, 0); end = max(end, 0) - return self.__class__(self.data[start:end]) - def __add__(self, other): if isinstance(other, UserString): return self.__class__(self.data + other.data) @@ -220,17 +216,6 @@ class MutableString(UserString): 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): - self.data = self.data[:start]+sub.data+self.data[end:] - elif isinstance(sub, basestring): - self.data = self.data[:start]+sub+self.data[end:] - else: - self.data = self.data[:start]+str(sub)+self.data[end:] - def __delslice__(self, start, end): - start = max(start, 0); end = max(end, 0) - self.data = self.data[:start] + self.data[end:] def immutable(self): return UserString(self.data) def __iadd__(self, other): diff --git a/Lib/opcode.py b/Lib/opcode.py index 4125a79..ac26583 100644 --- a/Lib/opcode.py +++ b/Lib/opcode.py @@ -70,20 +70,6 @@ def_op('BINARY_FLOOR_DIVIDE', 26) def_op('BINARY_TRUE_DIVIDE', 27) def_op('INPLACE_FLOOR_DIVIDE', 28) def_op('INPLACE_TRUE_DIVIDE', 29) -def_op('SLICE+0', 30) -def_op('SLICE+1', 31) -def_op('SLICE+2', 32) -def_op('SLICE+3', 33) - -def_op('STORE_SLICE+0', 40) -def_op('STORE_SLICE+1', 41) -def_op('STORE_SLICE+2', 42) -def_op('STORE_SLICE+3', 43) - -def_op('DELETE_SLICE+0', 50) -def_op('DELETE_SLICE+1', 51) -def_op('DELETE_SLICE+2', 52) -def_op('DELETE_SLICE+3', 53) def_op('INPLACE_ADD', 55) def_op('INPLACE_SUBTRACT', 56) diff --git a/Lib/sre_parse.py b/Lib/sre_parse.py index 7d5c0d6..22e762b 100644 --- a/Lib/sre_parse.py +++ b/Lib/sre_parse.py @@ -139,8 +139,6 @@ class SubPattern: return self.data[index] def __setitem__(self, index, code): self.data[index] = code - def __getslice__(self, start, stop): - return SubPattern(self.pattern, self.data[start:stop]) def insert(self, index, code): self.data.insert(index, code) def append(self, code): diff --git a/Lib/test/list_tests.py b/Lib/test/list_tests.py index a8a7e76..9d53077 100644 --- a/Lib/test/list_tests.py +++ b/Lib/test/list_tests.py @@ -178,10 +178,8 @@ class CommonTest(seq_tests.CommonTest): a[:] = tuple(range(10)) 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): diff --git a/Lib/test/seq_tests.py b/Lib/test/seq_tests.py index 6ec03e8..eb6d141 100644 --- a/Lib/test/seq_tests.py +++ b/Lib/test/seq_tests.py @@ -196,8 +196,6 @@ class CommonTest(unittest.TestCase): self.assertEqual(a[ -pow(2,128): 3 ], self.type2test([0,1,2])) self.assertEqual(a[ 3: pow(2,145) ], self.type2test([3,4])) - self.assertRaises(TypeError, u.__getslice__) - def test_contains(self): u = self.type2test([0, 1, 2]) for i in u: diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py index 916a98f..9e178ca 100644 --- a/Lib/test/string_tests.py +++ b/Lib/test/string_tests.py @@ -939,17 +939,17 @@ class MixinStrUnicodeUserStringTest: self.checkraises(TypeError, 'abc', '__getitem__', 'def') def test_slice(self): - self.checkequal('abc', 'abc', '__getslice__', 0, 1000) - self.checkequal('abc', 'abc', '__getslice__', 0, 3) - self.checkequal('ab', 'abc', '__getslice__', 0, 2) - self.checkequal('bc', 'abc', '__getslice__', 1, 3) - self.checkequal('b', 'abc', '__getslice__', 1, 2) - self.checkequal('', 'abc', '__getslice__', 2, 2) - self.checkequal('', 'abc', '__getslice__', 1000, 1000) - self.checkequal('', 'abc', '__getslice__', 2000, 1000) - self.checkequal('', 'abc', '__getslice__', 2, 1) - - self.checkraises(TypeError, 'abc', '__getslice__', 'def') + self.checkequal('abc', 'abc', '__getitem__', slice(0, 1000)) + self.checkequal('abc', 'abc', '__getitem__', slice(0, 3)) + self.checkequal('ab', 'abc', '__getitem__', slice(0, 2)) + self.checkequal('bc', 'abc', '__getitem__', slice(1, 3)) + self.checkequal('b', 'abc', '__getitem__', slice(1, 2)) + self.checkequal('', 'abc', '__getitem__', slice(2, 2)) + self.checkequal('', 'abc', '__getitem__', slice(1000, 1000)) + self.checkequal('', 'abc', '__getitem__', slice(2000, 1000)) + self.checkequal('', 'abc', '__getitem__', slice(2, 1)) + + self.checkraises(TypeError, 'abc', '__getitem__', 'def') def test_extended_getslice(self): # Test extended slicing by comparing with list slicing. diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index bae496e..db029f3 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -562,12 +562,10 @@ 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) diff --git a/Lib/test/test_class.py b/Lib/test/test_class.py index bde63a8..76b30a3 100644 --- a/Lib/test/test_class.py +++ b/Lib/test/test_class.py @@ -36,11 +36,8 @@ testmeths = [ # List/dict operations "contains", "getitem", - "getslice", "setitem", - "setslice", "delitem", - "delslice", # Unary operations "neg", @@ -288,15 +285,16 @@ class ClassTests(unittest.TestCase): callLst[:] = [] testme[:42] - self.assertCallStack([('__getslice__', (testme, 0, 42))]) + self.assertCallStack([('__getitem__', (testme, slice(None, 42)))]) callLst[:] = [] testme[:42] = "The Answer" - self.assertCallStack([('__setslice__', (testme, 0, 42, "The Answer"))]) + self.assertCallStack([('__setitem__', (testme, slice(None, 42), + "The Answer"))]) callLst[:] = [] del testme[:42] - self.assertCallStack([('__delslice__', (testme, 0, 42))]) + self.assertCallStack([('__delitem__', (testme, slice(None, 42)))]) callLst[:] = [] testme[2:1024:10] @@ -329,37 +327,6 @@ class ClassTests(unittest.TestCase): slice(None, 24, None), 24, 100)))]) - # Now remove the slice hooks to see if converting normal slices to - # slice object works. - - getslice = AllTests.__getslice__ - del AllTests.__getslice__ - setslice = AllTests.__setslice__ - del AllTests.__setslice__ - delslice = AllTests.__delslice__ - del AllTests.__delslice__ - - # XXX when using new-style classes the slice testme[:42] produces - # slice(None, 42, None) instead of slice(0, 42, None). py3k will have - # to change this test. - callLst[:] = [] - testme[0:42] - self.assertCallStack([('__getitem__', (testme, slice(0, 42, None)))]) - - callLst[:] = [] - testme[:42] = "The Answer" - self.assertCallStack([('__setitem__', (testme, slice(None, 42, None), - "The Answer"))]) - callLst[:] = [] - del testme[0:42] - self.assertCallStack([('__delitem__', (testme, slice(0, 42, None)))]) - - # Restore the slice methods, or the tests will fail with regrtest -R. - AllTests.__getslice__ = getslice - AllTests.__setslice__ = setslice - AllTests.__delslice__ = delslice - - def testUnaryOps(self): testme = AllTests() diff --git a/Lib/test/test_descr.py b/Lib/test/test_descr.py index 69400ee..47b647c 100644 --- a/Lib/test/test_descr.py +++ b/Lib/test/test_descr.py @@ -41,7 +41,7 @@ def testbinop(a, b, res, expr="a+b", meth="__add__"): bm = getattr(a, meth) vereq(bm(b), res) -def testternop(a, b, c, res, expr="a[b:c]", meth="__getslice__"): +def testsliceop(a, b, c, res, expr="a[b:c]", meth="__getitem__"): if verbose: print("checking", expr) dict = {'a': a, 'b': b, 'c': c} vereq(eval(expr, dict), res) @@ -50,9 +50,9 @@ def testternop(a, b, c, res, expr="a[b:c]", meth="__getslice__"): while meth not in t.__dict__: t = t.__bases__[0] vereq(m, t.__dict__[meth]) - vereq(m(a, b, c), res) + vereq(m(a, slice(b, c)), res) bm = getattr(a, meth) - vereq(bm(b, c), res) + vereq(bm(slice(b, c)), res) def testsetop(a, b, res, stmt="a+=b", meth="__iadd__"): if verbose: print("checking", stmt) @@ -90,7 +90,7 @@ def testset2op(a, b, c, res, stmt="a[b]=c", meth="__setitem__"): bm(b, c) vereq(dict['a'], res) -def testset3op(a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"): +def testsetsliceop(a, b, c, d, res, stmt="a[b:c]=d", meth="__setitem__"): if verbose: print("checking", stmt) dict = {'a': deepcopy(a), 'b': b, 'c': c, 'd': d} exec(stmt, dict) @@ -101,11 +101,11 @@ def testset3op(a, b, c, d, res, stmt="a[b:c]=d", meth="__setslice__"): m = getattr(t, meth) vereq(m, t.__dict__[meth]) dict['a'] = deepcopy(a) - m(dict['a'], b, c, d) + m(dict['a'], slice(b, c), d) vereq(dict['a'], res) dict['a'] = deepcopy(a) bm = getattr(dict['a'], meth) - bm(b, c, d) + bm(slice(b, c), d) vereq(dict['a'], res) def class_docstrings(): @@ -142,14 +142,15 @@ def lists(): testbinop([1,2,3], 2, 1, "b in a", "__contains__") testbinop([1,2,3], 4, 0, "b in a", "__contains__") testbinop([1,2,3], 1, 2, "a[b]", "__getitem__") - testternop([1,2,3], 0, 2, [1,2], "a[b:c]", "__getslice__") + testsliceop([1,2,3], 0, 2, [1,2], "a[b:c]", "__getitem__") testsetop([1], [2], [1,2], "a+=b", "__iadd__") testsetop([1,2], 3, [1,2,1,2,1,2], "a*=b", "__imul__") testunop([1,2,3], 3, "len(a)", "__len__") testbinop([1,2], 3, [1,2,1,2,1,2], "a*b", "__mul__") testbinop([1,2], 3, [1,2,1,2,1,2], "b*a", "__rmul__") testset2op([1,2], 1, 3, [1,3], "a[b]=c", "__setitem__") - testset3op([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d", "__setslice__") + testsetsliceop([1,2,3,4], 1, 3, [5,6], [1,5,6,4], "a[b:c]=d", + "__setitem__") def dicts(): if verbose: print("Testing dict operations...") @@ -485,8 +486,8 @@ def spamlists(): testbinop(spamlist([1,2,3]), 2, 1, "b in a", "__contains__") testbinop(spamlist([1,2,3]), 4, 0, "b in a", "__contains__") testbinop(spamlist([1,2,3]), 1, 2, "a[b]", "__getitem__") - testternop(spamlist([1,2,3]), 0, 2, spamlist([1,2]), - "a[b:c]", "__getslice__") + testsliceop(spamlist([1,2,3]), 0, 2, spamlist([1,2]), + "a[b:c]", "__getitem__") testsetop(spamlist([1]), spamlist([2]), spamlist([1,2]), "a+=b", "__iadd__") testsetop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*=b", "__imul__") @@ -494,8 +495,8 @@ def spamlists(): testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "a*b", "__mul__") testbinop(spamlist([1,2]), 3, spamlist([1,2,1,2,1,2]), "b*a", "__rmul__") testset2op(spamlist([1,2]), 1, 3, spamlist([1,3]), "a[b]=c", "__setitem__") - testset3op(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), - spamlist([1,5,6,4]), "a[b:c]=d", "__setslice__") + testsetsliceop(spamlist([1,2,3,4]), 1, 3, spamlist([5,6]), + spamlist([1,5,6,4]), "a[b:c]=d", "__setitem__") # Test subclassing class C(spam.spamlist): def foo(self): return 1 @@ -609,9 +610,9 @@ def pylists(): if verbose: print("Testing Python subclass of list...") class C(list): def __getitem__(self, i): + if isinstance(i, slice): + return (i.start, i.stop) return list.__getitem__(self, i) + 100 - def __getslice__(self, i, j): - return (i, j) a = C() a.extend([0,1,2]) vereq(a[0], 100) @@ -1651,13 +1652,6 @@ def overloading(): def __delitem__(self, key): self.delitem = key - def __getslice__(self, i, j): - return ("getslice", i, j) - def __setslice__(self, i, j, value): - self.setslice = (i, j, value) - def __delslice__(self, i, j): - self.delslice = (i, j) - a = C() vereq(a.foo, ("getattr", "foo")) a.foo = 12 @@ -1671,11 +1665,11 @@ def overloading(): del a[12] vereq(a.delitem, 12) - vereq(a[0:10], ("getslice", 0, 10)) + vereq(a[0:10], ("getitem", slice(0, 10))) a[0:10] = "foo" - vereq(a.setslice, (0, 10, "foo")) + vereq(a.setitem, (slice(0, 10), "foo")) del a[0:10] - vereq(a.delslice, (0, 10)) + vereq(a.delitem, slice(0, 10)) def methods(): if verbose: print("Testing methods...") @@ -4116,7 +4110,7 @@ def test_assign_slice(): # tp->tp_as_sequence->sq_ass_slice class C(object): - def __setslice__(self, start, stop, value): + def __setitem__(self, idx, value): self.value = value c = C() diff --git a/Lib/test/test_descrtut.py b/Lib/test/test_descrtut.py index 8080e41..ea75366 100644 --- a/Lib/test/test_descrtut.py +++ b/Lib/test/test_descrtut.py @@ -170,14 +170,12 @@ You can get the information from the list type: '__contains__', '__delattr__', '__delitem__', - '__delslice__', '__doc__', '__eq__', '__format__', '__ge__', '__getattribute__', '__getitem__', - '__getslice__', '__gt__', '__hash__', '__iadd__', @@ -197,7 +195,6 @@ You can get the information from the list type: '__rmul__', '__setattr__', '__setitem__', - '__setslice__', '__str__', 'append', 'count', diff --git a/Lib/test/test_index.py b/Lib/test/test_index.py index 653665e..6275e12 100644 --- a/Lib/test/test_index.py +++ b/Lib/test/test_index.py @@ -177,26 +177,17 @@ class OverflowTestCase(unittest.TestCase): self.assertEqual(self.pos.__index__(), self.pos) self.assertEqual(self.neg.__index__(), self.neg) - def _getitem_helper(self, base): - class GetItem(base): + def test_getitem(self): + class GetItem(object): def __len__(self): return maxint #cannot return long here def __getitem__(self, key): return key - def __getslice__(self, i, j): - return i, j x = GetItem() self.assertEqual(x[self.pos], self.pos) self.assertEqual(x[self.neg], self.neg) - self.assertEqual(x[self.neg:self.pos], (maxint+minsize, maxsize)) - self.assertEqual(x[self.neg:self.pos:1].indices(maxsize), (0, maxsize, 1)) - - def test_getitem(self): - self._getitem_helper(object) - - def test_getitem_classic(self): - class Empty: pass - self._getitem_helper(Empty) + self.assertEqual(x[self.neg:self.pos].indices(maxsize), + (0, maxsize, 1)) def test_sequence_repeat(self): self.failUnlessRaises(OverflowError, lambda: "a" * self.pos) diff --git a/Lib/test/test_long.py b/Lib/test/test_long.py index 4ba825a..bdda5d8 100644 --- a/Lib/test/test_long.py +++ b/Lib/test/test_long.py @@ -283,17 +283,6 @@ class LongTest(unittest.TestCase): self.assert_(type(y) is int, "overflowing int conversion must return long not long subtype") - # long -> Py_ssize_t conversion - class X(object): - def __getslice__(self, i, j): - return i, j - - self.assertEqual(X()[-5:7], (-5, 7)) - # use the clamping effect to test the smallest and largest longs - # that fit a Py_ssize_t - slicemin, slicemax = X()[-2**100:2**100] - self.assertEqual(X()[slicemin:slicemax], (slicemin, slicemax)) - # ----------------------------------- tests of auto int->long conversion def test_auto_overflow(self): diff --git a/Lib/test/test_slice.py b/Lib/test/test_slice.py index 97b3084..cb9d8b2 100644 --- a/Lib/test/test_slice.py +++ b/Lib/test/test_slice.py @@ -99,12 +99,12 @@ class SliceTest(unittest.TestCase): def test_setslice_without_getslice(self): tmp = [] class X(object): - def __setslice__(self, i, j, k): - tmp.append((i, j, k)) + def __setitem__(self, i, k): + tmp.append((i, k)) x = X() x[1:2] = 42 - self.assertEquals(tmp, [(1, 2, 42)]) + self.assertEquals(tmp, [(slice(1, 2), 42)]) def test_pickle(self): s = slice(10, 20, 3) diff --git a/Lib/test/test_support.py b/Lib/test/test_support.py index 637043d..5b2176e 100644 --- a/Lib/test/test_support.py +++ b/Lib/test/test_support.py @@ -423,11 +423,7 @@ _1M = 1024*1024 _1G = 1024 * _1M _2G = 2 * _1G -# Hack to get at the maximum value an internal index can take. -class _Dummy: - def __getslice__(self, i, j): - return j -MAX_Py_ssize_t = _Dummy()[:] +MAX_Py_ssize_t = sys.maxsize def set_memlimit(limit): import re |