Python ast 模块,With() 实例源码

我们从Python开源项目中,提取了以下10个代码示例,用于说明如何使用ast.With()

项目:ITAP-django    作者:krivers    | 项目源码 | 文件源码
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
项目:end    作者:nya3jp    | 项目源码 | 文件源码
def get_compound_bodies(node):
    """Returns a list of bodies of a compound statement node.

    Args:
        node: AST node.

    Returns:
        A list of bodies of the node. If the given node does not represent
        a compound statement, an empty list is returned.
    """
    if isinstance(node, (ast.Module, ast.FunctionDef, ast.ClassDef, ast.With)):
        return [node.body]
    elif isinstance(node, (ast.If, ast.While, ast.For)):
        return [node.body, node.orelse]
    elif PY2 and isinstance(node, ast.TryFinally):
        return [node.body, node.finalbody]
    elif PY2 and isinstance(node, ast.TryExcept):
        return [node.body, node.orelse] + [h.body for h in node.handlers]
    elif PY3 and isinstance(node, ast.Try):
        return ([node.body, node.orelse, node.finalbody]
                + [h.body for h in node.handlers])
    end
    return []
项目:peval    作者:fjarri    | 项目源码 | 文件源码
def _build_node_cfg(node):
    handlers = {
        ast.If: _build_if_cfg,
        ast.For: _build_loop_cfg,
        ast.While: _build_loop_cfg,
        ast.With: _build_with_cfg,
        ast.Break: _build_break_cfg,
        ast.Continue: _build_continue_cfg,
        ast.Return: _build_return_cfg,
        ast.Try: _build_try_cfg,
        }

    if type(node) in handlers:
        handler = handlers[type(node)]
    else:
        handler = _build_statement_cfg

    return handler(node)
项目:ITAP-django    作者:krivers    | 项目源码 | 文件源码
def staticVars(l, vars):
    """Determines whether the given lines change the given variables"""
    # First, if one of the variables can be modified, there might be a problem
    mutableVars = []
    for var in vars:
        if (not (hasattr(var, "type") and (var.type in [int, float, str, bool]))):
            mutableVars.append(var)

    for i in range(len(l)):
        if type(l[i]) == ast.Assign:
            for var in vars:
                if var.id in allVariableNamesUsed(l[i].targets[0]):
                    return False
        elif type(l[i]) == ast.AugAssign:
            for var in vars:
                if var.id in allVariableNamesUsed(l[i].target):
                    return False
        elif type(l[i]) in [ast.If, ast.While]:
            if not (staticVars(l[i].body, vars) and staticVars(l[i].orelse, vars)):
                return False
        elif type(l[i]) == ast.For:
            for var in vars:
                if var.id in allVariableNamesUsed(l[i].target):
                    return False
            if not (staticVars(l[i].body, vars) and staticVars(l[i].orelse, vars)):
                return False
        elif type(l[i]) in [ast.FunctionDef, ast.ClassDef, ast.Try, ast.With]:
            log("transformations\tstaticVars\tMissing type: " + str(type(l[i])), "bug")

        # If a mutable variable is used, we can't trust it
        for var in mutableVars:
            if var.id in allVariableNamesUsed(l[i]):
                return False
    return True
项目:ITAP-django    作者:krivers    | 项目源码 | 文件源码
def isStatement(a):
    """Determine whether the given node is a statement (vs an expression)"""
    return type(a) in [ ast.Module, ast.Interactive, ast.Expression, ast.Suite,
                        ast.FunctionDef, ast.ClassDef, ast.Return, ast.Delete,
                        ast.Assign, ast.AugAssign, ast.For, ast.While, 
                        ast.If, ast.With, ast.Raise, ast.Try, 
                        ast.Assert, ast.Import, ast.ImportFrom, ast.Global, 
                        ast.Expr, ast.Pass, ast.Break, ast.Continue ]
项目:web_ctp    作者:molebot    | 项目源码 | 文件源码
def test_with(self):
        p = ast.Pass()
        self.stmt(ast.With([], [p]), "empty items on With")
        i = ast.withitem(ast.Num(3), None)
        self.stmt(ast.With([i], []), "empty body on With")
        i = ast.withitem(ast.Name("x", ast.Store()), None)
        self.stmt(ast.With([i], [p]), "must have Load context")
        i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
        self.stmt(ast.With([i], [p]), "must have Store context")
