This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: TextWrap vs words 1-character shorter than the width
Type: behavior Stage:
Components: Versions: Python 2.5
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: georg.brandl Nosy List: georg.brandl, quentin.gallet-gilles, sam
Priority: normal Keywords: patch

Created on 2007-09-11 13:37 by sam, last changed 2022-04-11 14:56 by admin. This issue is now closed.

File name Uploaded Description Edit
textwrap_r58153.diff quentin.gallet-gilles, 2007-09-14 08:56
textwrap_revised_r58153.diff quentin.gallet-gilles, 2007-09-14 09:10
textwrap_revised_r60075.diff quentin.gallet-gilles, 2008-01-19 14:38
Messages (6)
msg55822 - (view) Author: (sam) Date: 2007-09-11 13:37
>>> from textwrap import wrap
>>> wrap("foobarbaz reallylongwordgoeshere", width = 10)
['foobarbaz r', 'eallylongw', 'ordgoesher', 'e']
>>> print [len(s) for s in _]
[11, 10, 10, 1]

This only seems to happen when the first word on the line is exactly one
character shorter than the width, and the next word is too long to fit,
so it is broken:

>>> wrap("foo bar reallylongwordgoeshere", width = 7)
['foo bar', 'reallyl', 'ongword', 'goesher', 'e']
>>> wrap("foobarbaz really longwordgoeshere", width = 10)
['foobarbaz', 'really lon', 'gwordgoesh', 'ere']
>>> wrap("foobarbaz reallylongwordgoeshere", width = 10,
break_long_words = False)
['foobarbaz', 'reallylongwordgoeshere']

This is on Python 2.5, on Windows XP SP2.
msg55909 - (view) Author: Quentin Gallet-Gilles (quentin.gallet-gilles) Date: 2007-09-14 08:56
The bug is present in trunk and has been there since rev 33955. This
revision contained a fix to an infinite loop when indentation was set
longer than width with long word breaking enabled. In that case, it
would strip off at least one character on every pass. The fix was
however a bit too general, leading to the reported bug.

The added patch makes sure this doesn't happen anymore when indentation
isn't involved.
msg55910 - (view) Author: Quentin Gallet-Gilles (quentin.gallet-gilles) Date: 2007-09-14 09:10
The previous patch is suboptimal and doesn't solve all cases. This one
does. My apologies.
msg55911 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2007-09-14 11:01
I'll look at this shortly.
msg60171 - (view) Author: Quentin Gallet-Gilles (quentin.gallet-gilles) Date: 2008-01-19 14:38
As i revisited this one for the Bug Day, I found I could optimize the
patch a little bit. Here's the updated diff.

Also... I think it's a good candidate for 2.5 maintenance release. Any
thoughts ?
msg60221 - (view) Author: Georg Brandl (georg.brandl) * (Python committer) Date: 2008-01-19 19:48
Thanks for the patches, committed in r60093!
Date User Action Args
2022-04-11 14:56:26adminsetgithub: 45487
2008-01-19 19:48:42georg.brandlsetstatus: open -> closed
resolution: fixed
messages: + msg60221
2008-01-19 14:38:36quentin.gallet-gillessetfiles: + textwrap_revised_r60075.diff
messages: + msg60171
2007-09-16 15:53:17loewissetkeywords: + patch
2007-09-14 11:01:22georg.brandlsetassignee: georg.brandl
messages: + msg55911
nosy: + georg.brandl
2007-09-14 09:10:41quentin.gallet-gillessetfiles: + textwrap_revised_r58153.diff
2007-09-14 09:10:08quentin.gallet-gillessetmessages: + msg55910
2007-09-14 08:56:10quentin.gallet-gillessetfiles: + textwrap_r58153.diff
nosy: + quentin.gallet-gilles
messages: + msg55909
2007-09-11 13:37:37samcreate