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 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]
if __name__ == "__main__": t = """ try: 1/0 except Exception: print "bad!" except Exception2: print "second bad!" else: print "else" finally: print "finally" print "done" """.strip() cfg = cfa(ast_utils.parse(t, "test")) cfg.show() sys.exit(-1) t = """ with f() as b: print b """.strip() t = """ x = 1 # 1 while x: # 2 if 1: # 3 continue elif 2: # 4 break