##// END OF EJS Templates
BUG: Fix pretty-printing for overzealous objects that will return something non-callable for any requested attribute (thus confusing hasattr).
Robert Kern -
Show More
@@ -10,6 +10,7 b' except:'
10 import nose.tools as nt
10 import nose.tools as nt
11
11
12 from IPython.core.formatters import FormatterABC, PlainTextFormatter
12 from IPython.core.formatters import FormatterABC, PlainTextFormatter
13 from IPython.lib import pretty
13
14
14 class A(object):
15 class A(object):
15 def __repr__(self):
16 def __repr__(self):
@@ -19,6 +20,16 b' class B(A):'
19 def __repr__(self):
20 def __repr__(self):
20 return 'B()'
21 return 'B()'
21
22
23 class BadPretty(object):
24 _repr_pretty_ = None
25
26 class GoodPretty(object):
27 def _repr_pretty_(self, pp, cycle):
28 pp.text('foo')
29
30 def __repr__(self):
31 return 'GoodPretty()'
32
22 def foo_printer(obj, pp, cycle):
33 def foo_printer(obj, pp, cycle):
23 pp.text('foo')
34 pp.text('foo')
24
35
@@ -27,9 +38,15 b' def test_pretty():'
27 f.for_type(A, foo_printer)
38 f.for_type(A, foo_printer)
28 nt.assert_equals(f(A()), 'foo')
39 nt.assert_equals(f(A()), 'foo')
29 nt.assert_equals(f(B()), 'foo')
40 nt.assert_equals(f(B()), 'foo')
41 nt.assert_equals(f(GoodPretty()), 'foo')
42 # Just don't raise an exception for the following:
43 f(BadPretty())
44
30 f.pprint = False
45 f.pprint = False
31 nt.assert_equals(f(A()), 'A()')
46 nt.assert_equals(f(A()), 'A()')
32 nt.assert_equals(f(B()), 'B()')
47 nt.assert_equals(f(B()), 'B()')
48 nt.assert_equals(f(GoodPretty()), 'GoodPretty()')
49
33
50
34 def test_deferred():
51 def test_deferred():
35 f = PlainTextFormatter()
52 f = PlainTextFormatter()
@@ -347,7 +347,11 b' class RepresentationPrinter(PrettyPrinter):'
347 return printer(obj, self, cycle)
347 return printer(obj, self, cycle)
348 # Finally look for special method names.
348 # Finally look for special method names.
349 if hasattr(obj_class, '_repr_pretty_'):
349 if hasattr(obj_class, '_repr_pretty_'):
350 return obj_class._repr_pretty_(obj, self, cycle)
350 # Some objects automatically create any requested
351 # attribute. Try to ignore most of them by checking for
352 # callability.
353 if callable(obj_class._repr_pretty_):
354 return obj_class._repr_pretty_(obj, self, cycle)
351 return _default_pprint(obj, self, cycle)
355 return _default_pprint(obj, self, cycle)
352 finally:
356 finally:
353 self.end_group()
357 self.end_group()
General Comments 0
You need to be logged in to leave comments. Login now