diff options
author | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-26 06:51:33 (GMT) |
---|---|---|
committer | Serhiy Storchaka <storchaka@gmail.com> | 2015-03-26 06:51:33 (GMT) |
commit | aa4c36fbbb47fe54d578cb7f04f53179360e5aa7 (patch) | |
tree | fae7bf62473c76949f1037bf1f3ca1d8c0358e69 /Lib/pprint.py | |
parent | f3fa308817a578c8809c70f6b24b1c489eeef803 (diff) | |
download | cpython-aa4c36fbbb47fe54d578cb7f04f53179360e5aa7.zip cpython-aa4c36fbbb47fe54d578cb7f04f53179360e5aa7.tar.gz cpython-aa4c36fbbb47fe54d578cb7f04f53179360e5aa7.tar.bz2 |
Issue #23775: pprint() of OrderedDict now outputs the same representation
as repr().
Diffstat (limited to 'Lib/pprint.py')
-rw-r--r-- | Lib/pprint.py | 21 |
1 files changed, 15 insertions, 6 deletions
diff --git a/Lib/pprint.py b/Lib/pprint.py index fc5395e..c79c713 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -34,10 +34,10 @@ saferepr() """ +import collections as _collections import re import sys as _sys import types as _types -from collections import OrderedDict as _OrderedDict from io import StringIO as _StringIO __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", @@ -188,16 +188,25 @@ class PrettyPrinter: write((self._indent_per_level - 1) * ' ') length = len(object) if length: - if isinstance(object, _OrderedDict): - items = list(object.items()) - else: - items = sorted(object.items(), key=_safe_tuple) + items = sorted(object.items(), key=_safe_tuple) self._format_dict_items(items, stream, indent, allowance + 1, context, level) write('}') _dispatch[dict.__repr__] = _pprint_dict - _dispatch[_OrderedDict.__repr__] = _pprint_dict + + def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level): + if not len(object): + stream.write(repr(object)) + return + cls = object.__class__ + stream.write(cls.__name__ + '(') + self._format(list(object.items()), stream, + indent + len(cls.__name__) + 1, allowance + 1, + context, level) + stream.write(')') + + _dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict def _pprint_list(self, object, stream, indent, allowance, context, level): stream.write('[') |