How to use the gast.Call 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 salesforce / matchbox / matchbox / macro.py View on Github external
for n in gast.walk(child):
                if isinstance(n, gast.Name) and isinstance(n.ctx, gast.Load):
                    loads[n.id].append(n)
            if isinstance(child, gast.Assign):
                name = child.targets[0].id
                if name in loads:
                    if name in lcds:
                        raise NotImplementedError("cannot process LCD "
                                                  "stored to twice")
                    lcds.add(name)
        node = SplitAttributes().visit(node)
        synchronizes = []
        for name in lcds:
            synchronize = gast.Assign(
                [gast.Name(name, gast.Store(), None)],
                gast.Call(
                    gast.Attribute(
                        gast.Name(name, gast.Load(), None),
                        gast.Name('_synchronize', gast.Load(), None),
                        None),
                    [], []))
            synchronizes.append(synchronize)
        node.body.extend(synchronizes)
        return node
github serge-sans-paille / pythran / pythran / types / types.py View on Github external
def visit_Call(self, node):
        self.generic_visit(node)

        func = node.func

        for alias in self.strict_aliases[func]:
            # this comes from a bind
            if isinstance(alias, ast.Call):
                a0 = alias.args[0]
                bounded_name = a0.id
                # by construction of the bind construct
                assert len(self.strict_aliases[a0]) == 1
                bounded_function = next(iter(self.strict_aliases[a0]))
                fake_name = ast.Name(bounded_name, ast.Load(), None, None)
                fake_node = ast.Call(fake_name, alias.args[1:] + node.args,
                                     [])
                self.combiners[bounded_function].combiner(self, fake_node)

            # handle backward type dependencies from function calls
            else:
                self.combiners[alias].combiner(self, node)

        UnknownType = self.builder.UnknownType

        # recurring nightmare
        def last_chance():
            # maybe we can get saved if we have a hint about
            # the called function return type
            for alias in self.strict_aliases[func]:
                if alias is self.current and alias in self.result:
                    # great we have a (partial) type information
github serge-sans-paille / pythran / pythran / optimizations / inline_builtins.py View on Github external
def inlineBuiltinsXMap(self, node):
        self.update = True

        elts = []
        nelts = min(len(n.elts) for n in node.args[1:])
        for i in range(nelts):
            elts.append([n.elts[i] for n in node.args[1:]])
        return ast.List([ast.Call(node.args[0], elt, []) for elt in elts],
                        ast.Load())
github serge-sans-paille / pythran / pythran / optimizations / comprehension_patterns.py View on Github external
def make_Iterator(self, gen):
        if gen.ifs:
            ldFilter = ast.Lambda(
                ast.arguments([ast.Name(gen.target.id, ast.Param(), None, None)],
                              [], None, [], [], None, []),
                ast.BoolOp(ast.And(), gen.ifs)
                if len(gen.ifs) > 1 else gen.ifs[0])
            self.use_itertools |= MODULE == 'itertools'
            ifilterName = ast.Attribute(
                value=ast.Name(id=ASMODULE,
                               ctx=ast.Load(),
                               annotation=None, type_comment=None),
                attr=IFILTER, ctx=ast.Load())
            return ast.Call(ifilterName, [ldFilter, gen.iter], [])
        else:
            return gen.iter
github serge-sans-paille / pythran / pythran / optimizations / comprehension_patterns.py View on Github external
def makeattr(*args):
            r = ast.Attribute(
                value=ast.Name(id='__builtin__',
                               ctx=ast.Load(),
                               annotation=None,
                              type_comment=None),
                attr='map', ctx=ast.Load())
            r = ast.Call(r, list(args), [])
            if sys.version_info.major == 3:
                r = ast.Call(ast.Attribute(ast.Name('__builtin__', ast.Load(),
                                                    None, None),
                                           'list', ast.Load()),
                             [r], [])
            return r
github serge-sans-paille / pythran / pythran / analyses / fixed_size_list.py View on Github external
def is_safe_call(self, node, index):
        func_aliases = list(self.aliases[node])
        for alias in func_aliases:
            if isinstance(alias, ast.Call):
                if not self.is_safe_call(alias.args[0],
                                         index + len(alias.args) - 1):
                    return False


            if alias in self.argument_effects:
                func_aes = self.argument_effects[alias]
                if func_aes[index]:
                    return False
        return True
github tensorflow / probability / tensorflow_probability / python / experimental / auto_batching / frontend.py View on Github external
# that's where divergence can happen.
  # Replacing all function calls because the downstream transformation
  # expects calls to lead directly to assignments.
  def maybe_replace_function_argument(parent, field_name, child):
    del field_name, child
    if not anno.hasanno(parent.func, anno.Basic.QN):
      return False
    func_name = anno.getanno(parent.func, anno.Basic.QN)
    if str(func_name) in autobatch_functions:
      return True
    return False

  anf_config = [
      (anf.ASTEdgePattern(gast.If, 'test', anf.ANY), anf.REPLACE),
      (anf.ASTEdgePattern(anf.ANY, anf.ANY, gast.Call), anf.REPLACE),
      (anf.ASTEdgePattern(gast.Call, 'args', anf.ANY),
       maybe_replace_function_argument),
  ]
  node = anf.transform(node, ctx, config=anf_config)
  node = converter.standard_analysis(node, ctx, is_initial=False)

  return node, ctx
github serge-sans-paille / pythran / pythran / optimizations / inline_builtins.py View on Github external
def inlineBuiltinsIMap(self, node):

        if not isinstance(node, ast.Call):
            return node

        func_aliases = self.aliases[node.func]
        if len(func_aliases) != 1:
            return node

        obj = next(iter(func_aliases))
        if obj is not MODULES['itertools']['imap']:
            return node

        if not all(isinstance(arg, (ast.List, ast.Tuple))
                   for arg in node.args[1:]):
            return node

        mapped_func_aliases = self.aliases[node.args[0]]
        if len(mapped_func_aliases) != 1:
github serge-sans-paille / pythran / pythran / transformations / remove_named_arguments.py View on Github external
extra_keyword_offset = max(keywords.keys()) if keywords else 0
        node.args.extend([None] * (1 + extra_keyword_offset - len(node.args)))

        replacements = {}
        for index, arg in enumerate(node.args):
            if arg is None:
                if index in keywords:
                    replacements[index] = deepcopy(keywords[index])
                else:  # must be a default value
                    replacements[index] = deepcopy(defaults[index - nargs])

        if not keywords_only:
            return replacements

        node.args.append(ast.Call(
            ast.Attribute(
                ast.Attribute(
                    ast.Name("__builtin__", ast.Load(), None, None),
                    "pythran",
                    ast.Load()),
                "kwonly",
                ast.Load()),
            [], [])
        )
        node.args.extend(keywords_only)

        return replacements

gast

Python AST that abstracts the underlying Python version

BSD-3-Clause
Latest version published 4 months ago

Package Health Score

78 / 100
Full package analysis