Python ast.Param() Examples
The following are 19
code examples of ast.Param().
You can vote up the ones you like or vote down the ones you don't like,
and go to the original project or source file by following the links above each example.
You may also want to check out all available functions/classes of the module
ast
, or try the search function
.
Example #1
Source File: checker.py From pyflakes with MIT License | 6 votes |
def NAME(self, node): """ Handle occurrence of Name (which can be a load/store/delete access.) """ # Locate the name in locals / function / globals scopes. if isinstance(node.ctx, ast.Load): self.handleNodeLoad(node) if (node.id == 'locals' and isinstance(self.scope, FunctionScope) and isinstance(node._pyflakes_parent, ast.Call)): # we are doing locals() call in current scope self.scope.usesLocals = True elif isinstance(node.ctx, ast.Store): self.handleNodeStore(node) elif PY2 and isinstance(node.ctx, ast.Param): self.handleNodeStore(node) elif isinstance(node.ctx, ast.Del): self.handleNodeDelete(node) else: # Unknown context raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
Example #2
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 6 votes |
def ctx_to_store(obj, store=ast.Store): if isinstance(obj, list): for i, x in enumerate(obj): obj[i] = ctx_to_store(x, store) return obj elif isinstance(obj, (ast.Attribute, ast.Subscript)): obj.ctx = store() return obj elif isinstance(obj, ast.AST): for attrib in obj._fields: value = getattr(obj, attrib) if isinstance(value, ast.Load): setattr(obj, attrib, store()) elif isinstance(value, ast.Param): setattr(obj, attrib, store()) elif isinstance(value, list): for i, x in enumerate(value): value[i] = ctx_to_store(x, store) elif isinstance(value, ast.AST): setattr(obj, attrib, ctx_to_store(value, store)) return obj else: return obj
Example #3
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_fplist_2(p): '''fplist : fpdef COMMA''' # 1 2 p[0] = ast.Tuple([p[1]], ast.Param(), rule=inspect.currentframe().f_code.co_name, paren=False) inherit_lineno(p[0], p[1])
Example #4
Source File: test_utils.py From pasta with Apache License 2.0 | 5 votes |
def checkAstsEqual(self, a, b): """Compares two ASTs and fails if there are differences. Ignores `ctx` fields and formatting info. """ if a is None and b is None: return try: self.assertIsNotNone(a) self.assertIsNotNone(b) for node_a, node_b in zip(ast.walk(a), ast.walk(b)): self.assertEqual(type(node_a), type(node_b)) for field in type(node_a)()._fields: a_val = getattr(node_a, field, None) b_val = getattr(node_b, field, None) if isinstance(a_val, list): for item_a, item_b in zip(a_val, b_val): self.checkAstsEqual(item_a, item_b) elif isinstance(a_val, ast.AST) or isinstance(b_val, ast.AST): if (not isinstance(a_val, (ast.Load, ast.Store, ast.Param)) and not isinstance(b_val, (ast.Load, ast.Store, ast.Param))): self.assertIsNotNone(a_val) self.assertIsNotNone(b_val) self.checkAstsEqual(a_val, b_val) else: self.assertEqual(a_val, b_val) except AssertionError as ae: self.fail('ASTs differ:\n%s\n !=\n%s\n\n%s' % ( ast.dump(a), ast.dump(b), ae))
Example #5
Source File: scope.py From pasta with Apache License 2.0 | 5 votes |
def visit_Name(self, node): if isinstance(node.ctx, (ast.Store, ast.Param)): self.scope.define_name(node.id, node) elif isinstance(node.ctx, ast.Load): self.scope.lookup_name(node.id).add_reference(node) self.root_scope.set_name_for_node(node, self.scope.lookup_name(node.id)) self.generic_visit(node)
Example #6
Source File: liveness.py From kappa with BSD 2-Clause "Simplified" License | 5 votes |
def visit_FunctionDef(self, func_def: ast.FunctionDef) -> None: self.visit_stmt_list(func_def.body) # The arguments aren't live before the function definition. self._live_vars -= find_variables_by_usage(func_def.args)[ast.Param] for decorator in func_def.decorator_list: self._live_vars |= find_variables_by_usage(decorator)[ast.Load]
Example #7
Source File: cps.py From kappa with BSD 2-Clause "Simplified" License | 5 votes |
def enter_function_scope(self, curr_func: ast.FunctionDef) -> "CPSTransformerContext": """Returns a new context with an updated current function and set of accessible global names.""" if self.curr_func: raise NodeNotSupportedError(curr_func, "Nested functions not supported") new_global_names = set(self.global_names) # Remove global names shadowed in function. vars_by_usage = find_variables_by_usage(curr_func) new_global_names -= vars_by_usage[ast.Param] new_global_names -= vars_by_usage[ast.Store] return CPSTransformerContext(subsequent_stmts=[], subsequent_live_vars=LivenessTracker(), curr_class=self.curr_class, curr_func=curr_func, global_names=new_global_names)
Example #8
Source File: core.py From vulture with MIT License | 5 votes |
def visit_Name(self, node): if ( isinstance(node.ctx, ast.Load) and node.id not in IGNORED_VARIABLE_NAMES ): self.used_names.add(node.id) elif isinstance(node.ctx, (ast.Param, ast.Store)): self._define_variable(node.id, node)
Example #9
Source File: commonast.py From pscript with BSD 2-Clause "Simplified" License | 5 votes |
def _convert_Name(self, n): if pyversion < (3, 4): # pragma: no cover M = {'None': None, 'False': False, 'True': True} if n.id in M: return NameConstant(M[n.id]) # Python < 3.4 if pyversion < (3, ) and isinstance(n.ctx , ast.Param): return Arg(n.id, None, None) return Name(n.id)
Example #10
Source File: hgawk_test.py From histogrammar-python with Apache License 2.0 | 5 votes |
def functionalize(node): # changes node in-place, but returns it anyway if isinstance(node, ast.Call) and isinstance(node.func, ast.Name): for i, x in enumerate(node.args): if (node.func.id, i) in expectFunction: numargs = highestDollar(x) if numargs > 0: # the parameter name "$args" can't conflict with any valid Python names out = ast.Lambda(ast.arguments([ast.Name("$args", ast.Param())], None, None, []), dollarToArg(x)) out.lineno, out.col_offset = x.lineno, x.col_offset out.args.lineno, out.args.col_offset = x.lineno, x.col_offset out.args.args[0].lineno, out.args.args[0].col_offset = x.lineno, x.col_offset out.args.args[0].ctx.lineno, out.args.args[0].ctx.col_offset = x.lineno, x.col_offset node.args[i] = out for keyword in node.keywords: if (node.func.id, keyword.arg) in expectFunction: x = keyword.value numargs = highestDollar(x) if numargs > 0: out = ast.Lambda(ast.arguments([ast.Name("$args", ast.Param())], None, None, []), dollarToArg(x)) out.lineno, out.col_offset = x.lineno, x.col_offset out.args.lineno, out.args.col_offset = x.lineno, x.col_offset out.args.args[0].lineno, out.args.args[0].col_offset = x.lineno, x.col_offset out.args.args[0].ctx.lineno, out.args.args[0].ctx.col_offset = x.lineno, x.col_offset keyword.value = out for field in node._fields: subnode = getattr(node, field) if isinstance(subnode, ast.AST): functionalize(subnode) elif isinstance(subnode, list): for x in subnode: if isinstance(x, ast.AST): functionalize(x) return node
Example #11
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_fplist_star_1(p): '''fplist_star : COMMA fpdef''' # 1 2 p[0] = ast.Tuple([p[2]], ast.Param(), rule=inspect.currentframe().f_code.co_name, paren=False)
Example #12
Source File: namedlist.py From appcompatprocessor with Apache License 2.0 | 5 votes |
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] ######################################################################## # Produce a docstring for the class.
Example #13
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_fpdef_1(p): '''fpdef : NAME''' # 1 p[0] = ast.Name(p[1][0], ast.Param(), rule=inspect.currentframe().f_code.co_name, **p[1][1])
Example #14
Source File: astutil.py From deco with MIT License | 5 votes |
def visit_Expr(self, node): if type(node.value) is ast.Call: call = node.value if self.is_concurrent_call(call): self.encounter_call(call) return node elif any([self.is_concurrent_call(arg) for arg in call.args]): conc_args = [(i, arg) for i, arg in enumerate(call.args) if self.is_concurrent_call(arg)] if len(conc_args) > 1: raise self.not_implemented_error(call, "Functions with multiple @concurrent parameters are unsupported") conc_call = conc_args[0][1] if isinstance(call.func, ast.Attribute): self.arguments.add(SchedulerRewriter.top_level_name(call.func.value)) self.encounter_call(conc_call) call.args[conc_args[0][0]] = ast.Name("__value__", ast.Load()) if sys.version_info >= (3, 0): args = [ast.arg("__value__", None)] else: args = [ast.Name("__value__", ast.Param())] call_lambda = ast.Lambda(ast.arguments(args = args, defaults = [], kwonlyargs = [], kw_defaults = []), call) copy_location_kwargs = { "func": ast.Attribute(conc_call.func, 'call', ast.Load()), "args": [call_lambda] + conc_call.args, "keywords": conc_call.keywords } if(sys.version_info < (3, 0)): copy_location_kwargs["kwargs"] = conc_call.kwargs return copy_location(ast.Expr(ast.Call(**copy_location_kwargs)), node) return self.generic_visit(node)
Example #15
Source File: ast2.py From gast with BSD 3-Clause "New" or "Revised" License | 5 votes |
def visit_arg(self, node): new_node = ast.Name(node.arg, ast.Param()) ast.copy_location(new_node, node) return new_node # arguments
Example #16
Source File: ast2.py From gast with BSD 3-Clause "New" or "Revised" License | 5 votes |
def visit_arguments(self, node): # missing locations for vararg and kwarg set at function level if node.vararg: vararg = ast.Name(node.vararg, ast.Param()) else: vararg = None if node.kwarg: kwarg = ast.Name(node.kwarg, ast.Param()) else: kwarg = None if node.vararg: vararg = ast.Name(node.vararg, ast.Param()) else: vararg = None new_node = gast.arguments( self._visit(node.args), [], # posonlyargs self._visit(vararg), [], # kwonlyargs [], # kw_defaults self._visit(kwarg), self._visit(node.defaults), ) return new_node
Example #17
Source File: checker.py From pyflakes with MIT License | 5 votes |
def handleNodeStore(self, node): name = getNodeName(node) if not name: return # if the name hasn't already been defined in the current scope if isinstance(self.scope, FunctionScope) and name not in self.scope: # for each function or module scope above us for scope in self.scopeStack[:-1]: if not isinstance(scope, (FunctionScope, ModuleScope)): continue # if the name was defined in that scope, and the name has # been accessed already in the current scope, and hasn't # been declared global used = name in scope and scope[name].used if used and used[0] is self.scope and name not in self.scope.globals: # then it's probably a mistake self.report(messages.UndefinedLocal, scope[name].used[1], name, scope[name].source) break parent_stmt = self.getParent(node) if isinstance(parent_stmt, (FOR_TYPES, ast.comprehension)) or ( parent_stmt != node._pyflakes_parent and not self.isLiteralTupleUnpacking(parent_stmt)): binding = Binding(name, node) elif name == '__all__' and isinstance(self.scope, ModuleScope): binding = ExportBinding(name, node._pyflakes_parent, self.scope) elif PY2 and isinstance(getattr(node, 'ctx', None), ast.Param): binding = Argument(name, self.getScopeNode(node)) else: binding = Assignment(name, node) self.addBinding(node, binding)
Example #18
Source File: _transformer.py From hope with GNU General Public License v3.0 | 4 votes |
def _create_signature(self, node): """ Creates a method signature for the given :py:class:`hope._ast.FunctionDef` :param node: instance of the FunctionDef :return signature: a list of :py:class:`hope._ast.Variable` """ signature = [] for name, arg in zip(node.args.args, self.args): if sys.version_info[0] == 2 and isinstance(name, ast.Name): if not isinstance(name.ctx, ast.Param): raise Exception("Invalid Structure") argname = name.id elif sys.version_info[0] == 3 and isinstance(name, ast.arg): if not name.annotation is None: raise Exception("Invalid Structure") argname = name.arg else: raise Exception("Invalid Structure") if isinstance(arg, (Variable, Object)): #TODO: maybe implement prototype pattern. #Overwrite attributes of passed arguments to avoid conflicts self.variables[argname] = copy.copy(arg) self.variables[argname].name = argname self.variables[argname].scope = "signature" elif isinstance(arg, np.ndarray): if arg.dtype.isbuiltin != 1: raise UnsupportedFeatureException("Only Builtin datatypes are supported: in {0} arg {1} has type {2!s}".format(self.fkt.__name__, argname, arg.dtype)) self.variables[argname] = Variable(argname, None, arg.dtype.type, "signature", True) self.variables[argname].shape = [(None, Dimension(self.variables[argname], dim)) for dim in range(len(arg.shape))] elif hasattr(arg, "dtype"): if hasattr(arg.dtype, "type"): self.variables[argname] = Variable(argname, [], arg.dtype.type, "signature", True) else: self.variables[argname] = Variable(argname, [], arg.dtype, "signature", True) elif isinstance(arg, int): self.variables[argname] = Variable(argname, [], int, "signature", True) elif isinstance(arg, float): self.variables[argname] = Variable(argname, [], float, "signature", True) else: self.variables[argname] = Object(argname, arg) signature.append(self.variables[argname]) return signature
Example #19
Source File: sembuilder.py From miasm with GNU General Public License v2.0 | 4 votes |
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]