我们从Python开源项目中,提取了以下8个代码示例,用于说明如何使用ast.excepthandler()。
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)
def _assertTrueorder(self, ast_node, parent_pos, reverse_check = False): def should_reverse_check(parent, child): # In some situations, the children of nodes occur before # their parents, for example in a.b.c, a occurs before b # but a is a child of b. if isinstance(parent, ast.Call): if parent.func == child: return True if isinstance(parent, (ast.Attribute, ast.Subscript)): return True return False 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) if reverse_check: self.assertTrue(node_pos <= parent_pos) else: 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, should_reverse_check(ast_node, child)) elif value is not None: self._assertTrueorder(value, parent_pos, should_reverse_check(ast_node, value))