本文整理匯總了Python中ast.Call方法的典型用法代碼示例。如果您正苦於以下問題:Python ast.Call方法的具體用法?Python ast.Call怎麽用?Python ast.Call使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類ast
的用法示例。
在下文中一共展示了ast.Call方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: visit_For
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def visit_For(self, node):
node.breakLabel = _Label("BREAK")
node.loopLabel = _Label("LOOP")
self.labelStack.append(node.breakLabel)
self.labelStack.append(node.loopLabel)
self.refStack.push()
self.visit(node.target)
var = node.target.id
self.tree.vardict[var] = int(-1)
cf = node.iter
self.visit(cf)
self.require(node, isinstance(cf, ast.Call), "Expected (down)range call")
f = self.getObj(cf.func)
self.require(node, f in (range, downrange), "Expected (down)range call")
for stmt in node.body:
self.visit(stmt)
self.refStack.pop()
self.require(node, not node.orelse, "for-else not supported")
self.labelStack.pop()
self.labelStack.pop()
示例2: compile_expression
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def compile_expression(exp):
cached = cache.get(exp)
if cached is not None:
return cached
_exp = ast.parse(exp)
nodes = [node for node in ast.walk(_exp)]
if len(nodes) < 2 or not isinstance(nodes[1], ast.Expr):
raise ExpressionError("%s is not Expression" % exp)
for node in nodes:
if isinstance(node, ast.Call):
raise ExpressionError("Call method is forbidden")
if isinstance(node, ast.Lambda):
raise ExpressionError("Lambda is strongly forbidden")
result = compile(exp, '<string>', mode='eval')
cache[exp] = result
return result
示例3: pre_Assign
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def pre_Assign(self):
value = self.cur_node.value
for target in self.cur_node.targets:
# ignore Subscripts for now
if isinstance(target, _ast.Name):
if target.id == '_target_model':
assert(isinstance(value, _ast.Call))
# done in three separate phases for safety with stringly-typed arguments
StateSpaceLabelWalker(value.args[0])
LabelWalker(value.args[1])
ExpressionWalker(value.args[2])
else:
LabelWalker(value)
# Build dependencies from the rhs
# We don't consider the function name a dependency so bypass that
critical_ast = value.args if isinstance(value, _ast.Call) else value
value._dependencies = DependencyWalker(critical_ast).dependencies
self.upgrades[target.id] = value
return False
# Build variables for filling in the template
示例4: _apply_imports_to_unparsed_expression
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def _apply_imports_to_unparsed_expression(exp_ast, imports):
"""Attempts to evaluate the code equivalent of `exp_ast`, with `imports`
as available symbols. If it's successful, it returns the evaluated object.
Otherwise this returns the unparsed code for `exp_ast`.
Args:
* exp_ast (Union[ast.Name, ast.Attribute, ast.Call]) - The expression to
evaluate.
* imports (Dict[str, object]) - The symbols to include during the evaluation
of `exp_ast`.
Returns the evaluation of `exp_ast` if it can successfully evaluate with
`imports`. Otherwise this returns the source-code representation of exp_ast as
a string.
"""
assert isinstance(exp_ast, (ast.Name, ast.Attribute, ast.Call)), type(exp_ast)
unparsed = _unparse(exp_ast).strip()
try:
return eval(unparsed, {'__builtins__': None}, imports)
except (NameError, AttributeError):
return unparsed
示例5: visit_Expr
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def visit_Expr(self, node):
newnode = ast.copy_location(ast.Expr(
value = ast.Call(
func = ast.Attribute(
value = ast.Name(id='__swirlypy_recorder__',
ctx=ast.Load()),
attr="record",
ctx=ast.Load()),
args=[node.value],
keywords=[],
starargs=None,
kwargs=None
)
),
node)
ast.fix_missing_locations(newnode)
return newnode
示例6: ensure_ctx_var
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def ensure_ctx_var(self, coro):
d_list = []
for d in coro.decorator_list:
if isinstance(d, ast.Attribute):
d_list.append(d.attr)
elif isinstance(d, ast.Call):
if isinstance(d.func, ast.Attribute):
d_list.append(d.func.attr)
if 'command' not in d_list:
return coro
coro_args = [arg.arg for arg in coro.args.args]
if not coro_args:
coro.args.args.append(ast.arg(arg='ctx', annotation=None))
elif 'self' in coro_args and 'ctx' not in coro_args:
coro.args.args.insert(1, ast.arg(arg='ctx', annotation=None))
elif 'self' not in coro_args and 'ctx' not in coro_args:
coro.args.args.insert(0, ast.arg(arg='ctx', annotation=None))
stats_counter['coro_changes'] += 1
return coro
示例7: NAME
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
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, ast.AugLoad)):
self.handleNodeLoad(node)
if (node.id == 'locals' and isinstance(self.scope, FunctionScope)
and isinstance(node.parent, ast.Call)):
# we are doing locals() call in current scope
self.scope.usesLocals = True
elif isinstance(node.ctx, (ast.Store, ast.AugStore)):
self.handleNodeStore(node)
elif isinstance(node.ctx, ast.Del):
self.handleNodeDelete(node)
else:
# must be a Param context -- this only happens for names in function
# arguments, but these aren't dispatched through here
raise RuntimeError("Got impossible expression context: %r" % (node.ctx,))
示例8: visit_With
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def visit_With(self, node):
parallel_tasks = []
if len(node.items) > 1:
raise ValueError(
"Only a single context is supported in 'with' statements inside the action."
)
context = eval(
compile(ast.Expression(node.items[0].context_expr), "", "eval"),
self._globals,
)
if context.__calm_type__ == "parallel":
for statement in node.body:
if not isinstance(statement.value, ast.Call):
raise ValueError(
"Only calls to 'CalmTask' methods supported inside parallel context."
)
task = self.visit_Call(statement.value, return_task=True)
if task:
parallel_tasks.append(task)
self.all_tasks.append(task)
self.task_list.append(parallel_tasks)
else:
raise ValueError(
"Unsupported context used in 'with' statement inside the action."
)
示例9: visit_BoolOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def visit_BoolOp(self, node, **kwargs):
def visitor(x, y):
lhs = self._try_visit_binop(x)
rhs = self._try_visit_binop(y)
op, op_class, lhs, rhs = self._maybe_transform_eq_ne(
node, lhs, rhs)
return self._maybe_evaluate_binop(op, node.op, lhs, rhs)
operands = node.values
return reduce(visitor, operands)
# ast.Call signature changed on 3.5,
# conditionally change which methods is named
# visit_Call depending on Python version, #11097
示例10: visit_BinOp
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def visit_BinOp(self, node):
'Replace bitwise operation with function call'
self.generic_visit(node)
if isinstance(node.op, ast.BitAnd):
return ast.Call(ast.Name('mand', ast.Load()),
[node.left, node.right], [], None, None)
if isinstance(node.op, ast.BitOr):
return ast.Call(ast.Name('mor', ast.Load()),
[node.left, node.right], [], None, None)
if isinstance(node.op, ast.BitXor):
return ast.Call(ast.Name('mxor', ast.Load()),
[node.left, node.right], [], None, None)
if isinstance(node.op, ast.LShift):
return ast.Call(ast.Name('mlshift', ast.Load()),
[node.left, node.right], [], None, None)
if isinstance(node.op, ast.RShift):
return ast.Call(ast.Name('mrshift', ast.Load()),
[node.left, node.right], [], None, None)
return node
示例11: _find_sub_setup_call
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def _find_sub_setup_call(
self, elements
): # type: (List[Any]) -> Tuple[Optional[ast.Call], Optional[List[Any]]]
for element in elements:
if not isinstance(element, (ast.FunctionDef, ast.If)):
continue
setup_call = self._find_setup_call(element.body)
if setup_call != (None, None):
setup_call, body = setup_call
body = elements + body
return setup_call, body
return None, None
示例12: atomic_type_literal_ast_to_ir2
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def atomic_type_literal_ast_to_ir2(ast_node: ast.Call,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str]):
if ast_node.keywords:
raise CompilationError(compilation_context, ast_node.keywords[0].value,
'Keyword arguments are not supported in Type()')
if len(ast_node.args) != 1:
raise CompilationError(compilation_context, ast_node, 'Type() takes 1 argument. Got: %s' % len(ast_node.args))
[arg] = ast_node.args
if not isinstance(arg, ast.Str):
raise CompilationError(compilation_context, arg, 'The argument passed to Type should be a string constant.')
_check_atomic_type(arg, compilation_context)
return ir2.AtomicTypeLiteral(cpp_type=arg.s)
示例13: _extract_single_type_expr_arg
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def _extract_single_type_expr_arg(ast_node: ast.Call,
called_fun_name: str,
compilation_context: CompilationContext,
in_match_pattern: bool,
check_var_reference: Callable[[ast.Name], None],
match_lambda_argument_names: Set[str],
current_stmt_line: int):
if ast_node.keywords:
raise CompilationError(compilation_context, ast_node.keywords[0].value,
'Keyword arguments are not supported in %s()' % called_fun_name)
if len(ast_node.args) != 1:
raise CompilationError(compilation_context, ast_node, '%s() takes 1 argument. Got: %s' % (called_fun_name, len(ast_node.args)))
[arg] = ast_node.args
arg_ir = expression_ast_to_ir2(arg,
compilation_context,
in_match_pattern,
check_var_reference,
match_lambda_argument_names,
current_stmt_line)
if arg_ir.expr_type != ir2.TypeType():
raise CompilationError(compilation_context, arg, 'The argument passed to %s() should have type Type, but was: %s' % (called_fun_name, str(arg_ir.expr_type)))
return arg_ir
示例14: test_dump
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def test_dump(self):
node = ast.parse('spam(eggs, "and cheese")')
self.assertEqual(ast.dump(node),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load()), "
"args=[Name(id='eggs', ctx=Load()), Str(s='and cheese')], "
"keywords=[], starargs=None, kwargs=None))])"
)
self.assertEqual(ast.dump(node, annotate_fields=False),
"Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), "
"Str('and cheese')], [], None, None))])"
)
self.assertEqual(ast.dump(node, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='spam', ctx=Load(), "
"lineno=1, col_offset=0), args=[Name(id='eggs', ctx=Load(), "
"lineno=1, col_offset=5), Str(s='and cheese', lineno=1, "
"col_offset=11)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0)])"
)
示例15: node_query
# 需要導入模塊: import ast [as 別名]
# 或者: from ast import Call [as 別名]
def node_query(self, node):
"""
Return the query for the gql call node
"""
if isinstance(node, ast.Call):
assert node.args
arg = node.args[0]
if not isinstance(arg, ast.Str):
return
else:
raise TypeError(type(node))
return arg.s