def visitRaise(self, node): if node.type: self.update_stable_rhs(get_symbols(node.type, ast.Load)) if node.inst: self.update_stable_rhs(get_symbols(node.inst, ast.Load)) if node.tback: self.update_stable_rhs(get_symbols(node.tback, ast.Load))
def visitExceptHandler(self, node): if node.type: self.update_stable_rhs(get_symbols(node.type, ast.Load)) if node.name: self.update_stable_lhs(get_symbols(node.name, ast.Store)) self.visit_list(node.body)
def visitWith(self, node): self.update_stable_rhs(get_symbols(node.context_expr, ast.Load)) if node.optional_vars: self.update_stable_lhs(get_symbols(node.optional_vars, ast.Load)) self.visit_list(node.body)
def handle_generators(self, generators): defined = set() required = set() for generator in generators: get_symbols(generator, _ast.Load) required.update(get_symbols(generator, _ast.Load) - defined) defined.update(get_symbols(generator, _ast.Store)) return defined, required
def visitExec(self, node): self.update_stable_rhs(get_symbols(node.body, ast.Load)) if node.globals: self.update_stable_rhs(get_symbols(node.globals, ast.Load)) if node.locals: self.update_stable_rhs(get_symbols(node.locals, ast.Load))
def visitAugAssign(self, node): values = get_symbols(node.value) self.update_stable_rhs(values) targets = get_symbols(node.target) self.update_stable_rhs(targets) self.update_stable_lhs(targets)
def visitDictComp(self, node): defined, required = self.handle_generators(node.generators) required.update(get_symbols(node.key, _ast.Load) - defined) required.update(get_symbols(node.value, _ast.Load) - defined) for symbol in required: if not self.graph.has_node(symbol): self.graph.add_node(symbol) self.undefined.add(symbol) return required
def visitFor(self, node): lhs_symbols = get_symbols(node.target, ast.Store) self.update_cond_lhs(lhs_symbols) rhs_symbols = get_symbols(node.iter, ast.Load) self.update_stable_rhs(rhs_symbols) remove_from_undef = lhs_symbols - self.undefined self.visit_loop(node) self.undefined -= remove_from_undef
def visitAssign(self, node): ids = set() for target in node.targets: ids.update(get_symbols(target, ast.Store)) rhs_ids = get_symbols(node.value, ast.Load) for target in node.targets: rhs_ids.update(get_symbols(target, ast.Load)) self.update_stable_rhs(rhs_ids) self.update_stable_lhs(ids)
def visitIf(self, node): rhs_symbols = get_symbols(node.test, ast.Load) self.update_stable_rhs(rhs_symbols) gen = ConditionalSymbolVisitor() for stmnt in node.body: gen.visit(stmnt) if gen.seen_break: self.seen_break = True self.update_cond_lhs(gen._cond_lhs) self.update_cond_rhs(gen._cond_rhs) outputs = gen.stable_lhs inputs = gen.stable_rhs gen = ConditionalSymbolVisitor() for stmnt in node.orelse: gen.visit(stmnt) self.update_cond_lhs(gen._cond_lhs) self.update_cond_rhs(gen._cond_rhs) orelse_outputs = gen.stable_lhs orelse_inputs = gen.stable_rhs self.update_stable_lhs(outputs.intersection(orelse_outputs)) self.update_stable_rhs(inputs.intersection(orelse_inputs)) self.update_cond_lhs(outputs.symmetric_difference(orelse_outputs)) self.update_cond_rhs(inputs.symmetric_difference(orelse_inputs))
def visitWhile(self, node): rhs_symbols = get_symbols(node.test, ast.Load) self.update_stable_rhs(rhs_symbols) self.visit_loop(node)
def visitAssign(self, node): target_ids = [get_symbols(target, ast.Store) for target in node.targets] target_ids = set.union(*target_ids) for id in target_ids: node_list = self.assign_id_map.setdefault(id, []) node_list.append(Assignment(root=node, assignments=(node,)))
def assertHasSymbols(self, codestring, expected_symbols, ctxts=(ast.Load, ast.Store)): root = ast.parse(codestring) symbols = get_symbols(root, ctxts) self.assertEqual(symbols, expected_symbols)
def visitAssign(self, node): target_ids = [ get_symbols(target, ast.Store) for target in node.targets ] target_ids = set.union(*target_ids) for id in target_ids: node_list = self.assign_id_map.setdefault(id, []) node_list.append(Assignment(root=node, assignments=(node, )))
def visitFunctionDef(self, node): for decorator in node.decorator_list: self.update_stable_rhs(get_symbols(decorator, ast.Load)) self.update_stable_lhs({node.name}) gen = ConditionalSymbolVisitor() gen.update_stable_lhs(symbols={arg for arg in node.args.args}) gen.visit_list(node.body) self.update_stable_rhs(gen.undefined)
def visitAssign(self, node): nodes = self.visit(node.value) tsymols = get_symbols(node, _ast.Store) re_defined = tsymols.intersection(set(self.graph.nodes())) if re_defined: add_edges(self.graph, re_defined, re_defined) targets = set() for target in node.targets: targets.update(self.visit(target)) add_edges(self.graph, targets, nodes) return targets | nodes
def visitImport(self, node): symbols = get_symbols(node) self.update_stable_lhs(symbols)
def visitRaise(self, node): if node.exc: self.update_stable_rhs(get_symbols(node.exc, ast.Load)) if node.cause: self.update_stable_rhs(get_symbols(node.cause, ast.Load))
def visitReturn(self, node): self.update_stable_rhs(get_symbols(node.value, ast.Load))
def visitPrint(self, node): rhs_ids = get_symbols(node, ast.Load) self.update_stable_rhs(rhs_ids)
def visitAssert(self, node): self.update_stable_rhs(get_symbols(node.test, ast.Load)) if node.msg: self.update_stable_rhs(get_symbols(node.msg, ast.Load))