我们从Python开源项目中,提取了以下12个代码示例,用于说明如何使用_ast.ExceptHandler()。
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 l = [] for x in ast.walk(node): if isinstance(x, _ast.stmt) or isinstance(x, _ast.ExceptHandler): l.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 l.append(val[0].lineno - 1 - 1) l.sort() insert_index = bisect_right(l, lineno) start = l[insert_index - 1] if insert_index >= len(l): end = None else: end = l[insert_index] return start, end
def visitTryExcept(self, node): self.reduce(node.body) self.reduce(node.orelse) len_body = len(node.body) if len_body == 0: node.body.append(Pass(node)) for hndlr in node.handlers: self.reduce(hndlr.body) if len(hndlr.body) == 0: hndlr.body.append(Pass(hndlr)) if len_body == 0: node.handlers = [_ast.ExceptHandler(type=None, name=None, body=[Pass(node)], lineno=node.lineno, col_offset=node.col_offset)] return len_body == 0 and len(node.orelse) == 0