我们从Python开源项目中,提取了以下11个代码示例,用于说明如何使用ast.ExceptHandler()。
def occursIn(sub, super): """Does the first AST occur as a subtree of the second?""" superStatementTypes = [ ast.Module, ast.Interactive, ast.Suite, ast.FunctionDef, ast.ClassDef, ast.For, ast.While, ast.If, ast.With, ast.Try, ast.ExceptHandler ] if (not isinstance(super, ast.AST)): return False if type(sub) == type(super) and compareASTs(sub, super, checkEquality=True) == 0: return True # we know that a statement can never occur in an expression # (or in a non-statement-holding statement), so cut the search off now to save time. if isStatement(sub) and type(super) not in superStatementTypes: return False for child in ast.iter_child_nodes(super): if occursIn(sub, child): return True return False
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")
def get_coverable_nodes(cls): return { ast.Assert, ast.Assign, ast.AugAssign, ast.Break, ast.Continue, ast.Delete, ast.Expr, ast.Global, ast.Import, ast.ImportFrom, ast.Nonlocal, ast.Pass, ast.Raise, ast.Return, ast.FunctionDef, ast.ClassDef, ast.TryExcept, ast.TryFinally, ast.ExceptHandler, ast.If, ast.For, ast.While, }
def get_coverable_nodes(cls): return { ast.Assert, ast.Assign, ast.AugAssign, ast.Break, ast.Continue, ast.Delete, ast.Expr, ast.Global, ast.Import, ast.ImportFrom, ast.Nonlocal, ast.Pass, ast.Raise, ast.Return, ast.ClassDef, ast.FunctionDef, ast.Try, ast.ExceptHandler, ast.If, ast.For, ast.While, }
def mutate_ExceptHandler(self, node): if node.body and isinstance(node.body[0], ast.Raise): raise MutationResign() return ast.ExceptHandler(type=node.type, name=node.name, body=[ast.Raise()])
def mutate_ExceptHandler(self, node): if len(node.body) == 1 and isinstance(node.body[0], ast.Pass): raise MutationResign() return ast.ExceptHandler(type=node.type, name=node.name, body=[ast.Pass()])
def __init__(self): super().__init__() for node_class in self.get_coverable_nodes(): visit_method_name = 'visit_' + node_class.__name__ if not hasattr(self, visit_method_name): if node_class == ast.ExceptHandler: setattr(self, visit_method_name, self.inject_inside_visit) else: setattr(self, visit_method_name, self.inject_before_visit)
def visit_Attribute(self, node): call_path = list(self.compose_call_path(node)) if '.'.join(call_path) == 'sys.maxint': self.errors.append( B304(node.lineno, node.col_offset) ) elif len(call_path) == 2 and call_path[1] == 'message': name = call_path[0] for elem in reversed(self.node_stack[:-1]): if isinstance(elem, ast.ExceptHandler) and elem.name == name: self.errors.append( B306(node.lineno, node.col_offset) ) break
def node_defines_name(node, name): """ Check if the specified statement node defines symbol *name*. :param node: The node to check. :param name: The symbol name to check. :return: Whether or not the node defines the symbole specified. :rtype: bool """ if isinstance(name, ast.Name): name = name.id if isinstance(node, ast.Assign): if node_targets_name(node, name): return True if isinstance(node.value, (ast.DictComp, ast.ListComp, ast.SetComp)): return node_defines_name(node.value, name) elif isinstance(node, ast.ClassDef): return node.name == name # these ones all assume the iterable will be executed at least once elif isinstance(node, (ast.DictComp, ast.GeneratorExp, ast.ListComp, ast.SetComp)): for generator in node.generators: target = generator.target if isinstance(target, ast.Name): if target.id == name: return True continue for child_node in iter_child_expr_nodes(target): if isinstance(child_node, ast.Name) and child_node.id == name: return True return False elif isinstance(node, ast.ExceptHandler): if isinstance(node.name, ast.Name): return node.name.id == name elif isinstance(node.name, str): return node.name == name elif isinstance(node, ast.Expr): if isinstance(node.value, (ast.DictComp, ast.GeneratorExp, ast.ListComp, ast.SetComp)): return node_defines_name(node.value, name) elif isinstance(node, ast.For): return isinstance(node.target, ast.Name) and node.target.id == name elif isinstance(node, ast.FunctionDef): return node.name == name elif isinstance(node, (ast.Import, ast.ImportFrom)): return next((alias for alias in node.names if (alias.asname or alias.name) == name), None) is not None return False