def try_call(obj, args=(), kwds={}): # The only entry point for function calls. callable_ = inspect_callable(obj) if callable_.self_obj is not None: args = (callable_.self_obj,) + args obj = callable_.func_obj if not is_pure(obj): return False, None try: sig = get_signature(obj) except ValueError: return False, None try: sig.bind(*args, **kwds) except TypeError: # binding failed return False, None try: value = obj(*args, **kwds) except Exception: return False, None return True, value
def test_builtin_bound_method(): assert inspect_callable("a".__getitem__) == Callable(str.__getitem__, self_obj="a")
def test_class_method_in_derived(cls): d = cls['derived']() classmethod_func = cls['base'].classmethod.__func__ assert inspect_callable(d.classmethod) == Callable(classmethod_func, self_obj=cls['derived'])
def test_builtin_unbound_method(): assert inspect_callable(str.__getitem__) == Callable(str.__getitem__)
def test_static_method(cls): d = cls['base']() assert inspect_callable(d.staticmethod) == Callable(cls['base'].staticmethod)
def test_bound_method_in_derived(cls): d = cls['derived']() ref_func = get_method_function(cls['base'].method) assert inspect_callable(d.method) == Callable(ref_func, self_obj=d)
def test_function(): assert inspect_callable(dummy) == Callable(dummy)
def test_builtin_function(): assert inspect_callable(isinstance) == Callable(isinstance)
def test_builtin_method_in_derived(): s1 = mystr1("a") ref_func = get_builtin_method_function(str.__getitem__) assert (inspect_callable(s1.__getitem__) == Callable(ref_func, self_obj=s1))
def test_builtin_method_overloaded_in_derived(): s2 = mystr2("a") ref_func = get_method_function(mystr2.__getitem__) assert (inspect_callable(s2.__getitem__) == Callable(ref_func, self_obj=s2))
def test_builtin_bound_method(): ref_func = get_builtin_method_function(str.__getitem__) assert inspect_callable("a".__getitem__) == Callable(ref_func, self_obj="a")
def test_builtin_unbound_method(): ref_func = get_builtin_method_function(str.__getitem__) assert inspect_callable(str.__getitem__) == Callable(ref_func)
def test_call_method(cls): d = cls['base']() ref_func = get_method_function(cls['base'].__call__) assert inspect_callable(d) == Callable(ref_func, self_obj=d)
def test_builtin_method_in_derived(): s1 = mystr1("a") assert (inspect_callable(s1.__getitem__) == Callable(str.__getitem__, self_obj=s1))
def test_unbound_method(cls): assert inspect_callable(cls['base'].method) == Callable(cls['base'].method)
def test_builtin_method_overloaded_in_derived(): s2 = mystr2("a") assert (inspect_callable(s2.__getitem__) == Callable(mystr2.__getitem__, self_obj=s2))
def test_bound_method_in_derived(cls): d = cls['derived']() assert inspect_callable(d.method) == Callable(cls['base'].method, self_obj=d)
def test_builtin_constructor(): assert inspect_callable(str) == Callable(str, init=True)
def test_call_method(cls): d = cls['base']() assert inspect_callable(d) == Callable(cls['base'].__call__, self_obj=d)
def test_constructor(cls): assert inspect_callable(cls['base']) == Callable(cls['base'], init=True)
def test_unbound_method(cls): ref_func = get_method_function(cls['base'].method) assert inspect_callable(cls['base'].method) == Callable(ref_func)