Add support for negative indices in UserString.MutableString.__setitem__
and UserString.MutableString.__delitem__.
diff --git a/Lib/UserString.py b/Lib/UserString.py
index e8e0fed..3251139 100755
--- a/Lib/UserString.py
+++ b/Lib/UserString.py
@@ -146,9 +146,13 @@
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:]
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:]
def __setslice__(self, start, end, sub):
diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py
index 95f1351..53114db 100755
--- a/Lib/test/test_userstring.py
+++ b/Lib/test/test_userstring.py
@@ -52,20 +52,20 @@
def test_setitem(self):
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")
+ s[-1] = "bar"
+ self.assertEqual(s, "fobar")
s[0] = "bar"
- self.assertEqual(s, "baroo")
- s[4] = "foo"
- self.assertEqual(s, "barofoo")
+ self.assertEqual(s, "barobar")
def test_delitem(self):
s = self.type2test("foo")
- self.assertRaises(IndexError, s.__delitem__, -1)
+ self.assertRaises(IndexError, s.__delitem__, -4)
self.assertRaises(IndexError, s.__delitem__, 3)
+ del s[-1]
+ self.assertEqual(s, "fo")
del s[0]
- self.assertEqual(s, "oo")
- del s[1]
self.assertEqual(s, "o")
del s[0]
self.assertEqual(s, "")