def test_call_obj_args(self): from pypy.interpreter.argument import Arguments space = self.space w_f = space.appexec([], """(): def f(x, y): return (x, y) return f """) w_a = space.appexec([], """(): class A(object): def __call__(self, x): return x return A() """) w_9 = space.wrap(9) w_1 = space.wrap(1) w_res = space.call_obj_args(w_f, w_9, Arguments(space, [w_1])) w_x, w_y = space.fixedview(w_res, 2) assert w_x is w_9 assert w_y is w_1 w_res = space.call_obj_args(w_a, w_9, Arguments(space, [])) assert w_res is w_9
def build_args(self, op, args_s): space = RPythonCallsSpace() if op == "simple_call": return Arguments(space, list(args_s)) elif op == "call_args": return Arguments.fromshape(space, args_s[0].const, # shape list(args_s[1:]))
def test_unwrap_error(self): space = DummySpace() valuedummy = object() def utf8_w(w): if w is None: raise OperationError(TypeError, None) if w is valuedummy: raise OperationError(ValueError, None) return bytes(w, 'utf-8') space.utf8_w = utf8_w space.text_w = utf8_w with py.test.raises(OperationError) as excinfo: Arguments(space, [], ["a"], [1], w_starstararg={None: 1}, fnname_parens="f1()") assert excinfo.value.w_type is TypeError assert excinfo.value._w_value is None with py.test.raises(OperationError) as excinfo: Arguments(space, [], ["a"], [1], w_starstararg={valuedummy: 1}, fnname_parens="f2()") assert excinfo.value.w_type is ValueError assert excinfo.value._w_value is None
def test_args_parsing_into_scope(self): space = DummySpace() args = Arguments(space, []) calls = [] def _match_signature(w_firstarg, scope_w, signature, defaults_w=[], blindargs=0): calls.append((w_firstarg, scope_w, signature.argnames, signature.has_vararg(), signature.has_kwarg(), defaults_w, blindargs)) args._match_signature = _match_signature scope_w = [None, None] args.parse_into_scope(None, scope_w, "foo", Signature(["a", "b"], None, None)) assert len(calls) == 1 assert calls[0] == (None, scope_w, ["a", "b"], False, False, [], 0) assert calls[0][1] is scope_w calls = [] scope_w = [None, None, None, None] args.parse_into_scope(None, scope_w, "foo", Signature(["a", "b"], "args", "kw"), defaults_w=['x', 'y']) assert len(calls) == 1 assert calls[0] == (None, scope_w, ["a", "b"], True, True, ["x", "y"], 0) calls = [] scope_w = [None, None, None, None] args.parse_into_scope("obj", scope_w, "foo", Signature(["a", "b"], "args", "kw"), defaults_w=['x', 'y']) assert len(calls) == 1 assert calls[0] == ("obj", scope_w, ["a", "b"], True, True, ["x", "y"], 0) class FakeArgErr(ArgErr): def getmsg(self, fname): return "msg " + fname def _match_signature(*args): raise FakeArgErr() args._match_signature = _match_signature excinfo = py.test.raises(OperationError, args.parse_into_scope, "obj", [None, None], "foo", Signature(["a", "b"], None, None)) assert excinfo.value.w_type is TypeError assert excinfo.value._w_value == "msg foo"
def appcaller(space, *args_w): if not isinstance(space, ObjSpace): raise TypeError("first argument must be a space instance.") # redirect if the space handles this specially # XXX can this be factored a bit less flow space dependently? if hasattr(space, 'specialcases'): sc = space.specialcases if ApplevelClass in sc: ret_w = sc[ApplevelClass](space, self, name, args_w) if ret_w is not None: # it was RPython return ret_w # the last argument can be an Arguments if not args_w: args = Arguments(space, []) else: args = args_w[-1] assert args is not None if not isinstance(args, AbstractArguments): args = Arguments(space, list(args_w)) else: # ...which is merged with the previous arguments, if any if len(args_w) > 1: more_args_w, more_kwds_w = args.unpack() args = Arguments(space, list(args_w[:-1]) + more_args_w, more_kwds_w) w_func = self.wget(space, name) return space.call_args(w_func, args)
def __init__(self, space, w_vector): W_AbstractSeqIterObject.__init__(self, w_vector) # TODO: this should live in rpythonize.py or something so that the # imports can move to the top w/o getting circles from pypy.module._cppyy import interp_cppyy assert isinstance(w_vector, interp_cppyy.W_CPPInstance) vector = space.interp_w(interp_cppyy.W_CPPInstance, w_vector) v_type = c_resolve_name(space, vector.clsdecl.name + '::value_type') v_size = c_size_of_type(space, v_type) if not v_type or not v_size: raise NotImplementedError # fallback on getitem from pypy.module._cppyy import converter self.converter = converter.get_converter(space, v_type, '') # this 'data' is from the decl, so not the pythonized data from pythonify.py w_arr = space.call_obj_args(vector.clsdecl.get_overload('data'), w_vector, Arguments(space, [])) arr = space.interp_w(W_ArrayInstance, w_arr, can_be_None=True) if not arr: raise OperationError(space.w_StopIteration, space.w_None) self.data = rffi.cast(rffi.CCHARP, space.uint_w(arr.getbuffer(space))) self.len = space.uint_w( space.call_obj_args(vector.clsdecl.get_overload('size'), w_vector, Arguments(space, []))) self.stride = v_size
def callparse(rtyper, graph, hop, opname, r_self=None): """Parse the arguments of 'hop' when calling the given 'graph'. """ rinputs = getrinputs(rtyper, graph) space = RPythonCallsSpace() def args_h(start): return [VarHolder(i, hop.args_s[i]) for i in range(start, hop.nb_args)] if r_self is None: start = 1 else: start = 0 rinputs[0] = r_self if opname == "simple_call": arguments = Arguments(space, args_h(start)) elif opname == "call_args": arguments = Arguments.fromshape(space, hop.args_s[start].const, args_h(start + 1)) # shape # parse the arguments according to the function we are calling signature = graph.signature defs_h = [] if graph.defaults: for x in graph.defaults: defs_h.append(ConstHolder(x)) try: holders = arguments.match_signature(signature, defs_h) except ArgErr, e: raise TyperError, "signature mismatch: %s" % e.getmsg(graph.name)
def callparse(rtyper, graph, hop, opname, r_self=None): """Parse the arguments of 'hop' when calling the given 'graph'. """ rinputs = getrinputs(rtyper, graph) space = RPythonCallsSpace() def args_h(start): return [VarHolder(i, hop.args_s[i]) for i in range(start, hop.nb_args)] if r_self is None: start = 1 else: start = 0 rinputs[0] = r_self if opname == "simple_call": arguments = Arguments(space, args_h(start)) elif opname == "call_args": arguments = Arguments.fromshape( space, hop.args_s[start].const, # shape args_h(start + 1)) # parse the arguments according to the function we are calling signature = graph.signature defs_h = [] if graph.defaults: for x in graph.defaults: defs_h.append(ConstHolder(x)) try: holders = arguments.match_signature(signature, defs_h) except ArgErr, e: raise TyperError, "signature mismatch: %s" % e.getmsg(graph.name)
def test_prepend(self): space = DummySpace() args = Arguments(space, ["0"]) args1 = args.prepend("thingy") assert args1 is not args assert args1.arguments_w == ["thingy", "0"] assert args1.keywords is args.keywords assert args1.keywords_w is args.keywords_w
def test_argument_unicode(self): space = DummySpace() w_starstar = space.wrap({u"abc": 5}) args = Arguments(space, [], w_starstararg=w_starstar) l = [None] args._match_signature(None, l, Signature(["abc"])) assert len(l) == 1 assert l[0] == space.wrap(5)
def test_argument_unicode(self): space = DummySpace() w_starstar = space.wrap({u'abc': 5}) args = Arguments(space, [], w_starstararg=w_starstar) l = [None] args._match_signature(None, l, Signature(['abc'])) assert len(l) == 1 assert l[0] == space.wrap(5)
def test_posonly_error(self): space = DummySpace() sig = Signature([], posonlyargnames=["x", "y", "z"]) with pytest.raises(ArgErrPosonlyAsKwds) as info: args = Arguments(space, [1, 2, 3, 4, 5], ["x"], [6]) l = [None] * 6 args._match_signature(None, l, sig) assert info.value.getmsg() == "got an unexpected keyword argument 'x'"
def test_topacked_frompacked(self): space = DummySpace() args = Arguments(space, [1], ['a', 'b'], [2, 3]) w_args, w_kwds = args.topacked() assert w_args == (1,) assert w_kwds == {'a': 2, 'b': 3} args1 = Arguments.frompacked(space, w_args, w_kwds) assert args.arguments_w == [1] assert set(args.keywords) == set(['a', 'b']) assert args.keywords_w[args.keywords.index('a')] == 2 assert args.keywords_w[args.keywords.index('b')] == 3
def test_fixedunpacked(self): space = DummySpace() args = Arguments(space, [], ["k"], [1]) py.test.raises(ValueError, args.fixedunpack, 1) args = Arguments(space, ["a", "b"]) py.test.raises(ValueError, args.fixedunpack, 0) py.test.raises(ValueError, args.fixedunpack, 1) py.test.raises(ValueError, args.fixedunpack, 3) py.test.raises(ValueError, args.fixedunpack, 4) assert args.fixedunpack(2) == ['a', 'b']
def test_match_kwds2(self): space = DummySpace() kwds = [("c", 3), ('d', 4)] for i in range(4): kwds_w = dict(kwds[:i]) keywords = kwds_w.keys() keywords_w = kwds_w.values() w_kwds = dict(kwds[i:]) if i == 3: w_kwds = None args = Arguments(space, [1, 2], keywords, keywords_w, w_starstararg=w_kwds) l = [None, None, None, None] args._match_signature(None, l, Signature(["a", "b", "c"], None, "**")) assert l == [1, 2, 3, {'d': 4}]
def test_bad_type_for_star(self): space = self.space with pytest.raises(OperationError) as excinfo: Arguments(space, [], w_stararg=space.wrap(42), fnname_parens="f1()") msg = space.text_w(excinfo.value.get_w_value(space)) assert msg == "f1() argument after * must be an iterable, not int" with pytest.raises(OperationError) as excinfo: Arguments(space, [], w_starstararg=space.wrap(42), fnname_parens="f2()") msg = space.text_w(excinfo.value.get_w_value(space)) assert msg == "f2() argument after ** must be a mapping, not int"
def test_match_kwds2(self): space = DummySpace() kwds = [("c", 3), ('d', 4)] for i in range(4): kwds_w = dict(kwds[:i]) keywords = kwds_w.keys() keywords_w = kwds_w.values() w_kwds = dummy_wrapped_dict(kwds[i:]) if i == 3: w_kwds = None args = Arguments(space, [1, 2], keywords, keywords_w, w_starstararg=w_kwds) l = [None, None, None, None] args._match_signature(None, l, Signature(["a", "b", "c"], None, "**")) assert l == [1, 2, 3, {'d': 4}]
def test_args_parsing_into_scope(self): space = DummySpace() args = Arguments(space, []) calls = [] def _match_signature(w_firstarg, scope_w, signature, defaults_w=None, blindargs=0): defaults_w = [] if defaults_w is None else defaults_w calls.append( ( w_firstarg, scope_w, signature.argnames, signature.has_vararg(), signature.has_kwarg(), defaults_w, blindargs, ) ) args._match_signature = _match_signature scope_w = [None, None] args.parse_into_scope(None, scope_w, "foo", Signature(["a", "b"], None, None)) assert len(calls) == 1 assert calls[0] == (None, scope_w, ["a", "b"], False, False, [], 0) assert calls[0][1] is scope_w calls = [] scope_w = [None, None, None, None] args.parse_into_scope(None, scope_w, "foo", Signature(["a", "b"], "args", "kw"), defaults_w=["x", "y"]) assert len(calls) == 1 assert calls[0] == (None, scope_w, ["a", "b"], True, True, ["x", "y"], 0) calls = [] scope_w = [None, None, None, None] args.parse_into_scope("obj", scope_w, "foo", Signature(["a", "b"], "args", "kw"), defaults_w=["x", "y"]) assert len(calls) == 1 assert calls[0] == ("obj", scope_w, ["a", "b"], True, True, ["x", "y"], 0) class FakeArgErr(ArgErr): def getmsg(self): return "msg" def _match_signature(*args): raise FakeArgErr() args._match_signature = _match_signature excinfo = py.test.raises( OperationError, args.parse_into_scope, "obj", [None, None], "foo", Signature(["a", "b"], None, None) ) assert excinfo.value.w_type is TypeError assert excinfo.value.get_w_value(space) == "foo() msg"
def send(self): st_to_py = utils.smalltalk_to_python wp_rcvr = st_to_py(self.space(), self.w_rcvr) wp_attrname = py_space.newtext(self.method_name) try: if self.method_name == '__call__': # special __call__ case w_meth = py_space.getattr(wp_rcvr, wp_attrname) args_wp = [st_to_py(self.space(), a) for a in self.args_w] # use call_args() to allow variable number of args_w # (but this disables speed hacks in Pypy) args = Arguments(py_space, args_wp) self.set_result(W_PythonObject( py_space.call_args(w_meth, args))) elif len(self.args_w) == 1: # setattr when one argument wp_value = st_to_py(self.space(), self.args_w[0]) py_space.setattr(wp_rcvr, wp_attrname, wp_value) self.set_result(W_PythonObject(py_space.w_None)) else: # otherwise getattr self.set_result(W_PythonObject( py_space.getattr(wp_rcvr, wp_attrname))) except OperationError as operr: print 'Python error caught: %s' % operr error = utils.operr_to_w_object(operr) self.set_error(error) self.set_result(error) except Exception as e: self.fail('Unable to call %s on %s: %s' % ( self.method_name, wp_rcvr, e))
def test_fail_call(self): space = self.space w_meth = descr_function_get(space, self.fn, space.wrap(5), space.type(space.wrap(5))) meth = space.unwrap(w_meth) args = Arguments(space, [space.wrap("spam"), space.wrap("egg")]) self.space.raises_w(self.space.w_TypeError, meth.call_args, args)
def test_call(self): space = self.space w_meth = descr_function_get(space, self.fn, space.wrap(5), space.type(space.wrap(5))) meth = space.unwrap(w_meth) w_result = meth.call_args(Arguments(space, [space.wrap(42)])) assert space.unwrap(w_result) == 42
def checkmodule(modname, backend, interactive=False, basepath='pypy.module'): "Compile a fake PyPy module." from pypy.objspace.fake.objspace import FakeObjSpace, W_Object from pypy.translator.driver import TranslationDriver space = FakeObjSpace() space.config.translating = True ModuleClass = __import__(basepath + '.%s' % modname, None, None, ['Module']).Module module = ModuleClass(space, space.wrap(modname)) w_moduledict = module.getdict() gateways = find_gateways(modname, basepath, module) functions = [gw.__spacebind__(space) for gw in gateways] arguments = Arguments.frompacked(space, W_Object(), W_Object()) dummy_function = copy(functions[0]) def main(argv): # use the standalone mode not to allow SomeObject dummy_rpython(dummy_function) for func in functions: func.call_args(arguments) return 0 patch_pypy() driver = TranslationDriver() driver.setup(main, None) try: driver.proceed(['compile_' + backend]) except SystemExit: raise except: if not interactive: raise debug(driver) raise SystemExit(1)
def call_many_to_one(space, shape, func, res_dtype, in_args, out): # out must hav been built. func needs no calc_type, is usually an # external ufunc nin = len(in_args) in_iters = [None] * nin in_states = [None] * nin for i in range(nin): in_i = in_args[i] assert isinstance(in_i, W_NDimArray) in_iter, in_state = in_i.create_iter(shape) in_iters[i] = in_iter in_states[i] = in_state shapelen = len(shape) assert isinstance(out, W_NDimArray) out_iter, out_state = out.create_iter(shape) vals = [None] * nin while not out_iter.done(out_state): call_many_to_one_driver.jit_merge_point(shapelen=shapelen, func=func, res_dtype=res_dtype, nin=nin) for i in range(nin): vals[i] = in_iters[i].getitem(in_states[i]) w_arglist = space.newlist(vals) w_out_val = space.call_args(func, Arguments.frompacked(space, w_arglist)) out_iter.setitem(out_state, res_dtype.coerce(space, w_out_val)) for i in range(nin): in_states[i] = in_iters[i].next(in_states[i]) out_state = out_iter.next(out_state) return out
def acquire(self, space, __args__): (w_user, w_password, w_cclass, w_purity) = __args__.parse_obj( None, "acquire", Signature(["user", "password", "cclass", "purity"]), defaults_w=[None, None, None, space.w_False]) if self.homogeneous and (w_user or w_password): raise OperationError( get(space).w_ProgrammingError, space.wrap("pool is homogeneous. " "Proxy authentication is not possible.")) self.checkConnected(space) if __args__.keywords: keywords = __args__.keywords + ["pool"] else: keywords = ["pool"] if __args__.keywords_w: keywords_w = __args__.keywords_w + [space.wrap(self)] else: keywords_w = [space.wrap(self)] newargs = Arguments(space, __args__.arguments_w, keywords, keywords_w) return space.call_args(self.w_connectionType, newargs)
def test_method_get(self): space = self.space # Create some function for this test only def m(self): return self func = Function(space, PyCode._from_code(self.space, m.func_code), space.newdict()) # Some shorthands obj1 = space.wrap(23) obj2 = space.wrap(42) args = Arguments(space, []) # Check method returned from func.__get__() w_meth1 = descr_function_get(space, func, obj1, space.type(obj1)) meth1 = space.unwrap(w_meth1) assert isinstance(meth1, Method) assert meth1.call_args(args) == obj1 # Check method returned from method.__get__() # --- meth1 is already bound so meth1.__get__(*) is meth1. w_meth2 = meth1.descr_method_get(obj2, space.type(obj2)) meth2 = space.unwrap(w_meth2) assert isinstance(meth2, Method) assert meth2.call_args(args) == obj1 # Check method returned from unbound_method.__get__() w_meth3 = descr_function_get(space, func, None, space.type(obj2)) meth3 = space.unwrap(w_meth3) w_meth4 = meth3.descr_method_get(obj2, space.w_None) meth4 = space.unwrap(w_meth4) assert isinstance(meth4, Method) assert meth4.call_args(args) == obj2 # Check method returned from unbound_method.__get__() # --- with an incompatible class w_meth5 = meth3.descr_method_get(space.wrap('hello'), space.w_str) assert space.is_w(w_meth5, w_meth3)
def checkmodule(modname, backend, interactive=False, basepath='pypy.module'): "Compile a fake PyPy module." from pypy.objspace.fake.objspace import FakeObjSpace, W_Object from pypy.translator.driver import TranslationDriver space = FakeObjSpace() space.config.translating = True ModuleClass = __import__(basepath + '.%s' % modname, None, None, ['Module']).Module module = ModuleClass(space, space.wrap(modname)) w_moduledict = module.getdict(space) gateways = find_gateways(modname, basepath, module) functions = [gw.__spacebind__(space) for gw in gateways] arguments = Arguments.frompacked(space, W_Object(), W_Object()) dummy_function = copy(functions[0]) def main(argv): # use the standalone mode not to allow SomeObject dummy_rpython(dummy_function) for func in functions: func.call_args(arguments) return 0 patch_pypy() driver = TranslationDriver() driver.setup(main, None) try: driver.proceed(['compile_' + backend]) except SystemExit: raise except: if not interactive: raise debug(driver) raise SystemExit(1)
def test_bad_type_for_star(self): space = self.space try: Arguments(space, [], w_stararg=space.wrap(42)) except OperationError as e: msg = space.str_w(space.str(e.get_w_value(space))) assert msg == "argument after * must be an iterable, not int" else: assert 0, "did not raise" try: Arguments(space, [], w_starstararg=space.wrap(42)) except OperationError as e: msg = space.str_w(space.str(e.get_w_value(space))) assert msg == "argument after ** must be a mapping, not int" else: assert 0, "did not raise"
def funccall(self, *args_w): # speed hack code = self.getcode() # hook for the jit if len(args_w) == 0: w_res = code.fastcall_0(self.space, self) if w_res is not None: return w_res elif len(args_w) == 1: w_res = code.fastcall_1(self.space, self, args_w[0]) if w_res is not None: return w_res elif len(args_w) == 2: w_res = code.fastcall_2(self.space, self, args_w[0], args_w[1]) if w_res is not None: return w_res elif len(args_w) == 3: w_res = code.fastcall_3(self.space, self, args_w[0], args_w[1], args_w[2]) if w_res is not None: return w_res elif len(args_w) == 4: w_res = code.fastcall_4(self.space, self, args_w[0], args_w[1], args_w[2], args_w[3]) if w_res is not None: return w_res return self.call_args(Arguments(self.space, list(args_w)))
def test_topacked_frompacked(self): space = DummySpace() args = Arguments(space, [1], ["a", "b"], [2, 3]) w_args, w_kwds = args.topacked() assert w_args == (1,) assert w_kwds == {"a": 2, "b": 3} args1 = Arguments.frompacked(space, w_args, w_kwds) assert args.arguments_w == [1] assert set(args.keywords) == set(["a", "b"]) assert args.keywords_w[args.keywords.index("a")] == 2 assert args.keywords_w[args.keywords.index("b")] == 3 args = Arguments(space, [1]) w_args, w_kwds = args.topacked() assert w_args == (1,) assert not w_kwds
def call_args_expand(hop, takes_kwds = True): hop = hop.copy() from pypy.interpreter.argument import Arguments arguments = Arguments.fromshape(None, hop.args_s[1].const, # shape range(hop.nb_args-2)) if arguments.w_starstararg is not None: raise TyperError("**kwds call not implemented") if arguments.w_stararg is not None: # expand the *arg in-place -- it must be a tuple from pypy.rpython.rtuple import AbstractTupleRepr if arguments.w_stararg != hop.nb_args - 3: raise TyperError("call pattern too complex") hop.nb_args -= 1 v_tuple = hop.args_v.pop() s_tuple = hop.args_s.pop() r_tuple = hop.args_r.pop() if not isinstance(r_tuple, AbstractTupleRepr): raise TyperError("*arg must be a tuple") for i in range(len(r_tuple.items_r)): v_item = r_tuple.getitem_internal(hop.llops, v_tuple, i) hop.nb_args += 1 hop.args_v.append(v_item) hop.args_s.append(s_tuple.items[i]) hop.args_r.append(r_tuple.items_r[i]) kwds = arguments.kwds_w or {} if not takes_kwds and kwds: raise TyperError("kwds args not supported") # prefix keyword arguments with 'i_' kwds_i = {} for key, index in kwds.items(): kwds_i['i_'+key] = index return hop, kwds_i
def test_create(self): space = DummySpace() args_w = [] args = Arguments(space, args_w) assert args.arguments_w is args_w assert args.keywords is None assert args.keywords_w is None assert args.firstarg() is None args = Arguments(space, args_w, w_stararg=["*"], w_starstararg={"k": 1}) assert args.arguments_w == ["*"] assert args.keywords == ["k"] assert args.keywords_w == [1] assert args.firstarg() == "*"
def test_globals_warnings(self): space = self.space w_mod = space.appexec((), '():\n import warnings\n return warnings\n') #sys.getmodule('warnings') w_filterwarnings = space.getattr(w_mod, space.wrap('filterwarnings')) filter_arg = Arguments(space, [ space.wrap('error') ], ["module"], [space.wrap("<tmp>")]) for code in (''' def wrong1(): a = 1 b = 2 global a global b ''', ''' def wrong2(): print x global x ''', ''' def wrong3(): print x x = 2 global x '''): space.call_args(w_filterwarnings, filter_arg) e = py.test.raises(OperationError, self.compiler.compile, code, '<tmp>', 'exec', 0) space.call_method(w_mod, 'resetwarnings') ex = e.value ex.normalize_exception(space) assert ex.match(space, space.w_SyntaxError)
def test_bad_type_for_star(self): space = self.space try: Arguments(space, [], w_stararg=space.wrap(42)) except OperationError, e: msg = space.str_w(space.str(e.get_w_value(space))) assert msg == "argument after * must be a sequence, not int"
def build_class(space, w_func, w_name, __args__): if not isinstance(w_func, Function): raise oefmt(space.w_TypeError, "__build_class__: func must be a function") bases_w, kwds_w = __args__.unpack() w_bases = space.newtuple(bases_w) w_meta = kwds_w.pop('metaclass', None) if w_meta is not None: isclass = space.isinstance_w(w_meta, space.w_type) else: if bases_w: w_meta = space.type(bases_w[0]) else: w_meta = space.w_type isclass = True if isclass: # w_meta is really a class, so check for a more derived # metaclass, or possible metaclass conflicts from pypy.objspace.std.typeobject import _calculate_metaclass w_meta = _calculate_metaclass(space, w_meta, bases_w) try: w_prep = space.getattr(w_meta, space.newtext("__prepare__")) except OperationError as e: if not e.match(space, space.w_AttributeError): raise w_namespace = space.newdict() else: keywords = kwds_w.keys() args = Arguments(space, args_w=[w_name, w_bases], keywords=keywords, keywords_w=kwds_w.values()) w_namespace = space.call_args(w_prep, args) code = w_func.getcode() frame = space.createframe(code, w_func.w_func_globals, w_func) frame.setdictscope(w_namespace) w_cell = frame.run() keywords = kwds_w.keys() args = Arguments(space, args_w=[w_name, w_bases, w_namespace], keywords=keywords, keywords_w=kwds_w.values()) w_class = space.call_args(w_meta, args) if isinstance(w_cell, Cell): w_cell.set(w_class) return w_class
def test_blindargs(self): space = DummySpace() kwds = [("a", 3), ("b", 4)] for i in range(4): kwds_w = dict(kwds[:i]) keywords = kwds_w.keys() keywords_w = kwds_w.values() w_kwds = dict(kwds[i:]) if i == 3: w_kwds = None args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None] args._match_signature(None, l, Signature(["a", "b"], None, "**"), blindargs=2) assert l == [1, 2, {"a": 3, "b": 4}] args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None] py.test.raises(ArgErrUnknownKwds, args._match_signature, None, l, Signature(["a", "b"]), blindargs=2)
def test_starstararg_wrong_type(self): space = DummySpace() with pytest.raises(OperationError) as excinfo: Arguments(space, [], ["a"], [1], w_starstararg="hello", fnname_parens="bar()") assert excinfo.value.w_type is TypeError assert excinfo.value.get_w_value( space) == "bar() argument after ** must be a mapping, not str"
def callback(ctx, js_function, js_this, js_args): arguments = Arguments(space, [self.js_to_python(arg) for arg in js_args]) w_callable = self.applevel_callbacks.get( rffi.cast(lltype.Signed, js_function), None) if w_callable is None: raise Exception("Got wrong callback, should not happen") w_res = space.call_args(w_callable, arguments) return self.python_to_js(w_res)
def test_duplicate_kwds(self): space = DummySpace() with pytest.raises(OperationError) as excinfo: Arguments(space, [], ["a"], [1], w_starstararg={"a": 2}, fnname_parens="foo()") assert excinfo.value.w_type is TypeError assert excinfo.value.get_w_value( space) == "foo() got multiple values for keyword argument 'a'"
def op_call_args(self): a = self.argnames shape = self.op.args[1].value args = Arguments.fromshape(None, shape, a[2:]) lst = args.arguments_w[:] for key, value in args.kwds_w.items(): lst.append("%s=%s" % (key, value)) if args.w_stararg is not None: lst.append("*%s" % args.w_stararg) if args.w_starstararg is not None: lst.append("**%s" % args.w_starstararg) return "%s = %s(%s)" % (self.resultname, a[0], ", ".join(lst))
def test_args_parsing(self): space = DummySpace() args = Arguments(space, []) calls = [] def _match_signature(w_firstarg, scope_w, signature, defaults_w=[], blindargs=0): calls.append((w_firstarg, scope_w, signature.argnames, signature.has_vararg(), signature.has_kwarg(), defaults_w, blindargs)) args._match_signature = _match_signature scope_w = args.parse_obj(None, "foo", Signature(["a", "b"], None, None)) assert len(calls) == 1 assert calls[0] == (None, [None, None], ["a", "b"], False, False, [], 0) assert calls[0][1] is scope_w calls = [] scope_w = args.parse_obj(None, "foo", Signature(["a", "b"], "args", None), blindargs=1) assert len(calls) == 1 assert calls[0] == (None, [None, None, None], ["a", "b"], True, False, [], 1) calls = [] scope_w = args.parse_obj(None, "foo", Signature(["a", "b"], "args", "kw"), defaults_w=['x', 'y']) assert len(calls) == 1 assert calls[0] == (None, [None, None, None, None], ["a", "b"], True, True, ["x", "y"], 0) calls = [] scope_w = args.parse_obj("obj", "foo", Signature(["a", "b"], "args", "kw"), defaults_w=['x', 'y'], blindargs=1) assert len(calls) == 1 assert calls[0] == ("obj", [None, None, None, None], ["a", "b"], True, True, ["x", "y"], 1) class FakeArgErr(ArgErr): def getmsg(self, fname): return "msg "+fname def _match_signature(*args): raise FakeArgErr() args._match_signature = _match_signature excinfo = py.test.raises(OperationError, args.parse_obj, "obj", "foo", Signature(["a", "b"], None, None)) assert excinfo.value.w_type is TypeError assert excinfo.value._w_value == "msg foo"
def call_many_to_many(space, shape, func, in_dtypes, out_dtypes, in_args, out_args): # out must hav been built. func needs no calc_type, is usually an # external ufunc nin = len(in_args) in_iters = [None] * nin in_states = [None] * nin nout = len(out_args) out_iters = [None] * nout out_states = [None] * nout for i in range(nin): in_i = in_args[i] assert isinstance(in_i, W_NDimArray) in_iter, in_state = in_i.create_iter(shape) in_iters[i] = in_iter in_states[i] = in_state for i in range(nout): out_i = out_args[i] assert isinstance(out_i, W_NDimArray) out_iter, out_state = out_i.create_iter(shape) out_iters[i] = out_iter out_states[i] = out_state shapelen = len(shape) vals = [None] * nin test_iter, test_state = in_iters[-1], in_states[-1] if nout > 0: test_iter, test_state = out_iters[0], out_states[0] while not test_iter.done(test_state): call_many_to_many_driver.jit_merge_point(shapelen=shapelen, func=func, in_dtypes=in_dtypes, out_dtypes=out_dtypes, nin=nin, nout=nout) for i in range(nin): vals[i] = in_dtypes[i].coerce(space, in_iters[i].getitem(in_states[i])) w_arglist = space.newlist(vals) w_outvals = space.call_args(func, Arguments.frompacked(space, w_arglist)) # w_outvals should be a tuple, but func can return a single value as well if space.isinstance_w(w_outvals, space.w_tuple): batch = space.listview(w_outvals) for i in range(len(batch)): out_iters[i].setitem(out_states[i], out_dtypes[i].coerce(space, batch[i])) out_states[i] = out_iters[i].next(out_states[i]) elif nout > 0: out_iters[0].setitem(out_states[0], out_dtypes[0].coerce(space, w_outvals)) out_states[0] = out_iters[0].next(out_states[0]) for i in range(nin): in_states[i] = in_iters[i].next(in_states[i]) test_state = test_iter.next(test_state) return space.newtuple([convert_to_array(space, o) for o in out_args])
def descr__setstate__(self, space, w_args): w_flags, w_state, w_thunk, w_parent = space.unpackiterable(w_args, expected_length=4) self.flags = space.int_w(w_flags) if space.is_w(w_parent, space.w_None): w_parent = self.w_getmain(space) self.parent = space.interp_w(AppCoroutine, w_parent) ec = self.space.getexecutioncontext() self.subctx.setstate(space, w_state) if space.is_w(w_thunk, space.w_None): if space.is_w(w_state, space.w_None): self.thunk = None else: self.bind(_ResumeThunk(space, self.costate, self.subctx.topframe)) else: w_func, w_args, w_kwds = space.unpackiterable(w_thunk, expected_length=3) args = Arguments.frompacked(space, w_args, w_kwds) self.bind(_AppThunk(space, self.costate, w_func, args))
def test_match_kwds(self): space = DummySpace() for i in range(3): kwds = [("c", 3)] kwds_w = dict(kwds[:i]) keywords = kwds_w.keys() keywords_w = kwds_w.values() w_kwds = dict(kwds[i:]) if i == 2: w_kwds = None assert len(keywords) == len(keywords_w) args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None] args._match_signature(None, l, Signature(["a", "b", "c"]), defaults_w=[4]) assert l == [1, 2, 3] args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None, None] args._match_signature(None, l, Signature(["a", "b", "b1", "c"]), defaults_w=[4, 5]) assert l == [1, 2, 4, 3] args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None, None] args._match_signature(None, l, Signature(["a", "b", "c", "d"]), defaults_w=[4, 5]) assert l == [1, 2, 3, 5] args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None, None] py.test.raises(ArgErr, args._match_signature, None, l, Signature(["c", "b", "a", "d"]), defaults_w=[4, 5]) args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None, None] py.test.raises(ArgErr, args._match_signature, None, l, Signature(["a", "b", "c1", "d"]), defaults_w=[4, 5]) args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None] args._match_signature(None, l, Signature(["a", "b"], None, "**")) assert l == [1, 2, {'c': 3}]
def call(self, w_callable, w_args, w_kwds=None): args = Arguments.frompacked(self, w_args, w_kwds) return self.call_args(w_callable, args)
def test_starstarargs_special(self): class kwargs(object): def __init__(self, k, v): self.k = k self.v = v class MyDummySpace(DummySpace): def view_as_kwargs(self, kw): if isinstance(kw, kwargs): return kw.k, kw.v return None, None space = MyDummySpace() for i in range(3): kwds = [("c", 3)] kwds_w = dict(kwds[:i]) keywords = kwds_w.keys() keywords_w = kwds_w.values() rest = dict(kwds[i:]) w_kwds = kwargs(rest.keys(), rest.values()) if i == 2: w_kwds = None assert len(keywords) == len(keywords_w) args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None] args._match_signature(None, l, Signature(["a", "b", "c"]), defaults_w=[4]) assert l == [1, 2, 3] args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None, None] args._match_signature(None, l, Signature(["a", "b", "b1", "c"]), defaults_w=[4, 5]) assert l == [1, 2, 4, 3] args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None, None] args._match_signature(None, l, Signature(["a", "b", "c", "d"]), defaults_w=[4, 5]) assert l == [1, 2, 3, 5] args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None, None] py.test.raises(ArgErr, args._match_signature, None, l, Signature(["c", "b", "a", "d"]), defaults_w=[4, 5]) args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None, None] py.test.raises(ArgErr, args._match_signature, None, l, Signature(["a", "b", "c1", "d"]), defaults_w=[4, 5]) args = Arguments(space, [1, 2], keywords[:], keywords_w[:], w_starstararg=w_kwds) l = [None, None, None] args._match_signature(None, l, Signature(["a", "b"], None, "**")) assert l == [1, 2, {'c': 3}] excinfo = py.test.raises(OperationError, Arguments, space, [], ["a"], [1], w_starstararg=kwargs(["a"], [2])) assert excinfo.value.w_type is TypeError
def test_unmatch_signature(self): space = DummySpace() args = Arguments(space, [1, 2, 3]) sig = (["a", "b", "c"], None, None) data = args.match_signature(sig, []) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() args = Arguments(space, [1]) sig = (["a", "b", "c"], None, None) data = args.match_signature(sig, [2, 3]) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() args = Arguments(space, [1, 2, 3, 4, 5]) sig = (["a", "b", "c"], "r", None) data = args.match_signature(sig, []) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() args = Arguments(space, [1], {"c": 3, "b": 2}) sig = (["a", "b", "c"], None, None) data = args.match_signature(sig, []) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() args = Arguments(space, [1], {"c": 5}) sig = (["a", "b", "c"], None, None) data = args.match_signature(sig, [2, 3]) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() args = Arguments(space, [1], {"c": 5, "d": 7}) sig = (["a", "b", "c"], None, "kw") data = args.match_signature(sig, [2, 3]) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() args = Arguments(space, [1, 2, 3, 4, 5], {"e": 5, "d": 7}) sig = (["a", "b", "c"], "r", "kw") data = args.match_signature(sig, [2, 3]) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() args = Arguments(space, [], {}, w_stararg=[1], w_starstararg={"c": 5, "d": 7}) sig = (["a", "b", "c"], None, "kw") data = args.match_signature(sig, [2, 3]) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack() args = Arguments(space, [1, 2], {"g": 9}, w_stararg=[3, 4, 5], w_starstararg={"e": 5, "d": 7}) sig = (["a", "b", "c"], "r", "kw") data = args.match_signature(sig, [2, 3]) new_args = args.unmatch_signature(sig, data) assert args.unpack() == new_args.unpack()
def test_match4(self): space = DummySpace() values = [4, 5, 6, 7] for havefirstarg in [0, 1]: for i in range(len(values)-havefirstarg): arglist = values[havefirstarg:i+havefirstarg] starargs = tuple(values[i+havefirstarg:]) if havefirstarg: firstarg = values[0] else: firstarg = None args = Arguments(space, arglist, w_stararg=starargs) l = [None, None, None, None] args._match_signature(firstarg, l, Signature(["a", "b", "c", "d"])) assert l == [4, 5, 6, 7] args = Arguments(space, arglist, w_stararg=starargs) l = [None, None, None, None, None, None] py.test.raises(ArgErr, args._match_signature, firstarg, l, Signature(["a"])) args = Arguments(space, arglist, w_stararg=starargs) l = [None, None, None, None, None, None] py.test.raises(ArgErr, args._match_signature, firstarg, l, Signature(["a", "b", "c", "d", "e"])) args = Arguments(space, arglist, w_stararg=starargs) l = [None, None, None, None, None, None] py.test.raises(ArgErr, args._match_signature, firstarg, l, Signature(["a", "b", "c", "d", "e"], "*")) l = [None, None, None, None, None] args = Arguments(space, arglist, w_stararg=starargs) args._match_signature(firstarg, l, Signature(["a", "b", "c", "d", "e"]), defaults_w=[1]) assert l == [4, 5, 6, 7, 1] for j in range(len(values)): l = [None] * (j + 1) args = Arguments(space, arglist, w_stararg=starargs) args._match_signature(firstarg, l, Signature(["a", "b", "c", "d", "e"][:j], "*")) assert l == values[:j] + [tuple(values[j:])] l = [None, None, None, None, None] args = Arguments(space, arglist, w_stararg=starargs) args._match_signature(firstarg, l, Signature(["a", "b", "c", "d"], None, "**")) assert l == [4, 5, 6, 7, {}]
def test_match0(self): space = DummySpace() args = Arguments(space, []) l = [] args._match_signature(None, l, Signature([])) assert len(l) == 0 l = [None, None] args = Arguments(space, []) py.test.raises(ArgErr, args._match_signature, None, l, Signature(["a"])) args = Arguments(space, []) py.test.raises(ArgErr, args._match_signature, None, l, Signature(["a"], "*")) args = Arguments(space, []) l = [None] args._match_signature(None, l, Signature(["a"]), defaults_w=[1]) assert l == [1] args = Arguments(space, []) l = [None] args._match_signature(None, l, Signature([], "*")) assert l == [()] args = Arguments(space, []) l = [None] args._match_signature(None, l, Signature([], None, "**")) assert l == [{}] args = Arguments(space, []) l = [None, None] py.test.raises(ArgErr, args._match_signature, 41, l, Signature([])) args = Arguments(space, []) l = [None] args._match_signature(1, l, Signature(["a"])) assert l == [1] args = Arguments(space, []) l = [None] args._match_signature(1, l, Signature([], "*")) assert l == [(1,)]
def slot_tp_init(space, w_self, w_args, w_kwds): w_descr = space.lookup(w_self, '__init__') args = Arguments.frompacked(space, w_args, w_kwds) space.get_and_call_args(w_descr, w_self, args) return 0