diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py index 84ddbdb6ee3..361a6730c06 100644 --- a/Lib/modulefinder.py +++ b/Lib/modulefinder.py @@ -69,15 +69,15 @@ def _find_module(name, path=None): # Some special cases: if spec.loader is importlib.machinery.BuiltinImporter: - return None, None, ("", _C_BUILTIN) + return None, None, ("", "", _C_BUILTIN) if spec.loader is importlib.machinery.FrozenImporter: - return None, None, ("", _PY_FROZEN) + return None, None, ("", "", _PY_FROZEN) file_path = spec.origin if spec.loader.is_package(name): - return None, os.path.dirname(file_path), ("", _PKG_DIRECTORY) + return None, os.path.dirname(file_path), ("", "", _PKG_DIRECTORY) if isinstance(spec.loader, importlib.machinery.SourceFileLoader): kind = _PY_SOURCE @@ -89,12 +89,12 @@ def _find_module(name, path=None): kind = _PY_COMPILED else: # Should never happen. - return None, None, ("", _SEARCH_ERROR) + return None, None, ("", "", _SEARCH_ERROR) file = io.open_code(file_path) suffix = os.path.splitext(file_path)[-1] - return file, file_path, (suffix, kind) + return file, file_path, (suffix, "rb", kind) class Module: @@ -159,14 +159,14 @@ class ModuleFinder: def run_script(self, pathname): self.msg(2, "run_script", pathname) with io.open_code(pathname) as fp: - stuff = ("", _PY_SOURCE) + stuff = ("", "rb", _PY_SOURCE) self.load_module('__main__', fp, pathname, stuff) def load_file(self, pathname): dir, name = os.path.split(pathname) name, ext = os.path.splitext(name) with io.open_code(pathname) as fp: - stuff = (ext, _PY_SOURCE) + stuff = (ext, "rb", _PY_SOURCE) self.load_module(name, fp, pathname, stuff) def import_hook(self, name, caller=None, fromlist=None, level=-1): @@ -320,6 +320,7 @@ class ModuleFinder: except ImportError: self.msgout(3, "import_module ->", None) return None + try: m = self.load_module(fqname, fp, pathname, stuff) finally: @@ -331,7 +332,7 @@ class ModuleFinder: return m def load_module(self, fqname, fp, pathname, file_info): - suffix, type = file_info + suffix, mode, type = file_info self.msgin(2, "load_module", fqname, fp and "fp", pathname) if type == _PKG_DIRECTORY: m = self.load_package(fqname, pathname) @@ -502,7 +503,7 @@ class ModuleFinder: if path is None: if name in sys.builtin_module_names: - return (None, None, ("", _C_BUILTIN)) + return (None, None, ("", "", _C_BUILTIN)) path = self.path diff --git a/Lib/test/test_modulefinder.py b/Lib/test/test_modulefinder.py index 1aa45011df0..23c7e5fb0f5 100644 --- a/Lib/test/test_modulefinder.py +++ b/Lib/test/test_modulefinder.py @@ -317,13 +317,12 @@ def create_package(source): if ofi: ofi.close() - class ModuleFinderTest(unittest.TestCase): - def _do_test(self, info, report=False, debug=0, replace_paths=[]): + def _do_test(self, info, report=False, debug=0, replace_paths=[], modulefinder_class=modulefinder.ModuleFinder): import_this, modules, missing, maybe_missing, source = info create_package(source) try: - mf = modulefinder.ModuleFinder(path=TEST_PATH, debug=debug, + mf = modulefinder_class(path=TEST_PATH, debug=debug, replace_paths=replace_paths) mf.import_hook(import_this) if report: @@ -421,5 +420,17 @@ b.py def test_coding_explicit_cp1252(self): self._do_test(coding_explicit_cp1252_test) + def test_load_module_api(self): + class CheckLoadModuleApi(modulefinder.ModuleFinder): + def __init__(self, *args, **kwds): + super().__init__(*args, **kwds) + + def load_module(self, fqname, fp, pathname, file_info): + # confirm that the fileinfo is a tuple of 3 elements + suffix, mode, type = file_info + return super().load_module(fqname, fp, pathname, file_info) + + self._do_test(absolute_import_test, modulefinder_class=CheckLoadModuleApi) + if __name__ == "__main__": unittest.main()