Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

protect against broken repr in lib.pretty #4459

Merged
merged 4 commits into from
Nov 4, 2013
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 2 additions & 5 deletions IPython/core/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ def __call__(self, obj):
"""
try:
return repr(obj)
except TypeError:
except Exception:
return None


Expand Down Expand Up @@ -465,10 +465,7 @@ def _deferred_printers_default(self):
def __call__(self, obj):
"""Compute the pretty representation of the object."""
if not self.pprint:
try:
return repr(obj)
except TypeError:
return ''
return pretty._safe_repr(obj)
else:
# This uses use StringIO, as cStringIO doesn't handle unicode.
stream = StringIO()
Expand Down
11 changes: 9 additions & 2 deletions IPython/lib/pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@ def _repr_pretty_(self, p, cycle):

_re_pattern_type = type(re.compile(''))

def _safe_repr(obj):
"""Don't trust repr to not be broken."""
try:
return repr(obj)
except Exception as e:
return "<repr(obj) failed: %s>" % e
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find it useful to include the type of the exception as well.

return "<repr(obj) failed: {0}: {1}>".format(type(e), e)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While I agree that reporting the exception message is useful, can we still at least include the usual <module.type at 0x.....> information? That will help locate the source of the problem.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

type info / address added, along with some extra paranoia about bad objects



def pretty(obj, verbose=False, max_width=79, newline='\n'):
"""
Expand Down Expand Up @@ -495,7 +502,7 @@ def _default_pprint(obj, p, cycle):
klass = getattr(obj, '__class__', None) or type(obj)
if getattr(klass, '__repr__', None) not in _baseclass_reprs:
# A user-provided repr. Find newlines and replace them with p.break_()
output = repr(obj)
output = _safe_repr(obj)
for idx,output_line in enumerate(output.splitlines()):
if idx:
p.break_()
Expand Down Expand Up @@ -673,7 +680,7 @@ def _type_pprint(obj, p, cycle):

def _repr_pprint(obj, p, cycle):
"""A pprint that just redirects to the normal repr function."""
p.text(repr(obj))
p.text(_safe_repr(obj))


def _function_pprint(obj, p, cycle):
Expand Down
11 changes: 10 additions & 1 deletion IPython/lib/tests/test_pretty.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ def _repr_pretty_(self, p, cycle):
with p.group(4,"TG: ",":"):
p.pretty(BreakingRepr())

class BadRepr(object):

def __repr__(self):
return 1/0


def test_indentation():
Expand Down Expand Up @@ -150,4 +154,9 @@ def test_pprint_break_repr():
"""
output = pretty.pretty(BreakingReprParent())
expected = "TG: Breaking(\n ):"
nt.assert_equal(output, expected)
nt.assert_equal(output, expected)

def test_bad_repr():
"""Don't raise, even when repr fails"""
output = pretty.pretty(BadRepr())
nt.assert_in("failed", output)