Python ast.TryExcept() Examples
The following are 18
code examples of ast.TryExcept().
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: modules.py From ansible-testing with GNU General Public License v3.0 | 6 votes |
def _find_blacklist_imports(self): for child in self.ast.body: names = [] if isinstance(child, ast.Import): names.extend(child.names) elif isinstance(child, ast.TryExcept): bodies = child.body for handler in child.handlers: bodies.extend(handler.body) for grandchild in bodies: if isinstance(grandchild, ast.Import): names.extend(grandchild.names) for name in names: for blacklist_import, options in BLACKLIST_IMPORTS.items(): if re.search(blacklist_import, name.name): msg = options['msg'] new_only = options['new_only'] if self._is_new_module() and new_only: self.errors.append(msg) elif not new_only: self.errors.append(msg)
Example #2
Source File: modules.py From ansible-testing with GNU General Public License v3.0 | 6 votes |
def _find_has_import(self): for child in self.ast.body: found_try_except_import = False found_has = False if isinstance(child, ast.TryExcept): bodies = child.body for handler in child.handlers: bodies.extend(handler.body) for grandchild in bodies: if isinstance(grandchild, ast.Import): found_try_except_import = True if isinstance(grandchild, ast.Assign): for target in grandchild.targets: if target.id.lower().startswith('has_'): found_has = True if found_try_except_import and not found_has: self.warnings.append('Found Try/Except block without HAS_ ' 'assginment')
Example #3
Source File: annotate.py From pasta with Apache License 2.0 | 6 votes |
def visit_TryFinally(self, node): # Try with except and finally is a TryFinally with the first statement as a # TryExcept in Python2 self.attr(node, 'open_try', ['try', self.ws, ':', self.ws_oneline], default='try:\n') # TODO(soupytwist): Find a cleaner solution for differentiating this. if len(node.body) == 1 and self.check_is_continued_try(node.body[0]): node.body[0].is_continued = True self.visit(node.body[0]) else: for stmt in self.indented(node, 'body'): self.visit(stmt) self.attr(node, 'open_finally', [self.ws, 'finally', self.ws, ':', self.ws_oneline], default='finally:\n') for stmt in self.indented(node, 'finalbody'): self.visit(stmt)
Example #4
Source File: ast2.py From gast with BSD 3-Clause "New" or "Revised" License | 6 votes |
def visit_Try(self, node): if node.finalbody: new_node = ast.TryFinally( self._visit(node.body), self._visit(node.finalbody) ) else: new_node = ast.TryExcept( self._visit(node.body), self._visit(node.handlers), self._visit(node.orelse), ) ast.copy_location(new_node, node) return new_node # expr
Example #5
Source File: ast_generator.py From bigcode-tools with MIT License | 5 votes |
def is_try(node): return hasattr(ast, "Try") and isinstance(node, ast.Try) or \ hasattr(ast, "TryExcept") and isinstance(node, ast.TryExcept) or \ hasattr(ast, "TryFinally") and isinstance(node, ast.TryFinally)
Example #6
Source File: annotate.py From pasta with Apache License 2.0 | 5 votes |
def check_is_continued_try(self, node): """Return True iff the TryExcept node is a continued `try` in the source.""" return (isinstance(node, ast.TryExcept) and self.tokens.peek_non_whitespace().src != 'try')
Example #7
Source File: checker.py From Transcrypt with Apache License 2.0 | 5 votes |
def getAlternatives(n): if isinstance(n, (ast.If, ast.TryFinally)): return [n.body] if isinstance(n, ast.TryExcept): return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
Example #8
Source File: checker.py From Transcrypt with Apache License 2.0 | 5 votes |
def getNodeType(node_class): return node_class.__name__.upper() # Python >= 3.3 uses ast.Try instead of (ast.TryExcept + ast.TryFinally)
Example #9
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_try_stmt_4(p): '''try_stmt : TRY COLON suite try_stmt_plus ELSE COLON suite FINALLY COLON suite''' # 1 2 3 4 5 6 7 8 9 10 p[0] = ast.TryFinally([ast.TryExcept(p[3], p[4], p[7], rule=inspect.currentframe().f_code.co_name, **p[1][1])], p[10], rule=inspect.currentframe().f_code.co_name, **p[1][1])
Example #10
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_try_stmt_3(p): '''try_stmt : TRY COLON suite try_stmt_plus ELSE COLON suite''' # 1 2 3 4 5 6 7 p[0] = ast.TryExcept(p[3], p[4], p[7], rule=inspect.currentframe().f_code.co_name, **p[1][1])
Example #11
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_try_stmt_2(p): '''try_stmt : TRY COLON suite try_stmt_plus FINALLY COLON suite''' # 1 2 3 4 5 6 7 p[0] = ast.TryFinally([ast.TryExcept(p[3], p[4], [], rule=inspect.currentframe().f_code.co_name, **p[1][1])], p[7], rule=inspect.currentframe().f_code.co_name, **p[1][1])
Example #12
Source File: hgawk_grammar.py From histogrammar-python with Apache License 2.0 | 5 votes |
def p_try_stmt_1(p): '''try_stmt : TRY COLON suite try_stmt_plus''' # 1 2 3 4 p[0] = ast.TryExcept(p[3], p[4], [], rule=inspect.currentframe().f_code.co_name, **p[1][1])
Example #13
Source File: checker.py From blackmamba with MIT License | 5 votes |
def getAlternatives(n): if isinstance(n, (ast.If, ast.TryFinally)): return [n.body] if isinstance(n, ast.TryExcept): return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
Example #14
Source File: checker.py From blackmamba with MIT License | 5 votes |
def getNodeType(node_class): return node_class.__name__.upper() # Python >= 3.3 uses ast.Try instead of (ast.TryExcept + ast.TryFinally)
Example #15
Source File: checker.py From pyflakes with MIT License | 5 votes |
def getAlternatives(n): if isinstance(n, (ast.If, ast.TryFinally)): return [n.body] if isinstance(n, ast.TryExcept): return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
Example #16
Source File: checker.py From linter-pylama with MIT License | 5 votes |
def getAlternatives(n): if isinstance(n, (ast.If, ast.TryFinally)): return [n.body] if isinstance(n, ast.TryExcept): return [n.body + n.orelse] + [[hdl] for hdl in n.handlers]
Example #17
Source File: translation.py From mochi with MIT License | 4 votes |
def translate_try(self, exp): if len(exp) < 2: raise MochiSyntaxError(exp, self.filename) body_exp = [] handler_exps = [] orelse_exp = [] final_body_exp = [] for expr in exp: if issequence_except_str(expr) and len(expr) > 1 and isinstance(expr[0], Symbol): expr_name = expr[0].name if expr_name == 'finally': final_body_exp = expr[1:] continue elif expr_name == 'orelse': orelse_exp = expr[1:] continue elif expr_name == 'except': handler_exps.append(expr[1:]) continue body_exp.append(expr) body = self._translate_sequence(body_exp, True) handlers = self._translate_handlers(handler_exps) orelse = self._translate_sequence(orelse_exp, True) final_body = self._translate_sequence(final_body_exp, True) if GE_PYTHON_34: return (ast.Try(body=body, handlers=handlers, orelse=orelse, finalbody=final_body, lineno=exp[0].lineno, col_offset=0),), self.translate(EMPTY_SYM, False)[1] else: if len(handlers) == 0: return (ast.TryFinally(body=body, finalbody=final_body, lineno=exp[0].lineno, col_offset=0),), self.translate(EMPTY_SYM, False)[1] else: return (ast.TryFinally(body=[ast.TryExcept(body=body, handlers=handlers, orelse=orelse, lineno=exp[0].lineno, col_offset=0)], finalbody=final_body, lineno=exp[0].lineno, col_offset=0),), self.translate(EMPTY_SYM, False)[1]
Example #18
Source File: ast_utils.py From pasta with Apache License 2.0 | 4 votes |
def get_last_child(node): """Get the last child node of a block statement. The input must be a block statement (e.g. ast.For, ast.With, etc). Examples: 1. with first(): second() last() 2. try: first() except: second() finally: last() In both cases, the last child is the node for `last`. """ if isinstance(node, ast.Module): try: return node.body[-1] except IndexError: return None if isinstance(node, ast.If): if (len(node.orelse) == 1 and isinstance(node.orelse[0], ast.If) and fmt.get(node.orelse[0], 'is_elif')): return get_last_child(node.orelse[0]) if node.orelse: return node.orelse[-1] elif isinstance(node, ast.With): if (len(node.body) == 1 and isinstance(node.body[0], ast.With) and fmt.get(node.body[0], 'is_continued')): return get_last_child(node.body[0]) elif hasattr(ast, 'Try') and isinstance(node, ast.Try): if node.finalbody: return node.finalbody[-1] if node.orelse: return node.orelse[-1] elif hasattr(ast, 'TryFinally') and isinstance(node, ast.TryFinally): if node.finalbody: return node.finalbody[-1] elif hasattr(ast, 'TryExcept') and isinstance(node, ast.TryExcept): if node.orelse: return node.orelse[-1] if node.handlers: return get_last_child(node.handlers[-1]) return node.body[-1]