]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.13] 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:51:41 +0000 (15:51 +0200)
committerGitHub <noreply@github.com>
Fri, 10 Oct 2025 13:51:41 +0000 (13:51 +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 dfbc2b93dfc0d6dc5a77c67bb88b3972f6cda4c9..1757a90dc49576253240c3e29867d8b02811eb0a 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 7ca393d1c371aad54a2d1d70fae15efa889175bb..686c9eb8d8d66b7c0eeea275862a93a44112f7f8 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`.