diff --git a/IPython/core/formatters.py b/IPython/core/formatters.py index a54fa42..5ffad9a 100644 --- a/IPython/core/formatters.py +++ b/IPython/core/formatters.py @@ -26,6 +26,7 @@ Authors: # Stdlib imports import abc import sys +import types import warnings from IPython.external.decorator import decorator @@ -312,7 +313,8 @@ class BaseFormatter(Configurable): return printer(obj) # Finally look for special method names method = pretty._safe_getattr(obj, self.print_method, None) - if method is not None: + # print_method must be a bound method: + if isinstance(method, types.MethodType) and method.__self__ is not None: return method() return None else: diff --git a/IPython/core/tests/test_formatters.py b/IPython/core/tests/test_formatters.py index 710ce1b..65105d8 100644 --- a/IPython/core/tests/test_formatters.py +++ b/IPython/core/tests/test_formatters.py @@ -8,6 +8,7 @@ except: numpy = None import nose.tools as nt +from IPython.config import Config from IPython.core.formatters import ( PlainTextFormatter, HTMLFormatter, PDFFormatter, _mod_name_key ) @@ -289,3 +290,33 @@ def test_pdf_formatter(): pdf = MakePDF() f = PDFFormatter() nt.assert_equal(f(pdf), 'PDF') + +def test_print_method_bound(): + f = HTMLFormatter() + class MyHTML(object): + def _repr_html_(self): + return "hello" + + with capture_output() as captured: + result = f(MyHTML) + nt.assert_is(result, None) + nt.assert_not_in("FormatterWarning", captured.stderr) + + with capture_output() as captured: + result = f(MyHTML()) + nt.assert_equal(result, "hello") + nt.assert_equal(captured.stderr, "") + +def test_format_config(): + """config objects don't pretend to support fancy reprs with lazy attrs""" + f = HTMLFormatter() + cfg = Config() + with capture_output() as captured: + result = f(cfg) + nt.assert_is(result, None) + nt.assert_equal(captured.stderr, "") + + with capture_output() as captured: + result = f(Config) + nt.assert_is(result, None) + nt.assert_equal(captured.stderr, "")