mirror of
https://github.com/python/cpython.git
synced 2025-10-21 22:22:48 +00:00
Add support for negative indices in UserString.MutableString.__setitem__
and UserString.MutableString.__delitem__.
This commit is contained in:
parent
612df8e21d
commit
af3b39a182
3 changed files with 14 additions and 7 deletions
|
@ -146,9 +146,13 @@ class MutableString(UserString):
|
||||||
def __hash__(self):
|
def __hash__(self):
|
||||||
raise TypeError, "unhashable type (it is mutable)"
|
raise TypeError, "unhashable type (it is mutable)"
|
||||||
def __setitem__(self, index, sub):
|
def __setitem__(self, index, sub):
|
||||||
|
if index < 0:
|
||||||
|
index += len(self.data)
|
||||||
if index < 0 or index >= len(self.data): raise IndexError
|
if index < 0 or index >= len(self.data): raise IndexError
|
||||||
self.data = self.data[:index] + sub + self.data[index+1:]
|
self.data = self.data[:index] + sub + self.data[index+1:]
|
||||||
def __delitem__(self, index):
|
def __delitem__(self, index):
|
||||||
|
if index < 0:
|
||||||
|
index += len(self.data)
|
||||||
if index < 0 or index >= len(self.data): raise IndexError
|
if index < 0 or index >= len(self.data): raise IndexError
|
||||||
self.data = self.data[:index] + self.data[index+1:]
|
self.data = self.data[:index] + self.data[index+1:]
|
||||||
def __setslice__(self, start, end, sub):
|
def __setslice__(self, start, end, sub):
|
||||||
|
|
|
@ -52,20 +52,20 @@ class MutableStringTest(UserStringTest):
|
||||||
|
|
||||||
def test_setitem(self):
|
def test_setitem(self):
|
||||||
s = self.type2test("foo")
|
s = self.type2test("foo")
|
||||||
self.assertRaises(IndexError, s.__setitem__, -1, "bar")
|
self.assertRaises(IndexError, s.__setitem__, -4, "bar")
|
||||||
self.assertRaises(IndexError, s.__setitem__, 3, "bar")
|
self.assertRaises(IndexError, s.__setitem__, 3, "bar")
|
||||||
|
s[-1] = "bar"
|
||||||
|
self.assertEqual(s, "fobar")
|
||||||
s[0] = "bar"
|
s[0] = "bar"
|
||||||
self.assertEqual(s, "baroo")
|
self.assertEqual(s, "barobar")
|
||||||
s[4] = "foo"
|
|
||||||
self.assertEqual(s, "barofoo")
|
|
||||||
|
|
||||||
def test_delitem(self):
|
def test_delitem(self):
|
||||||
s = self.type2test("foo")
|
s = self.type2test("foo")
|
||||||
self.assertRaises(IndexError, s.__delitem__, -1)
|
self.assertRaises(IndexError, s.__delitem__, -4)
|
||||||
self.assertRaises(IndexError, s.__delitem__, 3)
|
self.assertRaises(IndexError, s.__delitem__, 3)
|
||||||
|
del s[-1]
|
||||||
|
self.assertEqual(s, "fo")
|
||||||
del s[0]
|
del s[0]
|
||||||
self.assertEqual(s, "oo")
|
|
||||||
del s[1]
|
|
||||||
self.assertEqual(s, "o")
|
self.assertEqual(s, "o")
|
||||||
del s[0]
|
del s[0]
|
||||||
self.assertEqual(s, "")
|
self.assertEqual(s, "")
|
||||||
|
|
|
@ -152,6 +152,9 @@ Library
|
||||||
- The reconvert.quote function can now emit triple-quoted strings. The
|
- The reconvert.quote function can now emit triple-quoted strings. The
|
||||||
reconvert module now has some simple documentation.
|
reconvert module now has some simple documentation.
|
||||||
|
|
||||||
|
- ``UserString.MutableString`` now supports negative indices in
|
||||||
|
``__setitem__`` and ``__delitem__``
|
||||||
|
|
||||||
Build
|
Build
|
||||||
-----
|
-----
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue