summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2002-08-09 01:37:06 (GMT)
committerRaymond Hettinger <python@rcn.com>2002-08-09 01:37:06 (GMT)
commitc35491ee3a0f3999791de83e65ef94994058ac5e (patch)
treeeb74cd0ee121ab16cae10271641729da8b28d629
parent48923c5533865173894bb4b1d8f9851430f49f8b (diff)
downloadcpython-c35491ee3a0f3999791de83e65ef94994058ac5e.zip
cpython-c35491ee3a0f3999791de83e65ef94994058ac5e.tar.gz
cpython-c35491ee3a0f3999791de83e65ef94994058ac5e.tar.bz2
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.
-rwxr-xr-xLib/UserString.py22
-rw-r--r--Lib/test/string_tests.py6
-rw-r--r--Lib/test/test_string.py1
-rwxr-xr-xLib/test/test_userstring.py1
4 files changed, 19 insertions, 11 deletions
diff --git a/Lib/UserString.py b/Lib/UserString.py
index 8209820..8bd2c42 100755
--- a/Lib/UserString.py
+++ b/Lib/UserString.py
@@ -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:
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index 1729999..a071f20 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -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')
diff --git a/Lib/test/test_string.py b/Lib/test/test_string.py
index c92f5f7..a30cdbd 100644
--- a/Lib/test/test_string.py
+++ b/Lib/test/test_string.py
@@ -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
diff --git a/Lib/test/test_userstring.py b/Lib/test/test_userstring.py
index 5492f2e..67fbb5c 100755
--- a/Lib/test/test_userstring.py
+++ b/Lib/test/test_userstring.py
@@ -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)