Show More
@@ -49,7 +49,7 b' def test_pretty():' | |||||
49 | f = PlainTextFormatter() |
|
49 | f = PlainTextFormatter() | |
50 | f.for_type(A, foo_printer) |
|
50 | f.for_type(A, foo_printer) | |
51 | nt.assert_equal(f(A()), 'foo') |
|
51 | nt.assert_equal(f(A()), 'foo') | |
52 |
nt.assert_equal(f(B()), ' |
|
52 | nt.assert_equal(f(B()), 'B()') | |
53 | nt.assert_equal(f(GoodPretty()), 'foo') |
|
53 | nt.assert_equal(f(GoodPretty()), 'foo') | |
54 | # Just don't raise an exception for the following: |
|
54 | # Just don't raise an exception for the following: | |
55 | f(BadPretty()) |
|
55 | f(BadPretty()) |
@@ -398,6 +398,10 b' class RepresentationPrinter(PrettyPrinter):' | |||||
398 | meth = cls._repr_pretty_ |
|
398 | meth = cls._repr_pretty_ | |
399 | if callable(meth): |
|
399 | if callable(meth): | |
400 | return meth(obj, self, cycle) |
|
400 | return meth(obj, self, cycle) | |
|
401 | if cls is not object \ | |||
|
402 | and callable(cls.__dict__.get('__repr__')): | |||
|
403 | return _repr_pprint(obj, self, cycle) | |||
|
404 | ||||
401 | return _default_pprint(obj, self, cycle) |
|
405 | return _default_pprint(obj, self, cycle) | |
402 | finally: |
|
406 | finally: | |
403 | self.end_group() |
|
407 | self.end_group() | |
@@ -548,17 +552,12 b' def _default_pprint(obj, p, cycle):' | |||||
548 | p.end_group(1, '>') |
|
552 | p.end_group(1, '>') | |
549 |
|
553 | |||
550 |
|
554 | |||
551 |
def _seq_pprinter_factory(start, end |
|
555 | def _seq_pprinter_factory(start, end): | |
552 | """ |
|
556 | """ | |
553 | Factory that returns a pprint function useful for sequences. Used by |
|
557 | Factory that returns a pprint function useful for sequences. Used by | |
554 | the default pprint for tuples, dicts, and lists. |
|
558 | the default pprint for tuples, dicts, and lists. | |
555 | """ |
|
559 | """ | |
556 | def inner(obj, p, cycle): |
|
560 | def inner(obj, p, cycle): | |
557 | typ = type(obj) |
|
|||
558 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: |
|
|||
559 | # If the subclass provides its own repr, use it instead. |
|
|||
560 | return p.text(typ.__repr__(obj)) |
|
|||
561 |
|
||||
562 | if cycle: |
|
561 | if cycle: | |
563 | return p.text(start + '...' + end) |
|
562 | return p.text(start + '...' + end) | |
564 | step = len(start) |
|
563 | step = len(start) | |
@@ -575,21 +574,16 b' def _seq_pprinter_factory(start, end, basetype):' | |||||
575 | return inner |
|
574 | return inner | |
576 |
|
575 | |||
577 |
|
576 | |||
578 |
def _set_pprinter_factory(start, end |
|
577 | def _set_pprinter_factory(start, end): | |
579 | """ |
|
578 | """ | |
580 | Factory that returns a pprint function useful for sets and frozensets. |
|
579 | Factory that returns a pprint function useful for sets and frozensets. | |
581 | """ |
|
580 | """ | |
582 | def inner(obj, p, cycle): |
|
581 | def inner(obj, p, cycle): | |
583 | typ = type(obj) |
|
|||
584 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: |
|
|||
585 | # If the subclass provides its own repr, use it instead. |
|
|||
586 | return p.text(typ.__repr__(obj)) |
|
|||
587 |
|
||||
588 | if cycle: |
|
582 | if cycle: | |
589 | return p.text(start + '...' + end) |
|
583 | return p.text(start + '...' + end) | |
590 | if len(obj) == 0: |
|
584 | if len(obj) == 0: | |
591 | # Special case. |
|
585 | # Special case. | |
592 |
p.text( |
|
586 | p.text(type(obj).__name__ + '()') | |
593 | else: |
|
587 | else: | |
594 | step = len(start) |
|
588 | step = len(start) | |
595 | p.begin_group(step, start) |
|
589 | p.begin_group(step, start) | |
@@ -607,17 +601,12 b' def _set_pprinter_factory(start, end, basetype):' | |||||
607 | return inner |
|
601 | return inner | |
608 |
|
602 | |||
609 |
|
603 | |||
610 |
def _dict_pprinter_factory(start, end |
|
604 | def _dict_pprinter_factory(start, end): | |
611 | """ |
|
605 | """ | |
612 | Factory that returns a pprint function used by the default pprint of |
|
606 | Factory that returns a pprint function used by the default pprint of | |
613 | dicts and dict proxies. |
|
607 | dicts and dict proxies. | |
614 | """ |
|
608 | """ | |
615 | def inner(obj, p, cycle): |
|
609 | def inner(obj, p, cycle): | |
616 | typ = type(obj) |
|
|||
617 | if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__: |
|
|||
618 | # If the subclass provides its own repr, use it instead. |
|
|||
619 | return p.text(typ.__repr__(obj)) |
|
|||
620 |
|
||||
621 | if cycle: |
|
610 | if cycle: | |
622 | return p.text('{...}') |
|
611 | return p.text('{...}') | |
623 | step = len(start) |
|
612 | step = len(start) | |
@@ -754,12 +743,12 b' _type_pprinters = {' | |||||
754 | int: _repr_pprint, |
|
743 | int: _repr_pprint, | |
755 | float: _repr_pprint, |
|
744 | float: _repr_pprint, | |
756 | str: _repr_pprint, |
|
745 | str: _repr_pprint, | |
757 |
tuple: _seq_pprinter_factory('(', ')' |
|
746 | tuple: _seq_pprinter_factory('(', ')'), | |
758 |
list: _seq_pprinter_factory('[', ']' |
|
747 | list: _seq_pprinter_factory('[', ']'), | |
759 |
dict: _dict_pprinter_factory('{', '}' |
|
748 | dict: _dict_pprinter_factory('{', '}'), | |
760 |
|
749 | |||
761 |
set: _set_pprinter_factory('{', '}' |
|
750 | set: _set_pprinter_factory('{', '}'), | |
762 |
frozenset: _set_pprinter_factory('frozenset({', '})' |
|
751 | frozenset: _set_pprinter_factory('frozenset({', '})'), | |
763 | super: _super_pprint, |
|
752 | super: _super_pprint, | |
764 | _re_pattern_type: _re_pattern_pprint, |
|
753 | _re_pattern_type: _re_pattern_pprint, | |
765 | type: _type_pprint, |
|
754 | type: _type_pprint, |
@@ -534,3 +534,23 b' def test_dictproxy():' | |||||
534 | nt.assert_equal(pretty.pretty(obj), expected) |
|
534 | nt.assert_equal(pretty.pretty(obj), expected) | |
535 | nt.assert_equal(pretty.pretty(underlying_dict), |
|
535 | nt.assert_equal(pretty.pretty(underlying_dict), | |
536 | "{-3: {...}, 0: dict_proxy({-3: {...}, 0: {...}})}") |
|
536 | "{-3: {...}, 0: dict_proxy({-3: {...}, 0: {...}})}") | |
|
537 | ||||
|
538 | class OrderedCounter(Counter, OrderedDict): | |||
|
539 | 'Counter that remembers the order elements are first encountered' | |||
|
540 | ||||
|
541 | def __repr__(self): | |||
|
542 | return '%s(%r)' % (self.__class__.__name__, OrderedDict(self)) | |||
|
543 | ||||
|
544 | def __reduce__(self): | |||
|
545 | return self.__class__, (OrderedDict(self),) | |||
|
546 | ||||
|
547 | class MySet(set): # Override repr of a basic type | |||
|
548 | def __repr__(self): | |||
|
549 | return 'mine' | |||
|
550 | ||||
|
551 | def test_custom_repr(): | |||
|
552 | """A custom repr should override a pretty printer for a parent type""" | |||
|
553 | oc = OrderedCounter("abracadabra") | |||
|
554 | nt.assert_in("OrderedCounter(OrderedDict", pretty.pretty(oc)) | |||
|
555 | ||||
|
556 | nt.assert_equal(pretty.pretty(MySet()), 'mine') |
General Comments 0
You need to be logged in to leave comments.
Login now