##// END OF EJS Templates
Merge pull request #4832 from minrk/formatter-not-implemented...
Thomas Kluyver -
r14737:b823a5a0 merge
parent child Browse files
Show More
@@ -183,22 +183,31 b' class DisplayFormatter(Configurable):'
183 # Formatters for specific format types (text, html, svg, etc.)
183 # Formatters for specific format types (text, html, svg, etc.)
184 #-----------------------------------------------------------------------------
184 #-----------------------------------------------------------------------------
185
185
186 class FormatterWarning(UserWarning):
187 """Warning class for errors in formatters"""
188
186 @decorator
189 @decorator
187 def warn_format_error(method, self, *args, **kwargs):
190 def warn_format_error(method, self, *args, **kwargs):
188 """decorator for warning on failed format call"""
191 """decorator for warning on failed format call"""
189 try:
192 try:
190 r = method(self, *args, **kwargs)
193 r = method(self, *args, **kwargs)
194 except NotImplementedError as e:
195 # don't warn on NotImplementedErrors
196 return None
191 except Exception as e:
197 except Exception as e:
192 warn("Exception in %s formatter: %s" % (self.format_type, e))
198 warnings.warn("Exception in %s formatter: %s" % (self.format_type, e),
199 FormatterWarning,
200 )
193 return None
201 return None
194 if r is None or isinstance(r, self._return_type) or \
202 if r is None or isinstance(r, self._return_type) or \
195 (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
203 (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
196 return r
204 return r
197 else:
205 else:
198 warn("%s formatter returned invalid type %s (expected %s) for object: %s" % (
206 warnings.warn(
199 self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])
207 "%s formatter returned invalid type %s (expected %s) for object: %s" % \
200 ))
208 (self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])),
201
209 FormatterWarning
210 )
202
211
203
212
204 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
213 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
@@ -240,17 +240,29 b' def test_warn_error_method():'
240 with capture_output() as captured:
240 with capture_output() as captured:
241 result = f(bad)
241 result = f(bad)
242 nt.assert_is(result, None)
242 nt.assert_is(result, None)
243 nt.assert_in("WARNING", captured.stderr)
243 nt.assert_in("FormatterWarning", captured.stderr)
244 nt.assert_in("text/html", captured.stderr)
244 nt.assert_in("text/html", captured.stderr)
245 nt.assert_in("zero", captured.stderr)
245 nt.assert_in("zero", captured.stderr)
246
246
247 def test_nowarn_notimplemented():
248 f = HTMLFormatter()
249 class HTMLNotImplemented(object):
250 def _repr_html_(self):
251 raise NotImplementedError
252 return 1/0
253 h = HTMLNotImplemented()
254 with capture_output() as captured:
255 result = f(h)
256 nt.assert_is(result, None)
257 nt.assert_not_in("FormatterWarning", captured.stderr)
258
247 def test_warn_error_for_type():
259 def test_warn_error_for_type():
248 f = HTMLFormatter()
260 f = HTMLFormatter()
249 f.for_type(int, lambda i: name_error)
261 f.for_type(int, lambda i: name_error)
250 with capture_output() as captured:
262 with capture_output() as captured:
251 result = f(5)
263 result = f(5)
252 nt.assert_is(result, None)
264 nt.assert_is(result, None)
253 nt.assert_in("WARNING", captured.stderr)
265 nt.assert_in("FormatterWarning", captured.stderr)
254 nt.assert_in("text/html", captured.stderr)
266 nt.assert_in("text/html", captured.stderr)
255 nt.assert_in("name_error", captured.stderr)
267 nt.assert_in("name_error", captured.stderr)
256
268
@@ -263,7 +275,7 b' def test_warn_error_pretty_method():'
263 with capture_output() as captured:
275 with capture_output() as captured:
264 result = f(bad)
276 result = f(bad)
265 nt.assert_is(result, None)
277 nt.assert_is(result, None)
266 nt.assert_in("WARNING", captured.stderr)
278 nt.assert_in("FormatterWarning", captured.stderr)
267 nt.assert_in("text/plain", captured.stderr)
279 nt.assert_in("text/plain", captured.stderr)
268 nt.assert_in("argument", captured.stderr)
280 nt.assert_in("argument", captured.stderr)
269
281
General Comments 0
You need to be logged in to leave comments. Login now