示例#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)
示例#2
0
 def test_copy_and_shape(self):
     space = DummySpace()        
     args = ArgumentsForTranslation(space, ['a'], ['x'], [1],
                                    ['w1'], {'y': 'w2'})
     args1 = args.copy()
     args.combine_if_necessary()
     assert rawshape(args1) == (1, ('x',), True, True)
示例#3
0
 def test_copy_and_shape(self):
     space = DummySpace()
     args = ArgumentsForTranslation(space, ['a'], ['x'], [1], ['w1'],
                                    {'y': 'w2'})
     args1 = args.copy()
     args.combine_if_necessary()
     assert rawshape(args1) == (1, ('x', ), True, True)
示例#4
0
文件: callparse.py 项目: alkorzt/pypy
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)
示例#5
0
文件: bookkeeper.py 项目: njues/Sypy
 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:]))
示例#6
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
示例#7
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
示例#8
0
def make_arguments_for_translation(space,
                                   args_w,
                                   keywords_w={},
                                   w_stararg=None,
                                   w_starstararg=None):
    return ArgumentsForTranslation(space, args_w, keywords_w.keys(),
                                   keywords_w.values(), w_stararg,
                                   w_starstararg)
示例#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
示例#10
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
示例#11
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

        shape = ((0, (), True, True), [[1], {'c': 5, 'd': 7}])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((2, ('g', ), True, True),
                 [1, 2, 9, [3, 4, 5], {
                     'e': 5,
                     'd': 7
                 }])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape
示例#12
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

        shape = ((0, (), True, True), [[1], {"c": 5, "d": 7}])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((2, ("g",), True, True), [1, 2, 9, [3, 4, 5], {"e": 5, "d": 7}])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape
示例#13
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

        shape = ((0, (), True, True), [[1], {'c': 5, 'd': 7}])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape

        shape = ((2, ('g', ), True, True), [1, 2, 9, [3, 4, 5], {'e': 5, 'd': 7}])
        args = ArgumentsForTranslation.fromshape(space, *shape)
        assert args.flatten() == shape
示例#14
0
def call_args_expand(hop, takes_kwds = True):
    hop = hop.copy()
    from pypy.interpreter.argument import ArgumentsForTranslation
    arguments = ArgumentsForTranslation.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])

    keywords = arguments.keywords
    if not takes_kwds and keywords:
        raise TyperError("kwds args not supported")
    # prefix keyword arguments with 'i_'
    kwds_i = {}
    for i, key in enumerate(keywords):
        index = arguments.keywords_w[i]
        kwds_i['i_'+key] = index

    return hop, kwds_i
示例#15
0
文件: rbuiltin.py 项目: ieure/pypy
def call_args_expand(hop, takes_kwds = True):
    hop = hop.copy()
    from pypy.interpreter.argument import ArgumentsForTranslation
    arguments = ArgumentsForTranslation.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])

    keywords = arguments.keywords
    if not takes_kwds and keywords:
        raise TyperError("kwds args not supported")
    # prefix keyword arguments with 'i_'
    kwds_i = {}
    for i, key in enumerate(keywords):
        index = arguments.keywords_w[i]
        kwds_i['i_'+key] = index

    return hop, kwds_i
示例#16
0
 def argument_factory(self, *args):
     return ArgumentsForTranslation(self.space, *args)
示例#17
0
 def call_function(self, w_func, *args_w):
     from pypy.interpreter.argument import ArgumentsForTranslation
     nargs = len(args_w)
     args = ArgumentsForTranslation(self, list(args_w))
     return self.call_args(w_func, args)
示例#18
0
 def test_copy_and_shape(self):
     space = DummySpace()
     args = ArgumentsForTranslation(space, ["a"], ["x"], [1], ["w1"], {"y": "w2"})
     args1 = args.copy()
     args.combine_if_necessary()
     assert rawshape(args1) == (1, ("x",), True, True)
示例#19
0
 def make_arguments(self, nargs):
     return ArgumentsForTranslation(self.space, self.peekvalues(nargs))