Show More
@@ -512,7 +512,7 b' def _default_pprint(obj, p, cycle):' | |||||
512 | def _seq_pprinter_factory(start, end, basetype): |
|
512 | def _seq_pprinter_factory(start, end, basetype): | |
513 | """ |
|
513 | """ | |
514 | Factory that returns a pprint function useful for sequences. Used by |
|
514 | Factory that returns a pprint function useful for sequences. Used by | |
515 |
the default pprint for tuples, dicts, |
|
515 | the default pprint for tuples, dicts, and lists. | |
516 | """ |
|
516 | """ | |
517 | def inner(obj, p, cycle): |
|
517 | def inner(obj, p, cycle): | |
518 | typ = type(obj) |
|
518 | typ = type(obj) | |
@@ -536,6 +536,40 b' def _seq_pprinter_factory(start, end, basetype):' | |||||
536 | return inner |
|
536 | return inner | |
537 |
|
537 | |||
538 |
|
538 | |||
|
539 | def _set_pprinter_factory(start, end, basetype): | |||
|
540 | """ | |||
|
541 | Factory that returns a pprint function useful for sets and frozensets. | |||
|
542 | """ | |||
|
543 | def inner(obj, p, cycle): | |||
|
544 | typ = type(obj) | |||
|
545 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: | |||
|
546 | # If the subclass provides its own repr, use it instead. | |||
|
547 | return p.text(typ.__repr__(obj)) | |||
|
548 | ||||
|
549 | if cycle: | |||
|
550 | return p.text(start + '...' + end) | |||
|
551 | if len(obj) == 0: | |||
|
552 | # Special case. | |||
|
553 | p.text(basetype.__name__ + '()') | |||
|
554 | else: | |||
|
555 | step = len(start) | |||
|
556 | p.begin_group(step, start) | |||
|
557 | # Like dictionary keys, we will try to sort the items. | |||
|
558 | items = list(obj) | |||
|
559 | try: | |||
|
560 | items.sort() | |||
|
561 | except Exception: | |||
|
562 | # Sometimes the items don't sort. | |||
|
563 | pass | |||
|
564 | for idx, x in enumerate(items): | |||
|
565 | if idx: | |||
|
566 | p.text(',') | |||
|
567 | p.breakable() | |||
|
568 | p.pretty(x) | |||
|
569 | p.end_group(step, end) | |||
|
570 | return inner | |||
|
571 | ||||
|
572 | ||||
539 | def _dict_pprinter_factory(start, end, basetype=None): |
|
573 | def _dict_pprinter_factory(start, end, basetype=None): | |
540 | """ |
|
574 | """ | |
541 | Factory that returns a pprint function used by the default pprint of |
|
575 | Factory that returns a pprint function used by the default pprint of | |
@@ -668,8 +702,8 b' _type_pprinters = {' | |||||
668 | list: _seq_pprinter_factory('[', ']', list), |
|
702 | list: _seq_pprinter_factory('[', ']', list), | |
669 | dict: _dict_pprinter_factory('{', '}', dict), |
|
703 | dict: _dict_pprinter_factory('{', '}', dict), | |
670 |
|
704 | |||
671 |
set: _se |
|
705 | set: _set_pprinter_factory('{', '}', set), | |
672 |
frozenset: _se |
|
706 | frozenset: _set_pprinter_factory('frozenset({', '})', frozenset), | |
673 | super: _super_pprint, |
|
707 | super: _super_pprint, | |
674 | _re_pattern_type: _re_pattern_pprint, |
|
708 | _re_pattern_type: _re_pattern_pprint, | |
675 | type: _type_pprint, |
|
709 | type: _type_pprint, |
@@ -84,6 +84,20 b' def test_callability_checking():' | |||||
84 |
|
84 | |||
85 | nt.assert_equal(gotoutput, expectedoutput) |
|
85 | nt.assert_equal(gotoutput, expectedoutput) | |
86 |
|
86 | |||
|
87 | ||||
|
88 | def test_sets(): | |||
|
89 | """ | |||
|
90 | Test that set and frozenset use Python 3 formatting. | |||
|
91 | """ | |||
|
92 | objects = [set(), frozenset(), set([1]), frozenset([1]), set([1, 2]), | |||
|
93 | frozenset([1, 2]), set([-1, -2, -3])] | |||
|
94 | expected = ['set()', 'frozenset()', '{1}', 'frozenset({1})', '{1, 2}', | |||
|
95 | 'frozenset({1, 2})', '{-3, -2, -1}'] | |||
|
96 | for obj, expected_output in zip(objects, expected): | |||
|
97 | got_output = pretty.pretty(obj) | |||
|
98 | yield nt.assert_equal, got_output, expected_output | |||
|
99 | ||||
|
100 | ||||
87 | @skip_without('xxlimited') |
|
101 | @skip_without('xxlimited') | |
88 | def test_pprint_heap_allocated_type(): |
|
102 | def test_pprint_heap_allocated_type(): | |
89 | """ |
|
103 | """ |
General Comments 0
You need to be logged in to leave comments.
Login now