classification
Title: textwrap output may change if you wrap a paragraph twice
Type: behavior Stage: patch review
Components: Library (Lib) Versions: Python 3.8, Python 3.7, Python 3.6
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: larry, serhiy.storchaka
Priority: low Keywords: patch

Created on 2017-12-21 14:10 by larry, last changed 2018-02-11 09:39 by serhiy.storchaka.

Files
File name Uploaded Description Edit
textwrap.isnt.stable.py larry, 2017-12-21 14:10
Pull Requests
URL Status Linked Edit
PR 5615 open larry, 2018-02-11 01:48
Messages (2)
msg308872 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2017-12-21 14:10
If you word-wrap a paragraph twice with textwrap, you may get different results.  Specifically, you *will* get different results when:
* the original text has a line that is too long by one character,
* the last word on the line is the first word in a new sentence, and
* there are two spaces after the period.

The first textwrap will replace the two spaces after the period with a newline; the second textwrap will replace the newline with a single space.

Attached is a test case demonstrating the problem.

It's not a big problem, but it did cause an assertion failure in blurb.  The workaround was to word-wrap all paragraphs twice, which works but is kind of dumb.
msg308873 - (view) Author: Larry Hastings (larry) * (Python committer) Date: 2017-12-21 14:11
FWIW, the test program produces this output:

------------------------------

     original: 'xxxx xxxx xxxx xxxx xxxx.  xxxx'
      wrapped: 'xxxx xxxx xxxx xxxx xxxx.\nxxxx'
wrapped twice: 'xxxx xxxx xxxx xxxx xxxx. xxxx'

Traceback (most recent call last):
  File "textwrap.isnt.stable.py", line 24, in <module>
    assert wrapped == wrapped2
AssertionError
History
Date User Action Args
2018-02-11 09:39:18serhiy.storchakasetnosy: + serhiy.storchaka

type: behavior
versions: + Python 3.6, Python 3.8
2018-02-11 01:48:02larrysetkeywords: + patch
stage: patch review
pull_requests: + pull_request5425
2017-12-21 14:11:26larrysetmessages: + msg308873
2017-12-21 14:10:43larrycreate