[Python-checkins] bpo-40260: Revert breaking changes made in modulefinder (GH-19595)

Miss Islington (bot) webhook-mailer at python.org
Mon Apr 20 11:18:19 EDT 2020


https://github.com/python/cpython/commit/81de3c225774179cdc82a1733a64e4a876ff02b5
commit: 81de3c225774179cdc82a1733a64e4a876ff02b5
branch: 3.8
author: Miss Islington (bot) <31488909+miss-islington at users.noreply.github.com>
committer: GitHub <noreply at github.com>
date: 2020-04-20T08:18:11-07:00
summary:

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

(cherry picked from commit 9b0b5d2baebd0b6a545317200c313a6a7408731e)

Co-authored-by: Barry <barry at barrys-emacs.org>

files:
M Lib/modulefinder.py
M Lib/test/test_modulefinder.py

diff --git a/Lib/modulefinder.py b/Lib/modulefinder.py
index 84ddbdb6ee3df..361a6730c0674 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 @@ def msgout(self, *args):
     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 @@ def import_module(self, partname, fqname, parent):
         except ImportError:
             self.msgout(3, "import_module ->", None)
             return None
+
         try:
             m = self.load_module(fqname, fp, pathname, stuff)
         finally:
@@ -331,7 +332,7 @@ def import_module(self, partname, fqname, parent):
         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 @@ def find_module(self, name, path, parent=None):
 
         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 1aa45011df0bb..23c7e5fb0f563 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 @@ def test_coding_explicit_utf8(self):
     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()



More information about the Python-checkins mailing list