]> granicus.if.org Git - python/commitdiff
Moved inplace add and multiply methods from UserString to MutableString.
authorRaymond Hettinger <python@rcn.com>
Fri, 9 Aug 2002 01:37:06 +0000 (01:37 +0000)
committerRaymond Hettinger <python@rcn.com>
Fri, 9 Aug 2002 01:37:06 +0000 (01:37 +0000)
Closes SF Bug #592573 where inplace add mutated a UserString.
Added unittests to verify the bug is cleared.

Lib/UserString.py
Lib/test/string_tests.py
Lib/test/test_string.py
Lib/test/test_userstring.py

index 8209820aeb1db07aa5ec417fb05a96643810de8c..8bd2c422abca2386735efa05c0f8d80def2e01a0 100755 (executable)
@@ -52,20 +52,9 @@ class UserString:
             return self.__class__(other + self.data)
         else:
             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):
         return self.__class__(self.data*n)
     __rmul__ = __mul__
-    def __imul__(self, n):
-        self.data *= n
-        return self
 
     # the following methods are defined in alphabetical order:
     def capitalize(self): return self.__class__(self.data.capitalize())
@@ -168,6 +157,17 @@ class MutableString(UserString):
         self.data = self.data[:start] + self.data[end:]
     def immutable(self):
         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__":
     # execute the regression test to stdout, if called as a script:
index 1729999366cefc24c9b6a2ceebe68adddfa6cdf0..a071f2008960cb04dfc2930365241f5dcdaec2dd 100644 (file)
@@ -314,3 +314,9 @@ def run_contains_tests(test):
     test('__contains__', 'asdf', True, 'asdf') # vereq('asdf' in 'asdf', True)
     test('__contains__', 'asd', False, 'asdf') # vereq('asdf' in 'asd', 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')
index c92f5f7d1f77806f0a4a36e1f3361e0c8f6dac2d..a30cdbd9c3c2054aad570ec73369134197ac6a0a 100644 (file)
@@ -52,6 +52,7 @@ def test(name, input, output, *args):
 string_tests.run_module_tests(test)
 string_tests.run_method_tests(test)
 string_tests.run_contains_tests(test)
+string_tests.run_inplace_tests(str)
 
 string.whitespace
 string.lowercase
index 5492f2e526d23122c686106d68cc71d008267d5b..67fbb5c00e2dc842e2d6ba074131505acc0f5a4d 100755 (executable)
@@ -42,3 +42,4 @@ def test(methodname, input, output, *args):
 
 string_tests.run_method_tests(test)
 string_tests.run_contains_tests(test)
+string_tests.run_inplace_tests(UserString)