def visit_Subscript(self, node): if not isinstance(node.ctx, _ast.Load) or not isinstance(node.slice, _ast.Index): return ASTTransformer.visit_Subscript(self, node) func = _new(_ast.Name, "_lookup_item", _ast.Load()) args = [self.visit(node.value), _new(_ast.Tuple, (self.visit(node.slice.value),), _ast.Load())] return _new(_ast.Call, func, args, [])
def visit_Attribute(self, node): if not isinstance(node.ctx, _ast.Load): return ASTTransformer.visit_Attribute(self, node) func = _new(_ast.Name, '_lookup_attr', _ast.Load()) args = [self.visit(node.value), _new(_ast.Str, node.attr)] return _new(_ast.Call, func, args, [])
def visit_ClassDef(self, node): if len(self.locals) > 1: self.locals[-1].add(node.name) self.locals.append(set()) try: return ASTTransformer.visit_ClassDef(self, node) finally: self.locals.pop()
def visit_FunctionDef(self, node): if len(self.locals) > 1: self.locals[-1].add(node.name) self.locals.append(self._extract_names(node.args)) try: return ASTTransformer.visit_FunctionDef(self, node) finally: self.locals.pop()
def visit_Subscript(self, node): if not isinstance(node.ctx, _ast.Load) or \ not isinstance(node.slice, _ast.Index): return ASTTransformer.visit_Subscript(self, node) func = _new(_ast.Name, '_lookup_item', _ast.Load()) args = [ self.visit(node.value), _new(_ast.Tuple, (self.visit(node.slice.value), ), _ast.Load()) ] return _new(_ast.Call, func, args, [])
def visit_ImportFrom(self, node): if [a.name for a in node.names] == ['*']: if has_star_import_bug: # This is a Python 2.4 bug. Only if we have a broken Python # version do we need to apply this hack node = _new(_ast.Expr, _new(_ast.Call, _new(_ast.Name, '_star_import_patch'), [ _new(_ast.Name, '__data__'), _new(_ast.Str, node.module) ], (), ())) return node if len(self.locals) > 1: self.locals[-1].update(self._extract_names(node)) return ASTTransformer.visit_ImportFrom(self, node)
def visit_Subscript(self, node): if not isinstance(node.ctx, _ast.Load) or \ not isinstance(node.slice, (_ast.Index, _ast_Constant, _ast.Name, _ast.Call)): return ASTTransformer.visit_Subscript(self, node) # Before Python 3.9 "foo[key]" wrapped the load of "key" in # "ast.Index(ast.Name(...))" if isinstance(node.slice, (_ast.Name, _ast.Call)): slice_value = node.slice else: slice_value = node.slice.value func = _new(_ast.Name, '_lookup_item', _ast.Load()) args = [ self.visit(node.value), _new(_ast.Tuple, (self.visit(slice_value), ), _ast.Load()) ] return _new(_ast.Call, func, args, [])
def visit_ImportFrom(self, node): if [a.name for a in node.names] == ['*']: return node if len(self.locals) > 1: self.locals[-1].update(self._extract_names(node)) return ASTTransformer.visit_ImportFrom(self, node)
def visit_For(self, node): self.locals.append(set()) try: return ASTTransformer.visit_For(self, node) finally: self.locals.pop()
def visit_Lambda(self, node): self.locals.append(self._extract_names(node.args)) try: return ASTTransformer.visit_Lambda(self, node) finally: self.locals.pop()
def visit_Import(self, node): if len(self.locals) > 1: self.locals[-1].update(self._extract_names(node)) return ASTTransformer.visit_Import(self, node)