def _load_module_from_path(evaluator, file_io, base_names): """ This should pretty much only be used for get_modules_containing_name. It's here to ensure that a random path is still properly loaded into the Jedi module structure. """ e_sys_path = evaluator.get_sys_path() path = file_io.path if base_names: module_name = os.path.basename(path) module_name = sys_path.remove_python_path_suffix(module_name) is_package = module_name == '__init__' if is_package: import_names = base_names else: import_names = base_names + (module_name,) else: import_names, is_package = sys_path.transform_path_to_dotted(e_sys_path, path) module = _load_python_module( evaluator, file_io, sys_path=e_sys_path, import_names=import_names, is_package=is_package, ) evaluator.module_cache.add(import_names, ContextSet([module])) return module
def _get_module(self): name = '__main__' if self.path is not None: import_names = transform_path_to_dotted(self._evaluator.get_sys_path(), self.path) if import_names is not None: name = '.'.join(import_names) module = ModuleContext( self._evaluator, self._module_node, self.path, code_lines=self._code_lines ) imports.add_module_to_cache(self._evaluator, name, module) return module
def _get_module(self): name = '__main__' if self.path is not None: import_names = transform_path_to_dotted( self._evaluator.get_sys_path(), self.path) if import_names is not None: name = '.'.join(import_names) module = ModuleContext(self._evaluator, self._module_node, self.path, code_lines=self._code_lines) imports.add_module_to_cache(self._evaluator, name, module) return module
def check_fs(path): try: f = open(path, 'rb') except FileNotFoundError: return with f: code = python_bytes_to_unicode(f.read(), errors='replace') if name in code: e_sys_path = evaluator.get_sys_path() import_names = sys_path.transform_path_to_dotted(e_sys_path, path) module = _load_module( evaluator, path, code, sys_path=e_sys_path, import_names=import_names, ) return module
def _get_module(self): names = None is_package = False if self.path is not None: import_names, is_p = transform_path_to_dotted( self._evaluator.get_sys_path(add_parent_paths=False), self.path ) if import_names is not None: names = import_names is_package = is_p if self.path is None: file_io = None else: file_io = KnownContentFileIO(cast_path(self.path), self._code) if self.path is not None and self.path.endswith('.pyi'): # We are in a stub file. Try to load the stub properly. stub_module = load_proper_stub_module( self._evaluator, file_io, names, self._module_node ) if stub_module is not None: return stub_module if names is None: names = ('__main__',) module = ModuleContext( self._evaluator, self._module_node, file_io, string_names=names, code_lines=self._code_lines, is_package=is_package, ) if names[0] not in ('builtins', '__builtin__', 'typing'): # These modules are essential for Jedi, so don't overwrite them. self._evaluator.module_cache.add(names, ContextSet([module])) return module
def test_calculate_dotted_from_path(sys_path_, module_path, result): assert sys_path.transform_path_to_dotted(sys_path_, module_path) == result
def test_calculate_dotted_from_path(sys_path_, module_path, result): # tranform_path_to_dotted expects normalized absolute paths. sys_path_ = [os.path.abspath(path) for path in sys_path_] module_path = os.path.abspath(module_path) assert sys_path.transform_path_to_dotted(sys_path_, module_path) == result
def test_transform_path_to_dotted(sys_path_, module_path, expected, is_package): # transform_path_to_dotted expects normalized absolute paths. sys_path_ = [os.path.abspath(path) for path in sys_path_] module_path = os.path.abspath(module_path) assert sys_path.transform_path_to_dotted(sys_path_, module_path) \ == (expected, is_package)