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

allow NotImplementedError in formatters #4832

Merged
merged 3 commits into from
Jan 24, 2014
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 14 additions & 5 deletions IPython/core/formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,22 +183,31 @@ def format_types(self):
# Formatters for specific format types (text, html, svg, etc.)
#-----------------------------------------------------------------------------

class FormatterWarning(UserWarning):
"""Warning class for errors in formatters"""

@decorator
def warn_format_error(method, self, *args, **kwargs):
"""decorator for warning on failed format call"""
try:
r = method(self, *args, **kwargs)
except NotImplementedError as e:
# don't warn on NotImplementedErrors
return None
except Exception as e:
warn("Exception in %s formatter: %s" % (self.format_type, e))
warnings.warn("Exception in %s formatter: %s" % (self.format_type, e),
FormatterWarning,
)
return None
if r is None or isinstance(r, self._return_type) or \
(isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
return r
else:
warn("%s formatter returned invalid type %s (expected %s) for object: %s" % (
self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])
))

warnings.warn(
"%s formatter returned invalid type %s (expected %s) for object: %s" % \
(self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])),
FormatterWarning
)


class FormatterABC(with_metaclass(abc.ABCMeta, object)):
Expand Down
18 changes: 15 additions & 3 deletions IPython/core/tests/test_formatters.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,17 +240,29 @@ def _repr_html_(self):
with capture_output() as captured:
result = f(bad)
nt.assert_is(result, None)
nt.assert_in("WARNING", captured.stderr)
nt.assert_in("FormatterWarning", captured.stderr)
nt.assert_in("text/html", captured.stderr)
nt.assert_in("zero", captured.stderr)

def test_nowarn_notimplemented():
f = HTMLFormatter()
class HTMLNotImplemented(object):
def _repr_html_(self):
raise NotImplementedError
return 1/0
h = HTMLNotImplemented()
with capture_output() as captured:
result = f(h)
nt.assert_is(result, None)
nt.assert_not_in("FormatterWarning", captured.stderr)

def test_warn_error_for_type():
f = HTMLFormatter()
f.for_type(int, lambda i: name_error)
with capture_output() as captured:
result = f(5)
nt.assert_is(result, None)
nt.assert_in("WARNING", captured.stderr)
nt.assert_in("FormatterWarning", captured.stderr)
nt.assert_in("text/html", captured.stderr)
nt.assert_in("name_error", captured.stderr)

Expand All @@ -263,7 +275,7 @@ def _repr_pretty_(self):
with capture_output() as captured:
result = f(bad)
nt.assert_is(result, None)
nt.assert_in("WARNING", captured.stderr)
nt.assert_in("FormatterWarning", captured.stderr)
nt.assert_in("text/plain", captured.stderr)
nt.assert_in("argument", captured.stderr)

Expand Down