Show More
@@ -50,9 +50,6 b' def _valid_formatter(f):' | |||
|
50 | 50 | """ |
|
51 | 51 | if f is None: |
|
52 | 52 | return False |
|
53 | elif isinstance(f, type(str.find)): | |
|
54 | # unbound methods on compiled classes have type method_descriptor | |
|
55 | return False | |
|
56 | 53 | elif isinstance(f, types.BuiltinFunctionType): |
|
57 | 54 | # bound methods on compiled classes have type builtin_function |
|
58 | 55 | return True |
@@ -68,6 +65,9 b' def _valid_formatter(f):' | |||
|
68 | 65 | |
|
69 | 66 | def _safe_get_formatter_method(obj, name): |
|
70 | 67 | """Safely get a formatter method""" |
|
68 | if inspect.isclass(obj): | |
|
69 | # repr methods only make sense on instances, not classes | |
|
70 | return None | |
|
71 | 71 | method = pretty._safe_getattr(obj, name, None) |
|
72 | 72 | # formatter methods must be bound |
|
73 | 73 | if _valid_formatter(method): |
@@ -314,7 +314,6 b' def test_print_method_bound():' | |||
|
314 | 314 | class MyHTML(object): |
|
315 | 315 | def _repr_html_(self): |
|
316 | 316 | return "hello" |
|
317 | ||
|
318 | 317 | with capture_output() as captured: |
|
319 | 318 | result = f(MyHTML) |
|
320 | 319 | nt.assert_is(result, None) |
@@ -325,6 +324,45 b' def test_print_method_bound():' | |||
|
325 | 324 | nt.assert_equal(result, "hello") |
|
326 | 325 | nt.assert_equal(captured.stderr, "") |
|
327 | 326 | |
|
327 | def test_print_method_weird(): | |
|
328 | ||
|
329 | class TextMagicHat(object): | |
|
330 | def __getattr__(self, key): | |
|
331 | return key | |
|
332 | ||
|
333 | f = HTMLFormatter() | |
|
334 | ||
|
335 | text_hat = TextMagicHat() | |
|
336 | nt.assert_equal(text_hat._repr_html_, '_repr_html_') | |
|
337 | with capture_output() as captured: | |
|
338 | result = f(text_hat) | |
|
339 | ||
|
340 | nt.assert_is(result, None) | |
|
341 | nt.assert_not_in("FormatterWarning", captured.stderr) | |
|
342 | ||
|
343 | class CallableMagicHat(object): | |
|
344 | def __getattr__(self, key): | |
|
345 | return lambda : key | |
|
346 | ||
|
347 | call_hat = CallableMagicHat() | |
|
348 | with capture_output() as captured: | |
|
349 | result = f(call_hat) | |
|
350 | ||
|
351 | nt.assert_equal(result, '_repr_html_') | |
|
352 | nt.assert_not_in("FormatterWarning", captured.stderr) | |
|
353 | ||
|
354 | class BadReprArgs(object): | |
|
355 | def _repr_html_(self, extra, args): | |
|
356 | return "html" | |
|
357 | ||
|
358 | bad = BadReprArgs() | |
|
359 | with capture_output() as captured: | |
|
360 | result = f(bad) | |
|
361 | ||
|
362 | nt.assert_is(result, None) | |
|
363 | nt.assert_not_in("FormatterWarning", captured.stderr) | |
|
364 | ||
|
365 | ||
|
328 | 366 | def test_format_config(): |
|
329 | 367 | """config objects don't pretend to support fancy reprs with lazy attrs""" |
|
330 | 368 | f = HTMLFormatter() |
General Comments 0
You need to be logged in to leave comments.
Login now