diff --git a/Lib/pprint.py b/Lib/pprint.py index 92a2c543ac279c..ffba173c2bb24a 100644 --- a/Lib/pprint.py +++ b/Lib/pprint.py @@ -220,7 +220,14 @@ def _pprint_dataclass(self, object, stream, indent, allowance, context, level): def _pprint_dict(self, object, stream, indent, allowance, context, level): write = stream.write - write('{') + typ = object.__class__ + if typ is frozendict: + stream.write(typ.__name__ + '({') + end = '})' + indent += len(typ.__name__) + 1 + else: + write('{') + end = '}' if self._indent_per_level > 1: write((self._indent_per_level - 1) * ' ') length = len(object) @@ -231,9 +238,10 @@ def _pprint_dict(self, object, stream, indent, allowance, context, level): items = object.items() self._format_dict_items(items, stream, indent, allowance + 1, context, level) - write('}') + write(end) _dispatch[dict.__repr__] = _pprint_dict + _dispatch[frozendict.__repr__] = _pprint_dict def _pprint_ordered_dict(self, object, stream, indent, allowance, context, level): if not len(object): diff --git a/Lib/test/test_pprint.py b/Lib/test/test_pprint.py index 41c337ade7eca1..1061ce155840df 100644 --- a/Lib/test/test_pprint.py +++ b/Lib/test/test_pprint.py @@ -330,6 +330,16 @@ def test_basic_line_wrap(self): for type in [dict, dict2]: self.assertEqual(pprint.pformat(type(o)), exp) + exp = """\ +frozendict({'RPM_cal': 0, + 'RPM_cal2': 48059, + 'Speed_cal': 0, + 'controldesk_runtime_us': 0, + 'main_code_runtime_us': 0, + 'read_io_runtime_us': 0, + 'write_io_runtime_us': 43690})""" + self.assertEqual(pprint.pformat(frozendict(o)), exp) + o = range(100) exp = 'dict_keys([%s])' % ',\n '.join(map(str, o)) keys = dict.fromkeys(o).keys()