Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit aa4c36f

Browse files
Issue #23775: pprint() of OrderedDict now outputs the same representation
as repr().
1 parent f3fa308 commit aa4c36f

3 files changed

Lines changed: 40 additions & 24 deletions

File tree

Lib/pprint.py

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,10 @@
3434
3535
"""
3636

37+
import collections as _collections
3738
import re
3839
import sys as _sys
3940
import types as _types
40-
from collections import OrderedDict as _OrderedDict
4141
from io import StringIO as _StringIO
4242

4343
__all__ = ["pprint","pformat","isreadable","isrecursive","saferepr",
@@ -188,16 +188,25 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level):
188188
write((self._indent_per_level - 1) * ' ')
189189
length = len(object)
190190
if length:
191-
if isinstance(object, _OrderedDict):
192-
items = list(object.items())
193-
else:
194-
items = sorted(object.items(), key=_safe_tuple)
191+
items = sorted(object.items(), key=_safe_tuple)
195192
self._format_dict_items(items, stream, indent, allowance + 1,
196193
context, level)
197194
write('}')
198195

199196
_dispatch[dict.__repr__] = _pprint_dict
200-
_dispatch[_OrderedDict.__repr__] = _pprint_dict
197+
198+
def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level):
199+
if not len(object):
200+
stream.write(repr(object))
201+
return
202+
cls = object.__class__
203+
stream.write(cls.__name__ + '(')
204+
self._format(list(object.items()), stream,
205+
indent + len(cls.__name__) + 1, allowance + 1,
206+
context, level)
207+
stream.write(')')
208+
209+
_dispatch[_collections.OrderedDict.__repr__] = _pprint_ordered_dict
201210

202211
def _pprint_list(self, object, stream, indent, allowance, context, level):
203212
stream.write('[')

Lib/test/test_pprint.py

Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -272,19 +272,23 @@ def test_sorted_dict(self):
272272
r"{5: [[]], 'xy\tab\n': (3,), (): {}}")
273273

274274
def test_ordered_dict(self):
275+
d = collections.OrderedDict()
276+
self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()')
277+
d = collections.OrderedDict([])
278+
self.assertEqual(pprint.pformat(d, width=1), 'OrderedDict()')
275279
words = 'the quick brown fox jumped over a lazy dog'.split()
276280
d = collections.OrderedDict(zip(words, itertools.count()))
277281
self.assertEqual(pprint.pformat(d),
278282
"""\
279-
{'the': 0,
280-
'quick': 1,
281-
'brown': 2,
282-
'fox': 3,
283-
'jumped': 4,
284-
'over': 5,
285-
'a': 6,
286-
'lazy': 7,
287-
'dog': 8}""")
283+
OrderedDict([('the', 0),
284+
('quick', 1),
285+
('brown', 2),
286+
('fox', 3),
287+
('jumped', 4),
288+
('over', 5),
289+
('a', 6),
290+
('lazy', 7),
291+
('dog', 8)])""")
288292

289293
def test_mapping_proxy(self):
290294
words = 'the quick brown fox jumped over a lazy dog'.split()
@@ -303,15 +307,15 @@ def test_mapping_proxy(self):
303307
d = collections.OrderedDict(zip(words, itertools.count()))
304308
m = types.MappingProxyType(d)
305309
self.assertEqual(pprint.pformat(m), """\
306-
mappingproxy({'the': 0,
307-
'quick': 1,
308-
'brown': 2,
309-
'fox': 3,
310-
'jumped': 4,
311-
'over': 5,
312-
'a': 6,
313-
'lazy': 7,
314-
'dog': 8})""")
310+
mappingproxy(OrderedDict([('the', 0),
311+
('quick', 1),
312+
('brown', 2),
313+
('fox', 3),
314+
('jumped', 4),
315+
('over', 5),
316+
('a', 6),
317+
('lazy', 7),
318+
('dog', 8)]))""")
315319

316320
def test_subclassing(self):
317321
o = {'names with spaces': 'should be presented using repr()',

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,9 @@ Core and Builtins
3030
Library
3131
-------
3232

33+
- Issue #23775: pprint() of OrderedDict now outputs the same representation
34+
as repr().
35+
3336
- Issue #23765: Removed IsBadStringPtr calls in ctypes
3437

3538
- Issue #22364: Improved some re error messages using regex for hints.

0 commit comments

Comments
 (0)