]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.14] gh-139065: Fix trailing space before long word in textwrap (GH-139070) (GH...
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Fri, 10 Oct 2025 13:54:46 +0000 (15:54 +0200)
committerGitHub <noreply@github.com>
Fri, 10 Oct 2025 13:54:46 +0000 (13:54 +0000)
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 <storchaka@gmail.com>
Lib/test/test_textwrap.py
Lib/textwrap.py
Misc/NEWS.d/next/Library/2025-09-17-19-08-34.gh-issue-139065.Hu8fM5.rst [new file with mode: 0644]

index cbd383ea4e2656196d721de5ca5ec5308cd00569..aca1f427656bb506c3ad369972e1e3ea2757d7d0 100644 (file)
@@ -605,7 +605,7 @@ How *do* you spell that odd word, anyways?
         # 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 @@ How *do* you spell that odd word, anyways?
 
     def test_max_lines_long(self):
         self.check_wrap(self.text, 12,
-                        ['Did you say ',
+                        ['Did you say',
                          '"supercalifr',
                          'agilisticexp',
                          '[...]'],
index 5ae439f5cd3b784594cd87cadc476c65db2453b6..41366fbf443a4fc46768a6745d0b9e64c1d3b056 100644 (file)
@@ -211,7 +211,7 @@ class TextWrapper:
 
         # 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 (file)
index 0000000..20c0060
--- /dev/null
@@ -0,0 +1,2 @@
+Fix trailing space before a wrapped long word if the line length is exactly
+*width* in :mod:`textwrap`.