Commit c35491ee authored by Raymond Hettinger's avatar Raymond Hettinger

Moved inplace add and multiply methods from UserString to MutableString.

Closes SF Bug #592573 where inplace add mutated a UserString.
Added unittests to verify the bug is cleared.
parent 48923c55
...@@ -52,20 +52,9 @@ class UserString: ...@@ -52,20 +52,9 @@ class UserString:
return self.__class__(other + self.data) return self.__class__(other + self.data)
else: else:
return self.__class__(str(other) + self.data) return self.__class__(str(other) + self.data)
def __iadd__(self, other):
if isinstance(other, UserString):
self.data += other.data
elif isinstance(other, StringTypes):
self.data += other
else:
self.data += str(other)
return self
def __mul__(self, n): def __mul__(self, n):
return self.__class__(self.data*n) return self.__class__(self.data*n)
__rmul__ = __mul__ __rmul__ = __mul__
def __imul__(self, n):
self.data *= n
return self
# the following methods are defined in alphabetical order: # the following methods are defined in alphabetical order:
def capitalize(self): return self.__class__(self.data.capitalize()) def capitalize(self): return self.__class__(self.data.capitalize())
...@@ -168,6 +157,17 @@ class MutableString(UserString): ...@@ -168,6 +157,17 @@ class MutableString(UserString):
self.data = self.data[:start] + self.data[end:] self.data = self.data[:start] + self.data[end:]
def immutable(self): def immutable(self):
return UserString(self.data) return UserString(self.data)
def __iadd__(self, other):
if isinstance(other, UserString):
self.data += other.data
elif isinstance(other, StringTypes):
self.data += other
else:
self.data += str(other)
return self
def __imul__(self, n):
self.data *= n
return self
if __name__ == "__main__": if __name__ == "__main__":
# execute the regression test to stdout, if called as a script: # execute the regression test to stdout, if called as a script:
......
...@@ -314,3 +314,9 @@ def run_contains_tests(test): ...@@ -314,3 +314,9 @@ def run_contains_tests(test):
test('__contains__', 'asdf', True, 'asdf') # vereq('asdf' in 'asdf', True) test('__contains__', 'asdf', True, 'asdf') # vereq('asdf' in 'asdf', True)
test('__contains__', 'asd', False, 'asdf') # vereq('asdf' in 'asd', False) test('__contains__', 'asd', False, 'asdf') # vereq('asdf' in 'asd', False)
test('__contains__', '', False, 'asdf') # vereq('asdf' in '', False) test('__contains__', '', False, 'asdf') # vereq('asdf' in '', False)
def run_inplace_tests(constructor):
# Verify clearing of SF bug #592573
s = t = constructor('abc')
s += constructor('def')
verify(s != t, 'in-place concatenate should create a new object')
...@@ -52,6 +52,7 @@ def test(name, input, output, *args): ...@@ -52,6 +52,7 @@ def test(name, input, output, *args):
string_tests.run_module_tests(test) string_tests.run_module_tests(test)
string_tests.run_method_tests(test) string_tests.run_method_tests(test)
string_tests.run_contains_tests(test) string_tests.run_contains_tests(test)
string_tests.run_inplace_tests(str)
string.whitespace string.whitespace
string.lowercase string.lowercase
......
...@@ -42,3 +42,4 @@ def test(methodname, input, output, *args): ...@@ -42,3 +42,4 @@ def test(methodname, input, output, *args):
string_tests.run_method_tests(test) string_tests.run_method_tests(test)
string_tests.run_contains_tests(test) string_tests.run_contains_tests(test)
string_tests.run_inplace_tests(UserString)
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment