def case_transform(tree, gen_sym, parents): outer = shared_transform(tree, gen_sym) tree.bases = parents assign = ast.FunctionDef( gen_sym("prepare_"+tree.name), ast.arguments([], None, [], [], None, []), outer, [hq[apply]], None ) return [tree] + ([assign] if len(outer) > 0 else [])
def p_funcdef(p): '''funcdef : TAG_DEF NAME parameters suite''' p[0] = ast.FunctionDef(name=p[2], args=p[3], body=p[4], decorator_list=[], lineno=p.get_item(1).lineno, col_offset=p.get_item(1).lexpos) return
def __init__(self, path): self.GLOBAL_FN = ast.FunctionDef(name='_global_') # FunctionDef -> set(x, y) such that x reads y self.fn_to_ddg_map = {self.GLOBAL_FN: set()} self.fn_to_self_edge_set_map = {self.GLOBAL_FN: set()} self.fn_stack = [self.GLOBAL_FN] self.visited = set() code = open(path).read() self.tree = ast.parse(code)
def visit_If(self, node): self.generic_visit(node) tBranch_name = self.freshName("then") eBranch_name = self.freshName("else") tBranch = ast.FunctionDef(name=tBranch_name, args=ast.arguments(args=[], vararg=None, kwonlyargs=[], kwarg=None, defaults=[], kw_defaults=[]), body=node.body, decorator_list=[]) if len(node.orelse) is 0: node.orelse = [ast.Pass()] eBranch = ast.FunctionDef(name=eBranch_name, args=ast.arguments(args=[], vararg=None, kwonlyargs=[], kwarg=None, defaults=[], kw_defaults=[]), body=node.orelse, decorator_list=[]) ast.fix_missing_locations(tBranch) ast.fix_missing_locations(eBranch) new_node = ast.Expr( value=ast.Call(func=ast.Name(id='_if', ctx=ast.Load()), args=[ node.test, ast.Name(id=tBranch_name, ctx=ast.Load()), ast.Name(id=eBranch_name, ctx=ast.Load()) ], keywords=[])) ast.fix_missing_locations(new_node) mod = [tBranch, eBranch, new_node] return mod
def __init__(self, globalScope): self.globalScope = globalScope self.interpreter = globalScope.interpreter self.vars = {} # name -> varDecl self.varNames = {} # id(varDecl) -> name self.scopeStack = [] # FuncCodeblockScope self.astNode = ast.FunctionDef(args=ast.arguments(args=[], vararg=None, kwarg=None, defaults=[]), body=[], decorator_list=[])
def define_func(name, args, body): """ Construct AST function definition with specified name, args and body. """ return ast.FunctionDef( name=name, args=args, body=[ast.Return(value=body)], decorator_list=[], returns=None, )
def to_node(self): name = self.name.id if self.params is not None: args = self.params.to_node() else: args = ast.arguments(args=[], defaults=[], kw_defaults=[], kwarg=None, kwonlyargs=[], vararg=None) body = [] for statement in self.body.stmt: body.append(statement.to_node()) return ast.FunctionDef(name=name, args=args, body=body, decorator_list=[])
def visit_FunctionDef(self, node): node.parent = self.fundef self.fundef = node if len(list(filter(lambda n: isinstance(n, ast.Name) and n.id is 'rep_fun', node.decorator_list))) > 0: self.recs.append(node.name) self.generic_visit(node) r_args = {} for arg in node.args.args: arg_name = arg.arg try: if self.fundef.locals[arg_name] > 1: r_args[arg_name] = self.freshName('x') # self.fundef.locals[arg_name] += 1 except KeyError as e: pass # generate code to pre-initialize staged vars # we stage all vars that are written to more than once inits = [ast.Assign(targets=[ast.Name(id=id, ctx=ast.Store())], value=ast.Call( func=ast.Name(id='_var', ctx=ast.Load()), args=[], keywords=[])) \ for id in node.locals if node.locals[id] > 1] a_nodes = [ast.Expr( ast.Call( func=ast.Name(id='_assign', ctx=ast.Load()), args=[ast.Name(id=arg,ctx=ast.Load()), ast.Name(id=r_args[arg],ctx=ast.Load())], keywords=[])) \ for arg in r_args] new_node = ast.FunctionDef(name=node.name, args=ast.arguments( args=[ast.arg(arg=r_args[arg.arg],annotation=None) if arg.arg in r_args else arg for arg in node.args.args], vararg=None, kwonlyargs=[], kwarg=None, defaults=[], kw_defaults=[]), # node.args, body=[ast.Try(body=inits + a_nodes + node.body, handlers=[ast.ExceptHandler( type=ast.Name(id='NonLocalReturnValue', ctx=ast.Load()), name='r', body=[ast.Return(value=ast.Attribute(value=ast.Name(id='r', ctx=ast.Load()), attr='value', ctx=ast.Load()))])], orelse=[], finalbody=[])], decorator_list=list(filter(lambda n: isinstance(n, ast.Name) and n.id!='lms' and n.id!='rep_fun', node.decorator_list)) ) ast.copy_location(new_node, node) ast.fix_missing_locations(new_node) self.fundef = node.parent return new_node
def visit_FunctionDef(self, node): self.generic_visit(node) obf_name = self.__obfuscate_name(node, 'name') if obf_name: return ast.FunctionDef( name=obf_name, args=node.args, body=node.body, decorator_list=node.decorator_list, returns=node.returns, ) return node
def build_func(provider, definition, schema, is_async): """ Builds a python function from a GraphQL AST definition """ name = definition.name.value source = graphql.print_ast(definition) assert definition.operation != graphql.OperationType.SUBSCRIPTION params = [build_param(var) for var in definition.variable_definitions] query_func = '__aquery__' if is_async else '__query__' # TODO: Line numbers if sys.version_info >= (3, 8): py38 = { 'posonlyargs': [], } else: py38 = {} return ast.FunctionDef( name=name, args=ast.arguments(args=[], defaults=[], kwonlyargs=[ ast.arg(arg=name, annotation=None) for name, _ in params ], kw_defaults=[val for _, val in params], vararg=None, kwarg=None, **py38), body=[ ast.Return(value=ast.Call( func=ast.Name(id=query_func, ctx=ast.Load()), args=[ value2pyliteral(provider), value2pyliteral(source), ], keywords=[ ast.keyword(arg=name, value=ast.Name(id=name, ctx=ast.Load())) for name, _ in params ] + [ ast.keyword(arg=name, value=(val if isinstance(val, ast.AST) else value2pyliteral(val))) for name, val in get_additional_kwargs( provider, definition, schema).items() ], ), ), ], decorator_list=[], )
def visit_If(self, node): # Must recursively visit the body of both the if and else bodies to handle any nested if # and else statements. This also conveniently handles elif since those are just treated # as a nested if/else within the else branch. # See: https://greentreesnakes.readthedocs.io/en/latest/nodes.html#If node = self.generic_visit(node) if_function = ast.FunctionDef( name="_if_branch", body=node.body, decorator_list=[], args=_EMPTY_ARGUMENTS ) else_function = ast.FunctionDef( name="_else_branch", body=node.orelse, decorator_list=[], args=_EMPTY_ARGUMENTS ) if_function_name = ast.Name(id="_if_branch", ctx=ast.Load()) else_function_name = ast.Name(id="_else_branch", ctx=ast.Load()) if node.orelse: return [ if_function, else_function, ast.Expr( ast.Call( func=ast.Name(id="_if_statement", ctx=ast.Load()), args=[node.test, if_function_name, else_function_name], keywords=[], ) ), ] else: return [ if_function, ast.Expr( ast.Call( func=ast.Name(id="_if_statement", ctx=ast.Load()), args=[node.test, if_function_name, ast.NameConstant(None)], keywords=[], ) ), ]
def parse(self, func): """Function decorator, returning a correct method from a pseudo-Python one""" # Get the function AST parsed = ast.parse(inspect.getsource(func)) fc_ast = parsed.body[0] argument_names = [get_arg_name(name) for name in fc_ast.args.args] # Init local cache self._local_ctx = {} # Translate (blocks[0][0] is the current instr) blocks, body = self._parse_body(fc_ast.body, argument_names) # Build the new function fc_ast.args.args[0:0] = [ gen_arg('ir', ast.Param()), gen_arg('instr', ast.Param()) ] cur_instr = blocks[0][0] if len(blocks[-1][0]) == 0: ## Last block can be empty blocks.pop() other_blocks = blocks[1:] body.append( ast.Return(value=ast.Tuple(elts=[ ast.List(elts=cur_instr, ctx=ast.Load()), ast.List(elts=other_blocks, ctx=ast.Load()) ], ctx=ast.Load()))) ret = ast.parse('') ret.body = [ ast.FunctionDef(name=fc_ast.name, args=fc_ast.args, body=body, decorator_list=[]) ] # To display the generated function, use codegen.to_source # codegen: https://github.com/andreif/codegen # Compile according to the context fixed = ast.fix_missing_locations(ret) codeobj = compile(fixed, '<string>', 'exec') ctx = self._ctx.copy() eval(codeobj, ctx) # Get the function back self._functions[fc_ast.name] = ctx[fc_ast.name] return ctx[fc_ast.name]
def compile_func(arg_names, statements, name='_the_func', debug=False): """Compile a list of statements as the body of a function and return the resulting Python function. If `debug`, then print out the bytecode of the compiled function. """ if six.PY2: name = name.encode('utf-8') args = ast.arguments( args=[ast.Name(n, ast.Param()) for n in arg_names], vararg=None, kwarg=None, defaults=[ex_literal(None) for _ in arg_names], ) else: args_fields = { 'args': [ast.arg(arg=n, annotation=None) for n in arg_names], 'kwonlyargs': [], 'kw_defaults': [], 'defaults': [ex_literal(None) for _ in arg_names], } if 'posonlyargs' in ast.arguments._fields: # Added in Python 3.8. args_fields['posonlyargs'] = [] args = ast.arguments(**args_fields) func_def = ast.FunctionDef( name=name, args=args, body=statements, decorator_list=[], ) # The ast.Module signature changed in 3.8 to accept a list of types to # ignore. if sys.version_info >= (3, 8): mod = ast.Module([func_def], []) else: mod = ast.Module([func_def]) ast.fix_missing_locations(mod) prog = compile(mod, '<generated>', 'exec') # Debug: show bytecode. if debug: dis.dis(prog) for const in prog.co_consts: if isinstance(const, types.CodeType): dis.dis(const) the_locals = {} exec(prog, {}, the_locals) return the_locals[name]
def visit_FunctionDef(self, node): names = self.renamed_args types = [arg.annotation for arg in node.args.args] IO = [] for name, type_ in zip(names, types): self.IO[name + "_0"] = name # Add ssa rename IO.extend([ast.Str(name), self.qualify(type_, "In")]) if isinstance(node.returns, ast.Tuple): for i, elt in enumerate(node.returns.elts): IO.extend([ast.Str(f"O{i}"), self.qualify(elt, "Out")]) else: IO.extend([ast.Str("O"), self.qualify(node.returns, "Out")]) IO = ast.List(IO, ast.Load()) node.body = [self.visit(s) for s in node.body] if isinstance(node.returns, ast.Tuple): for i, elt in enumerate(node.returns.elts): node.body.append(ast.Expr(ast.Call( m_dot("wire"), [ast.Name(f"O{i}", ast.Load()), ast.Attribute(ast.Name("io", ast.Load()), f"O{i}", ast.Load())], [] ))) else: node.body.append(ast.Expr(ast.Call( m_dot("wire"), [ast.Name("O", ast.Load()), ast.Attribute(ast.Name("io", ast.Load()), "O", ast.Load())], [] ))) # class {node.name}(m.Circuit): # IO = {IO} # @classmethod # def definition(io): # {body} class_def = ast.ClassDef( node.name, [ast.Attribute(ast.Name("m", ast.Load()), "Circuit", ast.Load())], [], [ ast.Assign([ast.Name("IO", ast.Store())], IO), ast.FunctionDef( "definition", ast.arguments([ast.arg("io", None)], None, [], [], None, []), node.body, [ast.Name("classmethod", ast.Load())], None )], []) return class_def
def visit_FunctionDef(self, node): assert len(node.decorator_list ) == 1 # we expect that we are the only decorator assert isinstance(node.decorator_list[0], ast.Name) assert node.decorator_list[0].id == "conditional" body = [self.visit(x) for x in node.body] funcdef = ast.FunctionDef(name=node.name, args=node.args, body=body, decorator_list=[]) if "posonlyargs" in node.args._fields: funcdef.posonlyargs = [] return funcdef
def visitFunctionDef(self, n, *args): fargs = self.dispatch(n.args, *args) decorator_list = [ self.dispatch(dec, *args) for dec in n.decorator_list ] if self.examine_functions: body = self.dispatch_scope(n.body, *args) else: body = n.body if flags.PY_VERSION == 3: return ast.FunctionDef(name=n.name, args=fargs, body=body, decorator_list=decorator_list, returns=self.fix(n.returns), lineno=n.lineno) elif flags.PY_VERSION == 2: return ast.FunctionDef(name=n.name, args=fargs, body=body, decorator_list=decorator_list, lineno=n.lineno)
def test_assign_expr_slice(self): assert self.run([ ast.Assign([ast_store('test')], ast.List([ast.Num(x) for x in range(10)], ast.Load())), ast.FunctionDef('f_test', ast.arguments([], None, None, []), [ ast.Return(ast_load('test')), ], []), ast.Assign([ ast.Subscript(ast_call(ast_load('f_test')), ast.Slice(ast.Num(2), ast.Num(8), None), ast.Store()), ], ast.List([ast.Num(42), ast.Num(43)], ast.Load())), ], 'test', list) == [0, 1, 42, 43, 8, 9]
def setUp(self): self.func_node = ast.FunctionDef(name='func', args=ast.arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=[ast.Pass()], decorator_list=[], returns=None) self.func_lister = Function_Visitor() self.func_lister.visit(self.func_node)
def visit_FunctionDef(self, func_def: ast.FunctionDef) -> ActionsT: body = self.visit_stmt_list(func_def.body) for d in func_def.decorator_list: # TODO(zhangwen): this is a hack to specifically allow the `on_coordinator` decorator. if isinstance(d, ast.Name) and d.id == "on_coordinator": # `@on_coordinator` continue if isinstance(d, ast.Attribute) and isinstance(d.value, ast.Name) and d.value.id == "rt" \ and d.attr == "on_coordinator": # `@rt.on_coordinator` continue raise NodeNotSupportedError(d, "Function decorator not supported") return [ast.FunctionDef(name=func_def.name, args=func_def.args, body=body, decorator_list=func_def.decorator_list)]
def generate_function_def(max_depth=None): from control_flow import generate_block name = generate_variable_name() args = _generate_arguments(max_depth=max_depth) body = generate_block(max_depth=max_depth) num_decorators = random.choice([0, 0, 0, 0, 0, 1, 1, 2]) decorator_list = [generate_variable() for _ in range(num_decorators)] returns = random.choice([generate_variable()] + [None] * 4) return ast.FunctionDef(name, args, body, decorator_list, returns)
def visit_Module(self, node): module_body = list() init_body = list() for stmt in node.body: if type(stmt) in ExtractTopLevelStmts.TYPEDEFS: module_body.append(stmt) else: init_body.append(stmt) init = ast.FunctionDef('__init__', ast.arguments([], None, None, []), init_body, []) module_body.append(init) node.body = module_body return node
def buildModel(self, body): args = [ast.arg(name, None) for name in self.data_names] model = ast.FunctionDef(name='model', args=ast.arguments(args=args, vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=body, decorator_list=[], returns=None) return model
def visit_TemplateNode(self, node): _inner_body = [self.visit(x) for x in node.value] body = [ ast.FunctionDef(name='_inner', args=ast.arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=_inner_body, decorator_list=[], returns=None), ast.Return(value=ast.Call(func=ast.Attribute( value=ast.Str(s=''), attr='join', ctx=ast.Load()), args=[ ast.Call(func=ast.Name( id='_inner', ctx=ast.Load()), args=[], keywords=[]) ], keywords=[])) ] tree = ast.Interactive(body=[ ast.FunctionDef(name=self.funcname, args=ast.arguments(args=[ ast.arg(arg=n, annotation=None) for n in set(self.names) ], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=body, decorator_list=[], returns=None) ]) return ast.fix_missing_locations(tree)
def make_function_def(self, body, name): return ast.FunctionDef( name=name, body=body, args=ast.arguments(args=[ ast.arg(arg='self'), ast.arg(arg=CONTEXT_ARG_NAME), ], kwonlyargs=[], kw_defaults=[], defaults=[]), decorator_list=[], )
def write(self, functree): argList = [ ast.copy_location(ast.arg(a, None), functree) for a in self.argNames ] nameExpr = ast.copy_location(ast.Name(functree.name, ast.Load()), functree) returnExpr = ast.copy_location(ast.Return(nameExpr), functree) newfunctree = ast.FunctionDef( '_wrap', ast.arguments(argList, None, [], [], None, []), [functree, returnExpr], [], None) newfunctree = ast.copy_location(newfunctree, functree) return functionFromAst(newfunctree)(*self.argValues)
def make_function(): """FunctionDef(identifier name, arguments args, stmt* body, expr* decorator_list, expr? returns, string? type_comment)""" function_definition = ast.FunctionDef(name="new_function", args=make_arguments(), body=[make_statement()], decorator_list=[], returns=None, type_comment=None) return function_definition
def __build__init(self): super_func = ast.Call(func=ast.Name(id='super', ctx=ast.Load()), args=[], keywords=[]) if (sys.version_info[0], sys.version_info[1]) == (3, 5) or \ (sys.version_info[0], sys.version_info[1]) == (3, 6): super_func = ast.Call( func=ast.Attribute(value=super_func, attr='__init__', ctx=ast.Load()), args=[ast.Starred(value=ast.Name(id='args', ctx=ast.Load()), ctx=ast.Load())], keywords=[], kwargs=ast.Name(id='kwargs', ctx=ast.Load()), ) elif (sys.version_info[0], sys.version_info[1]) == (3,4): super_func = ast.Call( func=ast.Attribute(value=super_func, attr='__init__', ctx=ast.Load()), args=[], keywords=[], starargs=ast.Name(id='args', ctx=ast.Load()), kwargs=ast.Name(id='kwargs', ctx=ast.Load()), ) else: print("Version:", sys.version_info) raise RuntimeError("This script only functions on python 3.4, 3.5 or 3.6. Active python version {}.{}".format(*sys.version_info)) super_init = ast.Expr( value=super_func, lineno = self.__get_line(), col_offset = 0, ) body = [super_init] sig = ast.arguments( args=[ast.arg('self', None)], vararg=ast.arg(arg='args', annotation=None), kwarg=ast.arg(arg='kwargs', annotation=None), varargannotation=None, kwonlyargs=[], kwargannotation=None, defaults=[], kw_defaults=[]) func = ast.FunctionDef( name = "__init__", args = sig, body = body, decorator_list = [], lineno = self.__get_line(), col_offset = 0, ) return func
def visit_FunctionDef(self, node, root=False): if not root: return node new_body = self.generate_interactions(node.args) for external in self.external: new_body.extend( self.make_interaction( target=ast.Name(id=external, ctx=ast.Store()), ann=None, value=ast.Name(id="__ptera_ABSENT", ctx=ast.Load()), orig=node, )) new_args = ast.arguments( posonlyargs=[], args=list(node.args.args), vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[ ast.copy_location( ast.Name(id="__ptera_ABSENT", ctx=ast.Load()), arg) for arg in node.args.args ], ) for dflt, arg in zip(node.args.defaults, node.args.args[-len(node.args.defaults):]): self.defaults[arg.arg] = dflt new_args.args.insert(0, ast.arg("__self__", ast.Constant(None))) first = node.body[0] if isinstance(first, ast.Expr): v = first.value if (isinstance(v, ast.Str) or isinstance(v, ast.Constant) and isinstance(v.value, str)): new_body.insert(0, first) new_body += self.visit_body(node.body) return ast.copy_location( ast.FunctionDef( name=node.name, args=new_args, body=new_body, decorator_list=node.decorator_list, returns=node.returns, ), node, )
def setUp(self): """Define function without args ast-node and list it""" func_node_without_args = ast.FunctionDef(name='func', args=ast.arguments(args=[], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]), body=[ast.Pass()], decorator_list=None, returns=None) self.arg_lister = Arg_Visitor() self.arg_lister.visit(func_node_without_args)
def pyde_preprocessor(module): info = Program_info() # Walk throught the abstract syntax tree for the original sketch. for node in module.body: if isinstance(node, ast.FunctionDef): if (node.name == 'setup'): toremove = [] # The user has defined a setup() function. Look through setup() for calls # to size(), fullScreen(), noSmooth() and smooth(). info.found_setup = True for subNode in node.body: if not isinstance(subNode, ast.Expr): continue if not isinstance(subNode.value, ast.Call): continue func = subNode.value.func if hasattr(func, 'id') and func.id in ('size', 'fullScreen', 'noSmooth', 'smooth', 'pixelDensity'): toremove.append(subNode) setattr(info, func.id, subNode) for n in toremove: node.body.remove(n) elif (node.name == 'settings'): # The user has defined a settings() function. info.found_settings = True if (info.size or info.fullScreen or info.noSmooth or info.smooth): # The user called one of the settings() subfunctions inside setup. if (info.found_settings): # If a settings function was already defined, go through the tree to find it. # Place all of the special function calls inside settings function body. for node in module.body: if (isinstance(node, ast.FunctionDef)): if (node.name == 'settings'): info.insert(node.body) break else: # If a settings function is not defined, we define one and place all of # the special function calls within it. settingsArgs = ast.arguments(args=[], vararg=None, kwarg=None, defaults=[]) settingsFunc = ast.FunctionDef("settings", settingsArgs, [], []) info.insert(settingsFunc.body) # Place the newly defined settings() function within the module body. # It's like it's been there the whole time... module.body.insert(0, settingsFunc)