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.

classification
Title: pprint produces invalid output for long strings
Type: behavior Stage: resolved
Components: Library (Lib) Versions: Python 3.4, Python 3.5
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: serhiy.storchaka Nosy List: Arfrever, fdrake, pitrou, python-dev, r.david.murray, serhiy.storchaka
Priority: normal Keywords: easy, needs review, patch

Created on 2013-09-27 13:40 by serhiy.storchaka, last changed 2022-04-11 14:57 by admin. This issue is now closed.

Files
File name Uploaded Description Edit
pprint_str_evaluable.patch serhiy.storchaka, 2013-09-27 15:11 Use backslashes review
pprint_str_evaluable_2.patch serhiy.storchaka, 2013-09-27 17:01 Use parentheses review
Messages (15)
msg198474 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-27 13:40
The documentation says:
"""The pprint module provides a capability to “pretty-print” arbitrary Python data structures in a form which can be used as input to the interpreter."""

But #since issue17150 long lines are splitted on shorter lines without continuation character and the result can't be used as input to the interpreter.

>>> pprint.pprint('abc '*20)
'abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc abc '\
'abc '
msg198478 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2013-09-27 14:20
FYI this defect was mentioned (by Antoine) in Issue17530, though that issue is about bytes.
msg198480 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-27 15:11
Thank you David. In any case this is worth a separate issue.

Here is a patch. Personally I prefer to add continuation backslash even for inner wrapped lines, but I'm not sure that most core developers will agreed with it.
msg198488 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-09-27 16:11
According to PEP 8, parentheses should be used for multi-line expressions, not backslashes:

"""The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation."""

Also, please note that in both cases it adds a character to the lines, which must be taken into account in the algorithm.
msg198491 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-27 17:01
Second path uses parentheses. It is more complicated than first patch.

I prefer first patch, because it is simple, because second variant wastes more width, because parentheses looks too similar to tuple, and because "implicit string literal concatenation considered harmful"[1].

http://comments.gmane.org/gmane.comp.python.ideas/20525
msg198492 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-09-27 17:17
> "implicit string literal concatenation considered harmful"

Both patches rely on implicit string literal concatenation.

Another possibility is not fixing this issue, and deciding it is normal behaviour. It is trivial to add parentheses if you need them.
msg198494 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-27 18:12
> Both patches rely on implicit string literal concatenation.

But with backslash it *looks* less implicit.

> Another possibility is not fixing this issue, and deciding it is normal behaviour. It is trivial to add parentheses if you need them.

It would be safer just do not wrap long line on first level.
msg198502 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-09-27 20:53
> > Another possibility is not fixing this issue, and deciding it is
> normal behaviour. It is trivial to add parentheses if you need them.
> 
> It would be safer just do not wrap long line on first level.

That would defeat the whole point of pprint().
msg198666 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-29 22:48
The whole point of pprint() is formatted output of containers such as list or dict. Before 3.4 it does not wrap long strings at all.

Of course I don't think this alternative is good. But at least it is better than producing illegal output.
msg198667 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2013-09-29 22:51
> The whole point of pprint() is formatted output of containers such as
> list or dict.

Well, the whole point of pprint is pretty-printing. Being restricted to
containers is a bug, not a feature...
msg198668 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2013-09-29 22:54
I doubt we are able to fix it in maintained releases.
msg206271 - (view) Author: Mark Lawrence (BreamoreBoy) * Date: 2013-12-16 01:29
Would it pay to have a meta issue for all the outstanding pprint issues, or possibly make issue 7434 the meta issue?
msg232416 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-10 08:59
What to do with this issue?
msg232978 - (view) Author: Roundup Robot (python-dev) (Python triager) Date: 2014-12-20 19:02
New changeset 872f048f0403 by Serhiy Storchaka in branch '3.4':
Issue #19104: pprint now produces evaluable output for wrapped strings.
https://hg.python.org/cpython/rev/872f048f0403

New changeset 4d3066d4a5df by Serhiy Storchaka in branch 'default':
Issue #19104: pprint now produces evaluable output for wrapped strings.
https://hg.python.org/cpython/rev/4d3066d4a5df
msg232981 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2014-12-20 19:44
Thank you Antoine for your review.
History
Date User Action Args
2022-04-11 14:57:51adminsetgithub: 63303
2014-12-20 19:44:15serhiy.storchakasetstatus: open -> closed
messages: + msg232981

assignee: serhiy.storchaka
resolution: fixed
stage: patch review -> resolved
2014-12-20 19:02:26python-devsetnosy: + python-dev
messages: + msg232978
2014-12-20 18:50:23serhiy.storchakalinkissue17530 dependencies
2014-12-19 00:11:02Arfreversetnosy: + Arfrever
2014-12-18 15:37:44serhiy.storchakasetkeywords: + needs review
2014-12-10 08:59:00serhiy.storchakasetmessages: + msg232416
versions: + Python 3.5
2014-02-03 15:42:39BreamoreBoysetnosy: - BreamoreBoy
2013-12-16 01:29:41BreamoreBoysetnosy: + BreamoreBoy
messages: + msg206271
2013-09-29 22:54:51serhiy.storchakasetmessages: + msg198668
2013-09-29 22:51:40pitrousetmessages: + msg198667
2013-09-29 22:48:04serhiy.storchakasetmessages: + msg198666
2013-09-27 20:53:08pitrousetmessages: + msg198502
2013-09-27 18:12:23serhiy.storchakasetmessages: + msg198494
2013-09-27 17:17:46pitrousetmessages: + msg198492
2013-09-27 17:01:55serhiy.storchakasetfiles: + pprint_str_evaluable_2.patch

messages: + msg198491
2013-09-27 16:11:46pitrousetmessages: + msg198488
2013-09-27 15:12:04serhiy.storchakalinkissue19103 dependencies
2013-09-27 15:11:11serhiy.storchakasetfiles: + pprint_str_evaluable.patch
keywords: + patch
messages: + msg198480

stage: needs patch -> patch review
2013-09-27 14:20:22r.david.murraysetnosy: + r.david.murray
messages: + msg198478
2013-09-27 13:40:50serhiy.storchakacreate