##// 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 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 189 @decorator
187 190 def warn_format_error(method, self, *args, **kwargs):
188 191 """decorator for warning on failed format call"""
189 192 try:
190 193 r = method(self, *args, **kwargs)
194 except NotImplementedError as e:
195 # don't warn on NotImplementedErrors
196 return None
191 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 201 return None
194 202 if r is None or isinstance(r, self._return_type) or \
195 203 (isinstance(r, tuple) and r and isinstance(r[0], self._return_type)):
196 204 return r
197 205 else:
198 warn("%s formatter returned invalid type %s (expected %s) for object: %s" % (
199 self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])
200 ))
201
206 warnings.warn(
207 "%s formatter returned invalid type %s (expected %s) for object: %s" % \
208 (self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])),
209 FormatterWarning
210 )
202 211
203 212
204 213 class FormatterABC(with_metaclass(abc.ABCMeta, object)):
@@ -240,17 +240,29 b' def test_warn_error_method():'
240 240 with capture_output() as captured:
241 241 result = f(bad)
242 242 nt.assert_is(result, None)
243 nt.assert_in("WARNING", captured.stderr)
243 nt.assert_in("FormatterWarning", captured.stderr)
244 244 nt.assert_in("text/html", captured.stderr)
245 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 259 def test_warn_error_for_type():
248 260 f = HTMLFormatter()
249 261 f.for_type(int, lambda i: name_error)
250 262 with capture_output() as captured:
251 263 result = f(5)
252 264 nt.assert_is(result, None)
253 nt.assert_in("WARNING", captured.stderr)
265 nt.assert_in("FormatterWarning", captured.stderr)
254 266 nt.assert_in("text/html", captured.stderr)
255 267 nt.assert_in("name_error", captured.stderr)
256 268
@@ -263,7 +275,7 b' def test_warn_error_pretty_method():'
263 275 with capture_output() as captured:
264 276 result = f(bad)
265 277 nt.assert_is(result, None)
266 nt.assert_in("WARNING", captured.stderr)
278 nt.assert_in("FormatterWarning", captured.stderr)
267 279 nt.assert_in("text/plain", captured.stderr)
268 280 nt.assert_in("argument", captured.stderr)
269 281
General Comments 0
You need to be logged in to leave comments. Login now