]> granicus.if.org Git - python/commitdiff
Fix #1146: TextWrap vs words 1-character shorter than the width.
authorGeorg Brandl <georg@python.org>
Sat, 19 Jan 2008 19:48:19 +0000 (19:48 +0000)
committerGeorg Brandl <georg@python.org>
Sat, 19 Jan 2008 19:48:19 +0000 (19:48 +0000)
Patch by Quentin Gallet-Gilles.

Lib/test/test_textwrap.py
Lib/textwrap.py
Misc/NEWS

index 5f0b51b4e7f69752684938a09da08d00e4bbd102..787153e3cdfbc589703dce675d79d189c4afbd5b 100644 (file)
@@ -398,6 +398,19 @@ How *do* you spell that odd word, anyways?
                          '               o'],
                         subsequent_indent = ' '*15)
 
+        # 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 ',
+                         '"supercalifr',
+                         'agilisticexp',
+                         'ialidocious?',
+                         '" How *do*',
+                         'you spell',
+                         'that odd',
+                         'word,',
+                         'anyways?'])
+
     def test_nobreak_long(self):
         # Test with break_long_words disabled
         self.wrapper.break_long_words = 0
index e49644d22141ebd5e6fce1d31750ca60b0a17d66..473b98ac97f304dafdb8e3c151b1ff0b380a1471 100644 (file)
@@ -173,7 +173,12 @@ class TextWrapper:
         Handle a chunk of text (most likely a word, not whitespace) that
         is too long to fit in any line.
         """
-        space_left = max(width - cur_len, 1)
+        # Figure out when indent is larger than the specified width, and make
+        # sure at least one character is stripped off on every pass
+        if width < 1:
+            space_left = 1
+        else:
+            space_left = width - cur_len
 
         # 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.
index 8a6ca66ebece30b3eea45b736eee8d5f4dc43ab7..fdf1e860a0e6b9269ff25240af0ab3078151cfca 100644 (file)
--- a/Misc/NEWS
+++ b/Misc/NEWS
@@ -369,6 +369,9 @@ Core and builtins
 Library
 -------
 
+- #1146: fix how textwrap breaks a long word that would start in the
+  last column of a line.
+
 - #1693149: trace.py --ignore-module - accept multiple comma-separated
   modules to be given.