diff -r 123fb7054d07 Lib/pprint.py --- a/Lib/pprint.py Tue Mar 24 21:25:16 2015 -0700 +++ b/Lib/pprint.py Wed Mar 25 09:44:11 2015 +0200 @@ -37,7 +37,7 @@ saferepr() import re import sys as _sys import types as _types -from collections import OrderedDict as _OrderedDict +import collections as _collections from io import StringIO as _StringIO __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", @@ -185,16 +185,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('[') diff -r 123fb7054d07 Lib/test/test_pprint.py --- a/Lib/test/test_pprint.py Tue Mar 24 21:25:16 2015 -0700 +++ b/Lib/test/test_pprint.py Wed Mar 25 09:44:11 2015 +0200 @@ -258,20 +258,21 @@ class QueryTestCase(unittest.TestCase): self.assertEqual(pprint.pformat({"xy\tab\n": (3,), 5: [[]], (): {}}), r"{5: [[]], 'xy\tab\n': (3,), (): {}}") + maxDiff = 10000 def test_ordered_dict(self): words = 'the quick brown fox jumped over a lazy dog'.split() d = collections.OrderedDict(zip(words, itertools.count())) self.assertEqual(pprint.pformat(d), """\ -{'the': 0, - 'quick': 1, - 'brown': 2, - 'fox': 3, - 'jumped': 4, - 'over': 5, - 'a': 6, - 'lazy': 7, - 'dog': 8}""") +OrderedDict([('the', 0), + ('quick', 1), + ('brown', 2), + ('fox', 3), + ('jumped', 4), + ('over', 5), + ('a', 6), + ('lazy', 7), + ('dog', 8)])""") def test_mapping_proxy(self): words = 'the quick brown fox jumped over a lazy dog'.split() @@ -290,15 +291,15 @@ mappingproxy({'a': 6, d = collections.OrderedDict(zip(words, itertools.count())) m = types.MappingProxyType(d) self.assertEqual(pprint.pformat(m), """\ -mappingproxy({'the': 0, - 'quick': 1, - 'brown': 2, - 'fox': 3, - 'jumped': 4, - 'over': 5, - 'a': 6, - 'lazy': 7, - 'dog': 8})""") +mappingproxy(OrderedDict([('the', 0), + ('quick', 1), + ('brown', 2), + ('fox', 3), + ('jumped', 4), + ('over', 5), + ('a', 6), + ('lazy', 7), + ('dog', 8)]))""") def test_subclassing(self): o = {'names with spaces': 'should be presented using repr()',