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