def _iter_pytest_modules(module_context, skip_own_module=False): if not skip_own_module: yield module_context file_io = module_context.get_value().file_io if file_io is not None: folder = file_io.get_parent_folder() sys_path = module_context.inference_state.get_sys_path() # prevent an infinite loop when reaching the root of the current drive last_folder = None while any(folder.path.startswith(p) for p in sys_path): file_io = folder.get_file_io('conftest.py') if Path(file_io.path) != module_context.py__file__(): try: m = load_module_from_path(module_context.inference_state, file_io) yield m.as_context() except FileNotFoundError: pass folder = folder.get_parent_folder() # prevent an infinite for loop if the same parent folder is return twice if last_folder is not None and folder.path == last_folder.path: break last_folder = folder # keep track of the last found parent name for names in _PYTEST_FIXTURE_MODULES: for module_value in module_context.inference_state.import_module(names): yield module_value.as_context()
def _check_fs(inference_state, file_io, regex): try: code = file_io.read() except FileNotFoundError: return None code = python_bytes_to_unicode(code, errors='replace') if not regex.search(code): return None new_file_io = KnownContentFileIO(file_io.path, code) m = load_module_from_path(inference_state, new_file_io) if m.is_compiled(): return None return m.as_context()
def _iter_pytest_modules(module_context): yield module_context folder = module_context.get_value().file_io.get_parent_folder() sys_path = module_context.inference_state.get_sys_path() while any(folder.path.startswith(p) for p in sys_path): file_io = folder.get_file_io('conftest.py') try: m = load_module_from_path(module_context.inference_state, file_io) yield m.as_context() except FileNotFoundError: pass folder = folder.get_parent_folder() for names in _PYTEST_FIXTURE_MODULES: for module_value in module_context.inference_state.import_module( names): yield module_value.as_context()
def _iter_pytest_modules(module_context, skip_own_module=False): if not skip_own_module: yield module_context file_io = module_context.get_value().file_io if file_io is not None: folder = file_io.get_parent_folder() sys_path = module_context.inference_state.get_sys_path() while any(folder.path.startswith(p) for p in sys_path): file_io = folder.get_file_io('conftest.py') if Path(file_io.path) != module_context.py__file__(): try: m = load_module_from_path(module_context.inference_state, file_io) yield m.as_context() except FileNotFoundError: pass folder = folder.get_parent_folder() for names in _PYTEST_FIXTURE_MODULES: for module_value in module_context.inference_state.import_module( names): yield module_value.as_context()
def _search_func(self, string, complete=False, all_scopes=False): # Using a Script is they easiest way to get an empty module context. from jedi import Script s = Script('', project=self) inference_state = s._inference_state empty_module_context = s._get_module_context() debug.dbg('Search for string %s, complete=%s', string, complete) wanted_type, wanted_names = split_search_string(string) name = wanted_names[0] stub_folder_name = name + '-stubs' ios = recurse_find_python_folders_and_files(FolderIO(str(self._path))) file_ios = [] # 1. Search for modules in the current project for folder_io, file_io in ios: if file_io is None: file_name = folder_io.get_base_name() if file_name == name or file_name == stub_folder_name: f = folder_io.get_file_io('__init__.py') try: m = load_module_from_path(inference_state, f).as_context() except FileNotFoundError: f = folder_io.get_file_io('__init__.pyi') try: m = load_module_from_path(inference_state, f).as_context() except FileNotFoundError: m = load_namespace_from_path(inference_state, folder_io).as_context() else: continue else: file_ios.append(file_io) if Path(file_io.path).name in (name + '.py', name + '.pyi'): m = load_module_from_path(inference_state, file_io).as_context() else: continue debug.dbg('Search of a specific module %s', m) yield from search_in_module( inference_state, m, names=[m.name], wanted_type=wanted_type, wanted_names=wanted_names, complete=complete, convert=True, ignore_imports=True, ) # 2. Search for identifiers in the project. for module_context in search_in_file_ios(inference_state, file_ios, name, complete=complete): names = get_module_names(module_context.tree_node, all_scopes=all_scopes) names = [module_context.create_name(n) for n in names] names = _remove_imports(names) yield from search_in_module( inference_state, module_context, names=names, wanted_type=wanted_type, wanted_names=wanted_names, complete=complete, ignore_imports=True, ) # 3. Search for modules on sys.path sys_path = [ p for p in self._get_sys_path(inference_state) # Exclude folders that are handled by recursing of the Python # folders. if not p.startswith(str(self._path)) ] names = list(iter_module_names(inference_state, empty_module_context, sys_path)) yield from search_in_module( inference_state, empty_module_context, names=names, wanted_type=wanted_type, wanted_names=wanted_names, complete=complete, convert=True, )
def test_load_module_from_path(inference_state, path, base_names, is_package, names): file_io = KnownContentFileIO(path, '') m = imports.load_module_from_path(inference_state, file_io, base_names) assert m.is_package() == is_package assert m.string_names == names