def test_plain(self): space = self.space def g(space, w_a, w_x): return space.newtuple([space.wrap('g'), w_a, w_x]) w_g = space.wrap( gateway.interp2app_temp( g, unwrap_spec=[gateway.ObjSpace, gateway.W_Root, gateway.W_Root])) args = argument.Arguments(space, [space.wrap(-1), space.wrap(0)]) w_res = space.call_args(w_g, args) assert space.is_true(space.eq(w_res, space.wrap(('g', -1, 0)))) w_self = space.wrap('self') args0 = argument.Arguments(space, [space.wrap(0)]) args = args0.prepend(w_self) w_res = space.call_args(w_g, args) assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 0)))) args3 = argument.Arguments(space, [space.wrap(3)]) w_res = space.call_obj_args(w_g, w_self, args3) assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 3))))
def test_unwrap_spec_default_applevel(self): space = self.space @gateway.unwrap_spec(w_x = WrappedDefault(42)) def g(space, w_x): return w_x w_g = space.wrap(gateway.interp2app_temp(g)) args = argument.Arguments(space, []) w_res = space.call_args(w_g, args) assert space.eq_w(w_res, space.wrap(42)) # args = argument.Arguments(space, [space.wrap(84)]) w_res = space.call_args(w_g, args) assert space.eq_w(w_res, space.wrap(84))
def test_unwrap_spec_default_applevel_2(self): space = self.space @gateway.unwrap_spec(w_x = (WrappedDefault(42)), y=int) def g(space, w_x, y=10): return space.add(w_x, space.wrap(y)) w_g = space.wrap(gateway.interp2app_temp(g)) args = argument.Arguments(space, []) w_res = space.call_args(w_g, args) assert space.eq_w(w_res, space.wrap(52)) # args = argument.Arguments(space, [space.wrap(84)]) w_res = space.call_args(w_g, args) assert space.eq_w(w_res, space.wrap(94)) # args = argument.Arguments(space, [space.wrap(84), space.wrap(-1)]) w_res = space.call_args(w_g, args) assert space.eq_w(w_res, space.wrap(83))
def test_call_index(self): def c(space, index): assert type(index) is int code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace, "index"]) w = self.space.wrap args = argument.Arguments(self.space, [w(123)]) code.funcrun(FakeFunc(self.space, "c"), args)
def test_base_regular_descr_mismatch(self): space = self.space def f(): raise gateway.DescrMismatch w_f = space.wrap(gateway.interp2app_temp(f, unwrap_spec=[])) args = argument.Arguments(space, []) space.raises_w(space.w_SystemError, space.call_args, w_f, args)
def check(*args, **kwds): a = argument.Arguments(space, [], w_stararg=w(args), w_starstararg=w(kwds)) w_res = space.call_args(w_g, a) assert space.eq_w(w_res, w(expected(*args, **kwds))) del kwds['y1'] a = argument.Arguments(space, [], w_stararg=w(args), w_starstararg=w(kwds)) py.test.raises(gateway.OperationError, space.call_args, w_g, a) args += (1234, ) a = argument.Arguments(space, [], w_stararg=w(args), w_starstararg=w(kwds)) py.test.raises(gateway.OperationError, space.call_args, w_g, a)
def test_unwrap_spec_decorator(self): space = self.space @gateway.unwrap_spec(gateway.ObjSpace, gateway.W_Root, int) def g(space, w_thing, i): return space.newtuple([w_thing, space.wrap(i)]) w_g = space.wrap(gateway.interp2app_temp(g)) args = argument.Arguments(space, [space.wrap(-1), space.wrap(0)]) w_res = space.call_args(w_g, args) assert space.eq_w(w_res, space.wrap((-1, 0)))
def test_pass_trough_arguments0_descr_mismatch(self): space = self.space def f(space, __args__): raise gateway.DescrMismatch w_f = space.wrap( gateway.interp2app_temp( f, unwrap_spec=[gateway.ObjSpace, gateway.Arguments])) args = argument.Arguments(space, []) space.raises_w(space.w_SystemError, space.call_args, w_f, args)
def test_unwrap_spec_default_bytes(self): space = self.space @gateway.unwrap_spec(s='bufferstr') def g(space, s=''): return space.wrap(type(s) is str) w_g = space.wrap(gateway.interp2app_temp(g)) args = argument.Arguments(space, []) w_res = space.call_args(w_g, args) assert space.eq_w(w_res, space.w_True)
def test_unwrap_spec_kwonly(self): space = self.space def g(space, w_x, __kwonly__, w_y): return space.sub(w_x, w_y) w_g = space.wrap(gateway.interp2app_temp(g)) w = space.wrap w1 = w(1) for i in range(4): a = argument.Arguments(space, [w1, w1, w1]) py.test.raises(gateway.OperationError, space.call_args, w_g, a) py.test.raises(gateway.OperationError, space.call_function, w_g, *(i * (w1, ))) args = argument.Arguments(space, [w(1)], w_starstararg=w({'y': 10})) assert space.eq_w(space.call_args(w_g, args), w(-9)) args = argument.Arguments(space, [], w_starstararg=w({ 'x': 2, 'y': 10 })) assert space.eq_w(space.call_args(w_g, args), w(-8))
def test_call(self): def c(space, w_x, w_y, hello_w): u = space.unwrap w = space.wrap assert len(hello_w) == 2 assert u(hello_w[0]) == 0 assert u(hello_w[1]) == True return w((u(w_x) - u(w_y) + len(hello_w))) code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace, gateway.W_Root, gateway.W_Root, 'args_w']) w = self.space.wrap args = argument.Arguments(self.space, [w(123), w(23), w(0), w(True)]) w_result = code.funcrun(FakeFunc(self.space, "c"), args) assert self.space.eq_w(w_result, w(102))
def test_call_args(self): def c(space, w_x, w_y, __args__): args_w, kwds_w = __args__.unpack() u = space.unwrap w = space.wrap return w((u(w_x) - u(w_y) + len(args_w)) * u(kwds_w['boo'])) code = gateway.BuiltinCode(c, unwrap_spec=[gateway.ObjSpace, gateway.W_Root, gateway.W_Root, gateway.Arguments]) w = self.space.wrap args = argument.Arguments(self.space, [w(123), w(23)], [], [], w_stararg = w((0, True)), w_starstararg = w({'boo': 10})) w_result = code.funcrun(FakeFunc(self.space, "c"), args) assert self.space.eq_w(w_result, w(1020))
def test_pass_trough_arguments0(self): space = self.space called = [] def f(space, __args__): called.append(__args__) a_w, _ = __args__.unpack() return space.newtuple([space.wrap('f')] + a_w) w_f = space.wrap( gateway.interp2app_temp( f, unwrap_spec=[gateway.ObjSpace, gateway.Arguments])) args = argument.Arguments(space, [space.wrap(7)]) w_res = space.call_args(w_f, args) assert space.is_true(space.eq(w_res, space.wrap(('f', 7)))) # white-box check for opt assert called[0] is args
def test_pass_trough_arguments_method(self): space = self.space called = [] class W_Something(W_Root): def f(self, space, __args__): called.append(__args__) a_w, _ = __args__.unpack() return space.newtuple([space.wrap('f')] + a_w) w_f = space.wrap(gateway.interp2app_temp(W_Something.f)) w_self = space.wrap(W_Something()) args = argument.Arguments(space, [space.wrap(7)]) w_res = space.call_obj_args(w_f, w_self, args) assert space.is_true(space.eq(w_res, space.wrap(('f', 7)))) # white-box check for opt assert called[0] is args
def test_pass_trough_arguments1(self): space = self.space called = [] def g(space, w_self, __args__): called.append(__args__) a_w, _ = __args__.unpack() return space.newtuple([ space.wrap('g'), w_self, ] + a_w) w_g = space.wrap( gateway.interp2app_temp(g, unwrap_spec=[ gateway.ObjSpace, gateway.W_Root, gateway.Arguments ])) old_funcrun = w_g.code.funcrun def funcrun_witness(func, args): called.append('funcrun') return old_funcrun(func, args) w_g.code.funcrun = funcrun_witness w_self = space.wrap('self') args3 = argument.Arguments(space, [space.wrap(3)]) w_res = space.call_obj_args(w_g, w_self, args3) assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 3)))) # white-box check for opt assert len(called) == 1 assert called[0] is args3 called = [] args0 = argument.Arguments(space, [space.wrap(0)]) args = args0.prepend(w_self) w_res = space.call_args(w_g, args) assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 0)))) # no opt in this case assert len(called) == 2 assert called[0] == 'funcrun' called = [] # higher level interfaces w_res = space.call_function(w_g, w_self) assert space.is_true(space.eq(w_res, space.wrap(('g', 'self')))) assert len(called) == 1 assert isinstance(called[0], argument.Arguments) called = [] w_res = space.appexec([w_g], """(g): return g('self', 11) """) assert space.is_true(space.eq(w_res, space.wrap(('g', 'self', 11)))) assert len(called) == 1 assert isinstance(called[0], argument.Arguments) called = [] w_res = space.appexec([w_g], """(g): class A(object): m = g # not a builtin function, so works as method d = {'A': A} exec \"\"\" # own compiler a = A() y = a.m(33) \"\"\" in d return d['y'] == ('g', d['a'], 33) """) assert space.is_true(w_res) assert len(called) == 1 assert isinstance(called[0], argument.Arguments)
def descr_call_mismatch(self, space, name, reqcls, args): args_w, kwds_w = args.unpack() args_w = args_w[:] args_w[0] = space.wrap(name) args = argument.Arguments(space, args_w, kwds_w) return space.call_args(self.w_controller, args)
def get_and_call_function(space, w_descr, w_obj, *args_w): args = argument.Arguments(space, list(args_w)) w_impl = space.get(w_descr, w_obj) return space.call_args(w_impl, args)