Show More
@@ -183,6 +183,9 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""" | |
@@ -192,16 +195,19 b' def warn_format_error(method, self, *args, **kwargs):' | |||||
192 | # don't warn on NotImplementedErrors |
|
195 | # don't warn on NotImplementedErrors | |
193 | return None |
|
196 | return None | |
194 | except Exception as e: |
|
197 | except Exception as e: | |
195 |
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 | ) | |||
196 | return None |
|
201 | return None | |
197 | if r is None or isinstance(r, self._return_type) or \ |
|
202 | if r is None or isinstance(r, self._return_type) or \ | |
198 | (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)): | |
199 | return r |
|
204 | return r | |
200 | else: |
|
205 | else: | |
201 | warn("%s formatter returned invalid type %s (expected %s) for object: %s" % ( |
|
206 | warnings.warn( | |
202 | 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" % \ | |
203 | )) |
|
208 | (self.format_type, type(r), self._return_type, pretty._safe_repr(args[0])), | |
204 |
|
209 | FormatterWarning | ||
|
210 | ) | |||
205 |
|
211 | |||
206 |
|
212 | |||
207 | class FormatterABC(with_metaclass(abc.ABCMeta, object)): |
|
213 | class FormatterABC(with_metaclass(abc.ABCMeta, object)): |
@@ -240,7 +240,7 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(" |
|
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 | |||
@@ -254,7 +254,7 b' def test_nowarn_notimplemented():' | |||||
254 | with capture_output() as captured: |
|
254 | with capture_output() as captured: | |
255 | result = f(h) |
|
255 | result = f(h) | |
256 | nt.assert_is(result, None) |
|
256 | nt.assert_is(result, None) | |
257 |
nt.assert_not_in(" |
|
257 | nt.assert_not_in("FormatterWarning", captured.stderr) | |
258 |
|
258 | |||
259 | def test_warn_error_for_type(): |
|
259 | def test_warn_error_for_type(): | |
260 | f = HTMLFormatter() |
|
260 | f = HTMLFormatter() | |
@@ -262,7 +262,7 b' def test_warn_error_for_type():' | |||||
262 | with capture_output() as captured: |
|
262 | with capture_output() as captured: | |
263 | result = f(5) |
|
263 | result = f(5) | |
264 | nt.assert_is(result, None) |
|
264 | nt.assert_is(result, None) | |
265 |
nt.assert_in(" |
|
265 | nt.assert_in("FormatterWarning", captured.stderr) | |
266 | nt.assert_in("text/html", captured.stderr) |
|
266 | nt.assert_in("text/html", captured.stderr) | |
267 | nt.assert_in("name_error", captured.stderr) |
|
267 | nt.assert_in("name_error", captured.stderr) | |
268 |
|
268 | |||
@@ -275,7 +275,7 b' def test_warn_error_pretty_method():' | |||||
275 | with capture_output() as captured: |
|
275 | with capture_output() as captured: | |
276 | result = f(bad) |
|
276 | result = f(bad) | |
277 | nt.assert_is(result, None) |
|
277 | nt.assert_is(result, None) | |
278 |
nt.assert_in(" |
|
278 | nt.assert_in("FormatterWarning", captured.stderr) | |
279 | nt.assert_in("text/plain", captured.stderr) |
|
279 | nt.assert_in("text/plain", captured.stderr) | |
280 | nt.assert_in("argument", captured.stderr) |
|
280 | nt.assert_in("argument", captured.stderr) | |
281 |
|
281 |
General Comments 0
You need to be logged in to leave comments.
Login now