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

Skip to content

Commit 0fbbce9

Browse files
committed
Merge #15510: clarify textwrap's handling of whitespace, and add confirming tests.
Patch by Chris Jerdonek.
2 parents 63755f3 + 1585b70 commit 0fbbce9

2 files changed

Lines changed: 69 additions & 14 deletions

File tree

Doc/library/textwrap.rst

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ otherwise, you should use an instance of :class:`TextWrapper` for efficiency.
2525
Optional keyword arguments correspond to the instance attributes of
2626
:class:`TextWrapper`, documented below. *width* defaults to ``70``.
2727

28+
See the :meth:`TextWrapper.wrap` method for additional details on how
29+
:func:`wrap` behaves.
30+
2831

2932
.. function:: fill(text, width=70, **kwargs)
3033

@@ -167,15 +170,18 @@ in a block of text.
167170

168171
.. attribute:: drop_whitespace
169172

170-
(default: ``True``) If true, whitespace that, after wrapping, happens to
171-
end up at the beginning or end of a line is dropped (leading whitespace in
172-
the first line is always preserved, though).
173+
(default: ``True``) If true, whitespace at the beginning and ending of
174+
every line (after wrapping but before indenting) is dropped.
175+
Whitespace at the beginning of the paragraph, however, is not dropped
176+
if non-whitespace follows it. If whitespace being dropped takes up an
177+
entire line, the whole line is dropped.
173178

174179

175180
.. attribute:: initial_indent
176181

177182
(default: ``''``) String that will be prepended to the first line of
178-
wrapped output. Counts towards the length of the first line.
183+
wrapped output. Counts towards the length of the first line. The empty
184+
string is not indented.
179185

180186

181187
.. attribute:: subsequent_indent
@@ -236,8 +242,9 @@ in a block of text.
236242

237243
Wraps the single paragraph in *text* (a string) so every line is at most
238244
:attr:`width` characters long. All wrapping options are taken from
239-
instance attributes of the :class:`TextWrapper` instance. Returns a list
240-
of output lines, without final newlines.
245+
instance attributes of the :class:`TextWrapper` instance. Returns a list
246+
of output lines, without final newlines. If the wrapped output has no
247+
content, the returned list is empty.
241248

242249

243250
.. method:: fill(text)

Lib/test/test_textwrap.py

Lines changed: 56 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def show(self, textin):
2222
result = []
2323
for i in range(len(textin)):
2424
result.append(" %d: %r" % (i, textin[i]))
25-
result = '\n'.join(result)
25+
result = "\n".join(result) if result else " no lines"
2626
elif isinstance(textin, str):
2727
result = " %s\n" % repr(textin)
2828
return result
@@ -66,6 +66,15 @@ def test_simple(self):
6666
"I'm glad to hear it!"])
6767
self.check_wrap(text, 80, [text])
6868

69+
def test_empty_string(self):
70+
# Check that wrapping the empty string returns an empty list.
71+
self.check_wrap("", 6, [])
72+
self.check_wrap("", 6, [], drop_whitespace=False)
73+
74+
def test_empty_string_with_initial_indent(self):
75+
# Check that the empty string is not indented.
76+
self.check_wrap("", 6, [], initial_indent="++")
77+
self.check_wrap("", 6, [], initial_indent="++", drop_whitespace=False)
6978

7079
def test_whitespace(self):
7180
# Whitespace munging and end-of-sentence detection
@@ -331,7 +340,32 @@ def test_funky_parens (self):
331340
["blah", " ", "(ding", " ", "dong),",
332341
" ", "wubba"])
333342

334-
def test_initial_whitespace(self):
343+
def test_drop_whitespace_false(self):
344+
# Check that drop_whitespace=False preserves whitespace.
345+
# SF patch #1581073
346+
text = " This is a sentence with much whitespace."
347+
self.check_wrap(text, 10,
348+
[" This is a", " ", "sentence ",
349+
"with ", "much white", "space."],
350+
drop_whitespace=False)
351+
352+
def test_drop_whitespace_false_whitespace_only(self):
353+
# Check that drop_whitespace=False preserves a whitespace-only string.
354+
self.check_wrap(" ", 6, [" "], drop_whitespace=False)
355+
356+
def test_drop_whitespace_false_whitespace_only_with_indent(self):
357+
# Check that a whitespace-only string gets indented (when
358+
# drop_whitespace is False).
359+
self.check_wrap(" ", 6, [" "], drop_whitespace=False,
360+
initial_indent=" ")
361+
362+
def test_drop_whitespace_whitespace_only(self):
363+
# Check drop_whitespace on a whitespace-only string.
364+
self.check_wrap(" ", 6, [])
365+
366+
def test_drop_whitespace_leading_whitespace(self):
367+
# Check that drop_whitespace does not drop leading whitespace (if
368+
# followed by non-whitespace).
335369
# SF bug #622849 reported inconsistent handling of leading
336370
# whitespace; let's test that a bit, shall we?
337371
text = " This is a sentence with leading whitespace."
@@ -340,13 +374,27 @@ def test_initial_whitespace(self):
340374
self.check_wrap(text, 30,
341375
[" This is a sentence with", "leading whitespace."])
342376

343-
def test_no_drop_whitespace(self):
344-
# SF patch #1581073
345-
text = " This is a sentence with much whitespace."
346-
self.check_wrap(text, 10,
347-
[" This is a", " ", "sentence ",
348-
"with ", "much white", "space."],
377+
def test_drop_whitespace_whitespace_line(self):
378+
# Check that drop_whitespace skips the whole line if a non-leading
379+
# line consists only of whitespace.
380+
text = "abcd efgh"
381+
# Include the result for drop_whitespace=False for comparison.
382+
self.check_wrap(text, 6, ["abcd", " ", "efgh"],
349383
drop_whitespace=False)
384+
self.check_wrap(text, 6, ["abcd", "efgh"])
385+
386+
def test_drop_whitespace_whitespace_only_with_indent(self):
387+
# Check that initial_indent is not applied to a whitespace-only
388+
# string. This checks a special case of the fact that dropping
389+
# whitespace occurs before indenting.
390+
self.check_wrap(" ", 6, [], initial_indent="++")
391+
392+
def test_drop_whitespace_whitespace_indent(self):
393+
# Check that drop_whitespace does not drop whitespace indents.
394+
# This checks a special case of the fact that dropping whitespace
395+
# occurs before indenting.
396+
self.check_wrap("abcd efgh", 6, [" abcd", " efgh"],
397+
initial_indent=" ", subsequent_indent=" ")
350398

351399
def test_split(self):
352400
# Ensure that the standard _split() method works as advertised

0 commit comments

Comments
 (0)