Python ast.Module() Examples
The following are 30
code examples of ast.Module().
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: test_topython.py From pyrser with GNU General Public License v3.0 | 6 votes |
def test_topython_generates_code_for_alt(self): alt = parsing.Alt( ParseTreeStub('a', False), ParseTreeStub('b', False)) res = codegen.to_source(ast.Module(passes.rule_topython(alt))) self.assertEqual(res, ("try:\n" " try:\n" " if (not a):\n" " raise AltFalse()\n" " raise AltTrue()\n" " except AltFalse:\n" " pass\n" " try:\n" " if (not b):\n" " raise AltFalse()\n" " raise AltTrue()\n" " except AltFalse:\n" " pass\n" " return False\n" "except AltTrue:\n" " pass"))
Example #2
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 #3
Source File: _assertionnew.py From python-netsurv with MIT License | 6 votes |
def generic_visit(self, node): # Fallback when we don't have a special implementation. if _is_ast_expr(node): mod = ast.Expression(node) co = self._compile(mod) try: result = self.frame.eval(co) except Exception: raise Failure() explanation = self.frame.repr(result) return explanation, result elif _is_ast_stmt(node): mod = ast.Module([node]) co = self._compile(mod, "exec") try: self.frame.exec_(co) except Exception: raise Failure() return None, None else: raise AssertionError("can't handle %s" %(node,))
Example #4
Source File: _assertionnew.py From python-netsurv with MIT License | 6 votes |
def generic_visit(self, node): # Fallback when we don't have a special implementation. if _is_ast_expr(node): mod = ast.Expression(node) co = self._compile(mod) try: result = self.frame.eval(co) except Exception: raise Failure() explanation = self.frame.repr(result) return explanation, result elif _is_ast_stmt(node): mod = ast.Module([node]) co = self._compile(mod, "exec") try: self.frame.exec_(co) except Exception: raise Failure() return None, None else: raise AssertionError("can't handle %s" %(node,))
Example #5
Source File: pattern_matcher.py From sspam with BSD 3-Clause "New" or "Revised" License | 6 votes |
def __init__(self, patt_ast, target_ast, rep_ast, nbits=0): 'Pattern ast should have as root: BinOp, BoolOp, UnaryOp or Call' if isinstance(patt_ast, ast.Module): self.patt_ast = patt_ast.body[0].value elif isinstance(patt_ast, ast.Expression): self.patt_ast = patt_ast.body else: self.patt_ast = patt_ast if isinstance(rep_ast, ast.Module): self.rep_ast = deepcopy(rep_ast.body[0].value) elif isinstance(rep_ast, ast.Expression): self.rep_ast = deepcopy(rep_ast.body) else: self.rep_ast = deepcopy(rep_ast) if not nbits: getsize = asttools.GetSize() getsize.visit(target_ast) if getsize.result: self.nbits = getsize.result # default bitsize is 8 else: self.nbits = 8 else: self.nbits = nbits
Example #6
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 #7
Source File: input.py From GYP3 with BSD 3-Clause "New" or "Revised" License | 6 votes |
def CheckedEval(file_contents): """Return the eval of a gyp file. The gyp file is restricted to dictionaries and lists only, and repeated keys are not allowed. Note that this is slower than eval() is. """ syntax_tree = ast.parse(file_contents) assert isinstance(syntax_tree, ast.Module) c1 = syntax_tree.body assert len(c1) == 1 c2 = c1[0] assert isinstance(c2, ast.Expr) return CheckNode(c2.value, [])
Example #8
Source File: syntax.py From pynt with GNU General Public License v3.0 | 6 votes |
def find_method(module, namespace): """Filter away everything except the method Promote the method up to the global namespace so that it is indistinguishable from a regular function. Arguments: module (ast.Module): the entire parsed source code namespace (str): identifier for the method of interest Returns: module (ast.Module): the original module but with everything filtered away except the method name but with the name `namespace` and promoted to the global (i.e. top) level """ module_name, class_name, method_name = namespace.split('.') classdefs = [stmt for stmt in module.body if isinstance(stmt, ast.ClassDef)] classdef, = [classdef for classdef in classdefs if classdef.name == class_name] methods = [stmt for stmt in classdef.body if isinstance(stmt, ast.FunctionDef)] for method in methods: if method.name == method_name: method.name = f'{module_name}.{class_name}.{method_name}' module.body = [method] return module
Example #9
Source File: syntax.py From pynt with GNU General Public License v3.0 | 6 votes |
def find_func(module, namespace): """Filter away everything except the function Addionally rename the function for better readability. Args: module (ast.Module): the entire parsed code namespace (str): identifier for the function of interest `namspace` will be of the form <module_name>.<function_name> Returns: module (ast.Module): the original module but with everything filtered away except the function and the function with a more readable name """ module_name, func_name = namespace.split('.') funcs = [stmt for stmt in module.body if isinstance(stmt, ast.FunctionDef)] func, = [func for func in funcs if func.name == func_name] func.name = f'{module_name}.{func_name}' module.body = [func] return module
Example #10
Source File: interactiveshell.py From Computable with MIT License | 6 votes |
def transform_ast(self, node): """Apply the AST transformations from self.ast_transformers Parameters ---------- node : ast.Node The root node to be transformed. Typically called with the ast.Module produced by parsing user input. Returns ------- An ast.Node corresponding to the node it was called with. Note that it may also modify the passed object, so don't rely on references to the original AST. """ for transformer in self.ast_transformers: try: node = transformer.visit(node) except Exception: warn("AST transformer %r threw an error. It will be unregistered." % transformer) self.ast_transformers.remove(transformer) if self.ast_transformers: ast.fix_missing_locations(node) return node
Example #11
Source File: test_misc.py From qdb with Apache License 2.0 | 6 votes |
def test_progn_uses_custom_eval_fn(self): """ Assert that the progn function uses custom eval functions properly. """ eval_fn = mock.MagicMock() try: progn('2 + 2', eval_fn=eval_fn) except QdbPrognEndsInStatement: # This is the error that we are getting because our eval function # is not storing any results. pass calls = eval_fn.call_args_list self.assertEqual(len(calls), 1) call_args = calls[0][0] # This is constructed inside the function, but should be a module. self.assertIsInstance(call_args[0], ast.Module) self.assertEqual(call_args[1], sys._getframe()) self.assertEqual(call_args[2], 'exec')
Example #12
Source File: _assertionnew.py From py with MIT License | 6 votes |
def generic_visit(self, node): # Fallback when we don't have a special implementation. if _is_ast_expr(node): mod = ast.Expression(node) co = self._compile(mod) try: result = self.frame.eval(co) except Exception: raise Failure() explanation = self.frame.repr(result) return explanation, result elif _is_ast_stmt(node): mod = ast.Module([node]) co = self._compile(mod, "exec") try: self.frame.exec_(co) except Exception: raise Failure() return None, None else: raise AssertionError("can't handle %s" %(node,))
Example #13
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 #14
Source File: _func.py From deal with MIT License | 6 votes |
def from_ast(cls, tree: ast.Module) -> List['Func']: funcs = [] for expr in tree.body: if not isinstance(expr, ast.FunctionDef): continue contracts = [] for category, args in get_contracts(expr.decorator_list): contract = Contract( args=args, category=Category(category), func_args=expr.args, ) contracts.append(contract) funcs.append(cls( name=expr.name, args=expr.args, body=expr.body, contracts=contracts, line=expr.lineno, col=expr.col_offset, )) return funcs
Example #15
Source File: builder.py From staticfg with Apache License 2.0 | 6 votes |
def new_functionCFG(self, node, asynchr=False): """ Create a new sub-CFG for a function definition and add it to the function CFGs of the CFG being built. Args: node: The AST node containing the function definition. async: Boolean indicating whether the function for which the CFG is being built is asynchronous or not. """ self.current_id += 1 # A new sub-CFG is created for the body of the function definition and # added to the function CFGs of the current CFG. func_body = ast.Module(body=node.body) func_builder = CFGBuilder() self.cfg.functioncfgs[node.name] = func_builder.build(node.name, func_body, asynchr, self.current_id) self.current_id = func_builder.current_id + 1
Example #16
Source File: test_assertrewrite.py From pytest with MIT License | 5 votes |
def rewrite(src: str) -> ast.Module: tree = ast.parse(src) rewrite_asserts(tree, src.encode()) return tree
Example #17
Source File: _imports.py From deal with MIT License | 5 votes |
def _get_contracts(tree: ast.Module) -> List[ast.AST]: for node in tree.body: # type: Any if type(node) is not ast.Expr: continue if type(node.value) is not ast.Call: continue if get_name(node.value.func) != 'deal.module_load': continue return node.value.args return []
Example #18
Source File: test_assertrewrite.py From pytest with MIT License | 5 votes |
def test_rewrite_warning(self, testdir): testdir.makeconftest( """ import pytest pytest.register_assert_rewrite("_pytest") """ ) # needs to be a subprocess because pytester explicitly disables this warning result = testdir.runpytest_subprocess() result.stdout.fnmatch_lines(["*Module already imported*: _pytest"])
Example #19
Source File: transformer.py From pyt with GNU General Public License v2.0 | 5 votes |
def visit_Module(self, node): transformed = ast.Module(self.visit_body(node.body)) ast.copy_location(transformed, node) return self.generic_visit(transformed)
Example #20
Source File: _checker.py From deal with MIT License | 5 votes |
def __init__(self, tree: ast.Module, file_tokens=None, filename: str = 'stdin'): self._tree = tree self._filename = filename paths = list(StubsManager.default_paths) if filename != 'stdin': paths.append(Path(filename).absolute().parent) self._stubs = StubsManager(paths=paths)
Example #21
Source File: test_ast.py From oss-ftp with MIT License | 5 votes |
def test_invalid_string(self): m = ast.Module([ast.Expr(ast.Str(43))]) ast.fix_missing_locations(m) with self.assertRaises(TypeError) as cm: compile(m, "<test>", "exec") self.assertIn("string must be of type str or uni", str(cm.exception))
Example #22
Source File: execeval.py From altair with BSD 3-Clause "New" or "Revised" License | 5 votes |
def eval_block(code, namespace=None, filename="<string>"): """ Execute a multi-line block of code in the given namespace If the final statement in the code is an expression, return the result of the expression. """ tree = ast.parse(code, filename="<ast>", mode="exec") if namespace is None: namespace = {} catch_display = _CatchDisplay() if isinstance(tree.body[-1], ast.Expr): to_exec, to_eval = tree.body[:-1], tree.body[-1:] else: to_exec, to_eval = tree.body, [] for node in to_exec: compiled = compile(Module([node], []), filename=filename, mode="exec") exec(compiled, namespace) with catch_display: for node in to_eval: compiled = compile( ast.Interactive([node]), filename=filename, mode="single" ) exec(compiled, namespace) return catch_display.output
Example #23
Source File: test_ast.py From oss-ftp with MIT License | 5 votes |
def test_invalid_identitifer(self): m = ast.Module([ast.Expr(ast.Name(u"x", ast.Load()))]) ast.fix_missing_locations(m) with self.assertRaises(TypeError) as cm: compile(m, "<test>", "exec") self.assertIn("identifier must be of type str", str(cm.exception))
Example #24
Source File: test_ast.py From oss-ftp with MIT License | 5 votes |
def test_module(self): body = [ast.Num(42)] x = ast.Module(body) self.assertEqual(x.body, body)
Example #25
Source File: node_transformers.py From pynt with GNU General Public License v3.0 | 5 votes |
def generic_visit(self, node): if isinstance(node, ast.Module): return super().generic_visit(node) else: return make_annotation(node, buffer=self.buffer)
Example #26
Source File: test_node_transformers.py From pynt with GNU General Public License v3.0 | 5 votes |
def test_simple(self): code = """ a b c """ module = ast.parse(code) out = codebook.node_transformers.ExpressionFinder(lineno=4).visit(module) self.assertIsInstance(out, ast.Module) c = astor.to_source(out) self.assertIn('b', c) self.assertNotIn('a', c) self.assertNotIn('c', c)
Example #27
Source File: syntax.py From pynt with GNU General Public License v3.0 | 5 votes |
def filter_away_except(tree, namespace): """Filter away everything except the namespace In the case that `namespace` is a method promote the method to the global (i.e. top) level. Arguments: tree (ast.Module): the entire parsed code from the code buffer namespace (str): the namespace (i.e. region of code) of interest Returns: small_tree (ast.Module): `tree` but everything filtered except the namespace region """ ns_tokens = namespace.split('.') if len(ns_tokens) == 1: # module-level tree.body = [stmt for stmt in tree.body if not isinstance(stmt, ast.FunctionDef) and not isinstance(stmt, ast.ClassDef)] e = codebook.node_transformers.make_annotation(buffer=namespace, content=f'`{namespace}`', cell_type='1') tree.body.insert(0, e) small_tree = tree elif len(ns_tokens) == 2: # function small_tree = find_func(tree, namespace) else: # method assert len(ns_tokens) == 3 small_tree = find_method(tree, namespace) return small_tree
Example #28
Source File: test_ast.py From BinderFilter with MIT License | 5 votes |
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)])" )
Example #29
Source File: test_ast.py From BinderFilter with MIT License | 5 votes |
def test_invalid_string(self): m = ast.Module([ast.Expr(ast.Str(43))]) ast.fix_missing_locations(m) with self.assertRaises(TypeError) as cm: compile(m, "<test>", "exec") self.assertIn("string must be of type str or uni", str(cm.exception))
Example #30
Source File: test_ast.py From BinderFilter with MIT License | 5 votes |
def test_invalid_identitifer(self): m = ast.Module([ast.Expr(ast.Name(u"x", ast.Load()))]) ast.fix_missing_locations(m) with self.assertRaises(TypeError) as cm: compile(m, "<test>", "exec") self.assertIn("identifier must be of type str", str(cm.exception))