diff -r 06ce648ac6ac Doc/library/textwrap.rst --- a/Doc/library/textwrap.rst Mon Mar 17 19:22:59 2014 +0100 +++ b/Doc/library/textwrap.rst Wed Mar 19 17:12:02 2014 +0200 @@ -182,13 +182,6 @@ each tab character will be replaced by a single space, which is *not* the same as tab expansion. - .. note:: - - If :attr:`replace_whitespace` is false, newlines may appear in the - middle of a line and cause strange output. For this reason, text should - be split into paragraphs (using :meth:`str.splitlines` or similar) - which are wrapped separately. - .. attribute:: drop_whitespace diff -r 06ce648ac6ac Lib/test/test_textwrap.py --- a/Lib/test/test_textwrap.py Mon Mar 17 19:22:59 2014 +0100 +++ b/Lib/test/test_textwrap.py Wed Mar 19 17:12:02 2014 +0200 @@ -429,6 +429,36 @@ self.check_wrap(text, 7, ["aa \xe4\xe4-", "\xe4\xe4"]) +class WrapLineBreakTestCase(BaseTestCase): + + def test_line_break(self): + text = 'one two\nthree four' + self.check_wrap(text, 14, ['one two', 'three four'], + replace_whitespace=False) + self.check_wrap(text, 14, ['one two three', 'four']) + self.check_wrap(text, 5, ['one', 'two', 'three', 'four']) + self.check_wrap(text, 5, ['one', 'two', 'three', 'four'], + replace_whitespace=False) + text = '''\ +aaaaa aaaaa aaaaa aaaaa aaaaa +bbbbb bbbbb bbbbb bbbbb bbbbb +ccccc ccccc ccccc ccccc ccccc +ddddd ddddd ddddd ddddd ddddd +eeeee eeeee eeeee eeeee eeeee''' + expected = ['aaaaa aaaaa aaaaa aaaaa aaaaa', + 'bbbbb bbbbb bbbbb bbbbb bbbbb', + 'ccccc ccccc ccccc ccccc ccccc', + 'ddddd ddddd ddddd ddddd ddddd', + 'eeeee eeeee eeeee eeeee eeeee'] + self.check_wrap(text, 35, expected, replace_whitespace=False) + expected = ['aaaaa aaaaa aaaaa aaaaa aaaaa bbbbb', + 'bbbbb bbbbb bbbbb bbbbb ccccc ccccc', + 'ccccc ccccc ccccc ddddd ddddd ddddd', + 'ddddd ddddd eeeee eeeee eeeee eeeee', + 'eeeee'] + self.check_wrap(text, 35, expected) + + class MaxLinesTestCase(BaseTestCase): text = "Hello there, how are you this fine day? I'm glad to hear it!" diff -r 06ce648ac6ac Lib/textwrap.py --- a/Lib/textwrap.py Mon Mar 17 19:22:59 2014 +0100 +++ b/Lib/textwrap.py Wed Mar 19 17:12:02 2014 +0200 @@ -80,7 +80,7 @@ # Hello/ /there/ /--/ /you/ /goof-/ball,/ /use/ /the/ /-b/ /option! # (after stripping out empty strings). wordsep_re = re.compile( - r'(\s+|' # any whitespace + r'(\n|(?:\s(?