Show More
@@ -39,38 +39,21 b' else:' | |||
|
39 | 39 | #----------------------------------------------------------------------------- |
|
40 | 40 | |
|
41 | 41 | |
|
42 | def _valid_formatter(f): | |
|
43 | """Return whether an object is a valid formatter | |
|
44 | ||
|
45 | Cases checked: | |
|
42 | def _safe_get_formatter_method(obj, name): | |
|
43 | """Safely get a formatter method | |
|
46 | 44 | |
|
47 | - bound methods OK | |
|
48 | - unbound methods NO | |
|
49 | - callable with zero args OK | |
|
45 | - Classes cannot have formatter methods, only instance | |
|
46 | - protect against proxy objects that claim to have everything | |
|
50 | 47 | """ |
|
51 | if f is None: | |
|
52 | return False | |
|
53 | elif isinstance(f, types.BuiltinFunctionType): | |
|
54 | # bound methods on compiled classes have type builtin_function | |
|
55 | return True | |
|
56 | elif callable(f): | |
|
57 | # anything that works with zero args should be okay | |
|
58 | try: | |
|
59 | inspect.getcallargs(f) | |
|
60 | except Exception: | |
|
61 | return False | |
|
62 | else: | |
|
63 | return True | |
|
64 | return False | |
|
65 | ||
|
66 | def _safe_get_formatter_method(obj, name): | |
|
67 | """Safely get a formatter method""" | |
|
68 | 48 | if inspect.isclass(obj): |
|
69 | 49 | # repr methods only make sense on instances, not classes |
|
70 | 50 | return None |
|
71 | 51 | method = pretty._safe_getattr(obj, name, None) |
|
72 | # formatter methods must be bound | |
|
73 | if _valid_formatter(method): | |
|
52 | if callable(method): | |
|
53 | # obj claims to have repr method... | |
|
54 | if callable(pretty._safe_getattr(obj, '_ipython_canary_method_should_not_exist_', None)): | |
|
55 | # ...but don't trust proxy objects that claim to have everything | |
|
56 | return None | |
|
74 | 57 | return method |
|
75 | 58 | |
|
76 | 59 |
@@ -348,8 +348,7 b' def test_print_method_weird():' | |||
|
348 | 348 | with capture_output() as captured: |
|
349 | 349 | result = f(call_hat) |
|
350 | 350 | |
|
351 |
nt.assert_equal(result, |
|
|
352 | nt.assert_not_in("FormatterWarning", captured.stderr) | |
|
351 | nt.assert_equal(result, None) | |
|
353 | 352 | |
|
354 | 353 | class BadReprArgs(object): |
|
355 | 354 | def _repr_html_(self, extra, args): |
General Comments 0
You need to be logged in to leave comments.
Login now