Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit ffe33b7

Browse files
committed
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.
1 parent 5c16c7b commit ffe33b7

6 files changed

Lines changed: 64 additions & 58 deletions

File tree

Doc/lib/libstring.tex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,8 @@ \section{\module{string} ---
243243
removed. If given and not \code{None}, \var{chars} must be a string;
244244
the characters in the string will be stripped from the beginning of
245245
the string this method is called on.
246+
\versionchanged[The \var{chars} parameter was added. The \var{chars}
247+
parameter cannot be passed in earlier 2.2 versions]{2.2.3}
246248
\end{funcdesc}
247249

248250
\begin{funcdesc}{rstrip}{s\optional{, chars}}
@@ -251,6 +253,8 @@ \section{\module{string} ---
251253
removed. If given and not \code{None}, \var{chars} must be a string;
252254
the characters in the string will be stripped from the end of the
253255
string this method is called on.
256+
\versionchanged[The \var{chars} parameter was added. The \var{chars}
257+
parameter cannot be passed in 2.2 versions]{2.2.3}
254258
\end{funcdesc}
255259

256260
\begin{funcdesc}{strip}{s\optional{, chars}}
@@ -259,6 +263,8 @@ \section{\module{string} ---
259263
characters are removed. If given and not \code{None}, \var{chars}
260264
must be a string; the characters in the string will be stripped from
261265
the both ends of the string this method is called on.
266+
\versionchanged[The \var{chars} parameter was added. The \var{chars}
267+
parameter cannot be passed in 2.2 or 2.2.1]{2.2.2}
262268
\end{funcdesc}
263269

264270
\begin{funcdesc}{swapcase}{s}

Lib/UserString.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,21 +99,21 @@ def isupper(self): return self.data.isupper()
9999
def join(self, seq): return self.data.join(seq)
100100
def ljust(self, width): return self.__class__(self.data.ljust(width))
101101
def lower(self): return self.__class__(self.data.lower())
102-
def lstrip(self, sep=None): return self.__class__(self.data.lstrip(sep))
102+
def lstrip(self, chars=None): return self.__class__(self.data.lstrip(chars))
103103
def replace(self, old, new, maxsplit=-1):
104104
return self.__class__(self.data.replace(old, new, maxsplit))
105105
def rfind(self, sub, start=0, end=sys.maxint):
106106
return self.data.rfind(sub, start, end)
107107
def rindex(self, sub, start=0, end=sys.maxint):
108108
return self.data.rindex(sub, start, end)
109109
def rjust(self, width): return self.__class__(self.data.rjust(width))
110-
def rstrip(self, sep=None): return self.__class__(self.data.rstrip(sep))
110+
def rstrip(self, chars=None): return self.__class__(self.data.rstrip(chars))
111111
def split(self, sep=None, maxsplit=-1):
112112
return self.data.split(sep, maxsplit)
113113
def splitlines(self, keepends=0): return self.data.splitlines(keepends)
114114
def startswith(self, prefix, start=0, end=sys.maxint):
115115
return self.data.startswith(prefix, start, end)
116-
def strip(self, sep=None): return self.__class__(self.data.strip(sep))
116+
def strip(self, chars=None): return self.__class__(self.data.strip(chars))
117117
def swapcase(self): return self.__class__(self.data.swapcase())
118118
def title(self): return self.__class__(self.data.title())
119119
def translate(self, *args):

Lib/string.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -79,30 +79,31 @@ def strip(s, chars=None):
7979
8080
Return a copy of the string s with leading and trailing
8181
whitespace removed.
82-
If chars is given and not None, remove characters in sep instead.
82+
If chars is given and not None, remove characters in chars instead.
8383
If chars is unicode, S will be converted to unicode before stripping.
8484
8585
"""
8686
return s.strip(chars)
8787

8888
# Strip leading tabs and spaces
89-
def lstrip(s):
90-
"""lstrip(s) -> string
89+
def lstrip(s, chars=None):
90+
"""lstrip(s [,chars]) -> string
9191
9292
Return a copy of the string s with leading whitespace removed.
93+
If chars is given and not None, remove characters in chars instead.
9394
9495
"""
95-
return s.lstrip()
96+
return s.lstrip(chars)
9697

9798
# Strip trailing tabs and spaces
98-
def rstrip(s):
99-
"""rstrip(s) -> string
99+
def rstrip(s, chars=None):
100+
"""rstrip(s [,chars]) -> string
100101
101-
Return a copy of the string s with trailing whitespace
102-
removed.
102+
Return a copy of the string s with trailing whitespace removed.
103+
If chars is given and not None, remove characters in chars instead.
103104
104105
"""
105-
return s.rstrip()
106+
return s.rstrip(chars)
106107

107108

108109
# Split a string into a list of space/tab-separated words

Lib/test/string_tests.py

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,33 @@ def test_strip(self):
195195
self.checkequal(' hello', ' hello ', 'rstrip')
196196
self.checkequal('hello', 'hello', 'strip')
197197

198+
# strip/lstrip/rstrip with None arg
199+
self.checkequal('hello', ' hello ', 'strip', None)
200+
self.checkequal('hello ', ' hello ', 'lstrip', None)
201+
self.checkequal(' hello', ' hello ', 'rstrip', None)
202+
self.checkequal('hello', 'hello', 'strip', None)
203+
204+
# strip/lstrip/rstrip with str arg
205+
self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
206+
self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
207+
self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
208+
self.checkequal('hello', 'hello', 'strip', 'xyz')
209+
210+
# strip/lstrip/rstrip with unicode arg
211+
if test_support.have_unicode:
212+
self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
213+
'strip', unicode('xyz', 'ascii'))
214+
self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
215+
'lstrip', unicode('xyz', 'ascii'))
216+
self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
217+
'rstrip', unicode('xyz', 'ascii'))
218+
self.checkequal(unicode('hello', 'ascii'), 'hello',
219+
'strip', unicode('xyz', 'ascii'))
220+
221+
self.checkraises(TypeError, 'hello', 'strip', 42, 42)
222+
self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
223+
self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
224+
198225
def test_ljust(self):
199226
self.checkequal('abc ', 'abc', 'ljust', 10)
200227
self.checkequal('abc ', 'abc', 'ljust', 6)
@@ -432,34 +459,6 @@ def test_endswith(self):
432459
self.checkraises(TypeError, 'hello', 'endswith')
433460
self.checkraises(TypeError, 'hello', 'endswith', 42)
434461

435-
def test_strip_args(self):
436-
# strip/lstrip/rstrip with None arg
437-
self.checkequal('hello', ' hello ', 'strip', None)
438-
self.checkequal('hello ', ' hello ', 'lstrip', None)
439-
self.checkequal(' hello', ' hello ', 'rstrip', None)
440-
self.checkequal('hello', 'hello', 'strip', None)
441-
442-
# strip/lstrip/rstrip with str arg
443-
self.checkequal('hello', 'xyzzyhelloxyzzy', 'strip', 'xyz')
444-
self.checkequal('helloxyzzy', 'xyzzyhelloxyzzy', 'lstrip', 'xyz')
445-
self.checkequal('xyzzyhello', 'xyzzyhelloxyzzy', 'rstrip', 'xyz')
446-
self.checkequal('hello', 'hello', 'strip', 'xyz')
447-
448-
# strip/lstrip/rstrip with unicode arg
449-
if test_support.have_unicode:
450-
self.checkequal(unicode('hello', 'ascii'), 'xyzzyhelloxyzzy',
451-
'strip', unicode('xyz', 'ascii'))
452-
self.checkequal(unicode('helloxyzzy', 'ascii'), 'xyzzyhelloxyzzy',
453-
'lstrip', unicode('xyz', 'ascii'))
454-
self.checkequal(unicode('xyzzyhello', 'ascii'), 'xyzzyhelloxyzzy',
455-
'rstrip', unicode('xyz', 'ascii'))
456-
self.checkequal(unicode('hello', 'ascii'), 'hello',
457-
'strip', unicode('xyz', 'ascii'))
458-
459-
self.checkraises(TypeError, 'hello', 'strip', 42, 42)
460-
self.checkraises(TypeError, 'hello', 'lstrip', 42, 42)
461-
self.checkraises(TypeError, 'hello', 'rstrip', 42, 42)
462-
463462
def test___contains__(self):
464463
self.checkequal(True, '', '__contains__', '') # vereq('' in '', True)
465464
self.checkequal(True, 'abc', '__contains__', '') # vereq('' in 'abc', True)

Objects/stringobject.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1770,12 +1770,12 @@ do_argstrip(PyStringObject *self, int striptype, PyObject *args)
17701770

17711771

17721772
PyDoc_STRVAR(strip__doc__,
1773-
"S.strip([sep]) -> string or unicode\n\
1773+
"S.strip([chars]) -> string or unicode\n\
17741774
\n\
17751775
Return a copy of the string S with leading and trailing\n\
17761776
whitespace removed.\n\
1777-
If sep is given and not None, remove characters in sep instead.\n\
1778-
If sep is unicode, S will be converted to unicode before stripping");
1777+
If chars is given and not None, remove characters in chars instead.\n\
1778+
If chars is unicode, S will be converted to unicode before stripping");
17791779

17801780
static PyObject *
17811781
string_strip(PyStringObject *self, PyObject *args)
@@ -1788,11 +1788,11 @@ string_strip(PyStringObject *self, PyObject *args)
17881788

17891789

17901790
PyDoc_STRVAR(lstrip__doc__,
1791-
"S.lstrip([sep]) -> string or unicode\n\
1791+
"S.lstrip([chars]) -> string or unicode\n\
17921792
\n\
17931793
Return a copy of the string S with leading whitespace removed.\n\
1794-
If sep is given and not None, remove characters in sep instead.\n\
1795-
If sep is unicode, S will be converted to unicode before stripping");
1794+
If chars is given and not None, remove characters in chars instead.\n\
1795+
If chars is unicode, S will be converted to unicode before stripping");
17961796

17971797
static PyObject *
17981798
string_lstrip(PyStringObject *self, PyObject *args)
@@ -1805,11 +1805,11 @@ string_lstrip(PyStringObject *self, PyObject *args)
18051805

18061806

18071807
PyDoc_STRVAR(rstrip__doc__,
1808-
"S.rstrip([sep]) -> string or unicode\n\
1808+
"S.rstrip([chars]) -> string or unicode\n\
18091809
\n\
18101810
Return a copy of the string S with trailing whitespace removed.\n\
1811-
If sep is given and not None, remove characters in sep instead.\n\
1812-
If sep is unicode, S will be converted to unicode before stripping");
1811+
If chars is given and not None, remove characters in chars instead.\n\
1812+
If chars is unicode, S will be converted to unicode before stripping");
18131813

18141814
static PyObject *
18151815
string_rstrip(PyStringObject *self, PyObject *args)

Objects/unicodeobject.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5247,12 +5247,12 @@ do_argstrip(PyUnicodeObject *self, int striptype, PyObject *args)
52475247

52485248

52495249
PyDoc_STRVAR(strip__doc__,
5250-
"S.strip([sep]) -> unicode\n\
5250+
"S.strip([chars]) -> unicode\n\
52515251
\n\
52525252
Return a copy of the string S with leading and trailing\n\
52535253
whitespace removed.\n\
5254-
If sep is given and not None, remove characters in sep instead.\n\
5255-
If sep is a str, it will be converted to unicode before stripping");
5254+
If chars is given and not None, remove characters in chars instead.\n\
5255+
If chars is a str, it will be converted to unicode before stripping");
52565256

52575257
static PyObject *
52585258
unicode_strip(PyUnicodeObject *self, PyObject *args)
@@ -5265,11 +5265,11 @@ unicode_strip(PyUnicodeObject *self, PyObject *args)
52655265

52665266

52675267
PyDoc_STRVAR(lstrip__doc__,
5268-
"S.lstrip([sep]) -> unicode\n\
5268+
"S.lstrip([chars]) -> unicode\n\
52695269
\n\
52705270
Return a copy of the string S with leading whitespace removed.\n\
5271-
If sep is given and not None, remove characters in sep instead.\n\
5272-
If sep is a str, it will be converted to unicode before stripping");
5271+
If chars is given and not None, remove characters in chars instead.\n\
5272+
If chars is a str, it will be converted to unicode before stripping");
52735273

52745274
static PyObject *
52755275
unicode_lstrip(PyUnicodeObject *self, PyObject *args)
@@ -5282,11 +5282,11 @@ unicode_lstrip(PyUnicodeObject *self, PyObject *args)
52825282

52835283

52845284
PyDoc_STRVAR(rstrip__doc__,
5285-
"S.rstrip([sep]) -> unicode\n\
5285+
"S.rstrip([chars]) -> unicode\n\
52865286
\n\
52875287
Return a copy of the string S with trailing whitespace removed.\n\
5288-
If sep is given and not None, remove characters in sep instead.\n\
5289-
If sep is a str, it will be converted to unicode before stripping");
5288+
If chars is given and not None, remove characters in chars instead.\n\
5289+
If chars is a str, it will be converted to unicode before stripping");
52905290

52915291
static PyObject *
52925292
unicode_rstrip(PyUnicodeObject *self, PyObject *args)

0 commit comments

Comments
 (0)