def module_injector(inference_state, names, code): assert isinstance(names, tuple) file_io = KnownContentFileIO( Path('foo/bar/module-injector-%s.py' % next(counter)).absolute(), code) v = _load_python_module(inference_state, file_io, names) inference_state.module_cache.add(names, ValueSet([v]))
def _from_loader(loader, string): is_package = loader.is_package(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. f = type(loader).get_source if is_py3 and 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, cast_path(loader.archive)), is_package return KnownContentFileIO(module_path, code), is_package
def _get_module(self): return interpreter.MixedModuleContext( self._evaluator, self._module_node, self.namespaces, file_io=KnownContentFileIO(self.path, self._code), code_lines=self._code_lines, )
def _get_module_context(self): tree_module_value = ModuleValue( self._inference_state, self._module_node, file_io=KnownContentFileIO(self.path, self._code), string_names=('__main__',), code_lines=self._code_lines, ) return interpreter.MixedModuleContext( tree_module_value, self.namespaces, )
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 check_fs(file_io, base_names): try: code = file_io.read() except FileNotFoundError: return None code = python_bytes_to_unicode(code, errors='replace') if name not in code: return None new_file_io = KnownContentFileIO(file_io.path, code) m = load_module_from_path(inference_state, new_file_io, base_names) if isinstance(m, compiled.CompiledObject): return None return m.as_context()
def find_module_pre_py3(string, path=None, full_name=None, is_global_search=True): # This import is here, because in other places it will raise a # DeprecationWarning. import imp try: module_file, module_path, description = imp.find_module(string, path) module_type = description[2] is_package = module_type is imp.PKG_DIRECTORY if is_package: # In Python 2 directory package imports are returned as folder # paths, not __init__.py paths. p = os.path.join(module_path, '__init__.py') try: module_file = open(p) module_path = p except FileNotFoundError: pass elif module_type != imp.PY_SOURCE: if module_file is not None: module_file.close() module_file = None if module_file is None: code = None return None, is_package with module_file: code = module_file.read() return KnownContentFileIO(cast_path(module_path), code), is_package except ImportError: pass if path is None: path = sys.path for item in path: loader = pkgutil.get_importer(item) if loader: loader = loader.find_module(string) if loader is not None: return _from_loader(loader, string) raise ImportError("No module named {}".format(string))
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(self.path, self._code) if self.path is not None and self.path.suffix == '.pyi': # We are in a stub file. Try to load the stub properly. stub_module = load_proper_stub_module( self._inference_state, self._inference_state.latest_grammar, 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', 'typing'): # These modules are essential for Jedi, so don't overwrite them. self._inference_state.module_cache.add(names, ValueSet([module])) return module
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
def test_load_module_from_path(evaluator, path, base_names, is_package, names): file_io = KnownContentFileIO(path, '') m = imports._load_module_from_path(evaluator, file_io, base_names) assert m.is_package == is_package assert m.string_names == names