diff -r 7ba77d77b499 Lib/test/test_textwrap.py --- a/Lib/test/test_textwrap.py Mon Aug 12 17:39:51 2013 -0400 +++ b/Lib/test/test_textwrap.py Tue Aug 13 19:04:08 2013 +0800 @@ -833,6 +833,20 @@ def test_first_word_too_long_but_placeholder_fits(self): self.check_shorten("Helloo", 5, "(...)") + def test_all_whitespaces_in_placeholder(self): + text = "Hello there, how are you this fine day? I'm glad to hear it!" + self.check_shorten(text, 4, "", placeholder=" ") + self.check_shorten(text, 5, "Hello", placeholder=" ") + self.check_shorten(text, 13, "Hello there,", placeholder=" ") + + def test_text_with_leading_whitespaces(self): + text = " Hello world!" + self.check_shorten(text, 11, "Hello (...)") + + def test_placeholder_with_leading_whitespaces(self): + text = "Hello world!" + self.check_shorten(text, 9, "Hello $", placeholder=" $") + if __name__ == '__main__': unittest.main() diff -r 7ba77d77b499 Lib/textwrap.py --- a/Lib/textwrap.py Mon Aug 12 17:39:51 2013 -0400 +++ b/Lib/textwrap.py Tue Aug 13 19:04:08 2013 +0800 @@ -313,6 +313,8 @@ Collapse and truncate the given text to fit in 'self.width' columns. """ + placeholder = placeholder.rstrip() + text = text.strip() max_length = self.width if max_length < len(placeholder.strip()): raise ValueError("placeholder too large for max width")