bpo-28850: Fix PrettyPrinter.format overrides being ignored for contents of small containers#22120
Merged
Merged
Conversation
…the buggy behaviour
…t part of the PrettyPrinter class (no functional change)
…on container contents
Member
Author
ZackerySpytz
reviewed
Nov 4, 2020
Comment on lines
65
to
+75
| def saferepr(object): | ||
| """Version of repr() which can handle recursive data structures.""" | ||
| return _safe_repr(object, {}, None, 0, True)[0] | ||
| return PrettyPrinter()._safe_repr(object, {}, None, 0)[0] | ||
|
|
||
| def isreadable(object): | ||
| """Determine if saferepr(object) is readable by eval().""" | ||
| return _safe_repr(object, {}, None, 0, True)[1] | ||
| return PrettyPrinter()._safe_repr(object, {}, None, 0)[1] | ||
|
|
||
| def isrecursive(object): | ||
| """Determine if object requires a recursive representation.""" | ||
| return _safe_repr(object, {}, None, 0, True)[2] | ||
| return PrettyPrinter()._safe_repr(object, {}, None, 0)[2] |
Contributor
There was a problem hiding this comment.
It seems rather unfortunate that a PrettyPrinter() instance is now created here for every function call.
Member
Author
There was a problem hiding this comment.
I know. We could:
-
leave _safe_repr where it was and pass in a function that it should call when it needs to recurse (default is it calls itself, but PrettyPrinter passes in self.format)
-
move _safe_repr to a stateless superclass of PrettyPrinter, and instantiate a module level _defaultPrettyPrinter instance of this superclass for use in these functions.
Any other options?
Contributor
There was a problem hiding this comment.
IMO this is fine, and better than the alternatives.
adorilson
pushed a commit
to adorilson/cpython
that referenced
this pull request
Mar 13, 2021
… small containers (pythonGH-22120)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR fixes a bug where subclassing PrettyPrinter and overriding format doesn't work for container contents (see issue 28850 for details).
The solution suggested here is to make _safe_repr a method of PrettyPrinter, and make it call self.format() when it needs to recurse on container contents.
The diff looks substantial, but that's mostly due to the indentation of _safe_repr code. To make it easier to review, I split it into several commits, where the indentation is done in one commit as a noop change. The other commits have much smaller diffs.
https://bugs.python.org/issue28850