本文整理汇总了Python中ast.copy_location函数的典型用法代码示例。如果您正苦于以下问题:Python copy_location函数的具体用法?Python copy_location怎么用?Python copy_location使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了copy_location函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: visit_Assert
def visit_Assert(self, node): # pylint: disable=invalid-name, no-self-use
"""Visit Assert
Convert BinOp asserts into unittest's assert equals
"""
args = [node.test]
assertion = 'assertTrue'
if isinstance(node.test, ast.Compare) and len(node.test.ops) == 1:
args = [node.test.left, node.test.comparators[0]]
assertion = {
ast.Eq: 'assertEqual',
ast.NotEq: 'assertNotEqual',
ast.Lt: 'assertLess',
ast.LtE: 'assertLessEqual',
ast.Gt: 'assertGreater',
ast.GtE: 'assertGreaterEqual',
ast.Is: 'assertIs',
ast.IsNot: 'assertIsNot',
ast.In: 'assertIn',
ast.NotIn: 'assertNotIn',
}[node.test.ops[0].__class__]
if node.msg:
args.append(node.msg)
return ast.copy_location(ast.Expr(ast.copy_location(call(
ast.copy_location(ast.Attribute(
ast.Name('self', ast.Load()),
assertion, ast.Load()
), node),
args
), node)), node)
示例2: visit_Name
def visit_Name(self, old_node):
node = nodes.Name(old_node.id, old_node.ctx)
ast.copy_location(node, old_node)
# Set some defaults
node.cf_maybe_null = True
node.cf_is_null = False
node.allow_null = False
node.name = node.id
if isinstance(node.ctx, ast.Param):
var = self.symtab[node.name]
var.is_arg = True
self.flow.mark_assignment(node, None, var, assignment=None)
elif isinstance(node.ctx, ast.Load):
var = self.symtab.lookup(node.name)
if var:
# Local variable
self.flow.mark_reference(node, var)
# Set position of assignment of this definition
if isinstance(node.ctx, (ast.Param, ast.Store)):
var = self.symtab[node.name]
if var.lineno == -1:
var.lineno = getattr(node, "lineno", 0)
var.col_offset = getattr(node, "col_offset", 0)
return node
示例3: visit_BinOp
def visit_BinOp(self, node):
node = self.generic_visit(node)
if isinstance(node.left, ast.Num) and isinstance(node.right, ast.Num):
value = eval(compile(ast.copy_location(ast.Expression(body=node), node), '', 'eval'))
return ast.copy_location(ast.Num(n=value), node)
else:
return node
示例4: test_load_const
def test_load_const(self):
consts = [None,
True, False,
124,
2.0,
3j,
"unicode",
b'bytes',
(1, 2, 3)]
code = '\n'.join(map(repr, consts))
code += '\n...'
code_consts = [const for const in consts
if (not isinstance(const, (str, int, float, complex))
or isinstance(const, bool))]
code_consts.append(Ellipsis)
# the compiler adds a final "LOAD_CONST None"
code_consts.append(None)
tree = ast.parse(code)
self.assertEqual(self.get_load_const(tree), code_consts)
# Replace expression nodes with constants
for expr_node, const in zip(tree.body, consts):
assert isinstance(expr_node, ast.Expr)
new_node = ast.Constant(value=const)
ast.copy_location(new_node, expr_node.value)
expr_node.value = new_node
self.assertEqual(self.get_load_const(tree), code_consts)
示例5: prepare
def prepare(self):
for i in self.children:
i.prepare()
# Compile the keywords.
keyword_values = { }
keyword_keys = [ ]
keyword_exprs = [ ]
for k, expr in self.keyword:
node = py_compile(expr, 'eval', ast_node=True)
if is_constant(node):
keyword_values[k] = py_eval_bytecode(compile_expr(node))
else:
keyword_keys.append(ast.Str(s=k))
keyword_exprs.append(node)
if keyword_values:
self.keyword_values = keyword_values
else:
self.keyword_values = None
if keyword_keys:
node = ast.Dict(keys=keyword_keys, values=keyword_exprs)
ast.copy_location(node, keyword_exprs[0])
self.keyword_exprs = compile_expr(node)
else:
self.keyword_exprs = None
示例6: test_ast_2
def test_ast_2(self):
self.ns['FOOBAR'] = ''
expr = ast.Expression()
expr.body = ast.parse('FOO+BAR').body[0].value
ast.copy_location(expr, expr.body)
self.ns['FOOBAR'] = Expression(expr)
self.assertEqual(self.ns['FOOBAR'].get(), 'foobar')
示例7: visit_If
def visit_If(self, node):
exit_block = self.flow.exit_block(label='exit_if', pos=node)
# Condition
cond_block = self.flow.nextblock(self.flow.block, label='if_cond',
is_expr=True, pos=node.test)
node.test = self.visit(node.test)
# Body
if_block = self.flow.nextblock(label='if_body', pos=node.body[0])
self.visitlist(node.body)
if self.flow.block:
self.flow.block.add_child(exit_block)
# Else clause
if node.orelse:
else_block = self.flow.nextblock(cond_block,
label='else_body',
pos=node.orelse[0])
self.visitlist(node.orelse)
if self.flow.block:
self.flow.block.add_child(exit_block)
else:
cond_block.add_child(exit_block)
else_block = None
new_node = nodes.build_if(cond_block=cond_block, test=node.test,
if_block=if_block, body=node.body,
else_block=else_block, orelse=node.orelse,
exit_block=exit_block)
ast.copy_location(new_node, node)
return self.exit_block(exit_block, new_node)
示例8: visit_Name
def visit_Name(self, node):
new_node = ast.Name(
self._visit(node.id),
self._visit(node.ctx),
)
ast.copy_location(new_node, node)
return new_node
示例9: visit_FunctionDef
def visit_FunctionDef(self, node):
if node.name.startswith('test'):
statements = [stmt for stmt in node.body
if isinstance(stmt, ast.Assign) or
isinstance(stmt, ast.Expr)
]
self.statements += statements
self.tracking[node.name] = None
body = node.body
new_stmts = []
for _node in node.body:
new_stmts.append(createNode(node.name, _node))
new_node_body = []
for i in xrange(0, len(new_stmts)):
new_node = new_stmts[i]
old_node = body[i]
if isinstance(new_node, list):
for _new_node in new_node:
ast.copy_location(_new_node, old_node)
new_node_body.append(_new_node)
new_node_body.append(old_node)
elif isinstance(new_node, ast.TryExcept):
ast.copy_location(new_node, old_node)
new_node_body.append(new_node)
new_node_body.append(old_node)
else:
new_node_body.append(new_node)
node.body = new_node_body
ast.fix_missing_locations(node)
return node
ast.fix_missing_locations(node)
return node
示例10: __visit_FunctionDef
def __visit_FunctionDef(self, node):
new_node = ast.FunctionDef(args=self.visit_arguments(node.args),
body=self._visit_list(node.body),
decorator_list=self._visit_list(node.decorator_list),
name=node.name)
ast.copy_location(new_node, node)
return new_node
示例11: visit_BinOp
def visit_BinOp(self, node: ast.BinOp):
node = self.generic_visit(node)
if self._is_numeric_pow(node):
left, right = node.left, node.right
degree = ( right.n if isinstance(right, ast.Num)
else -right.operand.n if isinstance(right.op, ast.USub)
else right.operand.n )
degree = int(degree)
if abs(degree) == 0:
node = ast.copy_location(ast.Num(n = 1), node)
elif abs(degree) == 1:
node = node.left
elif 2 <= abs(degree) <= self.MAX_DEGREE:
for _ in range(1, abs(degree)):
new_node = ast.BinOp\
( left = left
, op = ast.Mult()
, right = copy(node.left)
)
left = new_node = ast.copy_location(new_node, node)
node = new_node
else:
return node
if degree < 0:
new_node = ast.BinOp\
( left = ast.Num(n = 1)
, op = ast.Div()
, right = node
)
node = ast.copy_location(new_node, node)
return node
示例12: visit_Name
def visit_Name(self, node):
if self.randomize():
if node.id == 'forall':
return ast.copy_location(_ast.Name(id='exists'), node)
elif node.id == 'exists':
return ast.copy_location(_ast.Name(id='forall'), node)
return node
示例13: visit_If
def visit_If(self, node):
node = self.generic_visit(node)
if (node.orelse
and len(node.orelse) == 1
and isinstance(node.orelse[0], ast.Pass)
):
node.orelse = []
if (len(node.body) == 1
and isinstance(node.body[0], ast.Pass)
):
if node.orelse:
node_test = ast.UnaryOp(op=ast.Not(), operand=node.test)
if (len(node.orelse) == 1
and isinstance(node.orelse[0], ast.If)
):
node_test = ast.BoolOp\
( op = ast.And()
, values = [node_test, node.orelse[0].test]
)
node.test = ast.copy_location(node_test, node.orelse[0].test)
node.body = node.orelse[0].body
node.orelse = node.orelse[0].orelse
else:
node.test = ast.copy_location(node_test, node.test)
node.body = node.orelse
node.orelse = []
else:
node = None
return node
示例14: test_load_const
def test_load_const(self):
consts = [None,
True, False,
124,
2.0,
3j,
"unicode",
b'bytes',
(1, 2, 3)]
code = '\n'.join(['x={!r}'.format(const) for const in consts])
code += '\nx = ...'
consts.extend((Ellipsis, None))
tree = ast.parse(code)
self.assertEqual(self.get_load_const(tree),
consts)
# Replace expression nodes with constants
for assign, const in zip(tree.body, consts):
assert isinstance(assign, ast.Assign), ast.dump(assign)
new_node = ast.Constant(value=const)
ast.copy_location(new_node, assign.value)
assign.value = new_node
self.assertEqual(self.get_load_const(tree),
consts)
示例15: visit_Assign
def visit_Assign(self, node):
if isinstance(node.value, ast.Subscript) and isinstance(node.value.value, ast.Call):
subscr = node.value
call = subscr.value
if len(node.targets) > 1:
error.error('Cannot use multiple assignment in array declaration.', node)
variable_name = node.targets[0].id
value_type = call.func.id
declaration_args = call.args
# Get the indices being accessed.
shape = slice_node_to_tuple_of_numbers(subscr.slice)
new_assigns = []
for indices in itertools.product(*[range(n) for n in shape]):
index_name = flattened_array_name(variable_name, indices)
new_index_name_node = ast.copy_location(ast.Name(index_name, ast.Store()), node)
new_value_type_node = ast.copy_location(ast.Name(value_type, ast.Load()), node)
new_declaration_args = [copy.deepcopy(arg) for arg in declaration_args]
new_call_node = ast.copy_location(ast.Call(new_value_type_node, new_declaration_args, [], None, None), node)
new_assign = ast.Assign([new_index_name_node], new_call_node)
new_assign = ast.copy_location(new_assign, node)
new_assigns.append(new_assign)
return new_assigns
else:
return node