Show More
@@ -50,9 +50,6 b' def _valid_formatter(f):' | |||||
50 | """ |
|
50 | """ | |
51 | if f is None: |
|
51 | if f is None: | |
52 | return False |
|
52 | return False | |
53 | elif isinstance(f, type(str.find)): |
|
|||
54 | # unbound methods on compiled classes have type method_descriptor |
|
|||
55 | return False |
|
|||
56 | elif isinstance(f, types.BuiltinFunctionType): |
|
53 | elif isinstance(f, types.BuiltinFunctionType): | |
57 | # bound methods on compiled classes have type builtin_function |
|
54 | # bound methods on compiled classes have type builtin_function | |
58 | return True |
|
55 | return True | |
@@ -68,6 +65,9 b' def _valid_formatter(f):' | |||||
68 |
|
65 | |||
69 | def _safe_get_formatter_method(obj, name): |
|
66 | def _safe_get_formatter_method(obj, name): | |
70 | """Safely get a formatter method""" |
|
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 | method = pretty._safe_getattr(obj, name, None) |
|
71 | method = pretty._safe_getattr(obj, name, None) | |
72 | # formatter methods must be bound |
|
72 | # formatter methods must be bound | |
73 | if _valid_formatter(method): |
|
73 | if _valid_formatter(method): |
@@ -314,7 +314,6 b' def test_print_method_bound():' | |||||
314 | class MyHTML(object): |
|
314 | class MyHTML(object): | |
315 | def _repr_html_(self): |
|
315 | def _repr_html_(self): | |
316 | return "hello" |
|
316 | return "hello" | |
317 |
|
||||
318 | with capture_output() as captured: |
|
317 | with capture_output() as captured: | |
319 | result = f(MyHTML) |
|
318 | result = f(MyHTML) | |
320 | nt.assert_is(result, None) |
|
319 | nt.assert_is(result, None) | |
@@ -325,6 +324,45 b' def test_print_method_bound():' | |||||
325 | nt.assert_equal(result, "hello") |
|
324 | nt.assert_equal(result, "hello") | |
326 | nt.assert_equal(captured.stderr, "") |
|
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 | def test_format_config(): |
|
366 | def test_format_config(): | |
329 | """config objects don't pretend to support fancy reprs with lazy attrs""" |
|
367 | """config objects don't pretend to support fancy reprs with lazy attrs""" | |
330 | f = HTMLFormatter() |
|
368 | f = HTMLFormatter() |
General Comments 0
You need to be logged in to leave comments.
Login now