[Python-checkins] cpython (2.7): #15510: clarify textwrap's handling of whitespace, and add confirming tests.
r.david.murray
python-checkins at python.org
Sat Sep 8 19:42:46 CEST 2012
http://hg.python.org/cpython/rev/fca48971ac35
changeset: 78886:fca48971ac35
branch: 2.7
parent: 78883:0ee8ce24fcd0
user: R David Murray <rdmurray at bitdance.com>
date: Sat Sep 08 13:42:01 2012 -0400
summary:
#15510: clarify textwrap's handling of whitespace, and add confirming tests.
Patch by Chris Jerdonek.
files:
Doc/library/textwrap.rst | 19 +++++--
Lib/test/test_textwrap.py | 62 +++++++++++++++++++++++---
2 files changed, 68 insertions(+), 13 deletions(-)
diff --git a/Doc/library/textwrap.rst b/Doc/library/textwrap.rst
--- a/Doc/library/textwrap.rst
+++ b/Doc/library/textwrap.rst
@@ -26,6 +26,9 @@
Optional keyword arguments correspond to the instance attributes of
:class:`TextWrapper`, documented below. *width* defaults to ``70``.
+ See the :meth:`TextWrapper.wrap` method for additional details on how
+ :func:`wrap` behaves.
+
.. function:: fill(text[, width[, ...]])
@@ -134,9 +137,11 @@
.. attribute:: drop_whitespace
- (default: ``True``) If true, whitespace that, after wrapping, happens to
- end up at the beginning or end of a line is dropped (leading whitespace in
- the first line is always preserved, though).
+ (default: ``True``) If true, whitespace at the beginning and ending of
+ every line (after wrapping but before indenting) is dropped.
+ Whitespace at the beginning of the paragraph, however, is not dropped
+ if non-whitespace follows it. If whitespace being dropped takes up an
+ entire line, the whole line is dropped.
.. versionadded:: 2.6
Whitespace was always dropped in earlier versions.
@@ -145,7 +150,8 @@
.. attribute:: initial_indent
(default: ``''``) String that will be prepended to the first line of
- wrapped output. Counts towards the length of the first line.
+ wrapped output. Counts towards the length of the first line. The empty
+ string is not indented.
.. attribute:: subsequent_indent
@@ -208,8 +214,9 @@
Wraps the single paragraph in *text* (a string) so every line is at most
:attr:`width` characters long. All wrapping options are taken from
- instance attributes of the :class:`TextWrapper` instance. Returns a list
- of output lines, without final newlines.
+ instance attributes of the :class:`TextWrapper` instance. Returns a list
+ of output lines, without final newlines. If the wrapped output has no
+ content, the returned list is empty.
.. method:: fill(text)
diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py
--- a/Lib/test/test_textwrap.py
+++ b/Lib/test/test_textwrap.py
@@ -66,6 +66,15 @@
"I'm glad to hear it!"])
self.check_wrap(text, 80, [text])
+ def test_empty_string(self):
+ # Check that wrapping the empty string returns an empty list.
+ self.check_wrap("", 6, [])
+ self.check_wrap("", 6, [], drop_whitespace=False)
+
+ def test_empty_string_with_initial_indent(self):
+ # Check that the empty string is not indented.
+ self.check_wrap("", 6, [], initial_indent="++")
+ self.check_wrap("", 6, [], initial_indent="++", drop_whitespace=False)
def test_whitespace(self):
# Whitespace munging and end-of-sentence detection
@@ -323,7 +332,32 @@
["blah", " ", "(ding", " ", "dong),",
" ", "wubba"])
- def test_initial_whitespace(self):
+ def test_drop_whitespace_false(self):
+ # Check that drop_whitespace=False preserves whitespace.
+ # SF patch #1581073
+ text = " This is a sentence with much whitespace."
+ self.check_wrap(text, 10,
+ [" This is a", " ", "sentence ",
+ "with ", "much white", "space."],
+ drop_whitespace=False)
+
+ def test_drop_whitespace_false_whitespace_only(self):
+ # Check that drop_whitespace=False preserves a whitespace-only string.
+ self.check_wrap(" ", 6, [" "], drop_whitespace=False)
+
+ def test_drop_whitespace_false_whitespace_only_with_indent(self):
+ # Check that a whitespace-only string gets indented (when
+ # drop_whitespace is False).
+ self.check_wrap(" ", 6, [" "], drop_whitespace=False,
+ initial_indent=" ")
+
+ def test_drop_whitespace_whitespace_only(self):
+ # Check drop_whitespace on a whitespace-only string.
+ self.check_wrap(" ", 6, [])
+
+ def test_drop_whitespace_leading_whitespace(self):
+ # Check that drop_whitespace does not drop leading whitespace (if
+ # followed by non-whitespace).
# SF bug #622849 reported inconsistent handling of leading
# whitespace; let's test that a bit, shall we?
text = " This is a sentence with leading whitespace."
@@ -332,13 +366,27 @@
self.check_wrap(text, 30,
[" This is a sentence with", "leading whitespace."])
- def test_no_drop_whitespace(self):
- # SF patch #1581073
- text = " This is a sentence with much whitespace."
- self.check_wrap(text, 10,
- [" This is a", " ", "sentence ",
- "with ", "much white", "space."],
+ def test_drop_whitespace_whitespace_line(self):
+ # Check that drop_whitespace skips the whole line if a non-leading
+ # line consists only of whitespace.
+ text = "abcd efgh"
+ # Include the result for drop_whitespace=False for comparison.
+ self.check_wrap(text, 6, ["abcd", " ", "efgh"],
drop_whitespace=False)
+ self.check_wrap(text, 6, ["abcd", "efgh"])
+
+ def test_drop_whitespace_whitespace_only_with_indent(self):
+ # Check that initial_indent is not applied to a whitespace-only
+ # string. This checks a special case of the fact that dropping
+ # whitespace occurs before indenting.
+ self.check_wrap(" ", 6, [], initial_indent="++")
+
+ def test_drop_whitespace_whitespace_indent(self):
+ # Check that drop_whitespace does not drop whitespace indents.
+ # This checks a special case of the fact that dropping whitespace
+ # occurs before indenting.
+ self.check_wrap("abcd efgh", 6, [" abcd", " efgh"],
+ initial_indent=" ", subsequent_indent=" ")
if test_support.have_unicode:
def test_unicode(self):
--
Repository URL: http://hg.python.org/cpython
More information about the Python-checkins
mailing list