Skip to content

bpo-40260: revert refactor of file_info tuple #19595

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Apr 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 10 additions & 9 deletions Lib/modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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:
Expand Down Expand Up @@ -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):
Expand Down Expand Up @@ -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:
Expand All @@ -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)
Expand Down Expand Up @@ -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

Expand Down
17 changes: 14 additions & 3 deletions Lib/test/test_modulefinder.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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()