Skip to content

Commit b5812ed

Browse files
author
rhettinger
committed
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. git-svn-id: http://svn.python.org/projects/python/trunk@28089 6015fed2-1504-0410-9fe1-9d1591cc4771
1 parent 2204237 commit b5812ed

4 files changed

Lines changed: 19 additions & 11 deletions

File tree

Lib/UserString.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -52,20 +52,9 @@ def __radd__(self, other):
5252
return self.__class__(other + self.data)
5353
else:
5454
return self.__class__(str(other) + self.data)
55-
def __iadd__(self, other):
56-
if isinstance(other, UserString):
57-
self.data += other.data
58-
elif isinstance(other, StringTypes):
59-
self.data += other
60-
else:
61-
self.data += str(other)
62-
return self
6355
def __mul__(self, n):
6456
return self.__class__(self.data*n)
6557
__rmul__ = __mul__
66-
def __imul__(self, n):
67-
self.data *= n
68-
return self
6958

7059
# the following methods are defined in alphabetical order:
7160
def capitalize(self): return self.__class__(self.data.capitalize())
@@ -168,6 +157,17 @@ def __delslice__(self, start, end):
168157
self.data = self.data[:start] + self.data[end:]
169158
def immutable(self):
170159
return UserString(self.data)
160+
def __iadd__(self, other):
161+
if isinstance(other, UserString):
162+
self.data += other.data
163+
elif isinstance(other, StringTypes):
164+
self.data += other
165+
else:
166+
self.data += str(other)
167+
return self
168+
def __imul__(self, n):
169+
self.data *= n
170+
return self
171171

172172
if __name__ == "__main__":
173173
# execute the regression test to stdout, if called as a script:

Lib/test/string_tests.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -314,3 +314,9 @@ def run_contains_tests(test):
314314
test('__contains__', 'asdf', True, 'asdf') # vereq('asdf' in 'asdf', True)
315315
test('__contains__', 'asd', False, 'asdf') # vereq('asdf' in 'asd', False)
316316
test('__contains__', '', False, 'asdf') # vereq('asdf' in '', False)
317+
318+
def run_inplace_tests(constructor):
319+
# Verify clearing of SF bug #592573
320+
s = t = constructor('abc')
321+
s += constructor('def')
322+
verify(s != t, 'in-place concatenate should create a new object')

Lib/test/test_string.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ def __repr__(self):
5252
string_tests.run_module_tests(test)
5353
string_tests.run_method_tests(test)
5454
string_tests.run_contains_tests(test)
55+
string_tests.run_inplace_tests(str)
5556

5657
string.whitespace
5758
string.lowercase

Lib/test/test_userstring.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,3 +42,4 @@ def test(methodname, input, output, *args):
4242

4343
string_tests.run_method_tests(test)
4444
string_tests.run_contains_tests(test)
45+
string_tests.run_inplace_tests(UserString)

0 commit comments

Comments
 (0)