##// END OF EJS Templates
Merge pull request #5229 from minrk/safe-get-ipdisplay...
Thomas Kluyver -
r15489:f286289c merge
parent child Browse files
Show More
@@ -22,6 +22,7 b' from __future__ import print_function'
22 22 import os
23 23 import struct
24 24
25 from IPython.core.formatters import _safe_get_formatter_method
25 26 from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode,
26 27 unicode_type)
27 28 from IPython.testing.skipdoctest import skip_doctest
@@ -116,7 +117,7 b' def display(*objs, **kwargs):'
116 117 for obj in objs:
117 118
118 119 # If _ipython_display_ is defined, use that to display this object.
119 display_method = getattr(obj, '_ipython_display_', None)
120 display_method = _safe_get_formatter_method(obj, '_ipython_display_')
120 121 if display_method is not None:
121 122 try:
122 123 display_method(**kwargs)
@@ -25,7 +25,7 b' from __future__ import print_function'
25 25
26 26 import sys
27 27
28
28 from IPython.core.formatters import _safe_get_formatter_method
29 29 from IPython.config.configurable import Configurable
30 30 from IPython.utils import io
31 31 from IPython.utils.py3compat import builtin_mod
@@ -242,7 +242,7 b' class DisplayHook(Configurable):'
242 242 self.check_for_underscore()
243 243 if result is not None and not self.quiet():
244 244 # If _ipython_display_ is defined, use that to display this object.
245 display_method = getattr(result, '_ipython_display_', None)
245 display_method = _safe_get_formatter_method(result, '_ipython_display_')
246 246 if display_method is not None:
247 247 try:
248 248 return display_method()
@@ -64,7 +64,9 b' def _valid_formatter(f):'
64 64 - unbound methods NO
65 65 - callable with zero args OK
66 66 """
67 if isinstance(f, type(str.find)):
67 if f is None:
68 return False
69 elif isinstance(f, type(str.find)):
68 70 # unbound methods on compiled classes have type method_descriptor
69 71 return False
70 72 elif isinstance(f, types.BuiltinFunctionType):
@@ -80,6 +82,14 b' def _valid_formatter(f):'
80 82 return True
81 83 return False
82 84
85 def _safe_get_formatter_method(obj, name):
86 """Safely get a formatter method"""
87 method = pretty._safe_getattr(obj, name, None)
88 # formatter methods must be bound
89 if _valid_formatter(method):
90 return method
91
92
83 93 class DisplayFormatter(Configurable):
84 94
85 95 # When set to true only the default plain text formatter will be used.
@@ -339,9 +349,8 b' class BaseFormatter(Configurable):'
339 349 else:
340 350 return printer(obj)
341 351 # Finally look for special method names
342 method = pretty._safe_getattr(obj, self.print_method, None)
343 # print_method must be a bound method:
344 if _valid_formatter(method):
352 method = _safe_get_formatter_method(obj, self.print_method)
353 if method is not None:
345 354 return method()
346 355 return None
347 356 else:
General Comments 0
You need to be logged in to leave comments. Login now