This commit is contained in:
Stan Ulbrych 2025-10-26 16:12:46 +00:00
parent d74a96366d
commit 8d02003b5d
No known key found for this signature in database
GPG key ID: B8E58DBDB2A1A0B8
3 changed files with 10 additions and 2 deletions

View file

@ -388,6 +388,8 @@ What a mess!
[" 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'])
def test_drop_whitespace_whitespace_line(self):
# Check that drop_whitespace skips the whole line if a non-leading

View file

@ -304,8 +304,12 @@ class TextWrapper:
# 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]
if cur_line:
if (self.max_lines is None or

View file

@ -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.