def get_defined_names(self): """ This method generates all `ArrayMethod` for one pr.Array. It returns e.g. for a list: append, pop, ... """ # `array.type` is a string with the type, e.g. 'list'. scope = evaluate.find_name(builtin.Builtin.scope, self._array.type)[0] scope = Instance(scope) names = scope.get_defined_names() return [ArrayMethod(n) for n in names]
def get_posibilities(module, func_name): try: possible_stmts = module.used_names[func_name] except KeyError: return [] for stmt in possible_stmts: if isinstance(stmt, pr.Import): continue calls = _scan_statement(stmt, func_name) for c in calls: # no execution means that params cannot be set call_path = list(c.generate_call_path()) pos = c.start_pos scope = stmt.parent # this whole stuff is just to not execute certain parts # (speed improvement), basically we could just call # ``follow_call_path`` on the call_path and it would # also work. def listRightIndex(lst, value): return len(lst) - lst[-1::-1].index(value) - 1 # Need to take right index, because there could be a # func usage before. i = listRightIndex(call_path, func_name) first, last = call_path[:i], call_path[i + 1:] if not last and not call_path.index(func_name) != i: continue scopes = [scope] if first: scopes = evaluate.follow_call_path( iter(first), scope, pos) pos = None for scope in scopes: s = evaluate.find_name(scope, func_name, position=pos, search_global=not first, resolve_decorator=False) c = [ getattr(escope, 'base_func', None) or escope.base for escope in s if escope.isinstance(er.Function, er.Class) ] if compare in c: # only if we have the correct function we execute # it, otherwise just ignore it. evaluate.follow_paths(iter(last), s, scope) return listener.param_possibilities
def get_super_classes(self): supers = [] # TODO care for mro stuff (multiple super classes). for s in self.base.supers: # Super classes are statements. for cls in evaluate.follow_statement(s): if not isinstance(cls, Class): debug.warning('Received non class, as a super class') continue # Just ignore other stuff (user input error). supers.append(cls) if not supers and self.base.parent != builtin.Builtin.scope: # add `object` to classes supers += evaluate.find_name(builtin.Builtin.scope, 'object') return supers
def get_posibilities(module, func_name): try: possible_stmts = module.used_names[func_name] except KeyError: return [] for stmt in possible_stmts: if isinstance(stmt, pr.Import): continue calls = _scan_statement(stmt, func_name) for c in calls: # no execution means that params cannot be set call_path = list(c.generate_call_path()) pos = c.start_pos scope = stmt.parent # this whole stuff is just to not execute certain parts # (speed improvement), basically we could just call # ``follow_call_path`` on the call_path and it would # also work. def listRightIndex(lst, value): return len(lst) - lst[-1::-1].index(value) - 1 # Need to take right index, because there could be a # func usage before. i = listRightIndex(call_path, func_name) first, last = call_path[:i], call_path[i + 1 :] if not last and not call_path.index(func_name) != i: continue scopes = [scope] if first: scopes = evaluate.follow_call_path(iter(first), scope, pos) pos = None for scope in scopes: s = evaluate.find_name( scope, func_name, position=pos, search_global=not first, resolve_decorator=False ) c = [ getattr(escope, "base_func", None) or escope.base for escope in s if escope.isinstance(er.Function, er.Class) ] if compare in c: # only if we have the correct function we execute # it, otherwise just ignore it. evaluate.follow_paths(iter(last), s, scope) return listener.param_possibilities
def follow(self, is_goto=False): """ Returns the imported modules. """ if evaluate.follow_statement.push_stmt(self.import_stmt): # check recursion return [] if self.import_path: try: scope, rest = self._follow_file_system() except ModuleNotFound: debug.warning('Module not found: ' + str(self.import_stmt)) evaluate.follow_statement.pop_stmt() return [] scopes = [scope] scopes += remove_star_imports(scope) # follow the rest of the import (not FS -> classes, functions) if len(rest) > 1 or rest and self.is_like_search: scopes = [] if ['os', 'path'] == self.import_path[:2] \ and not self._is_relative_import(): # This is a huge exception, we follow a nested import # ``os.path``, because it's a very important one in Python # that is being achieved by messing with ``sys.modules`` in # ``os``. scopes = evaluate.follow_path(iter(rest), scope, scope) elif rest: if is_goto: scopes = itertools.chain.from_iterable( evaluate.find_name(s, rest[0], is_goto=True) for s in scopes) else: scopes = itertools.chain.from_iterable( evaluate.follow_path(iter(rest), s, s) for s in scopes) scopes = list(scopes) if self._is_nested_import(): scopes.append(self._get_nested_import(scope)) else: scopes = [ImportPath.GlobalNamespace] debug.dbg('after import', scopes) evaluate.follow_statement.pop_stmt() return scopes
def follow(self, is_goto=False): """ Returns the imported modules. """ if evaluate.follow_statement.push_stmt(self.import_stmt): # check recursion return [] if self.import_path: try: scope, rest = self._follow_file_system() except ModuleNotFound: debug.warning('Module not found: ' + str(self.import_stmt)) evaluate.follow_statement.pop_stmt() return [] scopes = [scope] scopes += remove_star_imports(scope) # follow the rest of the import (not FS -> classes, functions) if len(rest) > 1 or rest and self.is_like_search: scopes = [] elif rest: if is_goto: scopes = itertools.chain.from_iterable( evaluate.find_name(s, rest[0], is_goto=True) for s in scopes) else: scopes = itertools.chain.from_iterable( evaluate.follow_path(iter(rest), s, s) for s in scopes) scopes = list(scopes) if self.is_nested_import(): scopes.append(self.get_nested_import(scope)) else: scopes = [ImportPath.GlobalNamespace] debug.dbg('after import', scopes) evaluate.follow_statement.pop_stmt() return scopes
def get_defined_names(self): result = self.instance_names() type_cls = evaluate.find_name(builtin.Builtin.scope, 'type')[0] return result + type_cls.base.get_defined_names()