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