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_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 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_args_parsing(self): space = DummySpace() args = Arguments(space, []) calls = [] def _match_signature(w_firstarg, scope_w, signature, defaults_w=None, w_kw_defs=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, w_kw_defs, 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, [], None, 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, [], None, 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"], None, 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"], None, 1) 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_obj, "obj", "foo", Signature(["a", "b"], None, None)) assert excinfo.value.w_type is TypeError assert excinfo.value.get_w_value(space) == "foo() msg"
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 test_posonly(self): space = DummySpace() sig = Signature(["a", "b", "c"], posonlyargnames=["x", "y", "z"]) args = Arguments(space, [1, 2, 3, 4, 5, 6]) l = [None] * 6 args._match_signature(None, l, sig) assert l == [1, 2, 3, 4, 5, 6] args = Arguments(space, [1, 2, 3, 4, 5], ["c"], [6]) l = [None] * 6 args._match_signature(None, l, sig) assert l == [1, 2, 3, 4, 5, 6]
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_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_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 = dummy_wrapped_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 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 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_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_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_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 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 test_args_parsing(self): space = DummySpace() args = Arguments(space, []) calls = [] def _match_signature(w_firstarg, scope_w, signature, defaults_w=None, w_kw_defs=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, w_kw_defs, 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, [], None, 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, [], None, 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"], None, 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"], None, 1) class FakeArgErr(ArgErr): def getmsg(self): return "msg" def _match_signature(*args): raise FakeArgErr() args._match_signature = _match_signature with pytest.raises(OperationError) as excinfo: args.parse_obj("obj", "foo", Signature(["a", "b"], None, None)) assert excinfo.value.w_type is TypeError assert excinfo.value.get_w_value(space) == "foo() msg"