bpo-40260: Revert breaking changes made in modulefinder (GH-19595)

(cherry picked from commit 9b0b5d2bae)

Co-authored-by: Barry <barry@barrys-emacs.org>
This commit is contained in:
Miss Islington (bot) 2020-04-20 08:18:11 -07:00 committed by GitHub
parent 984a567cbb
commit 81de3c2257
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 24 additions and 12 deletions

View File

@ -69,15 +69,15 @@ def _find_module(name, path=None):
# Some special cases: # Some special cases:
if spec.loader is importlib.machinery.BuiltinImporter: if spec.loader is importlib.machinery.BuiltinImporter:
return None, None, ("", _C_BUILTIN) return None, None, ("", "", _C_BUILTIN)
if spec.loader is importlib.machinery.FrozenImporter: if spec.loader is importlib.machinery.FrozenImporter:
return None, None, ("", _PY_FROZEN) return None, None, ("", "", _PY_FROZEN)
file_path = spec.origin file_path = spec.origin
if spec.loader.is_package(name): 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): if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
kind = _PY_SOURCE kind = _PY_SOURCE
@ -89,12 +89,12 @@ def _find_module(name, path=None):
kind = _PY_COMPILED kind = _PY_COMPILED
else: # Should never happen. else: # Should never happen.
return None, None, ("", _SEARCH_ERROR) return None, None, ("", "", _SEARCH_ERROR)
file = io.open_code(file_path) file = io.open_code(file_path)
suffix = os.path.splitext(file_path)[-1] suffix = os.path.splitext(file_path)[-1]
return file, file_path, (suffix, kind) return file, file_path, (suffix, "rb", kind)
class Module: class Module:
@ -159,14 +159,14 @@ class ModuleFinder:
def run_script(self, pathname): def run_script(self, pathname):
self.msg(2, "run_script", pathname) self.msg(2, "run_script", pathname)
with io.open_code(pathname) as fp: with io.open_code(pathname) as fp:
stuff = ("", _PY_SOURCE) stuff = ("", "rb", _PY_SOURCE)
self.load_module('__main__', fp, pathname, stuff) self.load_module('__main__', fp, pathname, stuff)
def load_file(self, pathname): def load_file(self, pathname):
dir, name = os.path.split(pathname) dir, name = os.path.split(pathname)
name, ext = os.path.splitext(name) name, ext = os.path.splitext(name)
with io.open_code(pathname) as fp: with io.open_code(pathname) as fp:
stuff = (ext, _PY_SOURCE) stuff = (ext, "rb", _PY_SOURCE)
self.load_module(name, fp, pathname, stuff) self.load_module(name, fp, pathname, stuff)
def import_hook(self, name, caller=None, fromlist=None, level=-1): def import_hook(self, name, caller=None, fromlist=None, level=-1):
@ -320,6 +320,7 @@ class ModuleFinder:
except ImportError: except ImportError:
self.msgout(3, "import_module ->", None) self.msgout(3, "import_module ->", None)
return None return None
try: try:
m = self.load_module(fqname, fp, pathname, stuff) m = self.load_module(fqname, fp, pathname, stuff)
finally: finally:
@ -331,7 +332,7 @@ class ModuleFinder:
return m return m
def load_module(self, fqname, fp, pathname, file_info): 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) self.msgin(2, "load_module", fqname, fp and "fp", pathname)
if type == _PKG_DIRECTORY: if type == _PKG_DIRECTORY:
m = self.load_package(fqname, pathname) m = self.load_package(fqname, pathname)
@ -502,7 +503,7 @@ class ModuleFinder:
if path is None: if path is None:
if name in sys.builtin_module_names: if name in sys.builtin_module_names:
return (None, None, ("", _C_BUILTIN)) return (None, None, ("", "", _C_BUILTIN))
path = self.path path = self.path

View File

@ -317,13 +317,12 @@ def create_package(source):
if ofi: if ofi:
ofi.close() ofi.close()
class ModuleFinderTest(unittest.TestCase): 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 import_this, modules, missing, maybe_missing, source = info
create_package(source) create_package(source)
try: try:
mf = modulefinder.ModuleFinder(path=TEST_PATH, debug=debug, mf = modulefinder_class(path=TEST_PATH, debug=debug,
replace_paths=replace_paths) replace_paths=replace_paths)
mf.import_hook(import_this) mf.import_hook(import_this)
if report: if report:
@ -421,5 +420,17 @@ b.py
def test_coding_explicit_cp1252(self): def test_coding_explicit_cp1252(self):
self._do_test(coding_explicit_cp1252_test) 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__": if __name__ == "__main__":
unittest.main() unittest.main()