我们从Python开源项目中,提取了以下5个代码示例,用于说明如何使用_ast.While()。
def semicolon(self, stmt): if not isinstance(stmt, _ast.If) and not isinstance(stmt, _ast.While) and not isinstance(stmt, _ast.For): return ";" else: return ""
def handleNodeDelete(self, node): def on_conditional_branch(): """ Return `True` if node is part of a conditional body. """ current = getattr(node, 'parent', None) while current: if isinstance(current, (ast.If, ast.While, ast.IfExp)): return True current = getattr(current, 'parent', None) return False name = getNodeName(node) if not name: return if on_conditional_branch(): # We cannot predict if this conditional branch is going to # be executed. return if isinstance(self.scope, FunctionScope) and name in self.scope.globals: self.scope.globals.remove(name) else: try: del self.scope[name] except KeyError: self.report(messages.UndefinedName, node, name)
def handleNodeDelete(self, node): def on_conditional_branch(): """ Return `True` if node is part of a conditional body. """ current = getattr(node, 'parent', None) while current: if isinstance(current, (ast.If, ast.While, ast.IfExp)): return True current = getattr(current, 'parent', None) return False name = getNodeName(node) if not name: return if on_conditional_branch(): # We can not predict if this conditional branch is going to # be executed. return if isinstance(self.scope, FunctionScope) and name in self.scope.globals: self.scope.globals.remove(name) else: try: del self.scope[name] except KeyError: self.report(messages.UndefinedName, node, name)
def while_loop(self, instr, loop_block): kw = dict(lineno=instr.lineno, col_offset=0) loop_block_map = {instr.i:instr.op for instr in loop_block} first_i = loop_block[0].i func = lambda instr: instr.opname == 'JUMP_ABSOLUTE' and instr.oparg == first_i body_index = rfind_index(loop_block[:-1], func) if body_index is None: const_while = True body_index = len(loop_block) - 1 else: if body_index + 1 < len(loop_block): pop_block = loop_block[body_index + 1] const_while = pop_block.opname != 'POP_BLOCK' const_else = True else: const_while = True const_else = False if const_while: test = _ast.Num(1, **kw) body_ = self.decompile_block(loop_block[:body_index]).stmnt() else_block = loop_block[body_index + 1:] if else_block: else_ = self.decompile_block(else_block).stmnt() else: else_ = [] else: pop_block = loop_block[body_index + 1] func = lambda instr: instr.opname in ['POP_JUMP_IF_FALSE', 'POP_JUMP_IF_TRUE'] and instr.oparg == pop_block.i idx = rfind_index(loop_block[:body_index], func) cond_block = loop_block[:idx] iter_stmnt = self.decompile_block(cond_block).stmnt() assert len(iter_stmnt) == 1 test = iter_stmnt[0] body_ = self.decompile_block(loop_block[idx + 1:body_index]).stmnt() else_block = loop_block[body_index + 2:] if else_block: else_ = self.decompile_block(else_block[:]).stmnt() else: else_ = [] while_ = _ast.While(test=test, body=body_, orelse=else_, **kw) self.ast_stack.append(while_)