Skip to content
Prev Previous commit
Next Next commit
react to feedback
  • Loading branch information
xi committed Sep 3, 2021
commit 97a2ec81f13e87ca5a9f89488bf633c3dfa86f91
2 changes: 2 additions & 0 deletions Doc/library/textwrap.rst
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,8 @@ hyphenated words; only then will long words be broken if necessary, unless
(default: ``len``) Used to determine the length of a string. You can
provide a custom function, e.g. to account for wide characters.
Comment thread
merwok marked this conversation as resolved.

.. versionadded:: 3.11


.. index:: single: ...; placeholder

Expand Down
11 changes: 4 additions & 7 deletions Lib/test/test_textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -1078,22 +1078,19 @@ def test_first_word_too_long_but_placeholder_fits(self):


class WideCharacterTestCase(BaseTestCase):
def setUp(self):
def test_wide_character(self):
def text_len(text):
n = 0
for c in text:
if unicodedata.east_asian_width(c) in ['F', 'W']:
if unicodedata.east_asian_width(c) in {'F', 'W'}:
n += 2
else:
n += 1
return n

self.wrapper = TextWrapper(width=5, text_len=text_len)

def test_wide_character(self):
text = "123 🔧"
result = self.wrapper.wrap(text, **kwargs)
self.check(result, ["123", "🔧"])
expected = ["123", "🔧"]
self.check_wrap(text, 6, expected, text_len=text_len)


if __name__ == '__main__':
Expand Down
4 changes: 2 additions & 2 deletions Lib/textwrap.py
Original file line number Diff line number Diff line change
Expand Up @@ -261,8 +261,8 @@ def _wrap_chunks(self, chunks):
indent = self.subsequent_indent
else:
indent = self.initial_indent
if self.text_len(indent) +
self.text_len(self.placeholder.lstrip()) > self.width:
if (self.text_len(indent) +
self.text_len(self.placeholder.lstrip()) > self.width):
raise ValueError("placeholder too large for max width")

# Arrange in reverse order so items can be efficiently popped
Expand Down