diff options
Diffstat (limited to 'Lib/pprint.py')
-rw-r--r-- | Lib/pprint.py | 15 |
1 files changed, 11 insertions, 4 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py index b3a644649e..ae96dde1e0 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -35,7 +35,7 @@ saferepr() """ import sys as _sys - +from collections import OrderedDict as _OrderedDict from io import StringIO as _StringIO __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", @@ -86,7 +86,11 @@ class _safe_key: self.obj = obj def __lt__(self, other): - rv = self.obj.__lt__(other.obj) + try: + rv = self.obj.__lt__(other.obj) + except TypeError: + rv = NotImplemented + if rv is NotImplemented: rv = (str(type(self.obj)), id(self.obj)) < \ (str(type(other.obj)), id(other.obj)) @@ -163,7 +167,7 @@ class PrettyPrinter: if sepLines: r = getattr(typ, "__repr__", None) - if issubclass(typ, dict) and r is dict.__repr__: + if issubclass(typ, dict): write('{') if self._indent_per_level > 1: write((self._indent_per_level - 1) * ' ') @@ -171,7 +175,10 @@ class PrettyPrinter: if length: context[objid] = 1 indent = indent + self._indent_per_level - items = sorted(object.items(), key=_safe_tuple) + if issubclass(typ, _OrderedDict): + items = list(object.items()) + else: + items = sorted(object.items(), key=_safe_tuple) key, ent = items[0] rep = self._repr(key, context, level) write(rep) |