pprint: Remove tracking of whether the object is recursive
This information is not used anywhere, we can simplify by just not tracking it
This commit is contained in:
parent
e5a448cd5f
commit
767f08cecd
|
@ -114,7 +114,6 @@ class PrettyPrinter:
|
||||||
objid = id(object)
|
objid = id(object)
|
||||||
if objid in context:
|
if objid in context:
|
||||||
stream.write(_recursion(object))
|
stream.write(_recursion(object))
|
||||||
self._recursive = True
|
|
||||||
self._readable = False
|
self._readable = False
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -487,21 +486,16 @@ class PrettyPrinter:
|
||||||
write("\n" + " " * indent)
|
write("\n" + " " * indent)
|
||||||
|
|
||||||
def _repr(self, object: Any, context: Dict[int, int], level: int) -> str:
|
def _repr(self, object: Any, context: Dict[int, int], level: int) -> str:
|
||||||
repr, readable, recursive = self.format(
|
repr, readable = self.format(object, context.copy(), self._depth, level)
|
||||||
object, context.copy(), self._depth, level
|
|
||||||
)
|
|
||||||
if not readable:
|
if not readable:
|
||||||
self._readable = False
|
self._readable = False
|
||||||
if recursive:
|
|
||||||
self._recursive = True
|
|
||||||
return repr
|
return repr
|
||||||
|
|
||||||
def format(
|
def format(
|
||||||
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
|
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
|
||||||
) -> Tuple[str, bool, bool]:
|
) -> Tuple[str, bool]:
|
||||||
"""Format object for a specific context, returning a string
|
"""Format object for a specific context, returning a string
|
||||||
and flags indicating whether the representation is 'readable'
|
and a flag indicating whether the representation is 'readable'.
|
||||||
and whether the object represents a recursive construct.
|
|
||||||
"""
|
"""
|
||||||
return self._safe_repr(object, context, maxlevels, level)
|
return self._safe_repr(object, context, maxlevels, level)
|
||||||
|
|
||||||
|
@ -621,31 +615,30 @@ class PrettyPrinter:
|
||||||
|
|
||||||
def _safe_repr(
|
def _safe_repr(
|
||||||
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
|
self, object: Any, context: Dict[int, int], maxlevels: Optional[int], level: int
|
||||||
) -> Tuple[str, bool, bool]:
|
) -> Tuple[str, bool]:
|
||||||
# Return triple (repr_string, isreadable, isrecursive).
|
# Return pair (repr_string, isreadable).
|
||||||
typ = type(object)
|
typ = type(object)
|
||||||
if typ in _builtin_scalars:
|
if typ in _builtin_scalars:
|
||||||
return repr(object), True, False
|
return repr(object), True
|
||||||
|
|
||||||
r = getattr(typ, "__repr__", None)
|
r = getattr(typ, "__repr__", None)
|
||||||
|
|
||||||
if issubclass(typ, int) and r is int.__repr__:
|
if issubclass(typ, int) and r is int.__repr__:
|
||||||
if self._underscore_numbers:
|
if self._underscore_numbers:
|
||||||
return f"{object:_d}", True, False
|
return f"{object:_d}", True
|
||||||
else:
|
else:
|
||||||
return repr(object), True, False
|
return repr(object), True
|
||||||
|
|
||||||
if issubclass(typ, dict) and r is dict.__repr__:
|
if issubclass(typ, dict) and r is dict.__repr__:
|
||||||
if not object:
|
if not object:
|
||||||
return "{}", True, False
|
return "{}", True
|
||||||
objid = id(object)
|
objid = id(object)
|
||||||
if maxlevels and level >= maxlevels:
|
if maxlevels and level >= maxlevels:
|
||||||
return "{...}", False, objid in context
|
return "{...}", False
|
||||||
if objid in context:
|
if objid in context:
|
||||||
return _recursion(object), False, True
|
return _recursion(object), False
|
||||||
context[objid] = 1
|
context[objid] = 1
|
||||||
readable = True
|
readable = True
|
||||||
recursive = False
|
|
||||||
components: List[str] = []
|
components: List[str] = []
|
||||||
append = components.append
|
append = components.append
|
||||||
level += 1
|
level += 1
|
||||||
|
@ -654,51 +647,46 @@ class PrettyPrinter:
|
||||||
else:
|
else:
|
||||||
items = object.items()
|
items = object.items()
|
||||||
for k, v in items:
|
for k, v in items:
|
||||||
krepr, kreadable, krecur = self.format(k, context, maxlevels, level)
|
krepr, kreadable = self.format(k, context, maxlevels, level)
|
||||||
vrepr, vreadable, vrecur = self.format(v, context, maxlevels, level)
|
vrepr, vreadable = self.format(v, context, maxlevels, level)
|
||||||
append(f"{krepr}: {vrepr}")
|
append(f"{krepr}: {vrepr}")
|
||||||
readable = readable and kreadable and vreadable
|
readable = readable and kreadable and vreadable
|
||||||
if krecur or vrecur:
|
|
||||||
recursive = True
|
|
||||||
del context[objid]
|
del context[objid]
|
||||||
return "{%s}" % ", ".join(components), readable, recursive
|
return "{%s}" % ", ".join(components), readable
|
||||||
|
|
||||||
if (issubclass(typ, list) and r is list.__repr__) or (
|
if (issubclass(typ, list) and r is list.__repr__) or (
|
||||||
issubclass(typ, tuple) and r is tuple.__repr__
|
issubclass(typ, tuple) and r is tuple.__repr__
|
||||||
):
|
):
|
||||||
if issubclass(typ, list):
|
if issubclass(typ, list):
|
||||||
if not object:
|
if not object:
|
||||||
return "[]", True, False
|
return "[]", True
|
||||||
format = "[%s]"
|
format = "[%s]"
|
||||||
elif len(object) == 1:
|
elif len(object) == 1:
|
||||||
format = "(%s,)"
|
format = "(%s,)"
|
||||||
else:
|
else:
|
||||||
if not object:
|
if not object:
|
||||||
return "()", True, False
|
return "()", True
|
||||||
format = "(%s)"
|
format = "(%s)"
|
||||||
objid = id(object)
|
objid = id(object)
|
||||||
if maxlevels and level >= maxlevels:
|
if maxlevels and level >= maxlevels:
|
||||||
return format % "...", False, objid in context
|
return format % "...", False
|
||||||
if objid in context:
|
if objid in context:
|
||||||
return _recursion(object), False, True
|
return _recursion(object), False
|
||||||
context[objid] = 1
|
context[objid] = 1
|
||||||
readable = True
|
readable = True
|
||||||
recursive = False
|
|
||||||
components = []
|
components = []
|
||||||
append = components.append
|
append = components.append
|
||||||
level += 1
|
level += 1
|
||||||
for o in object:
|
for o in object:
|
||||||
orepr, oreadable, orecur = self.format(o, context, maxlevels, level)
|
orepr, oreadable = self.format(o, context, maxlevels, level)
|
||||||
append(orepr)
|
append(orepr)
|
||||||
if not oreadable:
|
if not oreadable:
|
||||||
readable = False
|
readable = False
|
||||||
if orecur:
|
|
||||||
recursive = True
|
|
||||||
del context[objid]
|
del context[objid]
|
||||||
return format % ", ".join(components), readable, recursive
|
return format % ", ".join(components), readable
|
||||||
|
|
||||||
rep = repr(object)
|
rep = repr(object)
|
||||||
return rep, bool(rep and not rep.startswith("<")), False
|
return rep, bool(rep and not rep.startswith("<"))
|
||||||
|
|
||||||
|
|
||||||
_builtin_scalars = frozenset({str, bytes, bytearray, float, complex, bool, type(None)})
|
_builtin_scalars = frozenset({str, bytes, bytearray, float, complex, bool, type(None)})
|
||||||
|
|
Loading…
Reference in New Issue