##// END OF EJS Templates
remove _valid_formatter...
Min RK -
Show More
@@ -39,38 +39,21 b' else:'
39 #-----------------------------------------------------------------------------
39 #-----------------------------------------------------------------------------
40
40
41
41
42 def _valid_formatter(f):
42 def _safe_get_formatter_method(obj, name):
43 """Return whether an object is a valid formatter
43 """Safely get a formatter method
44
45 Cases checked:
46
44
47 - bound methods OK
45 - Classes cannot have formatter methods, only instance
48 - unbound methods NO
46 - protect against proxy objects that claim to have everything
49 - callable with zero args OK
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 if inspect.isclass(obj):
48 if inspect.isclass(obj):
69 # repr methods only make sense on instances, not classes
49 # repr methods only make sense on instances, not classes
70 return None
50 return None
71 method = pretty._safe_getattr(obj, name, None)
51 method = pretty._safe_getattr(obj, name, None)
72 # formatter methods must be bound
52 if callable(method):
73 if _valid_formatter(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 return method
57 return method
75
58
76
59
@@ -348,8 +348,7 b' def test_print_method_weird():'
348 with capture_output() as captured:
348 with capture_output() as captured:
349 result = f(call_hat)
349 result = f(call_hat)
350
350
351 nt.assert_equal(result, '_repr_html_')
351 nt.assert_equal(result, None)
352 nt.assert_not_in("FormatterWarning", captured.stderr)
353
352
354 class BadReprArgs(object):
353 class BadReprArgs(object):
355 def _repr_html_(self, extra, args):
354 def _repr_html_(self, extra, args):
General Comments 0
You need to be logged in to leave comments. Login now