diff --git a/IPython/lib/pretty.py b/IPython/lib/pretty.py index 0689b9d..ee67787 100644 --- a/IPython/lib/pretty.py +++ b/IPython/lib/pretty.py @@ -125,49 +125,6 @@ __all__ = ['pretty', 'pprint', 'PrettyPrinter', 'RepresentationPrinter', _re_pattern_type = type(re.compile('')) -def _failed_repr(obj, e): - """Render a failed repr, including the exception. - - Tries to get exception and type info - """ - # get exception name - if e.__class__.__module__ in ('exceptions', 'builtins'): - ename = e.__class__.__name__ - else: - ename = '{}.{}'.format( - e.__class__.__module__, - e.__class__.__name__, - ) - # and exception string, which sometimes fails - # (usually due to unicode error message) - try: - estr = str(e) - except Exception: - estr = "unknown" - - # and class name - try: - klass = _safe_getattr(obj, '__class__', None) or type(obj) - mod = _safe_getattr(klass, '__module__', None) - if mod in (None, '__builtin__', 'builtins', 'exceptions'): - classname = klass.__name__ - else: - classname = mod + '.' + klass.__name__ - except Exception: - # this may be paranoid, but we already know repr is broken - classname = "unknown type" - - # the informative repr - return ") failed: {}: {}>".format( - classname, id(obj), ename, estr, - ) - -def _safe_repr(obj): - """Don't assume repr is not broken.""" - try: - return repr(obj) - except Exception as e: - return _failed_repr(obj, e) def _safe_getattr(obj, attr, default=None): """Safe version of getattr. @@ -560,7 +517,7 @@ def _default_pprint(obj, p, cycle): klass = _safe_getattr(obj, '__class__', None) or type(obj) if _safe_getattr(klass, '__repr__', None) not in _baseclass_reprs: # A user-provided repr. Find newlines and replace them with p.break_() - output = _safe_repr(obj) + output = repr(obj) for idx,output_line in enumerate(output.splitlines()): if idx: p.break_() @@ -736,7 +693,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(_safe_repr(obj)) + p.text(repr(obj)) def _function_pprint(obj, p, cycle): diff --git a/IPython/lib/tests/test_pretty.py b/IPython/lib/tests/test_pretty.py index 2e487dd..b63197f 100644 --- a/IPython/lib/tests/test_pretty.py +++ b/IPython/lib/tests/test_pretty.py @@ -1,16 +1,8 @@ -"""Tests for IPython.lib.pretty. -""" -#----------------------------------------------------------------------------- -# Copyright (c) 2011, the IPython Development Team. -# +"""Tests for IPython.lib.pretty.""" + +# Copyright (c) IPython Development Team. # Distributed under the terms of the Modified BSD License. -# -# The full license is in the file COPYING.txt, distributed with this software. -#----------------------------------------------------------------------------- -#----------------------------------------------------------------------------- -# Imports -#----------------------------------------------------------------------------- from __future__ import print_function # Third-party imports @@ -20,9 +12,6 @@ import nose.tools as nt from IPython.lib import pretty from IPython.testing.decorators import skip_without -#----------------------------------------------------------------------------- -# Classes and functions -#----------------------------------------------------------------------------- class MyList(object): def __init__(self, content): @@ -161,11 +150,9 @@ def test_pprint_break_repr(): 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) - nt.assert_in("at 0x", output) - nt.assert_in("test_pretty", output) + """Don't catch bad repr errors""" + with nt.assert_raises(ZeroDivisionError): + output = pretty.pretty(BadRepr()) class BadException(Exception): def __str__(self): @@ -181,10 +168,8 @@ class ReallyBadRepr(object): raise BadException() def test_really_bad_repr(): - output = pretty.pretty(ReallyBadRepr()) - nt.assert_in("failed", output) - nt.assert_in("BadException: unknown", output) - nt.assert_in("unknown type", output) + with nt.assert_raises(BadException): + output = pretty.pretty(ReallyBadRepr()) class SA(object):