Python ast.stmt() Examples
The following are 30
code examples of ast.stmt().
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: code_generator.py From nni with MIT License | 6 votes |
def visit(self, node): if isinstance(node, (ast.expr, ast.stmt)): self.last_line = node.lineno # do nothing for root if not self.stack: return self._visit_children(node) annotation = self.stack[-1] # this is a standalone string, may be an annotation if type(node) is ast.Expr and type(node.value) is ast.Str: # must not annotate an annotation string assert annotation is None, 'Annotating an annotation' return self._visit_string(node) if annotation is not None: # this expression is annotated self.stack[-1] = None # so next expression is not if annotation.startswith('nni.variable'): return replace_variable_node(node, annotation) if annotation.startswith('nni.function_choice'): return replace_function_node(node, annotation) return self._visit_children(node)
Example #2
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_classdef(self): def cls(bases=None, keywords=None, body=None, decorator_list=None): if bases is None: bases = [] if keywords is None: keywords = [] if body is None: body = [ast.Pass()] if decorator_list is None: decorator_list = [] return ast.ClassDef("myclass", bases, keywords, body, decorator_list) self.stmt(cls(bases=[ast.Name("x", ast.Store())]), "must have Load context") self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]), "must have Load context") self.stmt(cls(body=[]), "empty body on ClassDef") self.stmt(cls(body=[None]), "None disallowed") self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]), "must have Load context")
Example #3
Source File: topython.py From pyrser with GNU General Public License v3.0 | 6 votes |
def __exit_scope(self) -> ast.stmt: """Create the appropriate scope exiting statement. The documentation only shows one level and always uses 'return False' in examples. 'raise AltFalse()' within a try. 'break' within a loop. 'return False' otherwise. """ if self.in_optional: return ast.Pass() if self.in_try: return ast.Raise( ast.Call(ast.Name('AltFalse', ast.Load()), [], [], None, None), None) if self.in_loop: return ast.Break() return ast.Return(ast.Name('False', ast.Load())) #TODO(bps): find a better name to describe what this does
Example #4
Source File: topython.py From pyrser with GNU General Public License v3.0 | 6 votes |
def visit_Rep0N(self, node: parsing.Rep0N) -> [ast.stmt]: """Generates python code for a clause repeated 0 or more times. #If all clauses can be inlined while clause: pass while True: <code for the clause> """ cl_ast = self.visit(node.pt) if isinstance(cl_ast, ast.expr): return [ast.While(cl_ast, [ast.Pass()], [])] self.in_loop += 1 clause = self._clause(self.visit(node.pt)) self.in_loop -= 1 return [ast.While(ast.Name('True', ast.Load()), clause, [])]
Example #5
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_classdef(self): def cls(bases=None, keywords=None, starargs=None, kwargs=None, body=None, decorator_list=None): if bases is None: bases = [] if keywords is None: keywords = [] if body is None: body = [ast.Pass()] if decorator_list is None: decorator_list = [] return ast.ClassDef("myclass", bases, keywords, starargs, kwargs, body, decorator_list) self.stmt(cls(bases=[ast.Name("x", ast.Store())]), "must have Load context") self.stmt(cls(keywords=[ast.keyword("x", ast.Name("x", ast.Store()))]), "must have Load context") self.stmt(cls(starargs=ast.Name("x", ast.Store())), "must have Load context") self.stmt(cls(kwargs=ast.Name("x", ast.Store())), "must have Load context") self.stmt(cls(body=[]), "empty body on ClassDef") self.stmt(cls(body=[None]), "None disallowed") self.stmt(cls(decorator_list=[ast.Name("x", ast.Store())]), "must have Load context")
Example #6
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 6 votes |
def test_try(self): p = ast.Pass() t = ast.Try([], [], [], [p]) self.stmt(t, "empty body on Try") t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p]) self.stmt(t, "must have Load context") t = ast.Try([p], [], [], []) self.stmt(t, "Try has neither except handlers nor finalbody") t = ast.Try([p], [], [p], [p]) self.stmt(t, "Try has orelse but no except handlers") t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], []) self.stmt(t, "empty body on ExceptHandler") e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])] self.stmt(ast.Try([p], e, [], []), "must have Load context") e = [ast.ExceptHandler(None, "x", [p])] t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p]) self.stmt(t, "must have Load context") t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))]) self.stmt(t, "must have Load context")
Example #7
Source File: source.py From python-netsurv with MIT License | 6 votes |
def get_statement_startend2(lineno, node): import ast # flatten all statements and except handlers into one lineno-list # AST's line numbers start indexing at 1 values = [] for x in ast.walk(node): if isinstance(x, (ast.stmt, ast.ExceptHandler)): values.append(x.lineno - 1) for name in ("finalbody", "orelse"): val = getattr(x, name, None) if val: # treat the finally/orelse part as its own statement values.append(val[0].lineno - 1 - 1) values.sort() insert_index = bisect_right(values, lineno) start = values[insert_index - 1] if insert_index >= len(values): end = None else: end = values[insert_index] return start, end
Example #8
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 6 votes |
def test_try(self): p = ast.Pass() t = ast.Try([], [], [], [p]) self.stmt(t, "empty body on Try") t = ast.Try([ast.Expr(ast.Name("x", ast.Store()))], [], [], [p]) self.stmt(t, "must have Load context") t = ast.Try([p], [], [], []) self.stmt(t, "Try has neither except handlers nor finalbody") t = ast.Try([p], [], [p], [p]) self.stmt(t, "Try has orelse but no except handlers") t = ast.Try([p], [ast.ExceptHandler(None, "x", [])], [], []) self.stmt(t, "empty body on ExceptHandler") e = [ast.ExceptHandler(ast.Name("x", ast.Store()), "y", [p])] self.stmt(ast.Try([p], e, [], []), "must have Load context") e = [ast.ExceptHandler(None, "x", [p])] t = ast.Try([p], e, [ast.Expr(ast.Name("x", ast.Store()))], [p]) self.stmt(t, "must have Load context") t = ast.Try([p], e, [p], [ast.Expr(ast.Name("x", ast.Store()))]) self.stmt(t, "must have Load context")
Example #9
Source File: source.py From pytest with MIT License | 6 votes |
def get_statement_startend2(lineno: int, node: ast.AST) -> Tuple[int, Optional[int]]: # flatten all statements and except handlers into one lineno-list # AST's line numbers start indexing at 1 values = [] # type: List[int] for x in ast.walk(node): if isinstance(x, (ast.stmt, ast.ExceptHandler)): values.append(x.lineno - 1) for name in ("finalbody", "orelse"): val = getattr(x, name, None) # type: Optional[List[ast.stmt]] if val: # treat the finally/orelse part as its own statement values.append(val[0].lineno - 1 - 1) values.sort() insert_index = bisect_right(values, lineno) start = values[insert_index - 1] if insert_index >= len(values): end = None else: end = values[insert_index] return start, end
Example #10
Source File: source.py From python-netsurv with MIT License | 6 votes |
def get_statement_startend2(lineno, node): import ast # flatten all statements and except handlers into one lineno-list # AST's line numbers start indexing at 1 values = [] for x in ast.walk(node): if isinstance(x, (ast.stmt, ast.ExceptHandler)): values.append(x.lineno - 1) for name in ("finalbody", "orelse"): val = getattr(x, name, None) if val: # treat the finally/orelse part as its own statement values.append(val[0].lineno - 1 - 1) values.sort() insert_index = bisect_right(values, lineno) start = values[insert_index - 1] if insert_index >= len(values): end = None else: end = values[insert_index] return start, end
Example #11
Source File: _assertionnew.py From scylla with Apache License 2.0 | 5 votes |
def visit_Module(self, mod): for stmt in mod.body: self.visit(stmt)
Example #12
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_importfrom(self): imp = ast.ImportFrom(None, [ast.alias("x", None)], -42) self.stmt(imp, "level less than -1") self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
Example #13
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def _assertTrueorder(self, ast_node, parent_pos): if not isinstance(ast_node, ast.AST) or ast_node._fields is None: return if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)): node_pos = (ast_node.lineno, ast_node.col_offset) self.assertTrue(node_pos >= parent_pos) parent_pos = (ast_node.lineno, ast_node.col_offset) for name in ast_node._fields: value = getattr(ast_node, name) if isinstance(value, list): for child in value: self._assertTrueorder(child, parent_pos) elif value is not None: self._assertTrueorder(value, parent_pos)
Example #14
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_base_classes(self): self.assertTrue(issubclass(ast.For, ast.stmt)) self.assertTrue(issubclass(ast.Name, ast.expr)) self.assertTrue(issubclass(ast.stmt, ast.AST)) self.assertTrue(issubclass(ast.expr, ast.AST)) self.assertTrue(issubclass(ast.comprehension, ast.AST)) self.assertTrue(issubclass(ast.Gt, ast.AST))
Example #15
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_nonlocal(self): self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
Example #16
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_starred(self): left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())], ast.Store()) assign = ast.Assign([left], ast.Num(4)) self.stmt(assign, "must have Store context")
Example #17
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def test_expr(self): e = ast.Expr(ast.Name("x", ast.Store())) self.stmt(e, "must have Load context")
Example #18
Source File: test_ast.py From ironpython3 with Apache License 2.0 | 5 votes |
def _assertTrueorder(self, ast_node, parent_pos): if not isinstance(ast_node, ast.AST) or ast_node._fields is None: return if isinstance(ast_node, (ast.expr, ast.stmt, ast.excepthandler)): node_pos = (ast_node.lineno, ast_node.col_offset) self.assertTrue(node_pos >= parent_pos) parent_pos = (ast_node.lineno, ast_node.col_offset) for name in ast_node._fields: value = getattr(ast_node, name) if isinstance(value, list): for child in value: self._assertTrueorder(child, parent_pos) elif value is not None: self._assertTrueorder(value, parent_pos)
Example #19
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_for(self): x = ast.Name("x", ast.Store()) y = ast.Name("y", ast.Load()) p = ast.Pass() self.stmt(ast.For(x, y, [], []), "empty body on For") self.stmt(ast.For(ast.Name("x", ast.Load()), y, [p], []), "must have Store context") self.stmt(ast.For(x, ast.Name("y", ast.Store()), [p], []), "must have Load context") e = ast.Expr(ast.Name("x", ast.Store())) self.stmt(ast.For(x, y, [e], []), "must have Load context") self.stmt(ast.For(x, y, [p], [e]), "must have Load context")
Example #20
Source File: _assertionnew.py From scylla with Apache License 2.0 | 5 votes |
def _is_ast_stmt(node): return isinstance(node, ast.stmt)
Example #21
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_starred(self): left = ast.List([ast.Starred(ast.Name("x", ast.Load()), ast.Store())], ast.Store()) assign = ast.Assign([left], ast.Num(4)) self.stmt(assign, "must have Store context")
Example #22
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_expr(self): e = ast.Expr(ast.Name("x", ast.Store())) self.stmt(e, "must have Load context")
Example #23
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_nonlocal(self): self.stmt(ast.Nonlocal([]), "empty names on Nonlocal")
Example #24
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_importfrom(self): imp = ast.ImportFrom(None, [ast.alias("x", None)], -42) self.stmt(imp, "level less than -1") self.stmt(ast.ImportFrom(None, [], 0), "empty names on ImportFrom")
Example #25
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_import(self): self.stmt(ast.Import([]), "empty names on Import")
Example #26
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_assert(self): self.stmt(ast.Assert(ast.Name("x", ast.Store()), None), "must have Load context") assrt = ast.Assert(ast.Name("x", ast.Load()), ast.Name("y", ast.Store())) self.stmt(assrt, "must have Load context")
Example #27
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_raise(self): r = ast.Raise(None, ast.Num(3)) self.stmt(r, "Raise with cause but no exception") r = ast.Raise(ast.Name("x", ast.Store()), None) self.stmt(r, "must have Load context") r = ast.Raise(ast.Num(4), ast.Name("x", ast.Store())) self.stmt(r, "must have Load context")
Example #28
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_if(self): self.stmt(ast.If(ast.Num(3), [], []), "empty body on If") i = ast.If(ast.Name("x", ast.Store()), [ast.Pass()], []) self.stmt(i, "must have Load context") i = ast.If(ast.Num(3), [ast.Expr(ast.Name("x", ast.Store()))], []) self.stmt(i, "must have Load context") i = ast.If(ast.Num(3), [ast.Pass()], [ast.Expr(ast.Name("x", ast.Store()))]) self.stmt(i, "must have Load context")
Example #29
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_while(self): self.stmt(ast.While(ast.Num(3), [], []), "empty body on While") self.stmt(ast.While(ast.Name("x", ast.Store()), [ast.Pass()], []), "must have Load context") self.stmt(ast.While(ast.Num(3), [ast.Pass()], [ast.Expr(ast.Name("x", ast.Store()))]), "must have Load context")
Example #30
Source File: test_ast.py From Fluid-Designer with GNU General Public License v3.0 | 5 votes |
def test_funcdef(self): a = ast.arguments([], None, [], [], None, []) f = ast.FunctionDef("x", a, [], [], None) self.stmt(f, "empty body on FunctionDef") f = ast.FunctionDef("x", a, [ast.Pass()], [ast.Name("x", ast.Store())], None) self.stmt(f, "must have Load context") f = ast.FunctionDef("x", a, [ast.Pass()], [], ast.Name("x", ast.Store())) self.stmt(f, "must have Load context") def fac(args): return ast.FunctionDef("x", args, [ast.Pass()], [], None) self._check_arguments(fac, self.stmt)