diff options
author | Walter Dörwald <walter@livinglogic.de> | 2003-12-03 20:15:28 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2003-12-03 20:15:28 (GMT) |
commit | 7a7ede54d4311d3bcb5000aeedeab8cc80391c70 (patch) | |
tree | ba1f5fdd118cc2d6c0b4c8098814c0eeb6e1c694 /Lib/pprint.py | |
parent | 291481b4dbb88560179c8064c1e2e198365bd3dd (diff) | |
download | cpython-7a7ede54d4311d3bcb5000aeedeab8cc80391c70.zip cpython-7a7ede54d4311d3bcb5000aeedeab8cc80391c70.tar.gz cpython-7a7ede54d4311d3bcb5000aeedeab8cc80391c70.tar.bz2 |
Patch #750542: pprint now will pretty print subclasses of list, tuple
and dict too, as long as they don't overwrite __repr__().
Diffstat (limited to 'Lib/pprint.py')
-rw-r--r-- | Lib/pprint.py | 22 |
1 files changed, 13 insertions, 9 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py index 16d8eae..19b9661 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -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]" |