How to use the gast.Tuple function in gast

To help you get started, we’ve selected a few gast examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github serge-sans-paille / pythran / pythran / transformations / normalize_static_if.py View on Github external
fdef = ast.FunctionDef(name, args, stmts, [], None)

        # this is part of a huge trick that plays with delayed type inference
        # it basically computes the return type based on out parameters, and
        # the return statement is unconditionally added so if we have other
        # returns, there will be a computation of the output type based on the
        # __combined of the regular return types and this one The original
        # returns have been patched above to have a different type that
        # cunningly combines with this output tuple
        #
        # This is the only trick I found to let pythran compute both the output
        # variable type and the early return type. But hey, a dirty one :-/

        stmts.append(
            ast.Return(
                ast.Tuple(
                    [ast.Name(fp, ast.Load(), None) for fp in out_parameters],
                    ast.Load()
                )
            )
        )
        if has_return:
            pr = PatchReturn(stmts[-1], has_break or has_cont)
            pr.visit(fdef)

        if has_break or has_cont:
            if not has_return:
                stmts[-1].value = ast.Tuple([ast.Num(LOOP_NONE),
                                             stmts[-1].value],
                                            ast.Load())
            pbc = PatchBreakContinue(stmts[-1])
            pbc.visit(fdef)
github serge-sans-paille / pythran / pythran / optimizations / list_to_tuple.py View on Github external
def convert(self, node):
        self.update = True

        if isinstance(node, ast.Call):
            if not node.args:
                node = ast.Tuple([])
            else:
                node = node.args[0]
        elif isinstance(node, ast.List):
            node = ast.Tuple(node.elts, ast.Load())

        return ast.Call(path_to_attr(('__builtin__', 'pythran', 'static_list')),
                        [node], [])
github serge-sans-paille / pythran / pythran / transformations / normalize_static_if.py View on Github external
def make_control_flow_handlers(self, cont_n, status_n, expected_return,
                                   has_cont, has_break):
        '''
        Create the statements in charge of gathering control flow information
        for the static_if result, and executes the expected control flow
        instruction
        '''
        if expected_return:
            assign = cont_ass = [ast.Assign(
                [ast.Tuple(expected_return, ast.Store())],
                ast.Name(cont_n, ast.Load(), None))]
        else:
            assign = cont_ass = []

        if has_cont:
            cmpr = ast.Compare(ast.Name(status_n, ast.Load(), None),
                               [ast.Eq()], [ast.Num(LOOP_CONT)])
            cont_ass = [ast.If(cmpr,
                               deepcopy(assign) + [ast.Continue()],
                               cont_ass)]
        if has_break:
            cmpr = ast.Compare(ast.Name(status_n, ast.Load(), None),
                               [ast.Eq()], [ast.Num(LOOP_BREAK)])
            cont_ass = [ast.If(cmpr,
                               deepcopy(assign) + [ast.Break()],
                               cont_ass)]
github serge-sans-paille / pythran / pythran / optimizations / pattern_transform.py View on Github external
def sub():
        return ast.Call(func=ast.Attribute(
            ast.Attribute(
                ast.Name('__builtin__', ast.Load(), None, None),
                'str',
                ast.Load()),
            'join', ast.Load()),
            args=[ast.Constant(Placeholder(1), None),
                  ast.Tuple([Placeholder(0), Placeholder(2)], ast.Load())],
            keywords=[])
github serge-sans-paille / pythran / pythran / optimizations / inline_builtins.py View on Github external
lbase, lsize = self.fixedSizeArray(node.left)
        rbase, rsize = self.fixedSizeArray(node_right)
        if not lbase or not rbase:
            return node

        if rsize != 1 and lsize != 1 and rsize != lsize:
            raise PythranSyntaxError("Invalid numpy broadcasting", node)

        self.update = True

        operands = [ast.Compare(self.make_array_index(lbase, lsize, i),
                                [type(node.ops[0])()],
                                [self.make_array_index(rbase, rsize, i)])
                    for i in range(max(lsize, rsize))]
        res = ast.Call(path_to_attr(('numpy', 'array')),
                       [ast.Tuple(operands, ast.Load())],
                       [])
        self.aliases[res.func] = {path_to_node(('numpy', 'array'))}
        return res
github pfnet-research / chainer-compiler / chainer_compiler / elichika / typing / type_checker.py View on Github external
#   self.tyenv[target.id] = self.nodetype[target] = copy_ty(ty_val)
            # will allow self.nodetype[target] to change afterwards, which will
            # be more suitable for elichika but contradict with python semantics.
            self.tyenv[target.id] = copy_ty(ty_val)
            self.nodetype[target] = copy_ty(ty_val)
            return

        if isinstance(target, gast.Attribute):
            self.infer_expr(target)
            ty_obj = self.nodetype[target.value]
            assert isinstance(ty_obj, TyUserDefinedClass)
            self.attribute_tyenv[(ty_obj.instance, target.attr)] = ty_val
            return

        if isinstance(target, (gast.Tuple, gast.List)):
            if isinstance(target, gast.Tuple):
                ty_target = TyTuple([self.generate_fresh_TyVar(e) for e in target.elts])
            else:
                ty_target = TyList([self.generate_fresh_TyVar(e) for e in target.elts])
            self.nodetype[target] = ty_target
            unify(ty_target, ty_val)
            for (var, ty) in zip(target.elts, ty_val.get_tys()):
                self.tyenv[var.id] = ty
                self.nodetype[var] = ty
            return
github serge-sans-paille / pythran / pythran / types / tog.py View on Github external
new_key_type, key_type),
                    node
                )
        new_value_type = TypeVariable()
        for value in node.values:
            value_type = analyse(value, env, non_generic)
            try:
                unify(new_value_type, value_type)
            except InferenceError:
                raise PythranTypeError(
                    "Incompatible dict value type `{}` and `{}`".format(
                        new_value_type, value_type),
                    node
                )
        return Dict(new_key_type, new_value_type)
    elif isinstance(node, gast.Tuple):
        return Tuple([analyse(elt, env, non_generic) for elt in node.elts])
    elif isinstance(node, gast.Index):
        return analyse(node.value, env, non_generic)
    elif isinstance(node, gast.Slice):
        def unify_int_or_none(t, name):
            try:
                unify(t, Integer())
            except InferenceError:
                try:
                    unify(t, NoneType)
                except InferenceError:
                    raise PythranTypeError(
                        "Invalid slice {} type `{}`, expecting int or None"
                        .format(name, t)
                    )
        if node.lower: