def get_binding(self, name, namespace): if name in namespace.global_names and not isinstance( namespace, ast.Module): return self.get_binding(name, get_global_namespace(namespace)) elif name in namespace.nonlocal_names and not isinstance( namespace, ast.Module): return self.get_binding(name, get_nonlocal_namespace(namespace)) if isinstance(namespace, ast.ClassDef): binding = self.get_binding(name, get_nonlocal_namespace(namespace)) binding.disallow_rename() return binding for binding in namespace.bindings: if binding.name == name: break else: # weeee! binding = NameBinding(name) namespace.bindings.append(binding) if name in dir(builtins): binding.disallow_rename() if name in namespace.nonlocal_names and isinstance( namespace, ast.Module): # This is actually a syntax error - but we want the same syntax error after minifying! binding.disallow_rename() return binding
def get_binding(name, namespace): if name in namespace.global_names and not isinstance( namespace, ast.Module): return get_binding(name, get_global_namespace(namespace)) elif name in namespace.nonlocal_names and not isinstance( namespace, ast.Module): return get_binding(name, get_nonlocal_namespace(namespace)) for binding in namespace.bindings: if binding.name == name: return binding if not isinstance(namespace, ast.Module): return get_binding(name, get_nonlocal_namespace(namespace)) else: # This is unresolved at global scope - is it a builtin? if name in dir(builtins): if name in ['exec', 'eval', 'locals', 'globals', 'vars']: namespace.tainted = True binding = BuiltinBinding(name, namespace, rename_cost=len(name) + 1) namespace.bindings.append(binding) return binding else: binding = NameBinding(name) binding.disallow_rename() namespace.bindings.append(binding) return binding
def visit_alias(self, node): if node.name == '*': get_global_namespace(node).tainted = True root_module = node.name.split('.')[0] if root_module == 'timeit': get_global_namespace(node).tainted = True if node.asname is not None: self.get_binding(node.asname, node.namespace).add_reference(node) else: # This binds the root module only for a dotted import binding = self.get_binding(root_module, node.namespace) binding.add_reference(node) if '.' in node.name: binding.disallow_rename()
def resolve_names(node): """ Resolve unbound names to a NameBinding :param node: The module to resolve names in :type node: :class:`ast.Module` """ if isinstance(node, ast.Name) and isinstance(node.ctx, ast.Load): get_binding(node.id, node.namespace).add_reference(node) elif hasattr(ast, 'Exec') and isinstance(node, ast.Exec): get_global_namespace(node).tainted = True for child in ast.iter_child_nodes(node): resolve_names(child)
def get_binding(self, name, namespace): if name in namespace.global_names and not isinstance( namespace, ast.Module): return self.get_binding(name, get_global_namespace(namespace)) elif name in namespace.nonlocal_names: return self.get_binding(name, get_nonlocal_namespace(namespace)) if isinstance(namespace, ast.ClassDef): binding = self.get_binding(name, get_nonlocal_namespace(namespace)) binding.disallow_rename() return binding for binding in namespace.bindings: if binding.name == name: return binding binding = NameBinding(name) namespace.bindings.append(binding) if name in dir(builtins): binding.disallow_rename() return binding