def BUILD_SET(self, instr): nitems = instr.oparg nodes = [] list_ = _ast.Set(elts=nodes, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0) for i in range(nitems): nodes.insert(0, self.pop_ast_item()) self.push_ast_item(list_)
def SLICE_2(self, instr): 'obj[:stop]' upper = self.pop_ast_item() value = self.pop_ast_item() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=None, step=None, upper=upper, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Load(), **kw) self.push_ast_item(subscr)
def SLICE_1(self, instr): 'obj[lower:]' lower = self.ast_stack.pop() value = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) slice = _ast.Slice(lower=lower, step=None, upper=None, **kw) subscr = _ast.Subscript(value=value, slice=slice, ctx=_ast.Load(), **kw) self.ast_stack.append(subscr)
def LOAD_ATTR(self, instr): name = self.pop_ast_item() attr = instr.arg get_attr = _ast.Attribute(value=name, attr=attr, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0) self.push_ast_item(get_attr)
def BUILD_LIST(self, instr): nitems = instr.oparg nodes = [] list_ = _ast.List(elts=nodes, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0) for i in range(nitems): nodes.insert(0, self.ast_stack.pop()) self.ast_stack.append(list_)
def LOAD_ATTR(self, instr): name = self.ast_stack.pop() attr = instr.arg get_attr = _ast.Attribute(value=name, attr=attr, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0) self.ast_stack.append(get_attr)
def test_issue793(self): import _ast as ast body = ast.Module([ ast.TryExcept([ast.Pass(lineno=2, col_offset=4)], [ ast.ExceptHandler(ast.Name( 'Exception', ast.Load(), lineno=3, col_offset=0), None, [], lineno=4, col_offset=0) ], [], lineno=1, col_offset=0) ]) exec(compile(body, '<string>', 'exec'))
def BUILD_TUPLE(self, instr): nitems = instr.oparg nodes = [] list_ = _ast.Tuple(elts=nodes, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0) for i in range(nitems): nodes.insert(0, self.ast_stack.pop()) if any([item == 'CLOSURE' for item in nodes]): assert all([item == 'CLOSURE' for item in nodes]) return self.ast_stack.append(list_)
def test_empty_initialization(self): ast = self.ast def com(node): return compile(node, "<test>", "exec") mod = ast.Module() raises(AttributeError, getattr, mod, "body") exc = raises(TypeError, com, mod).value assert str(exc) == "required field \"body\" missing from Module" expr = ast.Name() expr.id = "hi" expr.ctx = ast.Load() expr.lineno = 4 exc = raises(TypeError, com, ast.Module([ast.Expr(expr)])).value assert (str(exc) == "required field \"lineno\" missing from stmt" or # cpython str(exc) == "required field \"lineno\" missing from Expr") # pypy, better
def BINARY_SUBSCR(self, instr): index = self.ast_stack.pop() value = self.ast_stack.pop() kw = dict(lineno=instr.lineno, col_offset=0) index = self.format_slice(index, kw) subscr = _ast.Subscript(value=value, slice=index, ctx=_ast.Load(), **kw) self.ast_stack.append(subscr)
def queue_scope(self, node, state, parent_scope): assert (node in self.cfgs) == (node in self.scopes) if node in self.cfgs: assert state == self.input_states[(node, self.first_block( self.cfgs[node]))] else: if isinstance(node, _ast.FunctionDef): if ast_utils.has_yields(node): body = node.body else: body = node.body + [ _ast.Return(_ast.Name( "None", _ast.Load(), not_real=True), not_real=True) ] self.scopes[node] = FunctionScope(parent_scope) elif isinstance(node, _ast.Lambda): body = [ _ast.Return(node.body, lineno=node.lineno, col_offset=node.col_offset, not_real=True) ] self.scopes[node] = FunctionScope(parent_scope) elif isinstance(node, _ast.Module): body = node.body assert parent_scope is None self.scopes[node] = ModuleScope() elif isinstance(node, _ast.ClassDef): body = node.body self.scopes[node] = ClassScope(parent_scope) else: raise Exception(node) cfg = cfa(node, body) # cfg.show() self.cfgs[node] = cfg self.input_states[(node, self.first_block(cfg))] = state self.mark_changed((node, self.first_block(cfg))) if isinstance(node, (_ast.FunctionDef, _ast.ClassDef)): scope = self.scopes[node] for n in ast_utils.find_global_vars(node): scope._set_global(n) return self.scopes[node]
def _fix_ast(myast): import _ast # Remove Pass nodes from the end of the ast while len(myast.body) > 0 and isinstance(myast.body[-1], _ast.Pass): del myast.body[-1] # Add a return node at the end of the ast if not present if len(myast.body) < 1 or not isinstance(myast.body[-1], _ast.Return): name = _ast.Name(id='None', ctx=_ast.Load(), lineno=0, col_offset=0) myast.body.append(ast.Return(name)) # remove _decorator list which sometimes confuses ast visitor try: indx = myast._fields.index('decorator_list') except ValueError: return else: myast.decorator_list = []
def make_subscript(i, bytecode, context=None): op = bytecode[i][2] if op == STORE_SUBSCR: # TOS1[TOS] = TOS2 i, index_expr = Statement.make_expr(i - 1, bytecode) i, arr_expr = Statement.make_expr(i - 1, bytecode, context=_ast.Store()) i, rhs_expr = Statement.make_expr(i - 1, bytecode) lhs_expr = _ast.Subscript(arr_expr, index_expr, _ast.Store()) return i, _ast.Assign([lhs_expr], rhs_expr) else: if context is None: context = _ast.Load() # BINARY_SUBSCR: TOS1[TOS] and DELETE_SUBSCR TOS1[TOS] i, index_expr = Statement.make_expr(i - 1, bytecode) i, arr_expr = Statement.make_expr(i - 1, bytecode) return i, _ast.Subscript(arr_expr, index_expr, context)
def _assign_list_form_variables(self, where_function_ast_node): copy_of_body = copy.deepcopy(where_function_ast_node.body) where_function_ast_node.body = [] for assignment_expression in copy_of_body: variable_name = assignment_expression.targets[0].id self.spec_metadata.add_feature_variable(self.feature_name, variable_name) variable_values = assignment_expression.value where_function_ast_node.body.append( _ast.Assign(targets=[ _ast.Subscript( value=_ast.Name(id='injectable_values', ctx=_ast.Load()), slice=_ast.Index(value=_ast.Str(s=variable_name)), ctx=_ast.Store()) ], value=variable_values))
def wrap_value(val, ctx=_ast.Load()): if not val: return kast.none import nodes if isinstance(val, nodes.Argument): if val.name: return ast.Name(id=val.name, ctx=ctx) # if FunctionCall # return ast.Name(id=val.name, ctx=_ast.Param()) # if FunctionDef else: return wrap_value(val.value) if isinstance(val, ast.AST): return val if isinstance(val, str): return kast.Str(val) if isinstance(val, int): return kast.Num(val) if isinstance(val, float): return kast.Num(val) if isinstance(val, tuple): return kast.Tuple(list(map(wrap_value, val)), ast.Load()) if isinstance(val, list): return kast.List(list(map(wrap_value, val)), ast.Load()) if isinstance(val, dict): return kast.Dict(list(map(wrap_value, val)), ast.Load()) t = type(val) raise Exception("UNKNOWN TYPE %s : %s !" % (val, t))
def test_compare(self): import ast def _mod(mod, msg=None, mode="exec", exc=ValueError): mod.lineno = mod.col_offset = 0 ast.fix_missing_locations(mod) exc = raises(exc, compile, mod, "<test>", mode) if msg is not None: assert msg in str(exc.value) def _expr(node, msg=None, exc=ValueError): mod = ast.Module([ast.Expr(node)]) _mod(mod, msg, exc=exc) left = ast.Name("x", ast.Load()) comp = ast.Compare(left, [ast.In()], []) _expr(comp, "no comparators") comp = ast.Compare(left, [ast.In()], [ast.Num(4), ast.Num(5)]) _expr(comp, "different number of comparators and operands") comp = ast.Compare(ast.Num("blah"), [ast.In()], [left]) _expr(comp, "non-numeric", exc=TypeError) comp = ast.Compare(left, [ast.In()], [ast.Num("blah")]) _expr(comp, "non-numeric", exc=TypeError)
def LOAD_GLOBAL(self, instr): name = _ast.Name(id=instr.arg, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0) self.push_ast_item(name)
def test_bug_null_in_objspace_type(self): import ast code = ast.Expression(lineno=1, col_offset=1, body=ast.ListComp(lineno=1, col_offset=1, elt=ast.Call(lineno=1, col_offset=1, func=ast.Name(lineno=1, col_offset=1, id='str', ctx=ast.Load(lineno=1, col_offset=1)), args=[ast.Name(lineno=1, col_offset=1, id='x', ctx=ast.Load(lineno=1, col_offset=1))], keywords=[]), generators=[ast.comprehension(lineno=1, col_offset=1, target=ast.Name(lineno=1, col_offset=1, id='x', ctx=ast.Store(lineno=1, col_offset=1)), iter=ast.List(lineno=1, col_offset=1, elts=[ast.Num(lineno=1, col_offset=1, n=23)], ctx=ast.Load(lineno=1, col_offset=1, )), ifs=[], is_async=False)])) compile(code, '<template>', 'eval')
def STORE_NAME(self, instr): value = self.pop_ast_item() value = self.process_ifexpr(value) if isinstance(value, _ast.Import): if value.from_: assert isinstance(self._ast_stack[-1], _ast.ImportFrom) from_ = self.pop_ast_item() as_name = instr.arg name = from_.names[0].name if as_name != name: from_.names[0].asname = as_name self.push_ast_item(from_) else: as_name = instr.arg if value.names[0].asname is None: base_name = value.names[0].name.split('.')[0] if base_name != as_name: value.names[0].asname = as_name self.push_ast_item(value) elif isinstance(value, (_ast.Attribute)) and isinstance( value.value, (_ast.Import)): asname = instr.arg value = value.value value.names[0].asname = asname self.push_ast_item(value) elif isinstance(value, (_ast.ClassDef, _ast.FunctionDef)): as_name = instr.arg value.name = as_name self.push_ast_item(value) elif isinstance(value, _ast.AugAssign): self.push_ast_item(value) elif isinstance(value, _ast.Assign): _ = self.pop_ast_item() assname = _ast.Name(instr.arg, _ast.Store(), lineno=instr.lineno, col_offset=0) if _ is value.value or isinstance(_, _ast.Assign): value.targets.append(assname) else: if not isinstance(value.targets, _ast.Tuple): value.targets = [_ast.Tuple(value.targets, _ast.Store())] value.value = _ast.Tuple([value.value], _ast.Load()) value.targets[0].lineno = value.targets[0].elts[0].lineno value.targets[0].col_offset = value.targets[0].elts[ 0].col_offset value.value.lineno = value.value.elts[0].lineno value.value.col_offset = value.value.elts[0].col_offset value.targets[0].elts.append(assname) value.value.elts.append(_) self.push_ast_item(value) else: assname = _ast.Name(instr.arg, _ast.Store(), lineno=instr.lineno, col_offset=0) assign = _ast.Assign(targets=[assname], value=value, lineno=instr.lineno, col_offset=0) self.push_ast_item(assign)
def make_name(i, bytecode, context=None): arg = bytecode[i][3] if context is None: context = _ast.Load() return i, _ast.Name(arg, context)
def test_identifier(self): ast = self.ast name = ast.Name("name_word", ast.Load()) assert name.id == "name_word" name.id = "hi" assert name.id == "hi"
def _replace_with_block_context(with_node, block_type): with_node.items[0].context_expr = _ast.Call( func=_ast.Attribute(value=_ast.Name(id='self', ctx=_ast.Load()), attr='_feature_block_context', ctx=_ast.Load()), args=[ast_proxy.ast_str(s=block_type)], keywords=[])
def test_return_value_is_filled_tuple(): # when method return ('1') assert ReturnedExpression( _ast.Return(value=_ast.Tuple(elts=['1'], ctx=_ast.Load()), lineno=1), ).value_not_none() is True
def test_return_value_is_empty_tuple(): # when method return () assert ReturnedExpression( _ast.Return(value=_ast.Tuple(elts=[], ctx=_ast.Load()), lineno=1), ).value_not_none() is False
def LOAD_GLOBAL(self, instr): name = _ast.Name(id=instr.arg, ctx=_ast.Load(), lineno=instr.lineno, col_offset=0) self.ast_stack.append(name)
def test_invalid_identitifer(self): import ast m = ast.Module([ast.Expr(ast.Name(b"x", ast.Load()))]) ast.fix_missing_locations(m) exc = raises(TypeError, compile, m, "<test>", "exec")
def _cfa(body, state, on_gen): assert isinstance(on_gen, list) for c in on_gen: assert callable(c) cfg = state.cfg def make_connect(cur_bid): assert isinstance(cur_bid, int) def inner(new_bid): assert isinstance(new_bid, int) state.connect([cur_bid], [new_bid]) return inner def push_block(block): block_id = len(cfg.blocks) assert block_id not in cfg.blocks cfg.blocks[block_id] = block for c in on_gen: c(block_id) return block_id cur_block = [] for b in body: if REDUCE_FORS_TO_WHILES and isinstance(b, _ast.For): if isinstance(b.iter, _ast.Call) and isinstance( b.iter.func, _ast.Name) and b.iter.func.id in ("range", "xrange"): if not b.iter.keywords and not b.iter.starargs and not b.iter.kwargs: end_var = "__wfend_%d_%d_" % (b.lineno, b.col_offset) iter_var = "__wfiter_%d_%d_" % (b.lineno, b.col_offset) if len(b.iter.args) in (1, 2): if len(b.iter.args) == 1: start = _ast.Num(0) end = b.iter.args[0] elif len(b.iter.args) == 2: start = b.iter.args[0] end = b.iter.args[1] else: start = b.iter.args[0] end = b.iter.args[1] cur_block.append( _ast.Assign([ _ast.Name( iter_var, _ast.Store(), not_real=True) ], start, lineno=b.lineno, col_offset=b.col_offset, not_real=True)) cur_block.append( _ast.Assign([ _ast.Name(end_var, _ast.Store(), not_real=True) ], end, lineno=b.lineno, col_offset=b.col_offset, not_real=True)) body = [ _ast.Assign([b.target], _ast.Name(iter_var, _ast.Load(), not_real=True), lineno=b.lineno, col_offset=b.col_offset, not_real=True), _ast.Assign([ _ast.Name( iter_var, _ast.Store(), not_real=True) ], _ast.BinOp( _ast.Name(iter_var, _ast.Load(), not_real=True), _ast.Add(), _ast.Num(1)), lineno=b.lineno, col_offset=b.col_offset, not_real=True) ] + b.body b = _ast.While(_ast.Compare( _ast.Name(iter_var, _ast.Load(), not_real=True), [_ast.Lt()], [_ast.Name(end_var, _ast.Load(), not_real=True)], lineno=b.lineno, col_offset=b.col_offset, not_real=True), body, b.orelse, not_real=True) if isinstance(b, ( _ast.Assign, _ast.AugAssign, _ast.ClassDef, _ast.Delete, _ast.Exec, _ast.Expr, _ast.FunctionDef, _ast.Global, _ast.Import, _ast.ImportFrom, _ast.Print, _ast.Pass, )): cur_block.append(b) elif isinstance(b, _ast.Assert): cur_block.append(b) if isinstance(b.test, _ast.Call) and isinstance( b.test.func, _ast.Name ) and b.test.func.id == "isinstance" and isinstance( b.test.args[0], _ast.Name) and isinstance( b.test.args[1], _ast.Name): varname = b.test.args[0].id cast = _ast.Call(_ast.Name( "__cast__", _ast.Load(), not_real=True, **pos(b)), [ _ast.Name( varname, _ast.Store(), not_real=True, **pos(b)), b.test.args[1] ], [], None, None, not_real=True, **pos(b)) assign = _ast.Assign([ _ast.Name(varname, _ast.Store(), not_real=True, **pos(b)) ], cast, not_real=True, lineno=b.lineno, col_offset=b.col_offset) cur_block.append(assign) elif isinstance(b, (_ast.Break, _ast.Continue)): f = state.add_break if isinstance( b, _ast.Break) else state.add_continue if cur_block: j = Jump() cur_block.append(j) block_id = push_block(cur_block) f(j.set_dest) f(make_connect(block_id)) else: for c in on_gen: f(c) return [] elif isinstance(b, _ast.If): br = Branch(b.test, lineno=b.lineno) cur_block.append(br) next_block = push_block(cur_block) on_gen = None # make sure this doesn't get used cur_block = [] gen_true = [br.set_true, make_connect(next_block)] gen_false = [br.set_false, make_connect(next_block)] if ENFORCE_NO_MULTIMULTI: on_gen = gen_true j1 = Jump() gen_true = [make_connect(push_block([j1])), j1.set_dest] on_gen = gen_false j2 = Jump() gen_false = [make_connect(push_block([j2])), j2.set_dest] on_gen = None assert b.body body = b.body if isinstance(b.test, _ast.Call) and isinstance( b.test.func, _ast.Name ) and b.test.func.id == "isinstance" and isinstance( b.test.args[0], _ast.Name) and isinstance( b.test.args[1], _ast.Name): varname = b.test.args[0].id cast = _ast.Call(_ast.Name( "__cast__", _ast.Load(), not_real=True, **pos(b)), [ _ast.Name( varname, _ast.Store(), not_real=True, **pos(b)), b.test.args[1] ], [], None, None, not_real=True, **pos(b)) assign = _ast.Assign([ _ast.Name(varname, _ast.Store(), not_real=True, **pos(b)) ], cast, not_real=True, lineno=b.lineno, col_offset=b.col_offset) body = [assign] + body if ADD_IF_ASSERTS: body = [_ast.Assert(b.test, None, not_real=True, **pos(b)) ] + body ending_gen = _cfa(body, state, gen_true) if b.orelse: ending_gen += _cfa(b.orelse, state, gen_false) else: ending_gen += gen_false on_gen = ending_gen if not on_gen and PRUNE_UNREACHABLE_BLOCKS: return [] elif isinstance(b, _ast.TryExcept): j = Jump() cur_block.append(j) next_block = push_block(cur_block) on_gen = [j.set_dest, make_connect(next_block)] cur_block = [] on_gen = _cfa(b.body, state, on_gen) # Set this to evaluate a string to try to defeat simple flow analysis br = Branch(_ast.Str("nonzero")) next_block = push_block([br]) on_except = [br.set_false, make_connect(next_block)] on_fine = [br.set_true, make_connect(next_block)] assert len(b.handlers) >= 1 # for handler in b.handlers: # on_except = _cfa(b.handlers[0].body, state, on_except) if b.orelse: on_fine = _cfa(b.orelse, state, on_fine) if ENFORCE_NO_MULTIMULTI: j = Jump() on_gen = on_fine next_block = push_block([j]) on_fine = [j.set_dest, make_connect(next_block)] j = Jump() on_gen = on_except next_block = push_block([j]) on_except = [j.set_dest, make_connect(next_block)] on_gen = on_fine + on_except cur_block = [] elif isinstance(b, _ast.TryFinally): j = Jump() cur_block.append(j) next_block = push_block(cur_block) on_gen = [j.set_dest, make_connect(next_block)] cur_block = [] on_gen = _cfa(b.body, state, on_gen) on_gen = _cfa(b.finalbody, state, on_gen) elif isinstance(b, _ast.While): # This could also be architected as having no extra block and having two jump statements, but I don't like that if cur_block: j = Jump() cur_block.append(j) on_gen = [make_connect(push_block(cur_block)), j.set_dest] cur_block = [] always_true = False always_false = False if isinstance(b.test, _ast.Name): if b.test.id == "True": always_true = True elif b.test.id == "False": always_false = True elif isinstance(b.test, _ast.Num): if b.test.n: always_true = True else: always_false = True if always_true: br = Jump() on_true = br.set_dest elif always_false: br = Jump() on_false = br.set_dest else: br = Branch(b.test) on_true = br.set_true on_false = br.set_false init_id = push_block([br]) on_gen = None assert cur_block == [] # just checking if not always_false: gen_true = [on_true, make_connect(init_id)] if not always_true: gen_false = [on_false, make_connect(init_id)] if ENFORCE_NO_MULTIMULTI: if not always_false: on_gen = gen_true j1 = Jump() gen_true = [make_connect(push_block([j1])), j1.set_dest] if not always_true: on_gen = gen_false j2 = Jump() gen_false = [make_connect(push_block([j2])), j2.set_dest] on_gen = None ending_gen = [] if not always_false: state.push_loop() assert b.body loop_ending_gen = _cfa(b.body, state, gen_true) loop_ending_gen += state.get_continues() for c in loop_ending_gen: c(init_id) ending_gen = state.get_breaks() state.pop_loop() if not always_true: if b.orelse: ending_gen += _cfa(b.orelse, state, gen_false) else: ending_gen += gen_false on_gen = ending_gen if not on_gen and PRUNE_UNREACHABLE_BLOCKS: return [] elif isinstance(b, _ast.For): iter_func = _ast.Attribute(b.iter, "__iter__", _ast.Load(), not_real=True, lineno=b.lineno, col_offset=b.col_offset) iter_call = _ast.Call(iter_func, [], [], None, None, not_real=True, lineno=b.lineno, col_offset=b.col_offset) # iter_var = _make_temp_name() iter_var = "__foriter_%d_%d_" % (b.lineno, b.col_offset) iter_assign = _ast.Assign( [_ast.Name(iter_var, _ast.Store(), not_real=True, **pos(b))], iter_call, not_real=True, lineno=b.lineno, col_offset=b.col_offset) cur_block.append(iter_assign) j = Jump() cur_block.append(j) on_gen = [make_connect(push_block(cur_block)), j.set_dest] cur_block = [] br = Branch( HasNext( _ast.Name(iter_var, _ast.Load(), not_real=True, **pos(b)), **pos(b)), **pos(b)) init_id = push_block([br]) on_gen = None assert cur_block == [] # just checking gen_true = [br.set_true, make_connect(init_id)] gen_false = [br.set_false, make_connect(init_id)] if ENFORCE_NO_MULTIMULTI: on_gen = gen_true j1 = Jump() gen_true = [make_connect(push_block([j1])), j1.set_dest] on_gen = gen_false j2 = Jump() gen_false = [make_connect(push_block([j2])), j2.set_dest] on_gen = None ending_gen = [] state.push_loop() next_func = _ast.Attribute(_ast.Name(iter_var, _ast.Load(), not_real=True, **pos(b)), "next", _ast.Load(), not_real=True, lineno=b.lineno, col_offset=b.col_offset) next = _ast.Call(next_func, [], [], None, None, not_real=True, lineno=b.lineno, col_offset=b.col_offset) next_assign = _ast.Assign([b.target], next, not_real=True, lineno=b.lineno, col_offset=b.col_offset) next_iter_gen = _cfa([next_assign] + b.body, state, gen_true) next_iter_gen += state.get_continues() for c in next_iter_gen: c(init_id) loop_done_gen = list(state.get_breaks()) state.pop_loop() if b.orelse: # if b.orelse and loop_ending_blocks: loop_done_gen += _cfa(b.orelse, state, gen_false) else: loop_done_gen += gen_false on_gen = loop_done_gen if not on_gen and PRUNE_UNREACHABLE_BLOCKS: return [] elif isinstance(b, (_ast.Return, _ast.Raise)): cur_block.append(b) block_id = push_block(cur_block) state.returns.append(make_connect(block_id)) return [] elif isinstance(b, _ast.With): # XXX totally ignores the functionality of with statements # Have to save the context manager because the expression might not be valid later mgr_name = "__mgr_%s_%s_" % (b.lineno, b.col_offset) save_mgr = _ast.Assign([ _ast.Name(mgr_name, _ast.Store(), lineno=b.lineno, col_offset=b.col_offset, not_real=True) ], b.context_expr, lineno=b.lineno, col_offset=b.col_offset, not_real=True) enter_func = _ast.Attribute(_ast.Name(mgr_name, _ast.Load(), lineno=b.lineno, col_offset=b.col_offset, not_real=True), "__enter__", _ast.Load(), lineno=b.lineno, col_offset=b.col_offset, not_real=True) bind = _ast.Call(enter_func, [], [], None, None, lineno=b.lineno, col_offset="__enter__()", not_real=True) if b.optional_vars: assert isinstance(b.optional_vars, _ast.AST) init = _ast.Assign([b.optional_vars], bind, lineno=b.lineno, col_offset=b.col_offset, not_real=True) else: init = _ast.Expr(bind, lineno=b.lineno, col_offset=b.col_offset, not_real=True) exit_func = _ast.Attribute(_ast.Name(mgr_name, _ast.Load(), lineno=b.lineno, col_offset=b.col_offset, not_real=True), "__exit__", _ast.Load(), lineno=b.lineno, col_offset=b.col_offset, not_real=True) if SIMPLE_WITH_EXIT: exit_call = _ast.Call(exit_func, [], [], None, None, lineno=b.lineno, col_offset=b.col_offset, not_real=True) else: none_ = _ast.Name("None", _ast.Load(), lineno=b.lineno, col_offset=b.col_offset, not_real=True) exit_call = _ast.Call(exit_func, [none_, none_, none_], [], None, None, lineno=b.lineno, col_offset=b.col_offset, not_real=True) exit = _ast.Expr(exit_call, lineno=b.lineno, col_offset="__exit__()", not_real=True) cur_block.extend([save_mgr, init]) j = Jump() cur_block.append(j) next_block = push_block(cur_block) on_gen = [j.set_dest, make_connect(next_block)] cur_block = [] body = b.body + [exit] next_gen = _cfa(body, state, on_gen) on_gen = next_gen else: raise Exception(b) if cur_block: j = Jump() cur_block.append(j) next = push_block(cur_block) return [j.set_dest, make_connect(next)] return on_gen return on_gen