def visit_ExceptHandler(self, node): print(" in MyTransformer.visit_ExceptHandler()") print(" node =", node) # print(" type =", node.type.id) # print(" name =", node.name) # print(" body =", node.body) # create new node | ExceptHandler(expr? type, identifier? name, stmt* body) new_node = node new_body_dict = { 1: [ast.Pass], 2: [ast.Return(ast.Num(1))], 3: [ast.Return(ast.Num(0))], 4: [ast.Continue], 5: [ast.Return(ast.NameConstant(True))], 6: [ast.Return(ast.NameConstant(False))], 7: [ast.Break] } # change how the except block handles the exception (by doing stupid stuff) new_node.body = new_body_dict[random.randint(1, (len(new_body_dict) - 1))] # print(" new body =", new_node.body[0]) ast.copy_location(new_node, node) ast.fix_missing_locations(new_node) return new_node
def fn_defining_assertions(fn, suppress_definition=False): #print('def expectation', fn) #print(unparse(fn)) args = fn_args(fn) assertions = [ ast.FunctionDef(name=fn.name + '_isfunc', args=ast.arguments(args=[], defaults=[], vararg='', kwarg='', kwonlyargs=[]), body=[ ast.Return(value=astcall(ast.Name( id='isfunc'), ast.Name(id=fn.name))) ], decorator_list=[], returns=None), ] if ch_option_true(fn, 'use_definition', True) and not suppress_definition: varnamerefs = [ast.Name(id=a.arg) for a in args] call_by_name = astcall(ast.Name(id=fn.name), *varnamerefs) eq_expr = astcall(ast.Name(id='_z_eq'), call_by_name, fn_expr(fn)) assertions.append( ast.FunctionDef( name=fn.name + '_definition', args=remove_annotations(fn.args), body=[ ast.Return(value=astcall(ast.Name( id='_z_wrapbool'), eq_expr)) ], decorator_list=[], returns=astcall(ast.Name(id='istrue')), )) return assertions
def visit_Return(self, node): if type(node.value) == ast.IfExp: return self.visit( ast.If(test=node.value.test, body=[ast.Return(value=node.value.body)], orelse=[ast.Return(value=node.value.orelse)])) return node
def emit_def(name, args, body, scope): scope['callables'][name.v] = None args = [ast.arg(arg=a.v, annotation=None) for a in args.v] args = ast.arguments(args=args, vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[]) body = list(eat((c for c in body.v), [], scope)) if isinstance(body[-1], ast.If): body.append(ast.Return(value=ast.Name(id=',', ctx=ast.Load()))) else: lastv = body.pop() if isinstance(lastv, ast.Expr): lastv = lastv.value body.append(ast.Return(value=lastv)) yield ast.fix_missing_locations( ast.FunctionDef(name=name.v, args=args, body=body, decorator_list=[]))
def visit_IfExp_Rec(self, node: Type[ast.AST]) -> List[Type[ast.AST]]: return [ ast.If( test=node.test, body=[ast.Return(value=node.body)], orelse=self.visit_IfExp_Rec(node.orelse) if type(node.orelse) == ast.IfExp else [ast.Return(value=node.orelse)]) ]
def visit_Return(self, node): if is_recursive(node, self.name): #substitute the recursive call with accum, store the call in self.outer ast.NodeTransformer.generic_visit(self, node) self.outer.args.append(node.value) return ast.Return(value=self.outer) else: return ast.Return(value=self.make_Name('accum', ast.Load()))
def p_jump_statement_2(self, p): """ jump_statement : RETURN expression_opt SEMI """ if len(p) == 3: p[0] = ast.Return(None, coord=self._token_coord(p, 1)) else: p[0] = ast.Return(p[2], coord=self._token_coord(p, 1))
def return_stmt(self) -> ast.Return: toks = TokenList() if not toks.add(self.match(token.RETURN)): raise Exception("Expected return statement.") if toks.add(self.match(token.SEMICOLON)): return ast.Return(toks) expr = self.parse_commata_expressions() toks.add(self.match(token.SEMICOLON)) return ast.Return(toks, expr)
def test_return_optional_value(self): the_ast = ast.Return(lineno=0, col_offset=0) self.schema.verify(the_ast) the_ast = ast.Return(value=None, lineno=0, col_offset=0) self.schema.verify(the_ast) the_ast = ast.Return(value=ast.Name(id='x', ctx=ast.Load(), lineno=0, col_offset=0), lineno=0, col_offset=0) self.schema.verify(the_ast) # should not raise
def astForCReturn(funcEnv, stmnt): assert isinstance(stmnt, CReturnStatement) if not stmnt.body: assert isSameType(funcEnv.globalScope.stateStruct, funcEnv.func.type, CVoidType()) return ast.Return(value=None) assert isinstance(stmnt.body, CStatement) valueAst, valueType = astAndTypeForCStatement(funcEnv, stmnt.body) returnValueAst = getAstNode_newTypeInstance(funcEnv.interpreter, funcEnv.func.type, valueAst, valueType) return ast.Return(value=returnValueAst)
def _execute_comprehension( self, node: Union[ast.ListComp, ast.SetComp, ast.GeneratorExp, ast.DictComp] ) -> Any: """Compile the generator or comprehension from the node and execute the compiled code.""" # Please see "NOTE ABOUT NAME 🠒 VALUE STACKING". if any(value is PLACEHOLDER for value in self._name_to_value.values()): return PLACEHOLDER args = [ ast.arg(arg=name, annotation=None) for name in sorted(self._name_to_value.keys()) ] if sys.version_info < (3, ): raise NotImplementedError( "Python versions below 3 not supported, got: {}".format( sys.version_info)) if sys.version_info < (3, 8): func_def_node = ast.FunctionDef(name="generator_expr", args=ast.arguments(args=args, kwonlyargs=[], kw_defaults=[], defaults=[]), decorator_list=[], body=[ast.Return(node)]) module_node = ast.Module(body=[func_def_node]) else: func_def_node = ast.FunctionDef(name="generator_expr", args=ast.arguments(args=args, posonlyargs=[], kwonlyargs=[], kw_defaults=[], defaults=[]), decorator_list=[], body=[ast.Return(node)]) module_node = ast.Module(body=[func_def_node], type_ignores=[]) ast.fix_missing_locations(module_node) code = compile(source=module_node, filename='<ast>', mode='exec') module_locals = {} # type: Dict[str, Any] module_globals = {} # type: Dict[str, Any] exec(code, module_globals, module_locals) # pylint: disable=exec-used generator_expr_func = module_locals["generator_expr"] return generator_expr_func(**self._name_to_value)
def insert_return(self, body): stmt = body[-1] if isinstance(stmt, ast.Pass): return_none_node = ast.Return(value=ast.Name(id='None')) ast.copy_location(return_none_node, stmt) body[-1] = return_none_node elif not isinstance(stmt, ast.Return): assert not isinstance(stmt, (ast.For, ast.While)) lineno = get_max_lineno(body) + 1 return_none_node = ast.Return(value=ast.Name(id='None', lineno=lineno), lineno=lineno) #fix col_offset!! body.append(return_none_node)
def p_return_stmt(p): '''return_stmt : TAG_RETURN | TAG_RETURN testlist''' item = p.get_item(1) if len(p) == 2: p[0] = ast.Return(lineno=item.lineno, col_offset=item.lexpos) elif len(p) == 3: p[0] = ast.Return(value=p[2], lineno=item.lineno, col_offset=item.lexpos) return
def visit_Return(self, node): # blue returns are never transformed; # green returns are transformed UNLESS: # (a) they return a constant primitive value (num, string, bool, nothing) # (b) they return a variable reference (with no other expression) if not coloring.is_green(node): return v = node.value if v == None: return node if isinstance(v, ast.Num) or isinstance(v, ast.Str) or isinstance( v, ast.NameConstant): return node if isinstance(v, ast.Name) and isinstance(v.ctx, ast.Load): return node # return <expr> becomes: # ... # env[var] = transform[<expr>] # return env[var] # tmpVar = gensym.gensym("ret") # node.value may contain green nodes, so we need to visit it retvalue = self.visit(node.value) if isinstance(retvalue, ast.Name) and isinstance( retvalue.ctx, ast.Load): # no further transform needed replacement = ast.copy_location(ast.Return(value=retvalue), node) replacement.color = True return replacement # we got something more than a name (some sort of expression). # lift it out to an assignment. lifted = ast.Assign(targets=[ast.Name(id=tmpVar, ctx=ast.Store())], value=retvalue) ast.copy_location(lifted, node) # I think we don't need to color this because whatever green # is in there has been lifted? Need to prove/disprove this. self.pre_statements.append(lifted) replacement = ast.Return(value=ast.Name(id=tmpVar, ctx=ast.Load())) ast.copy_location(replacement, node) replacement.color = True return replacement
def visit_FunctionDef(self, node): self.yield_points = self.passmanager.gather(YieldPoints, node) map(self.visit, node.body) # Look for nodes that have no successors for n in self.cfg.predecessors(None): if type(n) not in (ast.Return, ast.Raise): if self.yield_points: node.body.append(ast.Return(None)) else: none = ast.Attribute(ast.Name("__builtin__", ast.Load()), 'None', ast.Load()) node.body.append(ast.Return(none)) break return node
def onFunctionBodyParsing(self, module_name, function_name, body): config = self.config.get(module_name) if not config: return context = {} context_code = config.get("context", "") if type(context_code) in (tuple, list): context_code = "\n".join(context_code) # We trust the yaml files, pylint: disable=eval-used,exec-used context_ready = not bool(context_code) for change_function_name, replace_code in ( config.get("change_function") or {} ).items(): if function_name != change_function_name: continue if not context_ready: exec(context_code, context) context_ready = True try: replacement = eval(replace_code, context) except Exception as e: # pylint: disable=broad-except self.sysexit( "Error, cannot evaluate code '%s' in '%s' due to: %s" % (replace_code, context_code, e) ) # Single node is required, extrace the generated module body with # single expression only statement value or a function body. replacement = ast.parse(replacement).body[0] if type(replacement) is ast.Expr: if type(replacement.value) is ast.Lambda: body[:] = [ast.Return(replacement.value.body)] else: body[:] = [ast.Return(replacement.value)] else: body[:] = replacement.body self.info( "Updated module '%s' function '%s'." % (module_name.asString(), function_name) )
def prefix_vars(astnode, prefix): if isinstance(astnode, ast.BoolOp): return ast.BoolOp(astnode.op, [prefix_vars(i, prefix) for i in astnode.values], []) elif isinstance(astnode, ast.BinOp): return ast.BinOp(prefix_vars(astnode.left, prefix), astnode.op, prefix_vars(astnode.right, prefix)) elif isinstance(astnode, ast.UnaryOp): return ast.UnaryOp(astnode.op, prefix_vars(astnode.operand, prefix)) elif isinstance(astnode, ast.Call): return ast.Call(prefix_vars(astnode.func, prefix), [prefix_vars(i, prefix) for i in astnode.args], astnode.keywords) elif isinstance(astnode, ast.Compare): return ast.Compare( prefix_vars(astnode.left, prefix), astnode.ops, [prefix_vars(i, prefix) for i in astnode.comparators]) elif isinstance(astnode, ast.Name): if astnode.id in {'And', 'Or', 'Not'}: return ast.Name('z3.%s' % (astnode.id), astnode.ctx) else: return ast.Name('%s%s' % (prefix, astnode.id), astnode.ctx) elif isinstance(astnode, ast.Return): return ast.Return(prefix_vars(astnode.value, env)) else: return astnode
def rename_variables(astnode, env): if isinstance(astnode, ast.BoolOp): fn = 'z3.And' if isinstance(astnode.op, ast.And) else 'z3.Or' return ast.Call(ast.Name(fn, None), [rename_variables(i, env) for i in astnode.values], []) elif isinstance(astnode, ast.BinOp): return ast.BinOp(rename_variables(astnode.left, env), astnode.op, rename_variables(astnode.right, env)) elif isinstance(astnode, ast.UnaryOp): if isinstance(astnode.op, ast.Not): return ast.Call(ast.Name('z3.Not', None), [rename_variables(astnode.operand, env)], []) else: return ast.UnaryOp(astnode.op, rename_variables(astnode.operand, env)) elif isinstance(astnode, ast.Call): return ast.Call(astnode.func, [rename_variables(i, env) for i in astnode.args], astnode.keywords) elif isinstance(astnode, ast.Compare): return ast.Compare( rename_variables(astnode.left, env), astnode.ops, [rename_variables(i, env) for i in astnode.comparators]) elif isinstance(astnode, ast.Name): if astnode.id not in env: env[astnode.id] = 0 num = env[astnode.id] return ast.Name('_%s_%d' % (astnode.id, num), astnode.ctx) elif isinstance(astnode, ast.Return): return ast.Return(rename_variables(astnode.value, env)) else: return astnode
def create_store_return_from_function(f_name, lineno, col_offset): """ Creates the return source code of any type inference function :param f_name: :param lineno: :param col_offset: :return: """ set_type_of_comment = create_src_comment( "Storing the return type of function '{0}' in the type store".format( f_name), ) set_type_of_method = core_language.create_attribute( default_module_type_store_var_name, "store_return_type_of_current_context") return_val_instr, val_var = create_get_type_of( default_function_ret_var_name, lineno, col_offset) set_type_of_call = functions.create_call_expression( set_type_of_method, [val_var]) return_comment = create_src_comment( "Return type of the function '{0}'".format(f_name)) return_ = ast.Return() return_.value = val_var context_unset = functions.create_context_unset() return flatten_lists(create_blank_line(), set_type_of_comment, return_val_instr, set_type_of_call, create_blank_line(), context_unset, create_blank_line(), return_comment, return_)
def leaf(val: float, useExpression: bool) -> ast.Return: """Creates AST nodes for a leaf""" res = astNum(n=val) if not useExpression: return ast.Return(value=res) else: return res
def add_dict_field_definition(self, resource, module_name): base_class = ast.Name(id="marshmallow.fields.Dict") # Define the base class class_node = ast.ClassDef( name=resource.name + "Field", bases=[base_class], keywords=[], decorator_list=[], body=[], ) class_node.body.append( ast.FunctionDef( name="_deserialize", args=ast.arguments( args=[ ast.arg(arg="self", annotation=None), ast.arg(arg="value", annotation=None), ast.arg(arg="attr", annotation=None), ast.arg(arg="data", annotation=None), ], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=ast.arg(arg="kwargs", annotation=None), defaults=[], ), body=[ ast.Assign( targets=[ast.Name(id="result")], value=ast.Call( func=ast.Attribute( value=ast.Call(func=ast.Name(id="super"), args=[], keywords=[]), attr="_deserialize", ), args=[ ast.Name(id="value"), ast.Name(id="attr"), ast.Name(id="data"), ], kwarg=ast.arg(arg="kwargs", annotation=None), keywords=[], ), ), ast.Return(value=ast.Call( func=ast.Name(id="types." + resource.name), args=[], keywords=[ ast.keyword(arg=None, value=ast.Name(id="result")) ], )), ], decorator_list=[], returns=None, )) self._field_nodes[module_name].append(class_node)
def translate(self): """Compile the template to a Python function.""" expressions, varnames, funcnames = self.expr.translate() argnames = [] for varname in varnames: argnames.append(VARIABLE_PREFIX + varname) for funcname in funcnames: argnames.append(FUNCTION_PREFIX + funcname) func = compile_func( argnames, [ast.Return(ast.List(expressions, ast.Load()))], ) def wrapper_func(values={}, functions={}): args = {} for varname in varnames: args[VARIABLE_PREFIX + varname] = values[varname] for funcname in funcnames: args[FUNCTION_PREFIX + funcname] = functions[funcname] parts = func(**args) return ''.join(parts) return wrapper_func
def CmpOp_In(self, left, comparator): self.visit(ast_call( ast.FunctionDef( '', ast.arguments([ast_load('l'), ast_load('c')], None, None, []), [ ast.Return( ast.IfExp( ast_call( ast_load('Array.isArray'), ast_load('c'), ), ast.Compare( ast_call( ast_load('Array.prototype.indexOf.call'), ast_load('c'), ast_load('l'), ), [ast.Gt()], [ast.Num(-1)] ), ast_call( ast_load('JS'), ast.Str("l in c"), ) ) ) ], [] ), left, comparator ))
def constification(func, spec): # takes an ast.Function node and a list of expressions # and replaces the node with a stmt which when executed will define # the function and closureify the expressions cvars = [] for east in spec: cn = 'ClOsUrE%i__' % c_no() replace_ast(func.code, east, cn) cvars.append((cn, east)) func_name = func.name subfunc = ast.Function(func.decorators, func_name, func.argnames, func.defaults, func.flags, func.code, func.lineno) stmt = [ ast.Assign([ast.AssName(cn, 'OP_ASSIGN')], east) for cn, east in cvars ] stmt.append(subfunc) stmt.append(ast.Return(ast.Name(func_name, 0))) fn = 'CoNsTiFiCaTiOn%i' % f_no() dfunc = ast.Function(None, fn, (), (), 0, ast.Stmt(stmt), 0) creat = ast.Stmt( [dfunc, ast_Assign(func_name, ast.CallFunc(ast.Name(fn, 0), ()))]) # XXX add del(CoNsTiFiCaTiOn) make_copy(func, creat)
def assertions_as_score(scoring_fn): """ Create a scoring function from a multi-assert test, allotting one point per successful assertion. Nota bene: if used in conjunction with other decorators, must be the first decorator applied to the function. """ score_var_name = '__score__' function_source = inspect.getsource(scoring_fn) fn_ast, = ast.parse(function_source).body fn_ast.body.insert( 0, ast.Assign(targets=[ast.Name(id=score_var_name, ctx=ast.Store())], value=ast.Num(n=0))) fn_ast.body.append( ast.Return(value=ast.Name(id=score_var_name, ctx=ast.Load()))) fn_ast.decorator_list = [] assertion_replacer = AssertionReplacer(score_var_name) fn_ast = assertion_replacer.visit(fn_ast) code = compile(ast.fix_missing_locations(ast.Module(body=[fn_ast])), '<string>', 'exec') context = {} exec(code, scoring_fn.__globals__, context) new_scoring_fn, = context.values() new_scoring_fn.__max_score = assertion_replacer.max_score return functools.wraps(scoring_fn)(new_scoring_fn)
def _create_repr_method(self): """Create the __repr__ method. def __repr__(self) -> str: return ('Attribute(name=%r, value=%r)' % (self.name, self.value)) """ node = ast.FunctionDef( name="__repr__", args=ast.arguments( args=[ast.arg(arg="self", annotation=None)], vararg=None, kwonlyargs=[], kw_defaults=[], kwarg=None, defaults=[], ), body=[], decorator_list=[], returns=ast.Name(id="str"), ) node.body.append( ast.Return(value=ast.BinOp( left=ast.Str(s="%s(%s)" % ( self.resource.name, ", ".join(f"{attr}=%r" for attr in self.attribute_names), )), op=ast.Mod(), right=ast.Tuple(elts=[ ast.Name(id=f"self.{attr}") for attr in self.attribute_names ]), ))) return node
def visit_While(self, node): self.generic_visit(node) tFun_name = self.freshName("cond") bFun_name = self.freshName("body") tFun = ast.FunctionDef(name=tFun_name, args=ast.arguments(args=[], vararg=None, kwonlyargs=[], kwarg=None, defaults=[], kw_defaults=[]), body=[ast.Return(node.test)], decorator_list=[]) bFun = ast.FunctionDef(name=bFun_name, args=ast.arguments(args=[], vararg=None, kwonlyargs=[], kwarg=None, defaults=[], kw_defaults=[]), body=node.body, decorator_list=[]) ast.fix_missing_locations(tFun) ast.fix_missing_locations(bFun) new_node = ast.Expr(ast.Call( func=ast.Name(id='_while', ctx=ast.Load()), args=[ast.Name(id=tFun_name, ctx=ast.Load()), ast.Name(id=bFun_name, ctx=ast.Load()), ], keywords=[] )) ast.fix_missing_locations(new_node) mod = [tFun, bFun, new_node] return mod
def visit_AnyComp(self, node, comp_type, *path): node.elt = self.visit(node.elt) name = "{0}_comprehension{1}".format(comp_type, self.count) self.count += 1 args = self.passmanager.gather(ImportedIds, node, self.ctx) self.count_iter = 0 starget = "__target" body = reduce( self.nest_reducer, reversed(node.generators), ast.Expr( ast.Call( reduce(lambda x, y: ast.Attribute(x, y, ast.Load()), path[1:], ast.Name(path[0], ast.Load())), [ast.Name(starget, ast.Load()), node.elt], [], None, None))) # add extra metadata to this node metadata.add(body, metadata.Comprehension(starget)) init = ast.Assign([ast.Name(starget, ast.Store())], ast.Call( ast.Attribute( ast.Name('__builtin__', ast.Load()), comp_type, ast.Load()), [], [], None, None)) result = ast.Return(ast.Name(starget, ast.Load())) sargs = sorted(ast.Name(arg, ast.Param()) for arg in args) fd = ast.FunctionDef(name, ast.arguments(sargs, None, None, []), [init, body, result], []) self.ctx.module.body.append(fd) return ast.Call(ast.Name(name, ast.Load()), [ast.Name(arg.id, ast.Load()) for arg in sargs], [], None, None) # no sharing !
def _make_fn(name, chain_fn, args, defaults): args_with_self = ['_self'] + list(args) arguments = [_ast.Name(id=arg, ctx=_ast.Load()) for arg in args_with_self] defs = [_ast.Name(id='_def{0}'.format(idx), ctx=_ast.Load()) for idx, _ in enumerate(defaults)] if _PY2: parameters = _ast.arguments(args=[_ast.Name(id=arg, ctx=_ast.Param()) for arg in args_with_self], defaults=defs) else: parameters = _ast.arguments(args=[_ast.arg(arg=arg) for arg in args_with_self], kwonlyargs=[], defaults=defs, kw_defaults=[]) module_node = _ast.Module(body=[_ast.FunctionDef(name=name, args=parameters, body=[_ast.Return(value=_ast.Call(func=_ast.Name(id='_chain', ctx=_ast.Load()), args=arguments, keywords=[]))], decorator_list=[])]) module_node = _ast.fix_missing_locations(module_node) # compile the ast code = compile(module_node, '<string>', 'exec') # and eval it in the right context globals_ = {'_chain': chain_fn} locals_ = dict(('_def{0}'.format(idx), value) for idx, value in enumerate(defaults)) eval(code, globals_, locals_) # extract our function from the newly created module return locals_[name]
def visit_Return(self, node): # Do not modify valueless returns if node.value is None: return node # Otherwise, replace the return with a yield & valueless return return ast.If( test=ast.NameConstant( value=True, # if True; aka unconditional, will be optimized out lineno=node.lineno, col_offset=node.col_offset ), body=[ # yield the value to be returned ast.Expr( value=ast.Yield( value=node.value, lineno=node.lineno, col_offset=node.col_offset ), lineno=node.lineno, col_offset=node.col_offset ), # return valuelessly ast.Return( value=None, lineno=node.lineno, col_offset=node.col_offset ) ], orelse=[], lineno=node.lineno, col_offset=node.col_offset )