Example #1
0
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 =  ArgumentsForTranslation(space, args_h(start))
    elif opname == "call_args":
        arguments = ArgumentsForTranslation.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)
Example #2
0
 def test_prepend(self):
     space = DummySpace()
     args = ArgumentsForTranslation(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
Example #3
0
    def test_fixedunpacked(self):
        space = DummySpace()

        args = ArgumentsForTranslation(space, [], ["k"], [1])
        py.test.raises(ValueError, args.fixedunpack, 1)

        args = ArgumentsForTranslation(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']
Example #4
0
def call_args_expand(hop, takes_kwds = True):
    hop = hop.copy()
    from rpython.annotator.argument import ArgumentsForTranslation
    arguments = ArgumentsForTranslation.fromshape(
            hop.args_s[1].const, # shape
            range(hop.nb_args-2))
    if arguments.w_stararg is not None:
        # expand the *arg in-place -- it must be a tuple
        from rpython.rtyper.rtuple import TupleRepr
        if arguments.w_stararg != hop.nb_args - 3:
            raise TyperError("call pattern too complex")
        v_tuple = hop.args_v.pop()
        s_tuple = hop.args_s.pop()
        r_tuple = hop.args_r.pop()
        if not isinstance(r_tuple, TupleRepr):
            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.args_v.append(v_item)
            hop.args_s.append(s_tuple.items[i])
            hop.args_r.append(r_tuple.items_r[i])

    keywords = arguments.keywords
    if not takes_kwds and keywords:
        raise TyperError("kwds args not supported")
    # prefix keyword arguments with 'i_'
    kwds_i = {}
    for key in keywords:
        kwds_i['i_' + key] = keywords[key]
    return hop, kwds_i
Example #5
0
 def build_args(self, op, args_s):
     if op == "simple_call":
         return ArgumentsForTranslation(list(args_s))
     elif op == "call_args":
         return ArgumentsForTranslation.fromshape(
                 args_s[0].const, # shape
                 list(args_s[1:]))
Example #6
0
def call_args_expand(hop, takes_kwds=True):
    hop = hop.copy()
    from rpython.annotator.argument import ArgumentsForTranslation
    arguments = ArgumentsForTranslation.fromshape(
        hop.args_s[1].const,  # shape
        range(hop.nb_args - 2))
    if arguments.w_stararg is not None:
        # expand the *arg in-place -- it must be a tuple
        from rpython.rtyper.rtuple import TupleRepr
        if arguments.w_stararg != hop.nb_args - 3:
            raise TyperError("call pattern too complex")
        v_tuple = hop.args_v.pop()
        s_tuple = hop.args_s.pop()
        r_tuple = hop.args_r.pop()
        if not isinstance(r_tuple, TupleRepr):
            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.args_v.append(v_item)
            hop.args_s.append(s_tuple.items[i])
            hop.args_r.append(r_tuple.items_r[i])

    keywords = arguments.keywords
    if not takes_kwds and keywords:
        raise TyperError("kwds args not supported")
    # prefix keyword arguments with 'i_'
    kwds_i = {}
    for key in keywords:
        kwds_i['i_' + key] = keywords[key]
    return hop, kwds_i
Example #7
0
 def build_args(self, op, args_s):
     space = RPythonCallsSpace()
     if op == "simple_call":
         return ArgumentsForTranslation(space, list(args_s))
     elif op == "call_args":
         return ArgumentsForTranslation.fromshape(
                 space, args_s[0].const, # shape
                 list(args_s[1:]))
Example #8
0
    def test_fromshape(self):
        space = DummySpace()
        shape = ((3, (), False, False), [1, 2, 3])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((1, (), False, False), [1])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((5, (), False, False), [1,2,3,4,5])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((1, ('b', 'c'), False, False), [1, 2, 3])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((1, ('c', ), False, False), [1, 5])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((1, ('c', 'd'), False, False), [1, 5, 7])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((5, ('d', 'e'), False, False), [1, 2, 3, 4, 5, 7, 5])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape
Example #9
0
    def test_stararg_flowspace_variable(self):
        space = DummySpace()
        var = object()
        shape = ((2, ('g', ), True, False), [1, 2, 9, var])
        args = make_arguments_for_translation(space, [1,2], {'g': 9},
                                       w_stararg=var)
        assert args.flatten() == shape

        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape
Example #10
0
def call_args_expand(hop):
    hop = hop.copy()
    from rpython.annotator.argument import ArgumentsForTranslation
    arguments = ArgumentsForTranslation.fromshape(
            hop.args_s[1].const, # shape
            range(hop.nb_args-2))
    assert arguments.w_stararg is None
    keywords = arguments.keywords
    # prefix keyword arguments with 'i_'
    kwds_i = {}
    for key in keywords:
        kwds_i['i_' + key] = keywords[key]
    return hop, kwds_i
Example #11
0
def call_args_expand(hop):
    hop = hop.copy()
    from rpython.annotator.argument import ArgumentsForTranslation
    arguments = ArgumentsForTranslation.fromshape(
        hop.args_s[1].const,  # shape
        range(hop.nb_args - 2))
    assert arguments.w_stararg is None
    keywords = arguments.keywords
    # prefix keyword arguments with 'i_'
    kwds_i = {}
    for key in keywords:
        kwds_i['i_' + key] = keywords[key]
    return hop, kwds_i
Example #12
0
 def build_args(self, args_s):
     return ArgumentsForTranslation.fromshape(args_s[0].const,
                                             list(args_s[1:]))
Example #13
0
 def build_args(self, args_s):
     return ArgumentsForTranslation(list(args_s))