Python ast.dump() Examples
The following are 30
code examples of ast.dump().
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: rule.py From streamalert with Apache License 2.0 | 6 votes |
def checksum(self): """Produce an md5 for the contents of this rule. This logic applies to expressions within the function only. It does not take into account: the function name, docstring, comments, or decorator arguments """ if not self._checksum: try: code = inspect.getsource(self.func) root = ast.parse(code) md5 = hashlib.md5() # nosec for expression in root.body[0].body: # This check is necessary to ensure changes to the docstring # are allowed without altering the checksum if not isinstance(expression, ast.Expr): md5.update(ast.dump(expression).encode('utf-8')) self._checksum = md5.hexdigest() except (TypeError, IndentationError, IndexError): LOGGER.exception('Could not checksum rule function') self._checksum = self.CHECKSUM_UNKNOWN return self._checksum
Example #2
Source File: test_ast.py From ironpython2 with Apache License 2.0 | 6 votes |
def test_increment_lineno(self): src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src, n=3), src) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' ) # issue10869: do not increment lineno of root twice src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' )
Example #3
Source File: test_ast.py From ironpython2 with Apache License 2.0 | 6 votes |
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)])" )
Example #4
Source File: test_ast.py From ironpython2 with Apache License 2.0 | 6 votes |
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)])" )
Example #5
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
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=[]))])" ) self.assertEqual(ast.dump(node, annotate_fields=False), "Module([Expr(Call(Name('spam', Load()), [Name('eggs', Load()), " "Str('and cheese')], []))])" ) 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=[], " "lineno=1, col_offset=0), lineno=1, col_offset=0)])" )
Example #6
Source File: _imports.py From deal with MIT License | 6 votes |
def exec_module(self, module: ModuleType) -> None: if not hasattr(self._loader, 'get_source'): return self._loader.exec_module(module) # get nodes with module-level contracts from the source code source = self._loader.get_source(module.__name__) if source is None: return self._loader.exec_module(module) tree = ast.parse(source) nodes = self._get_contracts(tree=tree) if not nodes: return self._loader.exec_module(module) # convert contracts nodes into real contracts contracts = [] for node in nodes: contract = self._exec_contract(node=node) if contract is None: msg = 'unsupported contract: {}'.format(ast.dump(node)) raise RuntimeError(msg) contracts.append(contract) # execute module with contracts wrapped = _aliases.chain(contract)(self._loader.exec_module) wrapped(module)
Example #7
Source File: vars_visitor.py From pyt with GNU General Public License v2.0 | 6 votes |
def visit_Call(self, node): # This will not visit Flask in Flask(__name__) but it will visit request in `request.args.get() if not isinstance(node.func, ast.Name): self.visit(node.func) for arg_node in itertools.chain(node.args, node.keywords): arg = arg_node.value if isinstance(arg_node, ast.keyword) else arg_node if isinstance(arg, ast.Call): if isinstance(arg.func, ast.Name): # We can't just visit because we need to add 'ret_' self.result.append('ret_' + arg.func.id) elif isinstance(arg.func, ast.Attribute): # e.g. html.replace('{{ param }}', param) # func.attr is replace # func.value.id is html # We want replace self.result.append('ret_' + arg.func.attr) elif isinstance(arg.func, ast.Call): self.visit_curried_call_inside_call_args(arg) else: raise Exception('Cannot visit vars of ' + ast.dump(arg)) else: self.visit(arg)
Example #8
Source File: test_ast.py From BinderFilter with MIT License | 6 votes |
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)])" )
Example #9
Source File: _Waiter.py From myhdl with GNU Lesser General Public License v2.1 | 6 votes |
def _inferWaiter(gen): f = gen.gi_frame s = inspect.getsource(f) s = _dedent(s) root = ast.parse(s) root.symdict = f.f_globals.copy() root.symdict.update(f.f_locals) # print ast.dump(root) v = _YieldVisitor(root) v.visit(root) if v.kind == _kind.EDGE_TUPLE: return _EdgeTupleWaiter(gen) if v.kind == _kind.SIGNAL_TUPLE: return _SignalTupleWaiter(gen) if v.kind == _kind.DELAY: return _DelayWaiter(gen) if v.kind == _kind.EDGE: return _EdgeWaiter(gen) if v.kind == _kind.SIGNAL: return _SignalWaiter(gen) # default return _Waiter(gen)
Example #10
Source File: transformer_test.py From pyt with GNU General Public License v2.0 | 6 votes |
def test_chained_function(self): chained_tree = ast.parse("\n".join([ "def a():", " b = c.d(e).f(g).h(i).j(k)", ])) separated_tree = ast.parse("\n".join([ "def a():", " __chain_tmp_3 = c.d(e)", " __chain_tmp_2 = __chain_tmp_3.f(g)", " __chain_tmp_1 = __chain_tmp_2.h(i)", " b = __chain_tmp_1.j(k)", ])) transformed = PytTransformer().visit(chained_tree) self.assertEqual(ast.dump(transformed), ast.dump(separated_tree))
Example #11
Source File: test_ast.py From BinderFilter with MIT License | 6 votes |
def test_increment_lineno(self): src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src, n=3), src) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' ) # issue10869: do not increment lineno of root twice src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' )
Example #12
Source File: test_ast.py From oss-ftp with MIT License | 6 votes |
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)])" )
Example #13
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 6 votes |
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)])" )
Example #14
Source File: test_ast.py From oss-ftp with MIT License | 6 votes |
def test_increment_lineno(self): src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src, n=3), src) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' ) # issue10869: do not increment lineno of root twice src = ast.parse('1 + 1', mode='eval') self.assertEqual(ast.increment_lineno(src.body, n=3), src.body) self.assertEqual(ast.dump(src, include_attributes=True), 'Expression(body=BinOp(left=Num(n=1, lineno=4, col_offset=0), ' 'op=Add(), right=Num(n=1, lineno=4, col_offset=4), lineno=4, ' 'col_offset=0))' )
Example #15
Source File: node_visitor.py From bandit with Apache License 2.0 | 5 votes |
def visit(self, node): 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) else: self.update_scores(self.tester.run_tests(self.context, name))
Example #16
Source File: coursera_submit.py From Coding-the-Matrix-Linear-Algebra-through-Computer-Science-Applications with MIT License | 5 votes |
def double_comprehension(varname): line = find_line(varname) try: parse_elements = ast.dump(ast.parse(line)) except SyntaxError: raise SyntaxError("Sorry---for this task, comprehension must be on one line.") return parse_elements.count("comprehension") == 2
Example #17
Source File: coursera_submit.py From Coding-the-Matrix-Linear-Algebra-through-Computer-Science-Applications with MIT License | 5 votes |
def use_comprehension(varname): line = find_line(varname) try: parse_elements = ast.dump(ast.parse(line)) except SyntaxError: raise SyntaxError("Sorry---for this task, comprehension must be on one line.") return "comprehension" in parse_elements
Example #18
Source File: test_common.py From deal with MIT License | 5 votes |
def test_get_name(text, expected): tree = ast.parse(text) print(ast.dump(tree)) expr = tree.body[0].value assert get_name(expr=expr) == expected tree = astroid.parse(text) print(tree.repr_tree()) expr = tree.body[0].value assert get_name(expr=expr) == expected
Example #19
Source File: test_imports.py From deal with MIT License | 5 votes |
def test_get_contracts(): text = """ import deal a = 1 1 / 0 not_a_deal.module_load(something) deal.module_load(deal.silent) """ text = dedent(text) tree = ast.parse(text) print(ast.dump(tree)) nodes = DealLoader._get_contracts(tree=tree) assert [get_name(node) for node in nodes] == ['deal.silent']
Example #20
Source File: test_exceptions.py From deal with MIT License | 5 votes |
def test_resolve_doesnt_fail_for_simple_ast(): text = """ def subf(): raise ValueError # explicit raise @deal.raises(KeyError) def f(): subf() """ tree = ast.parse(dedent(text)) print(ast.dump(tree)) func_tree = tree.body[-1].body tuple(get_exceptions(body=func_tree))
Example #21
Source File: test_exceptions.py From deal with MIT License | 5 votes |
def test_get_exceptions_simple(text, expected): tree = astroid.parse(text) print(tree.repr_tree()) returns = tuple(r.value for r in get_exceptions(body=tree.body)) assert returns == expected tree = ast.parse(text) print(ast.dump(tree)) returns = tuple(r.value for r in get_exceptions(body=tree.body)) assert returns == expected
Example #22
Source File: test_mock.py From rally-openstack with Apache License 2.0 | 5 votes |
def _get_value(self, node): """Get mock.patch string argument regexp. It is either a string (if we are lucky), string-format of ("%s.something" % GVAL) or (GVAL + ".something") """ val = None if isinstance(node, ast.Str): val = node.s elif isinstance(node, ast.BinOp): if pairwise_isinstance( (node.op, ast.Mod), (node.left, ast.Str), (node.right, ast.Name)): val = node.left.s % self.globals_[node.right.id] elif pairwise_isinstance( (node.op, ast.Add), (node.left, ast.Name), (node.right, ast.Str)): val = self.globals_[node.left.id] + node.right.s elif isinstance(node, ast.Name): val = self.globals_[node.id] if val is None: raise ValueError( "Unable to find value in %s, only the following are parsed: " "GLOBAL, 'pkg.foobar', '%%s.foobar' %% GLOBAL or 'GLOBAL + " "'.foobar'" % ast.dump(node)) return val
Example #23
Source File: test_globals.py From deal with MIT License | 5 votes |
def test_get_globals_simple(text, expected): tree = astroid.parse(text) print(tree.repr_tree()) globals = tuple(r.value for r in get_globals(body=tree.body)) assert globals == expected tree = ast.parse(text) print(ast.dump(tree)) globals = tuple(r.value for r in get_globals(body=tree.body)) assert globals == expected
Example #24
Source File: test_imports.py From deal with MIT License | 5 votes |
def test_get_imports_simple(text, expected): tree = astroid.parse(text) print(tree.repr_tree()) returns = tuple(r.value for r in get_imports(body=tree.body)) assert returns == expected tree = ast.parse(text) print(ast.dump(tree)) returns = tuple(r.value for r in get_imports(body=tree.body)) assert returns == expected
Example #25
Source File: test_contracts.py From deal with MIT License | 5 votes |
def test_get_contracts_decorators(text, expected): text += '\ndef f(x): pass' tree = astroid.parse(text) print(tree.repr_tree()) decos = tree.body[-1].decorators.nodes returns = tuple(cat for cat, _ in get_contracts(decos)) assert returns == expected tree = ast.parse(text) print(ast.dump(tree)) decos = tree.body[-1].decorator_list returns = tuple(cat for cat, _ in get_contracts(decos)) assert returns == expected
Example #26
Source File: test_prints.py From deal with MIT License | 5 votes |
def test_get_prints_simple(text, expected): tree = astroid.parse(text) print(tree.repr_tree()) returns = tuple(r.value for r in get_prints(body=tree.body)) assert returns == expected tree = ast.parse(text) print(ast.dump(tree)) returns = tuple(r.value for r in get_prints(body=tree.body)) assert returns == expected
Example #27
Source File: transformer_test.py From pyt with GNU General Public License v2.0 | 5 votes |
def test_if_exp(self): complex_if_exp_tree = ast.parse("\n".join([ "def a():", " b = c if d.e(f) else g if h else i if j.k(l) else m", ])) separated_tree = ast.parse("\n".join([ "def a():", " __if_exp_0 = d.e(f)", " __if_exp_1 = j.k(l)", " b = c if __if_exp_0 else g if h else i if __if_exp_1 else m", ])) transformed = PytTransformer().visit(complex_if_exp_tree) self.assertEqual(ast.dump(transformed), ast.dump(separated_tree))
Example #28
Source File: transformer_test.py From pyt with GNU General Public License v2.0 | 5 votes |
def test_async_removed_by_transformer(self): self.maxDiff = 99999 async_tree = ast.parse("\n".join([ "async def a():", " async for b in c():", " await b()", " async with d() as e:", " pass", " return await y()" ])) self.assertIsInstance(async_tree.body[0], ast.AsyncFunctionDef) self.assertIsInstance(async_tree.body[0].body[-1], ast.Return) self.assertIsInstance(async_tree.body[0].body[-1].value, ast.Await) sync_tree = ast.parse("\n".join([ "def a():", " for b in c():", " b()", " with d() as e:", " pass", " return y()" ])) self.assertIsInstance(sync_tree.body[0], ast.FunctionDef) transformed = PytTransformer().visit(async_tree) self.assertIsInstance(transformed.body[0], ast.FunctionDef) self.assertEqual(ast.dump(transformed), ast.dump(sync_tree))
Example #29
Source File: PyAstUtil.py From ufora with Apache License 2.0 | 5 votes |
def areAstsIdentical(ast1, ast2): return ast.dump(ast1) == ast.dump(ast2)
Example #30
Source File: processor.py From fastats with MIT License | 5 votes |
def process(self): source = inspect.getsource(self.top_level_func) # `ast.parse` can throw an IndentationError if passed # standalone nested function. In this case we take the # more expensive code path through `uncompile`. try: tree = ast.parse(source) except IndentationError: data = uncompile(self.top_level_func.__code__) tree = parse_snippet(*data) # We have to dynamically add the jit to nested functions # in order to get `nopython` mode working correctly. As # a result we always need `jit` in globals. # This can be removed if/when numba supports nested functions # in nopython mode by default. globs = self.top_level_func.__globals__ globs['jit'] = jit t = CallTransform(self._overrides, globs, self._replaced, self._new_funcs) new_tree = t.visit(tree) # TODO remove the fs decorator from within the ast code new_tree.body[0].decorator_list = [ast.Name(id='jit', ctx=ast.Load())] ast.fix_missing_locations(new_tree) if self._debug: pprint(ast.dump(new_tree)) code_obj = recompile(new_tree, '<fastats>', 'exec') self.top_level_func.__code__ = code_obj return convert_to_jit(self.top_level_func)