Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Lib/test/test_textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,14 @@ def test_subsequent_indent(self):
initial_indent=" * ", subsequent_indent=" ")
self.check(result, expect)

def test_indent_longer_than_width(self):
# gh-160558: wrap() looped forever when an indent was longer than
# width.
self.check_wrap(" ", 3, [], initial_indent="----")
self.check_wrap(" a", 3, ["----a"], initial_indent="----")
self.check_wrap("aaaa", 3, ["aaa", "----a", "----"],
subsequent_indent="----", drop_whitespace=False)


# Despite the similar names, DedentTestCase is *not* the inverse
# of IndentTestCase!
Expand Down
2 changes: 1 addition & 1 deletion Lib/textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -288,7 +288,7 @@ def _wrap_chunks(self, chunks):
l = len(chunks[-1])

# Can at least squeeze this chunk onto the current line.
if cur_len + l <= width:
if cur_len + l <= width or not l:
cur_line.append(chunks.pop())
cur_len += l

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix :func:`textwrap.wrap` and :func:`textwrap.fill` hanging in some cases
when *initial_indent* or *subsequent_indent* is longer than *width*.
Loading