From 5172dcc5e7e4bbf50e7c9992d109f91b313ac2c3 Mon Sep 17 00:00:00 2001 From: Thejesh <35212698+thejesh23@users.noreply.github.com> Date: Fri, 13 Nov 2026 20:34:11 -0800 Subject: [PATCH 1/7] Fix quadratic time in textwrap when breaking a long word TextWrapper._handle_long_word() broke off one line's worth of a long word at a time and stored the rest back, copying the rest of the word for every line. Break the rest of the word into the pieces of all the following lines in one pass instead. They all have the same width, so the result is the same. --- Lib/test/test_textwrap.py | 26 ++++++++++++++++++++++++++ Lib/textwrap.py | 27 ++++++++++++++++++++++++++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/Lib/test/test_textwrap.py b/Lib/test/test_textwrap.py index aca1f427656bb5..3d467f109e58fa 100644 --- a/Lib/test/test_textwrap.py +++ b/Lib/test/test_textwrap.py @@ -615,6 +615,32 @@ def test_break_long(self): 'word,', 'anyways?']) + def test_break_very_long_word(self): + # A word much longer than the width is broken into many lines. + text = ''.join(map(str, range(1000))) + lines = wrap(text, 7) + self.assertEqual(''.join(lines), text) + self.assertEqual([len(line) for line in lines], + [7] * (len(text) // 7) + [len(text) % 7]) + + # The first line can be narrower than the following ones, and the + # last piece of the word shares its line with the next words. + self.check_wrap('a' * 25 + ' b c', 10, + ['* aaaaaaaa', ' aaaaaaaa', ' aaaaaaaa', ' a b c'], + initial_indent='* ', subsequent_indent=' ') + self.check_wrap('x ' + 'a' * 25, 10, + ['x aaaaaaaa', ' aaaaaaaa', ' aaaaaaaa', ' a'], + subsequent_indent=' ') + + # Each piece is broken after its last hyphen. + self.check_wrap('12345-67890-12345-67890-12345', 10, + ['12345-', '67890-', '12345-', '67890-', '12345']) + self.check_wrap('abc-1234567890123-4567890123456', 10, + ['abc-', '1234567890', '123-', '4567890123', '456']) + self.check_wrap('abc-1234567890123-4567890123456', 10, + ['abc-123456', '7890123-45', '6789012345', '6'], + break_on_hyphens=False) + def test_nobreak_long(self): # Test with break_long_words disabled self.wrapper.break_long_words = 0 diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 2f213e34c2c329..134f9dce595d3f 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -221,7 +221,32 @@ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): if hyphen > 0 and any(c != '-' for c in chunk[:hyphen]): end = hyphen + 1 cur_line.append(chunk[:end]) - reversed_chunks[-1] = chunk[end:] + rest = chunk[end:] + # The rest of the word starts each of the following lines, which + # all have the same width. Break it into all of those lines' + # pieces now: breaking off one piece per line would copy the + # rest of the word every time, which takes quadratic time. + # Only do so if the word has no whitespace, so that no piece is + # dropped as whitespace at the start of a line. + line_width = self.width - len(self.subsequent_indent) + if (line_width >= 1 and len(rest) > line_width and + chunk.split(None, 1) == [chunk]): + pieces = [] + start = 0 + while len(rest) - start > line_width: + end = start + line_width + if self.break_on_hyphens: + hyphen = rest.rfind('-', start, end) + if (hyphen > start and + any(c != '-' for c in rest[start:hyphen])): + end = hyphen + 1 + pieces.append(rest[start:end]) + start = end + pieces.append(rest[start:]) + pieces.reverse() + reversed_chunks[-1:] = pieces + else: + reversed_chunks[-1] = rest # Otherwise, we have to preserve the long word intact. Only add # it to the current line if there's nothing already there -- From 7f538fc99c42adfcd16b09191189991c659944c9 Mon Sep 17 00:00:00 2001 From: Thejesh <35212698+thejesh23@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:18:59 -0800 Subject: [PATCH 2/7] Add NEWS entry --- .../next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst diff --git a/Misc/NEWS.d/next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst b/Misc/NEWS.d/next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst new file mode 100644 index 00000000000000..7e01f710b3e9e0 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst @@ -0,0 +1,2 @@ +Fix quadratic time complexity in :func:`textwrap.wrap` and +:func:`textwrap.fill` when breaking a long word into many lines. From eaf3cb27436cd097bf28ee89a4f775550a7a788e Mon Sep 17 00:00:00 2001 From: Thejesh <35212698+thejesh23@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:53:48 -0800 Subject: [PATCH 3/7] Shorten the comment --- Lib/textwrap.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 134f9dce595d3f..2c61acd69f5881 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -222,12 +222,10 @@ def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): end = hyphen + 1 cur_line.append(chunk[:end]) rest = chunk[end:] - # The rest of the word starts each of the following lines, which - # all have the same width. Break it into all of those lines' - # pieces now: breaking off one piece per line would copy the - # rest of the word every time, which takes quadratic time. - # Only do so if the word has no whitespace, so that no piece is - # dropped as whitespace at the start of a line. + # Pre-split the rest to avoid quadratic copying. This relies on + # _wrap_chunks() giving every following line the width + # self.width - len(self.subsequent_indent). Words with whitespace + # take the slow path, since a piece could be dropped. line_width = self.width - len(self.subsequent_indent) if (line_width >= 1 and len(rest) > line_width and chunk.split(None, 1) == [chunk]): From 43c0f59df0f4d5f4bb8917aeceeec8ea395c01b9 Mon Sep 17 00:00:00 2001 From: Thejesh <35212698+thejesh23@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:53:48 -0800 Subject: [PATCH 4/7] Mention the textwrap module in the NEWS entry --- .../Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Misc/NEWS.d/next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst b/Misc/NEWS.d/next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst index 7e01f710b3e9e0..412742067f2fa5 100644 --- a/Misc/NEWS.d/next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst +++ b/Misc/NEWS.d/next/Library/2026-11-14-05-18-59.gh-issue-161977.mZurHK.rst @@ -1,2 +1,2 @@ -Fix quadratic time complexity in :func:`textwrap.wrap` and -:func:`textwrap.fill` when breaking a long word into many lines. +Fix quadratic time complexity in :mod:`textwrap` when breaking a long word +into many lines. From 5e9713af6986b8c6bd26c7a84f300adc2f951388 Mon Sep 17 00:00:00 2001 From: Thejesh <35212698+thejesh23@users.noreply.github.com> Date: Fri, 13 Nov 2026 21:54:13 -0800 Subject: [PATCH 5/7] Factor out the hyphen rule into _break_end() --- Lib/textwrap.py | 38 ++++++++++++++++++-------------------- 1 file changed, 18 insertions(+), 20 deletions(-) diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 2c61acd69f5881..72af654b2f0aab 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -194,6 +194,17 @@ def _fix_sentence_endings(self, chunks): else: i += 1 + def _break_end(self, chunk, start, width): + """Return where to break chunk to fit width characters from start.""" + end = start + width + if self.break_on_hyphens and len(chunk) > end: + # break after last hyphen, but only if there are + # non-hyphens before it + hyphen = chunk.rfind('-', start, end) + if hyphen > start and any(c != '-' for c in chunk[start:hyphen]): + end = hyphen + 1 + return end + def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): """_handle_long_word(chunks : [string], cur_line : [string], @@ -212,39 +223,26 @@ 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 and space_left > 0: - end = space_left chunk = reversed_chunks[-1] - if self.break_on_hyphens and len(chunk) > space_left: - # break after last hyphen, but only if there are - # non-hyphens before it - hyphen = chunk.rfind('-', 0, space_left) - if hyphen > 0 and any(c != '-' for c in chunk[:hyphen]): - end = hyphen + 1 + end = self._break_end(chunk, 0, space_left) cur_line.append(chunk[:end]) - rest = chunk[end:] # Pre-split the rest to avoid quadratic copying. This relies on # _wrap_chunks() giving every following line the width # self.width - len(self.subsequent_indent). Words with whitespace # take the slow path, since a piece could be dropped. line_width = self.width - len(self.subsequent_indent) - if (line_width >= 1 and len(rest) > line_width and + if (line_width >= 1 and len(chunk) - end > line_width and chunk.split(None, 1) == [chunk]): pieces = [] - start = 0 - while len(rest) - start > line_width: - end = start + line_width - if self.break_on_hyphens: - hyphen = rest.rfind('-', start, end) - if (hyphen > start and - any(c != '-' for c in rest[start:hyphen])): - end = hyphen + 1 - pieces.append(rest[start:end]) + while len(chunk) - end > line_width: start = end - pieces.append(rest[start:]) + end = self._break_end(chunk, start, line_width) + pieces.append(chunk[start:end]) + pieces.append(chunk[end:]) pieces.reverse() reversed_chunks[-1:] = pieces else: - reversed_chunks[-1] = rest + reversed_chunks[-1] = chunk[end:] # Otherwise, we have to preserve the long word intact. Only add # it to the current line if there's nothing already there -- From bf3fffadee81209fbc9b6aaf9e12ac4f3d1c8ffe Mon Sep 17 00:00:00 2001 From: Thejesh <35212698+thejesh23@users.noreply.github.com> Date: Fri, 13 Nov 2026 22:16:44 -0800 Subject: [PATCH 6/7] Move the splitting into _wrap_chunks() --- Lib/textwrap.py | 48 +++++++++++++++++++----------------------------- 1 file changed, 19 insertions(+), 29 deletions(-) diff --git a/Lib/textwrap.py b/Lib/textwrap.py index 72af654b2f0aab..ca3a722de657c0 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -194,17 +194,6 @@ def _fix_sentence_endings(self, chunks): else: i += 1 - def _break_end(self, chunk, start, width): - """Return where to break chunk to fit width characters from start.""" - end = start + width - if self.break_on_hyphens and len(chunk) > end: - # break after last hyphen, but only if there are - # non-hyphens before it - hyphen = chunk.rfind('-', start, end) - if hyphen > start and any(c != '-' for c in chunk[start:hyphen]): - end = hyphen + 1 - return end - def _handle_long_word(self, reversed_chunks, cur_line, cur_len, width): """_handle_long_word(chunks : [string], cur_line : [string], @@ -223,26 +212,16 @@ 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 and space_left > 0: + end = space_left chunk = reversed_chunks[-1] - end = self._break_end(chunk, 0, space_left) + if self.break_on_hyphens and len(chunk) > space_left: + # break after last hyphen, but only if there are + # non-hyphens before it + hyphen = chunk.rfind('-', 0, space_left) + if hyphen > 0 and any(c != '-' for c in chunk[:hyphen]): + end = hyphen + 1 cur_line.append(chunk[:end]) - # Pre-split the rest to avoid quadratic copying. This relies on - # _wrap_chunks() giving every following line the width - # self.width - len(self.subsequent_indent). Words with whitespace - # take the slow path, since a piece could be dropped. - line_width = self.width - len(self.subsequent_indent) - if (line_width >= 1 and len(chunk) - end > line_width and - chunk.split(None, 1) == [chunk]): - pieces = [] - while len(chunk) - end > line_width: - start = end - end = self._break_end(chunk, start, line_width) - pieces.append(chunk[start:end]) - pieces.append(chunk[end:]) - pieces.reverse() - reversed_chunks[-1:] = pieces - else: - reversed_chunks[-1] = chunk[end:] + reversed_chunks[-1] = chunk[end:] # Otherwise, we have to preserve the long word intact. Only add # it to the current line if there's nothing already there -- @@ -300,6 +279,17 @@ def _wrap_chunks(self, chunks): # Maximum width for this line. width = self.width - len(indent) + # This and all following lines have the same width, so split a + # long word for all of them at once. Breaking off one line at a + # time would copy the rest of the word each time. + word = chunks[-1] + if (lines and self.break_long_words and 0 < width < len(word) and + not (self.break_on_hyphens and '-' in word) and + word.split(None, 1) == [word]): + del chunks[-1] + chunks.extend(word[i:i + width] + for i in reversed(range(0, len(word), width))) + # First chunk on line is whitespace -- drop it, unless this # is the very beginning of the text (ie. no lines started yet). if self.drop_whitespace and chunks[-1].strip() == '' and lines: From 19fd62651ec1ac4d9d0916aaedf511959885945d Mon Sep 17 00:00:00 2001 From: Thejesh <35212698+thejesh23@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:54:57 -0800 Subject: [PATCH 7/7] Don't pre-split long words when max_lines is set --- Lib/textwrap.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Lib/textwrap.py b/Lib/textwrap.py index ca3a722de657c0..cc5b30e91a9903 100644 --- a/Lib/textwrap.py +++ b/Lib/textwrap.py @@ -283,7 +283,8 @@ def _wrap_chunks(self, chunks): # long word for all of them at once. Breaking off one line at a # time would copy the rest of the word each time. word = chunks[-1] - if (lines and self.break_long_words and 0 < width < len(word) and + if (lines and self.max_lines is None and self.break_long_words and + 0 < width < len(word) and not (self.break_on_hyphens and '-' in word) and word.split(None, 1) == [word]): del chunks[-1]