mirror of
https://github.com/python/cpython.git
synced 2025-10-23 09:53:47 +00:00

This required moving the class from importlib/abc.py into importlib/_bootstrap.py and jiggering some code to work better with the class. This included changing how the file finder worked to better meet import semantics. This also led to fixing importlib to handle the empty string from sys.path as import currently does (and making me wish we didn't support that instead just required people to insert '.' instead to represent cwd). It also required making the new set_data abstractmethod create any needed subdirectories implicitly thanks to __pycache__ (it was either this or grow the SourceLoader ABC to gain an 'exists' method and either a mkdir method or have set_data with no data arg mean to create a directory). Lastly, as an optimization the file loaders cache the file path where the finder found something to use for loading (this is thanks to having a sourceless loader separate from the source loader to simplify the code and cut out stat calls). Unfortunately test_runpy assumed a loader would always work for a module, even if you changed from underneath it what it was expected to work with. By simply dropping the previous loader in test_runpy so the proper loader can be returned by the finder fixed the failure. At this point importlib deviates from import on two points: 1. The exception raised when trying to import a file is different (import does an explicit file check to print a special message, importlib just says the path cannot be imported as if it was just some module name). 2. the co_filename on a code object is not being set to where bytecode was actually loaded from instead of where the marshalled code object originally came from (a solution for this has already been agreed upon on python-dev but has not been implemented yet; issue8611).
131 lines
5.1 KiB
Python
131 lines
5.1 KiB
Python
from importlib import _bootstrap
|
|
from importlib import machinery
|
|
from .. import util
|
|
from . import util as import_util
|
|
import imp
|
|
import os
|
|
import sys
|
|
import tempfile
|
|
from test import support
|
|
from types import MethodType
|
|
import unittest
|
|
|
|
|
|
class FinderTests(unittest.TestCase):
|
|
|
|
"""Tests for PathFinder."""
|
|
|
|
def test_failure(self):
|
|
# Test None returned upon not finding a suitable finder.
|
|
module = '<test module>'
|
|
with util.import_state():
|
|
self.assertTrue(machinery.PathFinder.find_module(module) is None)
|
|
|
|
def test_sys_path(self):
|
|
# Test that sys.path is used when 'path' is None.
|
|
# Implicitly tests that sys.path_importer_cache is used.
|
|
module = '<test module>'
|
|
path = '<test path>'
|
|
importer = util.mock_modules(module)
|
|
with util.import_state(path_importer_cache={path: importer},
|
|
path=[path]):
|
|
loader = machinery.PathFinder.find_module(module)
|
|
self.assertTrue(loader is importer)
|
|
|
|
def test_path(self):
|
|
# Test that 'path' is used when set.
|
|
# Implicitly tests that sys.path_importer_cache is used.
|
|
module = '<test module>'
|
|
path = '<test path>'
|
|
importer = util.mock_modules(module)
|
|
with util.import_state(path_importer_cache={path: importer}):
|
|
loader = machinery.PathFinder.find_module(module, [path])
|
|
self.assertTrue(loader is importer)
|
|
|
|
def test_path_hooks(self):
|
|
# Test that sys.path_hooks is used.
|
|
# Test that sys.path_importer_cache is set.
|
|
module = '<test module>'
|
|
path = '<test path>'
|
|
importer = util.mock_modules(module)
|
|
hook = import_util.mock_path_hook(path, importer=importer)
|
|
with util.import_state(path_hooks=[hook]):
|
|
loader = machinery.PathFinder.find_module(module, [path])
|
|
self.assertTrue(loader is importer)
|
|
self.assertTrue(path in sys.path_importer_cache)
|
|
self.assertTrue(sys.path_importer_cache[path] is importer)
|
|
|
|
def test_path_importer_cache_has_None(self):
|
|
# Test that if sys.path_importer_cache has None that None is returned.
|
|
clear_cache = {path: None for path in sys.path}
|
|
with util.import_state(path_importer_cache=clear_cache):
|
|
for name in ('asynchat', 'sys', '<test module>'):
|
|
self.assertTrue(machinery.PathFinder.find_module(name) is None)
|
|
|
|
def test_path_importer_cache_has_None_continues(self):
|
|
# Test that having None in sys.path_importer_cache causes the search to
|
|
# continue.
|
|
path = '<test path>'
|
|
module = '<test module>'
|
|
importer = util.mock_modules(module)
|
|
with util.import_state(path=['1', '2'],
|
|
path_importer_cache={'1': None, '2': importer}):
|
|
loader = machinery.PathFinder.find_module(module)
|
|
self.assertTrue(loader is importer)
|
|
|
|
|
|
|
|
class DefaultPathFinderTests(unittest.TestCase):
|
|
|
|
"""Test importlib._bootstrap._DefaultPathFinder."""
|
|
|
|
def test_implicit_hooks(self):
|
|
# Test that the implicit path hooks are used.
|
|
bad_path = '<path>'
|
|
module = '<module>'
|
|
assert not os.path.exists(bad_path)
|
|
existing_path = tempfile.mkdtemp()
|
|
try:
|
|
with util.import_state():
|
|
nothing = _bootstrap._DefaultPathFinder.find_module(module,
|
|
path=[existing_path])
|
|
self.assertTrue(nothing is None)
|
|
self.assertTrue(existing_path in sys.path_importer_cache)
|
|
result = isinstance(sys.path_importer_cache[existing_path],
|
|
imp.NullImporter)
|
|
self.assertFalse(result)
|
|
nothing = _bootstrap._DefaultPathFinder.find_module(module,
|
|
path=[bad_path])
|
|
self.assertTrue(nothing is None)
|
|
self.assertTrue(bad_path in sys.path_importer_cache)
|
|
self.assertTrue(isinstance(sys.path_importer_cache[bad_path],
|
|
imp.NullImporter))
|
|
finally:
|
|
os.rmdir(existing_path)
|
|
|
|
|
|
def test_path_importer_cache_has_None(self):
|
|
# Test that the default hook is used when sys.path_importer_cache
|
|
# contains None for a path.
|
|
module = '<test module>'
|
|
importer = util.mock_modules(module)
|
|
path = '<test path>'
|
|
# XXX Not blackbox.
|
|
original_hook = _bootstrap._DEFAULT_PATH_HOOK
|
|
mock_hook = import_util.mock_path_hook(path, importer=importer)
|
|
_bootstrap._DEFAULT_PATH_HOOK = mock_hook
|
|
try:
|
|
with util.import_state(path_importer_cache={path: None}):
|
|
loader = _bootstrap._DefaultPathFinder.find_module(module,
|
|
path=[path])
|
|
self.assertTrue(loader is importer)
|
|
finally:
|
|
_bootstrap._DEFAULT_PATH_HOOK = original_hook
|
|
|
|
|
|
def test_main():
|
|
from test.support import run_unittest
|
|
run_unittest(FinderTests, DefaultPathFinderTests)
|
|
|
|
if __name__ == '__main__':
|
|
test_main()
|