Issue1859
Created on 2008-01-17 12:40 by palfrey, last changed 2008-01-17 18:40 by gvanrossum.
| File name |
Uploaded |
Description |
Edit |
Remove |
|
textwrap-fix.patch
|
palfrey,
2008-01-17 12:46
|
Patch for the issue |
|
|
| msg60026 (view) |
Author: Tom Parker (palfrey) |
Date: 2008-01-17 12:40 |
|
If a piece of text given to textwrap contains one or more "\n", textwrap
does not break at that point. I would have expected "\n" characters to
cause forced breaks.
|
| msg60027 (view) |
Author: Tom Parker (palfrey) |
Date: 2008-01-17 12:46 |
|
Attaching a patch that corrects the issue (against python 2.4)
|
| msg60031 (view) |
Author: Mark Dickinson (marketdickinson) |
Date: 2008-01-17 15:24 |
|
Could you give an example showing the unexpected behaviour, and
describing what behaviour you'd expect, please?
As far as I can tell, the patch has no effect on textwrap.wrap or
textwrap.fill, since any newlines have already been converted to spaces
by the time the _wrap_chunks method is called.
Thanks.
|
| msg60032 (view) |
Author: Tom Parker (palfrey) |
Date: 2008-01-17 15:51 |
|
If replace_whitespace in textwrap is set to False (True is default) then
there are newlines. Yes, if you haven't set this then the patch does
nothing (but that sounds sane to me)
The exact text was "RadioTest TOSSIM stress tester by Tom Parker
<t.e.v.parker@tudelft.nl>\nKeeps running TOSSIM with random seeds until
something fails", which with a width of 78 gets broken both before and
after the "Keeps".
|
| msg60038 (view) |
Author: Guido van Rossum (gvanrossum) |
Date: 2008-01-17 17:51 |
|
The original behavior is intentional. Please don't attempt to "fix" it.
|
| msg60039 (view) |
Author: Tom Parker (palfrey) |
Date: 2008-01-17 18:03 |
|
Is there any other way to do what I was trying to do then (both dynamic
wrapping for long segments + some static breaks)? Right now, the only
option I can think of is writing a textwrap.TextWrapper subclass that
implements my patch, and copying 70-ish lines of code to make a 2 line
change seems like overkill to me. Could this be added as a new option to
TextWrapper?
|
| msg60040 (view) |
Author: Guido van Rossum (gvanrossum) |
Date: 2008-01-17 18:14 |
|
Use .splitlines() to break the input into lines, wrap each "line"
separately, and join again?
|
| msg60042 (view) |
Author: Mark Dickinson (marketdickinson) |
Date: 2008-01-17 18:16 |
|
For what it's worth, I think there is a legitimate complaint here, though it was initially
unclear to me exactly what that complaint was. Consider the following:
>>> from textwrap import *
>>> T = TextWrapper(replace_whitespace=False, width=14)
>>> for line in T.wrap('one two\nthree four'): print line
...
one two
three
four
The surprise (if I understand correctly) is not the first line break, but the second, between
"three" and "four": it shouldn't be necessary, since "three four" fits quite happily on a
line of length 14.
|
| msg60043 (view) |
Author: Tom Parker (palfrey) |
Date: 2008-01-17 18:23 |
|
@Guido: Thanks for the suggestion, it fixes my immediate problem!
@Mark: Yup, that was exactly my issue. It took a while to figure out why
the heck it was ignoring my linebreaks, and then once I'd found
replace_whitespace it appeared to be doing the "wrong" thing to me.
I'm still for the changing of the behaviour to what I expected, but I
can live with this otherwise. Documenting that this is the behaviour in
the textwrap docs, and suggesting workarounds for those who want the
other choice might be a good idea tho.
|
| msg60045 (view) |
Author: Guido van Rossum (gvanrossum) |
Date: 2008-01-17 18:26 |
|
Mark, it looks like the replace_whitespace flag shouldn't be used with
input containing newlines.
|
| msg60047 (view) |
Author: Mark Dickinson (marketdickinson) |
Date: 2008-01-17 18:39 |
|
Is it worth double checking with Greg Ward that this behaviour really is
intentional?
|
| msg60048 (view) |
Author: Guido van Rossum (gvanrossum) |
Date: 2008-01-17 18:40 |
|
Good luck reaching him.
I'm pretty sure that the default behavior intentional *reflows* all
input text. Perhaps you can derive clues from reading the docs (which I
haven't)?
|
|
| Date |
User |
Action |
Args |
| 2008-01-17 18:40:34 | gvanrossum | set | messages:
+ msg60048 |
| 2008-01-17 18:39:15 | marketdickinson | set | messages:
+ msg60047 |
| 2008-01-17 18:26:06 | gvanrossum | set | messages:
+ msg60045 |
| 2008-01-17 18:23:19 | palfrey | set | messages:
+ msg60043 |
| 2008-01-17 18:16:28 | marketdickinson | set | messages:
+ msg60042 |
| 2008-01-17 18:14:05 | gvanrossum | set | messages:
+ msg60040 |
| 2008-01-17 18:03:31 | palfrey | set | messages:
+ msg60039 |
| 2008-01-17 17:51:40 | gvanrossum | set | status: open -> closed resolution: rejected messages:
+ msg60038 nosy:
+ gvanrossum |
| 2008-01-17 15:51:10 | palfrey | set | messages:
+ msg60032 |
| 2008-01-17 15:24:33 | marketdickinson | set | nosy:
+ marketdickinson messages:
+ msg60031 |
| 2008-01-17 12:46:49 | palfrey | set | files:
+ textwrap-fix.patch messages:
+ msg60027 |
| 2008-01-17 12:40:01 | palfrey | create | |
|