Skip to content

Commit 8d02003

Browse files
Commit
1 parent d74a963 commit 8d02003

File tree

3 files changed

+10
-2
lines changed

3 files changed

+10
-2
lines changed

Lib/test/test_textwrap.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,8 @@ def test_drop_whitespace_leading_whitespace(self):
388388
[" This is a sentence with leading whitespace."])
389389
self.check_wrap(text, 30,
390390
[" This is a sentence with", "leading whitespace."])
391+
self.check_wrap(' ABCDEFG', 1,
392+
[' ', 'A', 'B', 'C', 'D', 'E', 'F', 'G'])
391393

392394
def test_drop_whitespace_whitespace_line(self):
393395
# Check that drop_whitespace skips the whole line if a non-leading

Lib/textwrap.py

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,12 @@ def _wrap_chunks(self, chunks):
304304

305305
# If the last chunk on this line is all whitespace, drop it.
306306
if self.drop_whitespace and cur_line and cur_line[-1].strip() == '':
307-
cur_len -= len(cur_line[-1])
308-
del cur_line[-1]
307+
# If this is the first line keep it if a non-whitespace chunk follows
308+
if not lines and len(cur_line) == 1 and chunks and chunks[-1].strip() != '':
309+
pass
310+
else:
311+
cur_len -= len(cur_line[-1])
312+
del cur_line[-1]
309313

310314
if cur_line:
311315
if (self.max_lines is None or
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix :func:`textwrap.wrap` to preserve a whitespace only line at the start of
2+
a paragraph when non-whitespace text follows.

0 commit comments

Comments
 (0)