Patch #750542: pprint now will pretty print subclasses of list, tuple

and dict too, as long as they don't overwrite __repr__().
This commit is contained in:
Walter Dörwald 2003-12-03 20:15:28 +00:00
parent 291481b4db
commit 7a7ede54d4
3 changed files with 55 additions and 13 deletions

View file

@ -90,9 +90,9 @@ class PrettyPrinter:
"""
indent = int(indent)
width = int(width)
assert indent >= 0
assert indent >= 0, "indent must be >= 0"
assert depth is None or depth > 0, "depth must be > 0"
assert width
assert width, "width must be != 0"
self._depth = depth
self._indent_per_level = indent
self._width = width
@ -130,7 +130,8 @@ class PrettyPrinter:
write = stream.write
if sepLines:
if typ is dict:
r = typ.__repr__
if issubclass(typ, dict) and r is dict.__repr__:
write('{')
if self._indent_per_level > 1:
write((self._indent_per_level - 1) * ' ')
@ -157,8 +158,9 @@ class PrettyPrinter:
write('}')
return
if typ is list or typ is tuple:
if typ is list:
if (issubclass(typ, list) and r is list.__repr__) or \
(issubclass(typ, tuple) and r is tuple.__repr__):
if issubclass(typ, list):
write('[')
endchar = ']'
else:
@ -179,7 +181,7 @@ class PrettyPrinter:
allowance + 1, context, level)
indent = indent - self._indent_per_level
del context[objid]
if typ is tuple and length == 1:
if issubclass(typ, tuple) and length == 1:
write(',')
write(endchar)
return
@ -226,7 +228,8 @@ def _safe_repr(object, context, maxlevels, level):
write(qget(char, `char`[1:-1]))
return ("%s%s%s" % (closure, sio.getvalue(), closure)), True, False
if typ is dict:
r = typ.__repr__
if issubclass(typ, dict) and r is dict.__repr__:
if not object:
return "{}", True, False
objid = _id(object)
@ -251,8 +254,9 @@ def _safe_repr(object, context, maxlevels, level):
del context[objid]
return "{%s}" % _commajoin(components), readable, recursive
if typ is list or typ is tuple:
if typ is list:
if (issubclass(typ, list) and r is list.__repr__) or \
(issubclass(typ, tuple) and r is tuple.__repr__):
if issubclass(typ, list):
if not object:
return "[]", True, False
format = "[%s]"