def _test(self, outer, inner, args, kwargs, expected, expected_get, exp_src, exp_src_get): outer_f = support.f(outer, name='outer') inner_f = support.f(inner, name='inner') forw = specifiers.forwards_to_function(inner_f, *args, **kwargs)(outer_f) sig = specifiers.signature(forw) self.assertSigsEqual(sig, support.s(expected)) self.assertSourcesEqual( sig.sources, { 'outer': exp_src[0], 'inner': exp_src[1], '+depths': ['outer', 'inner'] }) sig_get = specifiers.signature(_util.safe_get(forw, object(), object)) self.assertSigsEqual(sig_get, support.s(expected_get)) self.assertSourcesEqual( sig_get.sources, { 'outer': exp_src_get[0], 'inner': exp_src_get[1], '+depths': ['outer', 'inner'] })
def test_call(self): outer = support.f('*args, **kwargs') inner = support.f('a, *, b') forw = specifiers.forwards_to_function(inner)(outer) instance = object() forw_get_prox = _util.safe_get(forw, instance, object) self.assertEqual( forw_get_prox(1, b=2), {'args': (instance, 1), 'kwargs': {'b': 2}})
def forwards_tests(self, outer, inner, args, kwargs, expected, expected_get): outer_f = support.f(outer) inner_f = support.f(inner) forw = specifiers.forwards_to_function(inner_f, *args, **kwargs)(outer_f) self.assertSigsEqual(specifiers.signature(forw), support.s(expected)) self.assertSigsEqual( specifiers.signature(_util.safe_get(forw, object(), object)), support.s(expected_get))
def test_specifiers_sig_after(self): inner = f('a, b', name='inner') outer = f('x, y, z, *args, **kwargs', name='outer') pt = modifiers.kwoargs('z')(outer) pt = specifiers.forwards_to_function(inner)(pt) sig = specifiers.signature(pt) self.assertSigsEqual(sig, s('x, y, a, b, *, z')) self.assertSourcesEqual( sig.sources, {'inner': 'ab', 'outer': 'xyz', '+depths': ['outer', 'inner']}) self.assertEqual(sig.sources['x'], [pt])
def test_call(self): outer = support.f('*args, **kwargs') inner = support.f('a, *, b') forw = specifiers.forwards_to_function(inner)(outer) instance = object() forw_get_prox = _util.safe_get(forw, instance, object) self.assertEqual(forw_get_prox(1, b=2), { 'args': (instance, 1), 'kwargs': { 'b': 2 } })
def test_specifiers_sig_after(self): inner = f('a, b', name='inner') outer = f('x, y, z, *args, **kwargs', name='outer') pt = modifiers.kwoargs('z')(outer) pt = specifiers.forwards_to_function(inner)(pt) sig = specifiers.signature(pt) self.assertSigsEqual(sig, s('x, y, a, b, *, z')) self.assertSourcesEqual(sig.sources, { 'inner': 'ab', 'outer': 'xyz', '+depths': ['outer', 'inner'] }) self.assertEqual(sig.sources['x'], [pt])
def test_emulation(self): func = specifiers.forwards_to_method('abc', emulate=False)(_func) self.assertTrue(_func is func) func = specifiers.forwards_to_method('abc')(_func) self.assertTrue(_func is func) class Cls(object): func = specifiers.forwards_to_method('abc')(_func) func = getattr(Cls.func, '__func__', func) self.assertTrue(_func is func) self.assertTrue(_func is Cls().func.__func__) class Cls(object): def func(self, abc, *args, **kwargs): raise NotImplementedError def abc(self, x): raise NotImplementedError method = Cls().func func = specifiers.forwards_to_method('abc')(method) self.assertTrue(isinstance(func, specifiers._ForgerWrapper)) self.assertEqual(func.__wrapped__, method) self.assertRaises(AttributeError, specifiers.forwards_to_method('abc', emulate=False), Cls().func) exp = support.s('abc, x') self.assertSigsEqual(signatures.signature(func), exp) self.assertSigsEqual(specifiers.signature(func), exp) class Emulator(object): def __init__(self, obj, forger): self.obj = obj self.forger = forger func = specifiers.forwards_to_function(func, emulate=Emulator)(_func) self.assertTrue(isinstance(func, Emulator)) @specifiers.forwards_to_function(_func, emulate=True) def func(x, y, *args, **kwargs): return x + y self.assertEqual(5, func(2, 3))
def _test(self, outer, inner, args, kwargs, expected, expected_get, exp_src, exp_src_get): outer_f = support.f(outer, name='outer') inner_f = support.f(inner, name='inner') forw = specifiers.forwards_to_function(inner_f, *args, **kwargs)(outer_f) sig = specifiers.signature(forw) self.assertSigsEqual(sig, support.s(expected)) self.assertSourcesEqual(sig.sources, { 'outer': exp_src[0], 'inner': exp_src[1], '+depths': ['outer', 'inner']}) sig_get = specifiers.signature(_util.safe_get(forw, object(), object)) self.assertSigsEqual(sig_get, support.s(expected_get)) self.assertSourcesEqual(sig_get.sources, { 'outer': exp_src_get[0], 'inner': exp_src_get[1], '+depths': ['outer', 'inner']})
def test_emulation(self): func = specifiers.forwards_to_method('abc', emulate=False)(_func) self.assertTrue(_func is func) func = specifiers.forwards_to_method('abc')(_func) self.assertTrue(_func is func) class Cls(object): func = specifiers.forwards_to_method('abc')(_func) func = getattr(Cls.func, '__func__', func) self.assertTrue(_func is func) self.assertTrue(_func is Cls().func.__func__) class Cls(object): def func(self, abc, *args, **kwargs): raise NotImplementedError def abc(self, x): raise NotImplementedError method = Cls().func func = specifiers.forwards_to_method('abc')(method) self.assertTrue(isinstance(func, specifiers._ForgerWrapper)) self.assertEqual(func.__wrapped__, method) self.assertRaises( AttributeError, specifiers.forwards_to_method('abc', emulate=False), Cls().func) exp = support.s('abc, x') self.assertSigsEqual(signatures.signature(func), exp) self.assertSigsEqual(specifiers.signature(func), exp) class Emulator(object): def __init__(self, obj, forger): self.obj = obj self.forger = forger func = specifiers.forwards_to_function(func, emulate=Emulator)(_func) self.assertTrue(isinstance(func, Emulator)) @specifiers.forwards_to_function(_func, emulate=True) def func(x, y, *args, **kwargs): return x + y self.assertEqual(5, func(2, 3))
def __init__(self): specifiers.forwards_to_function(function)(self)
def __init__(self): specifiers.forwards_to_function(function)(self)