MCPcopy Create free account
hub / github.com/ipython/ipython / get_real_method

Function get_real_method

IPython/utils/dir2.py:54–84  ·  view source on GitHub ↗

Like getattr, but with a few extra sanity checks: - If obj is a class, ignore everything except class methods - Check if obj is a proxy that claims to have all attributes - Catch attribute access failing with any exception - Check that the attribute is a callable object Returns

(obj, name)

Source from the content-addressed store, hash-verified

52
53
54def get_real_method(obj, name):
55 """Like getattr, but with a few extra sanity checks:
56
57 - If obj is a class, ignore everything except class methods
58 - Check if obj is a proxy that claims to have all attributes
59 - Catch attribute access failing with any exception
60 - Check that the attribute is a callable object
61
62 Returns the method or None.
63 """
64 try:
65 canary = getattr(obj, '_ipython_canary_method_should_not_exist_', None)
66 except Exception:
67 return None
68
69 if canary is not None:
70 # It claimed to have an attribute it should never have
71 return None
72
73 try:
74 m = getattr(obj, name, None)
75 except Exception:
76 return None
77
78 if inspect.isclass(obj) and not isinstance(m, types.MethodType):
79 return None
80
81 if callable(m):
82 return m
83
84 return None

Callers 4

get_keysMethod · 0.90
__call__Method · 0.85
__call__Method · 0.85
__call__Method · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected