diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py index 6d1d12b..ef010c3 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -50,9 +50,6 @@ def _valid_formatter(f): """ if f is None: return False - elif isinstance(f, type(str.find)): - # unbound methods on compiled classes have type method_descriptor - return False elif isinstance(f, types.BuiltinFunctionType): # bound methods on compiled classes have type builtin_function return True @@ -68,6 +65,9 @@ def _valid_formatter(f): def _safe_get_formatter_method(obj, name): """Safely get a formatter method""" + if inspect.isclass(obj): + # repr methods only make sense on instances, not classes + return None method = pretty._safe_getattr(obj, name, None) # formatter methods must be bound if _valid_formatter(method): diff --git a/IPython/core/tests/test_formatters.py b/IPython/core/tests/test_formatters.py index 1c511ea..6061ed4 100644 --- a/IPython/core/tests/test_formatters.py +++ b/IPython/core/tests/test_formatters.py @@ -314,7 +314,6 @@ def test_print_method_bound(): class MyHTML(object): def _repr_html_(self): return "hello" - with capture_output() as captured: result = f(MyHTML) nt.assert_is(result, None) @@ -325,6 +324,45 @@ def test_print_method_bound(): nt.assert_equal(result, "hello") nt.assert_equal(captured.stderr, "") +def test_print_method_weird(): + + class TextMagicHat(object): + def __getattr__(self, key): + return key + + f = HTMLFormatter() + + text_hat = TextMagicHat() + nt.assert_equal(text_hat._repr_html_, '_repr_html_') + with capture_output() as captured: + result = f(text_hat) + + nt.assert_is(result, None) + nt.assert_not_in("FormatterWarning", captured.stderr) + + class CallableMagicHat(object): + def __getattr__(self, key): + return lambda : key + + call_hat = CallableMagicHat() + with capture_output() as captured: + result = f(call_hat) + + nt.assert_equal(result, '_repr_html_') + nt.assert_not_in("FormatterWarning", captured.stderr) + + class BadReprArgs(object): + def _repr_html_(self, extra, args): + return "html" + + bad = BadReprArgs() + with capture_output() as captured: + result = f(bad) + + nt.assert_is(result, None) + nt.assert_not_in("FormatterWarning", captured.stderr) + + def test_format_config(): """config objects don't pretend to support fancy reprs with lazy attrs""" f = HTMLFormatter()