##// END OF EJS Templates
Merge pull request #10959 from takluyver/i10950...
Min RK -
r24121:819efc48 merge
parent child Browse files
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()), 'foo')
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())
@@ -395,6 +395,10 b' class RepresentationPrinter(PrettyPrinter):'
395 meth = cls._repr_pretty_
395 meth = cls._repr_pretty_
396 if callable(meth):
396 if callable(meth):
397 return meth(obj, self, cycle)
397 return meth(obj, self, cycle)
398 if cls is not object \
399 and callable(cls.__dict__.get('__repr__')):
400 return _repr_pprint(obj, self, cycle)
401
398 return _default_pprint(obj, self, cycle)
402 return _default_pprint(obj, self, cycle)
399 finally:
403 finally:
400 self.end_group()
404 self.end_group()
@@ -540,17 +544,12 b' def _default_pprint(obj, p, cycle):'
540 p.end_group(1, '>')
544 p.end_group(1, '>')
541
545
542
546
543 def _seq_pprinter_factory(start, end, basetype):
547 def _seq_pprinter_factory(start, end):
544 """
548 """
545 Factory that returns a pprint function useful for sequences. Used by
549 Factory that returns a pprint function useful for sequences. Used by
546 the default pprint for tuples, dicts, and lists.
550 the default pprint for tuples, dicts, and lists.
547 """
551 """
548 def inner(obj, p, cycle):
552 def inner(obj, p, cycle):
549 typ = type(obj)
550 if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
551 # If the subclass provides its own repr, use it instead.
552 return p.text(typ.__repr__(obj))
553
554 if cycle:
553 if cycle:
555 return p.text(start + '...' + end)
554 return p.text(start + '...' + end)
556 step = len(start)
555 step = len(start)
@@ -567,21 +566,16 b' def _seq_pprinter_factory(start, end, basetype):'
567 return inner
566 return inner
568
567
569
568
570 def _set_pprinter_factory(start, end, basetype):
569 def _set_pprinter_factory(start, end):
571 """
570 """
572 Factory that returns a pprint function useful for sets and frozensets.
571 Factory that returns a pprint function useful for sets and frozensets.
573 """
572 """
574 def inner(obj, p, cycle):
573 def inner(obj, p, cycle):
575 typ = type(obj)
576 if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
577 # If the subclass provides its own repr, use it instead.
578 return p.text(typ.__repr__(obj))
579
580 if cycle:
574 if cycle:
581 return p.text(start + '...' + end)
575 return p.text(start + '...' + end)
582 if len(obj) == 0:
576 if len(obj) == 0:
583 # Special case.
577 # Special case.
584 p.text(basetype.__name__ + '()')
578 p.text(type(obj).__name__ + '()')
585 else:
579 else:
586 step = len(start)
580 step = len(start)
587 p.begin_group(step, start)
581 p.begin_group(step, start)
@@ -599,17 +593,12 b' def _set_pprinter_factory(start, end, basetype):'
599 return inner
593 return inner
600
594
601
595
602 def _dict_pprinter_factory(start, end, basetype=None):
596 def _dict_pprinter_factory(start, end):
603 """
597 """
604 Factory that returns a pprint function used by the default pprint of
598 Factory that returns a pprint function used by the default pprint of
605 dicts and dict proxies.
599 dicts and dict proxies.
606 """
600 """
607 def inner(obj, p, cycle):
601 def inner(obj, p, cycle):
608 typ = type(obj)
609 if basetype is not None and typ is not basetype and typ.__repr__ != basetype.__repr__:
610 # If the subclass provides its own repr, use it instead.
611 return p.text(typ.__repr__(obj))
612
613 if cycle:
602 if cycle:
614 return p.text('{...}')
603 return p.text('{...}')
615 step = len(start)
604 step = len(start)
@@ -750,12 +739,12 b' _type_pprinters = {'
750 int: _repr_pprint,
739 int: _repr_pprint,
751 float: _repr_pprint,
740 float: _repr_pprint,
752 str: _repr_pprint,
741 str: _repr_pprint,
753 tuple: _seq_pprinter_factory('(', ')', tuple),
742 tuple: _seq_pprinter_factory('(', ')'),
754 list: _seq_pprinter_factory('[', ']', list),
743 list: _seq_pprinter_factory('[', ']'),
755 dict: _dict_pprinter_factory('{', '}', dict),
744 dict: _dict_pprinter_factory('{', '}'),
756
745
757 set: _set_pprinter_factory('{', '}', set),
746 set: _set_pprinter_factory('{', '}'),
758 frozenset: _set_pprinter_factory('frozenset({', '})', frozenset),
747 frozenset: _set_pprinter_factory('frozenset({', '})'),
759 super: _super_pprint,
748 super: _super_pprint,
760 _re_pattern_type: _re_pattern_pprint,
749 _re_pattern_type: _re_pattern_pprint,
761 type: _type_pprint,
750 type: _type_pprint,
@@ -420,4 +420,24 b' def test_function_pretty():'
420 return "Don't panic"
420 return "Don't panic"
421
421
422 nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life))
422 nt.assert_in('meaning_of_life(question=None)', pretty.pretty(meaning_of_life))
423
423
424
425 class OrderedCounter(Counter, OrderedDict):
426 'Counter that remembers the order elements are first encountered'
427
428 def __repr__(self):
429 return '%s(%r)' % (self.__class__.__name__, OrderedDict(self))
430
431 def __reduce__(self):
432 return self.__class__, (OrderedDict(self),)
433
434 class MySet(set): # Override repr of a basic type
435 def __repr__(self):
436 return 'mine'
437
438 def test_custom_repr():
439 """A custom repr should override a pretty printer for a parent type"""
440 oc = OrderedCounter("abracadabra")
441 nt.assert_in("OrderedCounter(OrderedDict", pretty.pretty(oc))
442
443 nt.assert_equal(pretty.pretty(MySet()), 'mine')
General Comments 0
You need to be logged in to leave comments. Login now