Skip to content

Commit 9b0b5d2

Browse files
authored
bpo-40260: Revert breaking changes made in modulefinder (GH-19595)
1 parent df8913f commit 9b0b5d2

File tree

2 files changed

+24
-12
lines changed

2 files changed

+24
-12
lines changed

Lib/modulefinder.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -69,15 +69,15 @@ def _find_module(name, path=None):
6969
# Some special cases:
7070

7171
if spec.loader is importlib.machinery.BuiltinImporter:
72-
return None, None, ("", _C_BUILTIN)
72+
return None, None, ("", "", _C_BUILTIN)
7373

7474
if spec.loader is importlib.machinery.FrozenImporter:
75-
return None, None, ("", _PY_FROZEN)
75+
return None, None, ("", "", _PY_FROZEN)
7676

7777
file_path = spec.origin
7878

7979
if spec.loader.is_package(name):
80-
return None, os.path.dirname(file_path), ("", _PKG_DIRECTORY)
80+
return None, os.path.dirname(file_path), ("", "", _PKG_DIRECTORY)
8181

8282
if isinstance(spec.loader, importlib.machinery.SourceFileLoader):
8383
kind = _PY_SOURCE
@@ -89,12 +89,12 @@ def _find_module(name, path=None):
8989
kind = _PY_COMPILED
9090

9191
else: # Should never happen.
92-
return None, None, ("", _SEARCH_ERROR)
92+
return None, None, ("", "", _SEARCH_ERROR)
9393

9494
file = io.open_code(file_path)
9595
suffix = os.path.splitext(file_path)[-1]
9696

97-
return file, file_path, (suffix, kind)
97+
return file, file_path, (suffix, "rb", kind)
9898

9999

100100
class Module:
@@ -159,14 +159,14 @@ def msgout(self, *args):
159159
def run_script(self, pathname):
160160
self.msg(2, "run_script", pathname)
161161
with io.open_code(pathname) as fp:
162-
stuff = ("", _PY_SOURCE)
162+
stuff = ("", "rb", _PY_SOURCE)
163163
self.load_module('__main__', fp, pathname, stuff)
164164

165165
def load_file(self, pathname):
166166
dir, name = os.path.split(pathname)
167167
name, ext = os.path.splitext(name)
168168
with io.open_code(pathname) as fp:
169-
stuff = (ext, _PY_SOURCE)
169+
stuff = (ext, "rb", _PY_SOURCE)
170170
self.load_module(name, fp, pathname, stuff)
171171

172172
def import_hook(self, name, caller=None, fromlist=None, level=-1):
@@ -320,6 +320,7 @@ def import_module(self, partname, fqname, parent):
320320
except ImportError:
321321
self.msgout(3, "import_module ->", None)
322322
return None
323+
323324
try:
324325
m = self.load_module(fqname, fp, pathname, stuff)
325326
finally:
@@ -331,7 +332,7 @@ def import_module(self, partname, fqname, parent):
331332
return m
332333

333334
def load_module(self, fqname, fp, pathname, file_info):
334-
suffix, type = file_info
335+
suffix, mode, type = file_info
335336
self.msgin(2, "load_module", fqname, fp and "fp", pathname)
336337
if type == _PKG_DIRECTORY:
337338
m = self.load_package(fqname, pathname)
@@ -502,7 +503,7 @@ def find_module(self, name, path, parent=None):
502503

503504
if path is None:
504505
if name in sys.builtin_module_names:
505-
return (None, None, ("", _C_BUILTIN))
506+
return (None, None, ("", "", _C_BUILTIN))
506507

507508
path = self.path
508509

Lib/test/test_modulefinder.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -317,13 +317,12 @@ def create_package(source):
317317
if ofi:
318318
ofi.close()
319319

320-
321320
class ModuleFinderTest(unittest.TestCase):
322-
def _do_test(self, info, report=False, debug=0, replace_paths=[]):
321+
def _do_test(self, info, report=False, debug=0, replace_paths=[], modulefinder_class=modulefinder.ModuleFinder):
323322
import_this, modules, missing, maybe_missing, source = info
324323
create_package(source)
325324
try:
326-
mf = modulefinder.ModuleFinder(path=TEST_PATH, debug=debug,
325+
mf = modulefinder_class(path=TEST_PATH, debug=debug,
327326
replace_paths=replace_paths)
328327
mf.import_hook(import_this)
329328
if report:
@@ -421,5 +420,17 @@ def test_coding_explicit_utf8(self):
421420
def test_coding_explicit_cp1252(self):
422421
self._do_test(coding_explicit_cp1252_test)
423422

423+
def test_load_module_api(self):
424+
class CheckLoadModuleApi(modulefinder.ModuleFinder):
425+
def __init__(self, *args, **kwds):
426+
super().__init__(*args, **kwds)
427+
428+
def load_module(self, fqname, fp, pathname, file_info):
429+
# confirm that the fileinfo is a tuple of 3 elements
430+
suffix, mode, type = file_info
431+
return super().load_module(fqname, fp, pathname, file_info)
432+
433+
self._do_test(absolute_import_test, modulefinder_class=CheckLoadModuleApi)
434+
424435
if __name__ == "__main__":
425436
unittest.main()

0 commit comments

Comments
 (0)