diff -r 1ea89e5f40cf Lib/test/test_textwrap.py --- a/Lib/test/test_textwrap.py Tue Aug 13 12:54:29 2013 +0300 +++ b/Lib/test/test_textwrap.py Tue Aug 13 23:47:37 2013 +0800 @@ -833,6 +833,18 @@ 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, 0, "", placeholder=" ") + self.check_shorten(text, 4, "", placeholder=" ") + self.check_shorten(text, 5, "Hello", placeholder=" ") + self.check_shorten(text, 12, "Hello there,", placeholder=" ") + + def test_placeholder_with_whitespaces_on_the_left_right_and_middle(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, 11, "Hello $ $", placeholder=" $ $ ") + if __name__ == '__main__': unittest.main() diff -r 1ea89e5f40cf Lib/textwrap.py --- a/Lib/textwrap.py Tue Aug 13 12:54:29 2013 +0300 +++ b/Lib/textwrap.py Tue Aug 13 23:47:37 2013 +0800 @@ -7,7 +7,7 @@ import re -__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent', 'indent'] +__all__ = ['TextWrapper', 'wrap', 'fill', 'dedent', 'indent', 'shorten'] # Hardcode the recognized whitespace characters to the US-ASCII # whitespace characters. The main reason for doing this is that in @@ -313,6 +313,7 @@ Collapse and truncate the given text to fit in 'self.width' columns. """ + placeholder = placeholder.rstrip() max_length = self.width if max_length < len(placeholder.strip()): raise ValueError("placeholder too large for max width")