Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

BUG: Fix pretty-printing for overzealous objects that will return som…

…ething non-callable for any requested attribute (thus confusing hasattr).
  • Loading branch information...
commit 1f5362dc416890e8d429dcbbb4ae42c4de425684 1 parent 3912ea7
Robert Kern authored
Showing with 22 additions and 1 deletion.
  1. +17 −0 IPython/core/tests/test_formatters.py
  2. +5 −1 IPython/lib/pretty.py
17 IPython/core/tests/test_formatters.py
View
@@ -10,6 +10,7 @@
import nose.tools as nt
from IPython.core.formatters import FormatterABC, PlainTextFormatter
+from IPython.lib import pretty
class A(object):
def __repr__(self):
@@ -19,6 +20,16 @@ class B(A):
def __repr__(self):
return 'B()'
+class BadPretty(object):
+ _repr_pretty_ = None
+
+class GoodPretty(object):
+ def _repr_pretty_(self, pp, cycle):
+ pp.text('foo')
+
+ def __repr__(self):
+ return 'GoodPretty()'
+
def foo_printer(obj, pp, cycle):
pp.text('foo')
@@ -27,9 +38,15 @@ def test_pretty():
f.for_type(A, foo_printer)
nt.assert_equals(f(A()), 'foo')
nt.assert_equals(f(B()), 'foo')
+ nt.assert_equals(f(GoodPretty()), 'foo')
+ # Just don't raise an exception for the following:
+ f(BadPretty())
+
f.pprint = False
nt.assert_equals(f(A()), 'A()')
nt.assert_equals(f(B()), 'B()')
+ nt.assert_equals(f(GoodPretty()), 'GoodPretty()')
+
def test_deferred():
f = PlainTextFormatter()
6 IPython/lib/pretty.py
View
@@ -347,7 +347,11 @@ def pretty(self, obj):
return printer(obj, self, cycle)
# Finally look for special method names.
if hasattr(obj_class, '_repr_pretty_'):
- return obj_class._repr_pretty_(obj, self, cycle)
+ # Some objects automatically create any requested
+ # attribute. Try to ignore most of them by checking for
+ # callability.
+ if callable(obj_class._repr_pretty_):
+ return obj_class._repr_pretty_(obj, self, cycle)
return _default_pprint(obj, self, cycle)
finally:
self.end_group()
Please sign in to comment.
Something went wrong with that request. Please try again.