项目:ouroboros    作者:pybee    | 项目源码 | 文件源码
def test_with(self):
        p = ast.Pass()
        self.stmt(ast.With([], [p]), "empty items on With")
        i = ast.withitem(ast.Num(3), None)
        self.stmt(ast.With([i], []), "empty body on With")
        i = ast.withitem(ast.Name("x", ast.Store()), None)
        self.stmt(ast.With([i], [p]), "must have Load context")
        i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
        self.stmt(ast.With([i], [p]), "must have Store context")
项目:Typpete    作者:caterinaurban    | 项目源码 | 文件源码
def infer(node, context, solver):
    if isinstance(node, ast.Assign):
        return _infer_assign(node, context, solver)
    elif isinstance(node, ast.AugAssign):
        return _infer_augmented_assign(node, context, solver)
    elif isinstance(node, ast.Return):
        if not node.value:
            return solver.z3_types.none
        return expr.infer(node.value, context, solver)
    elif isinstance(node, ast.Delete):
        return _infer_delete(node, context, solver)
    elif isinstance(node, (ast.If, ast.While)):
        return _infer_control_flow(node, context, solver)
    elif isinstance(node, ast.For):
        return _infer_for(node, context, solver)
    elif sys.version_info[0] >= 3 and sys.version_info[1] >= 5 and isinstance(node, ast.AsyncFor):
        # AsyncFor is introduced in Python 3.5
        return _infer_for(node, context, solver)
    elif isinstance(node, ast.With):
        return _infer_with(node, context, solver)
    elif sys.version_info[0] >= 3 and sys.version_info[1] >= 5 and isinstance(node, ast.AsyncWith):
        # AsyncWith is introduced in Python 3.5
        return _infer_with(node, context, solver)
    elif isinstance(node, ast.Try):
        return _infer_try(node, context, solver)
    elif isinstance(node, ast.FunctionDef):
        return _infer_func_def(node, context, solver)
    elif isinstance(node, ast.ClassDef):
        return _infer_class_def(node, context, solver)
    elif isinstance(node, ast.Expr):
        expr.infer(node.value, context, solver)
    elif isinstance(node, ast.Import):
        return _infer_import(node, context, solver)
    elif isinstance(node, ast.ImportFrom):
        return _infer_import_from(node, context, solver)
    return solver.z3_types.none
项目:kbe_server    作者:xiaohaoppy    | 项目源码 | 文件源码
def test_with(self):
        p = ast.Pass()
        self.stmt(ast.With([], [p]), "empty items on With")
        i = ast.withitem(ast.Num(3), None)
        self.stmt(ast.With([i], []), "empty body on With")
        i = ast.withitem(ast.Name("x", ast.Store()), None)
        self.stmt(ast.With([i], [p]), "must have Load context")
        i = ast.withitem(ast.Num(3), ast.Name("x", ast.Load()))
        self.stmt(ast.With([i], [p]), "must have Store context")
项目:tidy    作者:cyrus-    | 项目源码 | 文件源码
def _yield_items(stmts):
        match_head = None
        match_rules = None
        for stmt in stmts:
            if match_head is not None:
                if (isinstance(stmt, ast.With) and
                        not BlockWithBinding.is_with_binding(stmt)):
                    if stmt.optional_vars is not None:
                        raise _errors.TyError("Invalid rule form.", stmt)
                    match_rules.append(stmt)
                    continue
                else:
                    if len(match_rules) == 0:
                        raise _errors.TyError(
                            "No match rules.", match_head)
                    yield BlockMatchExpr(match_head, match_rules)
                    match_head = match_rules = None
                    # falls through below
            if match_head is None:
                if isinstance(stmt, ast.Expr):
                    if BlockMatchExpr.is_match_head(stmt):
                        match_head = stmt
                        match_rules = []
                    else:
                        yield BlockExprExpr(stmt)
                elif isinstance(stmt, ast.With):
                    if (BlockWithBinding.is_with_binding(stmt)):
                        yield BlockWithBinding(stmt)
                    else:
                        raise _errors.TyError("Invalid with form.", stmt)
                elif isinstance(stmt, ast.Assign):
                    yield BlockLet(stmt)
                elif isinstance(stmt, ast.If):
                    yield BlockIfExpr(stmt)
                elif isinstance(stmt, ast.Pass):
                    yield BlockPassExpr(stmt)
                elif isinstance(stmt, ast.FunctionDef):
                    if BlockFunctionDefExpr.check_valid_function_def(stmt):
                        yield BlockFunctionDefExpr(stmt)
                else:
                    raise _errors.TyError("Statement form not supported.", stmt)
        # finish making match expression at the end of the block if necessary
        if match_head is not None:
            if len(match_rules) == 0:
                raise _errors.TyError(
                    "No match rules.", match_head)
            yield BlockMatchExpr(match_head, match_rules)