Attempt to make all the various string *strip methods the same.
* Doc - add doc for when functions were added
* UserString
* string object methods
* string module functions
'chars' is used for the last parameter everywhere.
These changes will be backported, since part of the changes
have already been made, but they were inconsistent.
diff --git a/Lib/UserString.py b/Lib/UserString.py
index 2819ab0..fcd115d 100755
--- a/Lib/UserString.py
+++ b/Lib/UserString.py
@@ -99,7 +99,7 @@
def join(self, seq): return self.data.join(seq)
def ljust(self, width): return self.__class__(self.data.ljust(width))
def lower(self): return self.__class__(self.data.lower())
- def lstrip(self, sep=None): return self.__class__(self.data.lstrip(sep))
+ def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
def replace(self, old, new, maxsplit=-1):
return self.__class__(self.data.replace(old, new, maxsplit))
def rfind(self, sub, start=0, end=sys.maxint):
@@ -107,13 +107,13 @@
def rindex(self, sub, start=0, end=sys.maxint):
return self.data.rindex(sub, start, end)
def rjust(self, width): return self.__class__(self.data.rjust(width))
- def rstrip(self, sep=None): return self.__class__(self.data.rstrip(sep))
+ def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars))
def split(self, sep=None, maxsplit=-1):
return self.data.split(sep, maxsplit)
def splitlines(self, keepends=0): return self.data.splitlines(keepends)
def startswith(self, prefix, start=0, end=sys.maxint):
return self.data.startswith(prefix, start, end)
- def strip(self, sep=None): return self.__class__(self.data.strip(sep))
+ def strip(self, chars=None): return self.__class__(self.data.strip(chars))
def swapcase(self): return self.__class__(self.data.swapcase())
def title(self): return self.__class__(self.data.title())
def translate(self, *args):
diff --git a/Lib/string.py b/Lib/string.py
index 021469c..a375249 100644
--- a/Lib/string.py
+++ b/Lib/string.py
@@ -79,30 +79,31 @@
Return a copy of the string s with leading and trailing
whitespace removed.
- If chars is given and not None, remove characters in sep instead.
+ If chars is given and not None, remove characters in chars instead.
If chars is unicode, S will be converted to unicode before stripping.
"""
return s.strip(chars)
# Strip leading tabs and spaces
-def lstrip(s):
- """lstrip(s) -> string
+def lstrip(s, chars=None):
+ """lstrip(s [,chars]) -> string
Return a copy of the string s with leading whitespace removed.
+ If chars is given and not None, remove characters in chars instead.
"""
- return s.lstrip()
+ return s.lstrip(chars)
# Strip trailing tabs and spaces
-def rstrip(s):
- """rstrip(s) -> string
+def rstrip(s, chars=None):
+ """rstrip(s [,chars]) -> string
- Return a copy of the string s with trailing whitespace
- removed.
+ Return a copy of the string s with trailing whitespace removed.
+ If chars is given and not None, remove characters in chars instead.
"""
- return s.rstrip()
+ return s.rstrip(chars)
# Split a string into a list of space/tab-separated words
diff --git a/Lib/test/string_tests.py b/Lib/test/string_tests.py
index dcf961d..591b409 100644
--- a/Lib/test/string_tests.py
+++ b/Lib/test/string_tests.py
@@ -195,6 +195,33 @@
self.checkequal(' hello', ' hello ', 'rstrip')
self.checkequal('hello', 'hello', 'strip')
+ # strip/lstrip/rstrip with None arg
+ self.checkequal('hello', ' hello ', 'strip', None)
+ self.checkequal('hello ', ' hello ', 'lstrip', None)
+ self.checkequal(' hello', ' hello ', 'rstrip', None)
+ self.checkequal('hello', 'hello', 'strip', None)
+
+ # strip/lstrip/rstrip with str arg
+ self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
+ self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
+ self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
+ self.checkequal('hello', 'hello', 'strip', 'xyz')
+
+ # strip/lstrip/rstrip with unicode arg
+ if test_support.have_unicode:
+ self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
+ 'strip', unicode('xyz', 'ascii'))
+ self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
+ 'lstrip', unicode('xyz', 'ascii'))
+ self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
+ 'rstrip', unicode('xyz', 'ascii'))
+ self.checkequal(unicode('hello', 'ascii'), 'hello',
+ 'strip', unicode('xyz', 'ascii'))
+
+ self.checkraises(TypeError, 'hello', 'strip', 42, 42)
+ self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
+ self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
+
def test_ljust(self):
self.checkequal('abc ', 'abc', 'ljust', 10)
self.checkequal('abc ', 'abc', 'ljust', 6)
@@ -432,34 +459,6 @@
self.checkraises(TypeError, 'hello', 'endswith')
self.checkraises(TypeError, 'hello', 'endswith', 42)
- def test_strip_args(self):
- # strip/lstrip/rstrip with None arg
- self.checkequal('hello', ' hello ', 'strip', None)
- self.checkequal('hello ', ' hello ', 'lstrip', None)
- self.checkequal(' hello', ' hello ', 'rstrip', None)
- self.checkequal('hello', 'hello', 'strip', None)
-
- # strip/lstrip/rstrip with str arg
- self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
- self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
- self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
- self.checkequal('hello', 'hello', 'strip', 'xyz')
-
- # strip/lstrip/rstrip with unicode arg
- if test_support.have_unicode:
- self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
- 'strip', unicode('xyz', 'ascii'))
- self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
- 'lstrip', unicode('xyz', 'ascii'))
- self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
- 'rstrip', unicode('xyz', 'ascii'))
- self.checkequal(unicode('hello', 'ascii'), 'hello',
- 'strip', unicode('xyz', 'ascii'))
-
- self.checkraises(TypeError, 'hello', 'strip', 42, 42)
- self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
- self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
-
def test___contains__(self):
self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)