def test_fix_missing_locations(self):
src = ast.parse('write("spam")')
src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
[ast.Str('eggs')], [], None, None)))
self.assertEqual(src, ast.fix_missing_locations(src))
self.assertEqual(ast.dump(src, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
"col_offset=6)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
"keywords=[], starargs=None, kwargs=None, lineno=1, "
"col_offset=0), lineno=1, col_offset=0)])"
)
python类dump()的实例源码
def test_parse(self):
a = ast.parse('foo(1 + 1)')
b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
self.assertEqual(ast.dump(a), ast.dump(b))
def test_copy_location(self):
src = ast.parse('1 + 1', mode='eval')
src.body.right = ast.copy_location(ast.Num(2), src.body.right)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
'col_offset=0))'
)
def test_fix_missing_locations(self):
src = ast.parse('write("spam")')
src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
[ast.Str('eggs')], [], None, None)))
self.assertEqual(src, ast.fix_missing_locations(src))
self.assertEqual(ast.dump(src, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
"col_offset=6)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
"keywords=[], starargs=None, kwargs=None, lineno=1, "
"col_offset=0), lineno=1, col_offset=0)])"
)
def assertASTEqual(self, ast1, ast2):
dump1 = ast.dump(ast1)
dump2 = ast.dump(ast2)
self.assertEqual(ast.dump(ast1), ast.dump(ast2))
def test_parse(self):
a = ast.parse('foo(1 + 1)')
b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
self.assertEqual(ast.dump(a), ast.dump(b))
def test_copy_location(self):
src = ast.parse('1 + 1', mode='eval')
src.body.right = ast.copy_location(ast.Num(2), src.body.right)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
'col_offset=0))'
)
def test_fix_missing_locations(self):
src = ast.parse('write("spam")')
src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
[ast.Str('eggs')], [], None, None)))
self.assertEqual(src, ast.fix_missing_locations(src))
self.assertEqual(ast.dump(src, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
"col_offset=6)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
"keywords=[], starargs=None, kwargs=None, lineno=1, "
"col_offset=0), lineno=1, col_offset=0)])"
)
def test_iter_child_nodes(self):
node = ast.parse("spam(23, 42, eggs='leek')", mode='eval')
self.assertEqual(len(list(ast.iter_child_nodes(node.body))), 4)
iterator = ast.iter_child_nodes(node.body)
self.assertEqual(next(iterator).id, 'spam')
self.assertEqual(next(iterator).n, 23)
self.assertEqual(next(iterator).n, 42)
self.assertEqual(ast.dump(next(iterator)),
"keyword(arg='eggs', value=Str(s='leek'))"
)
def pre_visit(self, node, preprocess=False):
self.context = {}
self.context['imports'] = self.imports
self.context['import_aliases'] = self.import_aliases
if self.debug:
LOG.debug(ast.dump(node))
self.metaast.add_node(node, '', self.depth)
if hasattr(node, 'lineno'):
self.context['lineno'] = node.lineno
if node.lineno in self.nosec_lines:
LOG.debug("skipped, nosec")
self.metrics.note_nosec()
return False
if preprocess:
return True
self.context['imports'] = self.imports
self.context['import_aliases'] = self.import_aliases
self.context['node'] = node
self.context['linerange'] = b_utils.linerange_fix(node)
self.context['filename'] = self.fname
self.seen += 1
LOG.debug("entering: %s %s [%s]", hex(id(node)), type(node),
self.depth)
self.depth += 1
LOG.debug(self.context)
return True
def visit(self, node, phase=None):
phase = phase or constants.PRIMARY
name = node.__class__.__name__
method = 'visit_' + name
visitor = getattr(self, method, None)
if visitor is not None:
if self.debug:
LOG.debug("%s called (%s)", method, ast.dump(node))
visitor(node, phase)
else:
self.update_scores(self.tester.run_tests(self.context, name, phase=phase))
def visit(self, node):
method = 'visit_' + node.__class__.__name__
visitor = getattr(self, method, None)
if self.verbose:
lineno = getattr(node, 'lineno', 1)
line = self.code[lineno - 1] if self.code else ''
self._log(lineno, ast.dump(node), line)
if visitor:
visitor(node)
return self.generic_visit(node)
def test_parse(self):
a = ast.parse('foo(1 + 1)')
b = compile('foo(1 + 1)', '<unknown>', 'exec', ast.PyCF_ONLY_AST)
self.assertEqual(ast.dump(a), ast.dump(b))
def test_copy_location(self):
src = ast.parse('1 + 1', mode='eval')
src.body.right = ast.copy_location(ast.Num(2), src.body.right)
self.assertEqual(ast.dump(src, include_attributes=True),
'Expression(body=BinOp(left=Num(n=1, lineno=1, col_offset=0), '
'op=Add(), right=Num(n=2, lineno=1, col_offset=4), lineno=1, '
'col_offset=0))'
)
def test_fix_missing_locations(self):
src = ast.parse('write("spam")')
src.body.append(ast.Expr(ast.Call(ast.Name('spam', ast.Load()),
[ast.Str('eggs')], [], None, None)))
self.assertEqual(src, ast.fix_missing_locations(src))
self.assertEqual(ast.dump(src, include_attributes=True),
"Module(body=[Expr(value=Call(func=Name(id='write', ctx=Load(), "
"lineno=1, col_offset=0), args=[Str(s='spam', lineno=1, "
"col_offset=6)], keywords=[], starargs=None, kwargs=None, "
"lineno=1, col_offset=0), lineno=1, col_offset=0), "
"Expr(value=Call(func=Name(id='spam', ctx=Load(), lineno=1, "
"col_offset=0), args=[Str(s='eggs', lineno=1, col_offset=0)], "
"keywords=[], starargs=None, kwargs=None, lineno=1, "
"col_offset=0), lineno=1, col_offset=0)])"
)
def _get_assign_names(targets, load_names, store_names):
for target in targets:
orig_target = target
target = _get_ast_name_node(target)
if (isinstance(target, ast.Name)
and isinstance(target.ctx, ast.Store)):
# 'x = value': store name 'x'
store_names.add(target.id)
elif (isinstance(target, ast.Name)
and isinstance(target.ctx, ast.Load)):
# 'obj.attr = value': load name 'obj'
load_names.add(target.id)
elif isinstance(target, ast.Tuple):
# x, y = ...
_get_assign_names(target.elts, load_names, store_names)
elif isinstance(target, ast.Constant):
# '(1).__class__ = MyInt': it raises a TypeError
raise ComplexAssignment(orig_target)
elif isinstance(target, (ast.Dict, ast.List)):
# '{...}[key] = ...', '[...][index] = ...'
pass
elif isinstance(target, ast.Call):
# 'globals()[key] = value'
# 'type(mock)._mock_check_sig = checksig'
raise ComplexAssignment(orig_target)
else:
raise Exception("unsupported assign target: %s"
% ast.dump(target))
def compact_dump(node, maxlen=COMPACT_DUMP_MAXLEN):
if isinstance(node, list):
return repr([compact_dump(node_item, maxlen) for node_item in node])
node_repr = ast.dump(node)
if len(node_repr) > maxlen:
node_repr = node_repr[:maxlen] + '(...)'
return node_repr
# FIXME: replace it with FindNodes, see unroll.py
def call_builtin(self, node, pure_func):
value = pure_func.call_func(node)
if value is UNSET:
return
new_node = self.new_constant(node, value)
if new_node is None:
return
self.log(node, "call pure builtin function: replace %s with %r",
ast.dump(node), value, add_line=True)
self.add_guard(BuiltinGuard(node.func.id, 'call builtin'))
return new_node
def test_files(self):
"""Keep Python stdlib tree the same after roundtrip parse-unparse."""
for path in PATHS:
with open(path, 'r', encoding='utf-8') as py_file:
original_code = py_file.read()
tree = typed_ast.ast3.parse(source=original_code, filename=path)
code = typed_astunparse.unparse(tree)
roundtrip_tree = typed_ast.ast3.parse(source=code)
tree_dump = typed_ast.ast3.dump(tree, include_attributes=False)
roundtrip_tree_dump = typed_ast.ast3.dump(roundtrip_tree, include_attributes=False)
self.assertEqual(tree_dump, roundtrip_tree_dump, msg=path)
def test_untyped_files(self):
"""Unparse Python stdlib correctly even if parsed using built-in ast package."""
for path in PATHS:
with open(path, 'r', encoding='utf-8') as py_file:
original_code = py_file.read()
tree = ast.parse(source=original_code, filename=path)
code = typed_astunparse.unparse(tree)
roundtrip_tree = ast.parse(source=code)
tree_dump = ast.dump(tree, include_attributes=False)
roundtrip_tree_dump = ast.dump(roundtrip_tree, include_attributes=False)
self.assertEqual(tree_dump, roundtrip_tree_dump, msg=path)