mirror of
https://github.com/python/cpython.git
synced 2025-10-09 16:34:44 +00:00
Add an experimental mechanism to support extending the pprint formatting.
Partly responds to SF bug #505152.
This commit is contained in:
parent
4993c51b94
commit
aee113d368
3 changed files with 57 additions and 9 deletions
|
@ -115,15 +115,11 @@ class PrettyPrinter:
|
|||
return sio.getvalue()
|
||||
|
||||
def isrecursive(self, object):
|
||||
self.__recursive = 0
|
||||
self.__repr(object, {}, 0)
|
||||
return self.__recursive
|
||||
return self.format(object, {}, 0)[2]
|
||||
|
||||
def isreadable(self, object):
|
||||
self.__recursive = 0
|
||||
self.__readable = 1
|
||||
self.__repr(object, {}, 0)
|
||||
return self.__readable and not self.__recursive
|
||||
s, readable, recursive = self.format(object, {}, 0)
|
||||
return readable and not recursive
|
||||
|
||||
def __format(self, object, stream, indent, allowance, context, level):
|
||||
level = level + 1
|
||||
|
@ -196,14 +192,22 @@ class PrettyPrinter:
|
|||
write(rep)
|
||||
|
||||
def __repr(self, object, context, level):
|
||||
repr, readable, recursive = _safe_repr(object, context,
|
||||
self.__depth, level)
|
||||
repr, readable, recursive = self.format(object, context.copy(),
|
||||
self.__depth, level)
|
||||
if not readable:
|
||||
self.__readable = 0
|
||||
if recursive:
|
||||
self.__recursive = 1
|
||||
return repr
|
||||
|
||||
def format(self, object, context, maxlevels, level):
|
||||
"""Format object for a specific context, returning a string
|
||||
and flags indicating whether the representation is 'readable'
|
||||
and whether the object represents a recursive construct.
|
||||
"""
|
||||
return _safe_repr(object, context, maxlevels, level)
|
||||
|
||||
|
||||
# Return triple (repr_string, isreadable, isrecursive).
|
||||
|
||||
def _safe_repr(object, context, maxlevels, level):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue