Skip to content

Commit b1bc743

Browse files
[3.13] pythongh-139065: Fix trailing space before long word in textwrap (pythonGH-139070) (pythonGH-139903)
Fix trailing space before a wrapped long word if the line length with a space is exactly "width". (cherry picked from commit 1c598e0) Co-authored-by: Serhiy Storchaka <[email protected]>
1 parent b367d10 commit b1bc743

File tree

3 files changed

+5
-3
lines changed

3 files changed

+5
-3
lines changed

Lib/test/test_textwrap.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -605,7 +605,7 @@ def test_break_long(self):
605605
# bug 1146. Prevent a long word to be wrongly wrapped when the
606606
# preceding word is exactly one character shorter than the width
607607
self.check_wrap(self.text, 12,
608-
['Did you say ',
608+
['Did you say',
609609
'"supercalifr',
610610
'agilisticexp',
611611
'ialidocious?',
@@ -633,7 +633,7 @@ def test_nobreak_long(self):
633633

634634
def test_max_lines_long(self):
635635
self.check_wrap(self.text, 12,
636-
['Did you say ',
636+
['Did you say',
637637
'"supercalifr',
638638
'agilisticexp',
639639
'[...]'],

Lib/textwrap.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width):
211211

212212
# If we're allowed to break long words, then do so: put as much
213213
# of the next chunk onto the current line as will fit.
214-
if self.break_long_words:
214+
if self.break_long_words and space_left > 0:
215215
end = space_left
216216
chunk = reversed_chunks[-1]
217217
if self.break_on_hyphens and len(chunk) > space_left:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix trailing space before a wrapped long word if the line length is exactly
2+
*width* in :mod:`textwrap`.

0 commit comments

Comments
 (0)