Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Lib/test/test_textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -388,6 +388,8 @@ def test_drop_whitespace_leading_whitespace(self):
[" This is a sentence with leading whitespace."])
self.check_wrap(text, 30,
[" This is a sentence with", "leading whitespace."])
self.check_wrap(' ABCDEFG', 1,
[' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G'])
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add other cases

>>> textwrap.wrap('   ABCDEFG', width=1)
[' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G']
>>> textwrap.wrap('   ABCDEFG', width=2)
[' A', 'BC', 'DE', 'FG']
>>> textwrap.wrap('   ABCDEFG', width=3)
['   ', 'ABC', 'DEF', 'G']


def test_drop_whitespace_whitespace_line(self):
# Check that drop_whitespace skips the whole line if a non-leading
Expand Down
8 changes: 6 additions & 2 deletions Lib/textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,12 @@ def _wrap_chunks(self, chunks):

# If the last chunk on this line is all whitespace, drop it.
if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
cur_len -= len(cur_line[-1])
del cur_line[-1]
# If this is the first line keep it if a non-whitespace chunk follows
if not lines and len(cur_line) == 1 and chunks and chunks[-1].strip() != '':
pass
else:
cur_len -= len(cur_line[-1])
del cur_line[-1]
Comment on lines +307 to +312
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# If this is the first line keep it if a non-whitespace chunk follows
if not lines and len(cur_line) == 1 and chunks and chunks[-1].strip() != '':
pass
else:
cur_len -= len(cur_line[-1])
del cur_line[-1]
# Keep first non-whitespace chunk on the first line
if not (not lines and len(cur_line) == 1 and chunks and chunks[-1].strip()):
cur_len -= len(cur_line[-1])
del cur_line[-1]

Maybe this is better?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or

                if lines or len(cur_line) != 1 or not chunks or not chunks[-1].strip():
                    cur_len -= len(cur_line[-1])
                    del cur_line[-1]


if cur_line:
if (self.max_lines is None or
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`textwrap.wrap` to preserve a whitespace only line at the start of
a paragraph when non-whitespace text follows.
Loading