def _spec_from_modpath(modpath, path=None, context=None): """given a mod path (i.e. split module / package name), return the corresponding spec this function is used internally, see `file_from_modpath`'s documentation for more information """ assert modpath location = None if context is not None: try: found_spec = spec.find_spec(modpath, [context]) location = found_spec.location except ImportError: found_spec = spec.find_spec(modpath, path) location = found_spec.location else: found_spec = spec.find_spec(modpath, path) if found_spec.type == spec.ModuleType.PY_COMPILED: try: location = get_source_file(found_spec.location) return found_spec._replace( location=location, type=spec.ModuleType.PY_SOURCE ) except NoSourceFile: return found_spec._replace(location=location) elif found_spec.type == spec.ModuleType.C_BUILTIN: # integrated builtin module return found_spec._replace(location=None) elif found_spec.type == spec.ModuleType.PKG_DIRECTORY: location = _has_init(found_spec.location) return found_spec._replace(location=location, type=spec.ModuleType.PY_SOURCE) return found_spec
def test_find_egg_module(self): found_spec = spec.find_spec( [self.package], [resources.find('data/MyPyPa-0.1.0-py2.5.egg')]) self.assertEqual(found_spec.type, spec.ModuleType.PY_ZIPMODULE) self.assertEqual(found_spec.location.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.egg", self.package])
def test_find_zipped_module(self) -> None: found_spec = spec.find_spec( [self.package], [resources.find("data/MyPyPa-0.1.0-py2.5.zip")]) self.assertEqual(found_spec.type, spec.ModuleType.PY_ZIPMODULE) self.assertEqual( found_spec.location.split(os.sep)[-3:], ["data", "MyPyPa-0.1.0-py2.5.zip", self.package], )
def test_find_distutils_submodules_in_virtualenv(self) -> None: found_spec = spec.find_spec(["distutils", "version"]) self.assertEqual(found_spec.location, distutils.version.__file__)