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 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, cast_path(self.path), 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, cast_path(self.path), string_names=names, code_lines=self._code_lines, is_package=is_package, ) #module, = try_to_merge_with_stub( # self._evaluator, None, module.string_names, ContextSet([module]) #) 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 _from_loader(loader, string): try: is_package_method = loader.is_package except AttributeError: is_package = False else: is_package = is_package_method(string) try: get_filename = loader.get_filename except AttributeError: return None, is_package else: module_path = cast_path(get_filename(string)) # To avoid unicode and read bytes, "overwrite" loader.get_source if # possible. try: f = type(loader).get_source except AttributeError: raise ImportError("get_source was not defined on loader") if f is not importlib.machinery.SourceFileLoader.get_source: # Unfortunately we are reading unicode here, not bytes. # It seems hard to get bytes, because the zip importer # logic just unpacks the zip file and returns a file descriptor # that we cannot as easily access. Therefore we just read it as # a string in the cases where get_source was overwritten. code = loader.get_source(string) else: code = _get_source(loader, string) if code is None: return None, is_package if isinstance(loader, zipimporter): return ZipFileIO(module_path, code, Path(cast_path(loader.archive))), is_package return KnownContentFileIO(module_path, code), is_package
def generate(): try: listed = os.listdir(directory_path_info.path) except (FileNotFoundError, NotADirectoryError): return for entry in listed: entry = cast_path(entry) path = os.path.join(directory_path_info.path, entry) if os.path.isdir(path): init = os.path.join(path, '__init__.pyi') if os.path.isfile(init): yield entry, PathInfo(init, directory_path_info.is_third_party) elif entry.endswith('.pyi') and os.path.isfile(path): name = entry[:-4] if name != '__init__': yield name, PathInfo(path, directory_path_info.is_third_party)
def generate(): try: listed = os.listdir(directory) except (FileNotFoundError, OSError): # OSError is Python 2 return for entry in listed: entry = cast_path(entry) path = os.path.join(directory, entry) if os.path.isdir(path): init = os.path.join(path, '__init__.pyi') if os.path.isfile(init): yield entry, init elif entry.endswith('.pyi') and os.path.isfile(path): name = entry[:-4] if name != '__init__': yield name, path
def _get_module(self): names = None is_package = False if self.path is not None: import_names, is_p = transform_path_to_dotted( self._inference_state.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._inference_state, file_io, names, self._module_node ) if stub_module is not None: return stub_module if names is None: names = ('__main__',) module = ModuleValue( self._inference_state, self._module_node, file_io=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._inference_state.module_cache.add(names, ValueSet([module])) return module
def get_module_info(evaluator, sys_path=None, full_name=None, **kwargs): if sys_path is not None: sys.path, temp = sys_path, sys.path try: module_file, module_path, is_pkg = find_module(full_name=full_name, **kwargs) except ImportError: return None, None, None finally: if sys_path is not None: sys.path = temp code = None if is_pkg: # In this case, we don't have a file yet. Search for the # __init__ file. if module_path.endswith(('.zip', '.egg')): code = module_file.loader.get_source(full_name) else: module_path = _get_init_path(module_path) elif module_file: if module_path.endswith(('.zip', '.egg')): # Unfortunately we are reading unicode here already, not byes. # It seems however hard to get bytes, because the zip importer # logic just unpacks the zip file and returns a file descriptor # that we cannot as easily access. Therefore we just read it as # a string. code = module_file.read() else: # Read the code with a binary file, because the binary file # might not be proper unicode. This is handled by the parser # wrapper. with open(module_path, 'rb') as f: code = f.read() module_file.close() return code, cast_path(module_path), is_pkg
def _try_to_load_stub(inference_state, import_names, python_value_set, parent_module_value, sys_path): """ Trying to load a stub for a set of import_names. This is modelled to work like "PEP 561 -- Distributing and Packaging Type Information", see https://www.python.org/dev/peps/pep-0561. """ if parent_module_value is None and len(import_names) > 1: try: parent_module_value = try_to_load_stub_cached( inference_state, import_names[:-1], NO_VALUES, parent_module_value=None, sys_path=sys_path) except KeyError: pass # 1. Try to load foo-stubs folders on path for import name foo. if len(import_names) == 1: # foo-stubs for p in sys_path: p = cast_path(p) init = os.path.join( p, *import_names) + '-stubs' + os.path.sep + '__init__.pyi' m = _try_to_load_stub_from_file( inference_state, python_value_set, file_io=FileIO(init), import_names=import_names, ) if m is not None: return m if import_names[0] == 'django' and python_value_set: return _try_to_load_stub_from_file( inference_state, python_value_set, file_io=FileIO(DJANGO_INIT_PATH), import_names=import_names, ) # 2. Try to load pyi files next to py files. for c in python_value_set: try: method = c.py__file__ except AttributeError: pass else: file_path = method() file_paths = [] if c.is_namespace(): file_paths = [ os.path.join(p, '__init__.pyi') for p in c.py__path__() ] elif file_path is not None and file_path.endswith('.py'): file_paths = [file_path + 'i'] for file_path in file_paths: m = _try_to_load_stub_from_file( inference_state, python_value_set, # The file path should end with .pyi file_io=FileIO(file_path), import_names=import_names, ) if m is not None: return m # 3. Try to load typeshed m = _load_from_typeshed(inference_state, python_value_set, parent_module_value, import_names) if m is not None: return m # 4. Try to load pyi file somewhere if python_value_set was not defined. if not python_value_set: if parent_module_value is not None: check_path = parent_module_value.py__path__() or [] # In case import_names names_for_path = (import_names[-1], ) else: check_path = sys_path names_for_path = import_names for p in check_path: m = _try_to_load_stub_from_file( inference_state, python_value_set, file_io=FileIO(os.path.join(p, *names_for_path) + '.pyi'), import_names=import_names, ) if m is not None: return m # If no stub is found, that's fine, the calling function has to deal with # it. return None
def py__file__(self): return cast_path(self.access_handle.py__file__())
def py__file__(self): path = cast_path(self.access_handle.py__file__()) if path is None: return None return Path(path)
# -*- coding: utf8 -*- import os import types from jedi._compatibility import ImplicitNSInfo, cast_path from jedi.evaluate.base_context import ContextSet, NO_CONTEXTS from jedi.evaluate import imports # create the path to the sibling virtual_root path root = os.path.dirname(os.path.dirname(__file__)) virtual_mod_path = cast_path(os.path.join(root, 'virtual_root')) def jedi_importer_test1a(importer, import_parts, import_path, sys_path): module_name = '.'.join(import_parts) if module_name == 'mylib': implicit_ns_info = ImplicitNSInfo('mylib', [virtual_mod_path]) module = imports._load_module( importer._evaluator, implicit_ns_info, None, sys_path, # module_name = module_name, import_names=import_parts, safe_module_name=True, ) if not module is None: return ContextSet(module)