Add support for negative indices in UserString.MutableString.__setitem__

and UserString.MutableString.__delitem__.
This commit is contained in:
Walter Dörwald 2005-02-18 13:22:43 +00:00
parent 612df8e21d
commit af3b39a182
3 changed files with 14 additions and 7 deletions

View file

@ -146,9 +146,13 @@ class MutableString(UserString):
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):