##// END OF EJS Templates
check return types of formatters...
MinRK -
Show More
@@ -33,6 +33,7 b' from IPython.external.decorator import decorator'
33 # Our own imports
33 # Our own imports
34 from IPython.config.configurable import Configurable
34 from IPython.config.configurable import Configurable
35 from IPython.lib import pretty
35 from IPython.lib import pretty
36 from IPython.utils import io
36 from IPython.utils.traitlets import (
37 from IPython.utils.traitlets import (
37 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
38 Bool, Dict, Integer, Unicode, CUnicode, ObjectName, List,
38 )
39 )
@@ -182,15 +183,21 b' class DisplayFormatter(Configurable):'
182 # Formatters for specific format types (text, html, svg, etc.)
183 # Formatters for specific format types (text, html, svg, etc.)
183 #-----------------------------------------------------------------------------
184 #-----------------------------------------------------------------------------
184
185
185
186 @decorator
186 @decorator
187 def warn_format_error(method, self, *args, **kwargs):
187 def warn_format_error(method, self, *args, **kwargs):
188 """decorator for warning on failed format call"""
188 """decorator for warning on failed format call"""
189 try:
189 try:
190 return method(self, *args, **kwargs)
190 r = method(self, *args, **kwargs)
191 except Exception as e:
191 except Exception as e:
192 warn("Exception in %s formatter: %s" % (self.format_type, e))
192 warn("Exception in %s formatter: %s" % (self.format_type, e))
193 return None
193 return None
194 if r is None or isinstance(r, self._return_type):
195 return r
196 else:
197 warn("%s formatter returned invalid type %s for object: %s" % (
198 self.format_type, type(r), pretty._safe_repr(obj)
199 ))
200
194
201
195
202
196 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
203 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
@@ -262,6 +269,7 b' class BaseFormatter(Configurable):'
262 """
269 """
263
270
264 format_type = Unicode('text/plain')
271 format_type = Unicode('text/plain')
272 _return_type = string_types
265
273
266 enabled = Bool(True, config=True)
274 enabled = Bool(True, config=True)
267
275
@@ -278,7 +286,7 b' class BaseFormatter(Configurable):'
278 # The deferred-import type-specific printers.
286 # The deferred-import type-specific printers.
279 # Map (modulename, classname) pairs to the format functions.
287 # Map (modulename, classname) pairs to the format functions.
280 deferred_printers = Dict(config=True)
288 deferred_printers = Dict(config=True)
281
289
282 @warn_format_error
290 @warn_format_error
283 def __call__(self, obj):
291 def __call__(self, obj):
284 """Compute the format for an object."""
292 """Compute the format for an object."""
@@ -679,6 +687,8 b' class PNGFormatter(BaseFormatter):'
679 format_type = Unicode('image/png')
687 format_type = Unicode('image/png')
680
688
681 print_method = ObjectName('_repr_png_')
689 print_method = ObjectName('_repr_png_')
690
691 _return_type = bytes
682
692
683
693
684 class JPEGFormatter(BaseFormatter):
694 class JPEGFormatter(BaseFormatter):
@@ -696,6 +706,8 b' class JPEGFormatter(BaseFormatter):'
696
706
697 print_method = ObjectName('_repr_jpeg_')
707 print_method = ObjectName('_repr_jpeg_')
698
708
709 _return_type = bytes
710
699
711
700 class LatexFormatter(BaseFormatter):
712 class LatexFormatter(BaseFormatter):
701 """A LaTeX formatter.
713 """A LaTeX formatter.
General Comments 0
You need to be logged in to leave comments. Login now