diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index bc50114..ccb07f6 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -512,7 +512,7 @@ def _default_pprint(obj, p, cycle): def _seq_pprinter_factory(start, end, basetype): """ Factory that returns a pprint function useful for sequences. Used by - the default pprint for tuples, dicts, lists, sets and frozensets. + the default pprint for tuples, dicts, and lists. """ def inner(obj, p, cycle): typ = type(obj) @@ -536,6 +536,33 @@ def _seq_pprinter_factory(start, end, basetype): return inner +def _set_pprinter_factory(start, end, basetype): + """ + Factory that returns a pprint function useful for sets and frozensets. + """ + def inner(obj, p, cycle): + typ = type(obj) + if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: + # If the subclass provides its own repr, use it instead. + return p.text(typ.__repr__(obj)) + + if cycle: + return p.text(start + '...' + end) + if len(obj) == 0: + # Special case. + p.text(basetype.__name__ + '()') + else: + step = len(start) + p.begin_group(step, start) + for idx, x in enumerate(obj): + if idx: + p.text(',') + p.breakable() + p.pretty(x) + p.end_group(step, end) + return inner + + def _dict_pprinter_factory(start, end, basetype=None): """ Factory that returns a pprint function used by the default pprint of @@ -668,8 +695,8 @@ _type_pprinters = { list: _seq_pprinter_factory('[', ']', list), dict: _dict_pprinter_factory('{', '}', dict), - set: _seq_pprinter_factory('{', '}', set), - frozenset: _seq_pprinter_factory('frozenset([', '])', frozenset), + set: _set_pprinter_factory('{', '}', set), + frozenset: _set_pprinter_factory('frozenset({', '})', frozenset), super: _super_pprint, _re_pattern_type: _re_pattern_pprint, type: _type_pprint,