From e9667ef737e072eed9fa141fd6c07cb4a1cba862 2013-04-16 17:40:31 From: Min RK Date: 2013-04-16 17:40:31 Subject: [PATCH] Merge pull request #3163 from rkern/fix-empty-frozen-set-pretty Fix the set and frozenset pretty printer to handle the empty case correctly --- diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index bc50114..9a60f43 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,40 @@ 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) + # Like dictionary keys, we will try to sort the items. + items = list(obj) + try: + items.sort() + except Exception: + # Sometimes the items don't sort. + pass + for idx, x in enumerate(items): + 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 +702,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, diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index 817616c..eff710f 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -84,6 +84,20 @@ def test_callability_checking(): nt.assert_equal(gotoutput, expectedoutput) + +def test_sets(): + """ + Test that set and frozenset use Python 3 formatting. + """ + objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]), + frozenset([1, 2]), set([-1, -2, -3])] + expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}', + 'frozenset({1, 2})', '{-3, -2, -1}'] + for obj, expected_output in zip(objects, expected): + got_output = pretty.pretty(obj) + yield nt.assert_equal, got_output, expected_output + + @skip_without('xxlimited') def test_pprint_heap_allocated_type(): """