SF bug 797650: Infinite loop in textwrap.py

When the indents were set to longer than the width and long word breaking
was enabled, an infinite loop would result because the inner loop did not
assure that at least one character was stripped off on every pass.
This commit is contained in:
Raymond Hettinger 2003-08-30 14:43:55 +00:00
parent 6229713221
commit c11dbcd4bf
2 changed files with 11 additions and 1 deletions

View File

@ -305,6 +305,16 @@ def test_break_long(self):
['Did you say "supercalifragilisticexpialidocious?"', ['Did you say "supercalifragilisticexpialidocious?"',
'How *do* you spell that odd word, anyways?']) 'How *do* you spell that odd word, anyways?'])
# SF bug 797650. Prevent an infinite loop by making sure that at
# least one character gets split off on every pass.
self.check_wrap('-'*10+'hello', 10,
['----------',
' h',
' e',
' l',
' l',
' o'],
subsequent_indent = ' '*15)
def test_nobreak_long(self): def test_nobreak_long(self):
# Test with break_long_words disabled # Test with break_long_words disabled

View File

@ -168,7 +168,7 @@ def _handle_long_word(self, chunks, cur_line, cur_len, width):
Handle a chunk of text (most likely a word, not whitespace) that Handle a chunk of text (most likely a word, not whitespace) that
is too long to fit in any line. is too long to fit in any line.
""" """
space_left = width - cur_len space_left = max(width - cur_len, 1)
# If we're allowed to break long words, then do so: put as much # 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. # of the next chunk onto the current line as will fit.