def visitName(self, node): # If the name refers to a local inside a lambda, list comprehension, or # generator expression, leave it alone if node.name not in flatten(self.locals): # Otherwise, translate the name ref into a context lookup func_args = [ast.Name('__data__'), ast.Const(node.name)] node = ast.CallFunc(ast.Name('_lookup_name'), func_args) return node
def visit_Name(self, node): # If the name refers to a local inside a lambda, list comprehension, or # generator expression, leave it alone if isinstance(node.ctx, _ast.Load) and node.id not in flatten(self.locals): # Otherwise, translate the name ref into a context lookup name = _new(_ast.Name, "_lookup_name", _ast.Load()) namearg = _new(_ast.Name, "__data__", _ast.Load()) strarg = _new(_ast.Str, node.id) node = _new(_ast.Call, name, [namearg, strarg], []) elif isinstance(node.ctx, _ast.Store): if len(self.locals) > 1: self.locals[-1].add(node.id) return node
def visit_Name(self, node): # If the name refers to a local inside a lambda, list comprehension, or # generator expression, leave it alone if isinstance(node.ctx, _ast.Load) and \ node.id not in flatten(self.locals): # Otherwise, translate the name ref into a context lookup name = _new(_ast.Name, '_lookup_name', _ast.Load()) namearg = _new(_ast.Name, '__data__', _ast.Load()) strarg = _new(_ast.Str, node.id) node = _new(_ast.Call, name, [namearg, strarg], []) elif isinstance(node.ctx, _ast.Store): if len(self.locals) > 1: self.locals[-1].add(node.id) return node
def visitAugAssign(self, node): if isinstance(node.node, ast.Name) \ and node.node.name not in flatten(self.locals): name = node.node.name node.node = ast.Subscript(ast.Name('__data__'), 'OP_APPLY', [ast.Const(name)]) node.expr = self.visit(node.expr) return ast.If([ (ast.Compare(ast.Const(name), [('in', ast.Name('__data__'))]), ast.Stmt([node]))], ast.Stmt([ast.Raise(ast.CallFunc(ast.Name('UndefinedError'), [ast.Const(name)]), None, None)])) else: return ASTTransformer.visitAugAssign(self, node)
def visitLambda(self, node): self.locals.append(set(flatten(node.argnames))) try: return ASTTransformer.visitLambda(self, node) finally: self.locals.pop()
def visitLambda(self, node): self.locals.append(set(flatten(node.argnames))) node = ASTTransformer.visitLambda(self, node) self.locals.pop() return node