|
34 | 34 |
|
35 | 35 | """ |
36 | 36 |
|
| 37 | +import collections as _collections |
37 | 38 | import re |
38 | 39 | import sys as _sys |
39 | 40 | import types as _types |
40 | | -from collections import OrderedDict as _OrderedDict |
41 | 41 | from io import StringIO as _StringIO |
42 | 42 |
|
43 | 43 | __all__ = ["pprint","pformat","isreadable","isrecursive","saferepr", |
@@ -188,16 +188,25 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level): |
188 | 188 | write((self._indent_per_level - 1) * ' ') |
189 | 189 | length = len(object) |
190 | 190 | 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) |
195 | 192 | self._format_dict_items(items, stream, indent, allowance + 1, |
196 | 193 | context, level) |
197 | 194 | write('}') |
198 | 195 |
|
199 | 196 | _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 |
201 | 210 |
|
202 | 211 | def _pprint_list(self, object, stream, indent, allowance, context, level): |
203 | 212 | stream.write('[') |
|
0 commit comments