https://github.com/python/cpython/commit/ade6dea24bcb3daa18447edc46a4dbfa351df45d
commit: ade6dea24bcb3daa18447edc46a4dbfa351df45d
branch: 3.14
author: Miss Islington (bot) <[email protected]>
committer: serhiy-storchaka <[email protected]>
date: 2025-10-10T13:54:46Z
summary:

[3.14] gh-139065: Fix trailing space before long word in textwrap (GH-139070) 
(GH-139902)

Fix trailing space before a wrapped long word if the line length with
a space is exactly "width".
(cherry picked from commit 1c598e04361dbfc9cf465f3a02f83715c11b028c)

Co-authored-by: Serhiy Storchaka <[email protected]>

files:
A Misc/NEWS.d/next/Library/2025-09-17-19-08-34.gh-issue-139065.Hu8fM5.rst
M Lib/test/test_textwrap.py
M Lib/textwrap.py

diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py
index cbd383ea4e2656..aca1f427656bb5 100644
--- a/Lib/test/test_textwrap.py
+++ b/Lib/test/test_textwrap.py
@@ -605,7 +605,7 @@ def test_break_long(self):
         # bug 1146.  Prevent a long word to be wrongly wrapped when the
         # preceding word is exactly one character shorter than the width
         self.check_wrap(self.text, 12,
-                        ['Did you say ',
+                        ['Did you say',
                          '"supercalifr',
                          'agilisticexp',
                          'ialidocious?',
@@ -633,7 +633,7 @@ def test_nobreak_long(self):
 
     def test_max_lines_long(self):
         self.check_wrap(self.text, 12,
-                        ['Did you say ',
+                        ['Did you say',
                          '"supercalifr',
                          'agilisticexp',
                          '[...]'],
diff --git a/Lib/textwrap.py b/Lib/textwrap.py
index 5ae439f5cd3b78..41366fbf443a4f 100644
--- a/Lib/textwrap.py
+++ b/Lib/textwrap.py
@@ -211,7 +211,7 @@ def _handle_long_word(self, reversed_chunks, cur_line, 
cur_len, width):
 
         # If we're allowed to break long words, then do so: put as much
         # of the next chunk onto the current line as will fit.
-        if self.break_long_words:
+        if self.break_long_words and space_left > 0:
             end = space_left
             chunk = reversed_chunks[-1]
             if self.break_on_hyphens and len(chunk) > space_left:
diff --git 
a/Misc/NEWS.d/next/Library/2025-09-17-19-08-34.gh-issue-139065.Hu8fM5.rst 
b/Misc/NEWS.d/next/Library/2025-09-17-19-08-34.gh-issue-139065.Hu8fM5.rst
new file mode 100644
index 00000000000000..20c00603959276
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2025-09-17-19-08-34.gh-issue-139065.Hu8fM5.rst
@@ -0,0 +1,2 @@
+Fix trailing space before a wrapped long word if the line length is exactly
+*width* in :mod:`textwrap`.

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to