##// 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 import os
22 import os
23 import struct
23 import struct
24
24
25 from IPython.core.formatters import _safe_get_formatter_method
25 from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode,
26 from IPython.utils.py3compat import (string_types, cast_bytes_py2, cast_unicode,
26 unicode_type)
27 unicode_type)
27 from IPython.testing.skipdoctest import skip_doctest
28 from IPython.testing.skipdoctest import skip_doctest
@@ -116,7 +117,7 b' def display(*objs, **kwargs):'
116 for obj in objs:
117 for obj in objs:
117
118
118 # If _ipython_display_ is defined, use that to display this object.
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 if display_method is not None:
121 if display_method is not None:
121 try:
122 try:
122 display_method(**kwargs)
123 display_method(**kwargs)
@@ -25,7 +25,7 b' from __future__ import print_function'
25
25
26 import sys
26 import sys
27
27
28
28 from IPython.core.formatters import _safe_get_formatter_method
29 from IPython.config.configurable import Configurable
29 from IPython.config.configurable import Configurable
30 from IPython.utils import io
30 from IPython.utils import io
31 from IPython.utils.py3compat import builtin_mod
31 from IPython.utils.py3compat import builtin_mod
@@ -242,7 +242,7 b' class DisplayHook(Configurable):'
242 self.check_for_underscore()
242 self.check_for_underscore()
243 if result is not None and not self.quiet():
243 if result is not None and not self.quiet():
244 # If _ipython_display_ is defined, use that to display this object.
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 if display_method is not None:
246 if display_method is not None:
247 try:
247 try:
248 return display_method()
248 return display_method()
@@ -64,7 +64,9 b' def _valid_formatter(f):'
64 - unbound methods NO
64 - unbound methods NO
65 - callable with zero args OK
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 # unbound methods on compiled classes have type method_descriptor
70 # unbound methods on compiled classes have type method_descriptor
69 return False
71 return False
70 elif isinstance(f, types.BuiltinFunctionType):
72 elif isinstance(f, types.BuiltinFunctionType):
@@ -80,6 +82,14 b' def _valid_formatter(f):'
80 return True
82 return True
81 return False
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 class DisplayFormatter(Configurable):
93 class DisplayFormatter(Configurable):
84
94
85 # When set to true only the default plain text formatter will be used.
95 # When set to true only the default plain text formatter will be used.
@@ -339,9 +349,8 b' class BaseFormatter(Configurable):'
339 else:
349 else:
340 return printer(obj)
350 return printer(obj)
341 # Finally look for special method names
351 # Finally look for special method names
342 method = pretty._safe_getattr(obj, self.print_method, None)
352 method = _safe_get_formatter_method(obj, self.print_method)
343 # print_method must be a bound method:
353 if method is not None:
344 if _valid_formatter(method):
345 return method()
354 return method()
346 return None
355 return None
347 else:
356 else:
General Comments 0
You need to be logged in to leave comments. Login now