##// 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 10 import nose.tools as nt
11 11
12 12 from IPython.core.formatters import FormatterABC, PlainTextFormatter
13 from IPython.lib import pretty
13 14
14 15 class A(object):
15 16 def __repr__(self):
@@ -19,6 +20,16 b' class B(A):'
19 20 def __repr__(self):
20 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 33 def foo_printer(obj, pp, cycle):
23 34 pp.text('foo')
24 35
@@ -27,9 +38,15 b' def test_pretty():'
27 38 f.for_type(A, foo_printer)
28 39 nt.assert_equals(f(A()), 'foo')
29 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 45 f.pprint = False
31 46 nt.assert_equals(f(A()), 'A()')
32 47 nt.assert_equals(f(B()), 'B()')
48 nt.assert_equals(f(GoodPretty()), 'GoodPretty()')
49
33 50
34 51 def test_deferred():
35 52 f = PlainTextFormatter()
@@ -347,7 +347,11 b' class RepresentationPrinter(PrettyPrinter):'
347 347 return printer(obj, self, cycle)
348 348 # Finally look for special method names.
349 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 355 return _default_pprint(obj, self, cycle)
352 356 finally:
353 357 self.end_group()
General Comments 0
You need to be logged in to leave comments. Login now