summaryrefslogtreecommitdiffstats
path: root/Lib
diff options
context:
space:
mode:
authorBarry Warsaw <barry@python.org>2023-05-03 11:55:22 (GMT)
committerGitHub <noreply@github.com>2023-05-03 11:55:22 (GMT)
commit326997829d02458246dfd5b6d03297e2418bde52 (patch)
tree05b94e8dbb81f6b7a8d9054bd773c3cf24e0797e /Lib
parentbcea36f8db9ad4fd542b38997e065987e829cb9f (diff)
downloadcpython-326997829d02458246dfd5b6d03297e2418bde52.zip
cpython-326997829d02458246dfd5b6d03297e2418bde52.tar.gz
cpython-326997829d02458246dfd5b6d03297e2418bde52.tar.bz2
gh-98040: Remove find_loader, find_module and other deprecated APIs (#98059)
* Remove deprecated classes from pkgutil * Remove some other PEP 302 obsolescence * Use find_spec instead of load_module * Remove more tests of PEP 302 obsolete APIs * Remove another bunch of tests using obsolete load_modules() * Remove deleted names from __all__ * Remove obsolete footnote * imp is removed * Remove `imp` from generated stdlib names * What's new and blurb * Update zipimport documentation for the removed methods * Fix some Windows tests * Remove any test (or part of a test) that references `find_module()`. * Use assertIsNone() / assertIsNotNone() consistently. * Update Doc/reference/import.rst * We don't need pkgutil._get_spec() any more either * test.test_importlib.fixtures.NullFinder * ...BadLoaderFinder.find_module * ...test_api.InvalidatingNullFinder.find_module * ...test.test_zipimport test of z.find_module * Suppress cross-references to find_loader and find_module * Suppress cross-references to Finder * Suppress cross-references to pkgutil.ImpImporter and pkgutil.ImpLoader --------- Co-authored-by: Oleg Iarygin <oleg@arhadthedev.net> Co-authored-by: Adam Turner <9087854+aa-turner@users.noreply.github.com>
Diffstat (limited to 'Lib')
-rw-r--r--Lib/importlib/__init__.py34
-rw-r--r--Lib/importlib/_bootstrap.py41
-rw-r--r--Lib/importlib/_bootstrap_external.py95
-rw-r--r--Lib/importlib/abc.py92
-rw-r--r--Lib/pkgutil.py14
-rwxr-xr-xLib/pydoc.py2
-rw-r--r--Lib/test/test_importlib/builtin/test_finder.py33
-rw-r--r--Lib/test/test_importlib/extension/test_path_hook.py2
-rw-r--r--Lib/test/test_importlib/fixtures.py5
-rw-r--r--Lib/test/test_importlib/frozen/test_finder.py40
-rw-r--r--Lib/test/test_importlib/frozen/test_loader.py82
-rw-r--r--Lib/test/test_importlib/import_/test___loader__.py43
-rw-r--r--Lib/test/test_importlib/import_/test___package__.py36
-rw-r--r--Lib/test/test_importlib/import_/test_api.py5
-rw-r--r--Lib/test/test_importlib/import_/test_caching.py25
-rw-r--r--Lib/test/test_importlib/import_/test_meta_path.py10
-rw-r--r--Lib/test/test_importlib/import_/test_path.py73
-rw-r--r--Lib/test/test_importlib/source/test_case_sensitivity.py13
-rw-r--r--Lib/test/test_importlib/source/test_finder.py29
-rw-r--r--Lib/test/test_importlib/source/test_path_hook.py9
-rw-r--r--Lib/test/test_importlib/test_abc.py78
-rw-r--r--Lib/test/test_importlib/test_api.py67
-rw-r--r--Lib/test/test_importlib/test_windows.py18
-rw-r--r--Lib/test/test_importlib/util.py27
-rw-r--r--Lib/test/test_pkgutil.py10
-rw-r--r--Lib/test/test_zipimport.py1
26 files changed, 59 insertions, 825 deletions
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
index 21d9dee..707c081 100644
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -70,40 +70,6 @@ def invalidate_caches():
finder.invalidate_caches()
-def find_loader(name, path=None):
- """Return the loader for the specified module.
-
- This is a backward-compatible wrapper around find_spec().
-
- This function is deprecated in favor of importlib.util.find_spec().
-
- """
- warnings.warn('Deprecated since Python 3.4 and slated for removal in '
- 'Python 3.12; use importlib.util.find_spec() instead',
- DeprecationWarning, stacklevel=2)
- try:
- loader = sys.modules[name].__loader__
- if loader is None:
- raise ValueError(f'{name}.__loader__ is None')
- else:
- return loader
- except KeyError:
- pass
- except AttributeError:
- raise ValueError(f'{name}.__loader__ is not set') from None
-
- spec = _bootstrap._find_spec(name, path)
- # We won't worry about malformed specs (missing attributes).
- if spec is None:
- return None
- if spec.loader is None:
- if spec.submodule_search_locations is None:
- raise ImportError(f'spec for {name} missing loader', name=name)
- raise ImportError('namespace packages do not have loaders',
- name=name)
- return spec.loader
-
-
def import_module(name, package=None):
"""Import a module.
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py
index e4fcaa6..c48fd50 100644
--- a/Lib/importlib/_bootstrap.py
+++ b/Lib/importlib/_bootstrap.py
@@ -892,21 +892,6 @@ class BuiltinImporter:
else:
return None
- @classmethod
- def find_module(cls, fullname, path=None):
- """Find the built-in module.
-
- If 'path' is ever specified then the search is considered a failure.
-
- This method is deprecated. Use find_spec() instead.
-
- """
- _warnings.warn("BuiltinImporter.find_module() is deprecated and "
- "slated for removal in Python 3.12; use find_spec() instead",
- DeprecationWarning)
- spec = cls.find_spec(fullname, path)
- return spec.loader if spec is not None else None
-
@staticmethod
def create_module(spec):
"""Create a built-in module"""
@@ -1076,18 +1061,6 @@ class FrozenImporter:
spec.submodule_search_locations.insert(0, pkgdir)
return spec
- @classmethod
- def find_module(cls, fullname, path=None):
- """Find a frozen module.
-
- This method is deprecated. Use find_spec() instead.
-
- """
- _warnings.warn("FrozenImporter.find_module() is deprecated and "
- "slated for removal in Python 3.12; use find_spec() instead",
- DeprecationWarning)
- return cls if _imp.is_frozen(fullname) else None
-
@staticmethod
def create_module(spec):
"""Set __file__, if able."""
@@ -1170,16 +1143,6 @@ def _resolve_name(name, package, level):
return f'{base}.{name}' if name else base
-def _find_spec_legacy(finder, name, path):
- msg = (f"{_object_name(finder)}.find_spec() not found; "
- "falling back to find_module()")
- _warnings.warn(msg, ImportWarning)
- loader = finder.find_module(name, path)
- if loader is None:
- return None
- return spec_from_loader(name, loader)
-
-
def _find_spec(name, path, target=None):
"""Find a module's spec."""
meta_path = sys.meta_path
@@ -1200,9 +1163,7 @@ def _find_spec(name, path, target=None):
try:
find_spec = finder.find_spec
except AttributeError:
- spec = _find_spec_legacy(finder, name, path)
- if spec is None:
- continue
+ continue
else:
spec = find_spec(name, path, target)
if spec is not None:
diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py
index cb22737..7a3fdba 100644
--- a/Lib/importlib/_bootstrap_external.py
+++ b/Lib/importlib/_bootstrap_external.py
@@ -659,26 +659,6 @@ def _check_name(method):
return _check_name_wrapper
-def _find_module_shim(self, fullname):
- """Try to find a loader for the specified module by delegating to
- self.find_loader().
-
- This method is deprecated in favor of finder.find_spec().
-
- """
- _warnings.warn("find_module() is deprecated and "
- "slated for removal in Python 3.12; use find_spec() instead",
- DeprecationWarning)
- # Call find_loader(). If it returns a string (indicating this
- # is a namespace package portion), generate a warning and
- # return None.
- loader, portions = self.find_loader(fullname)
- if loader is None and len(portions):
- msg = f'Not importing directory {portions[0]}: missing __init__'
- _warnings.warn(msg, ImportWarning)
- return loader
-
-
def _classify_pyc(data, name, exc_details):
"""Perform basic validity checking of a pyc header and return the flags field,
which determines how the pyc should be further validated against the source.
@@ -985,22 +965,6 @@ class WindowsRegistryFinder:
origin=filepath)
return spec
- @classmethod
- def find_module(cls, fullname, path=None):
- """Find module named in the registry.
-
- This method is deprecated. Use find_spec() instead.
-
- """
- _warnings.warn("WindowsRegistryFinder.find_module() is deprecated and "
- "slated for removal in Python 3.12; use find_spec() instead",
- DeprecationWarning)
- spec = cls.find_spec(fullname, path)
- if spec is not None:
- return spec.loader
- else:
- return None
-
class _LoaderBasics:
@@ -1518,27 +1482,6 @@ class PathFinder:
return finder
@classmethod
- def _legacy_get_spec(cls, fullname, finder):
- # This would be a good place for a DeprecationWarning if
- # we ended up going that route.
- if hasattr(finder, 'find_loader'):
- msg = (f"{_bootstrap._object_name(finder)}.find_spec() not found; "
- "falling back to find_loader()")
- _warnings.warn(msg, ImportWarning)
- loader, portions = finder.find_loader(fullname)
- else:
- msg = (f"{_bootstrap._object_name(finder)}.find_spec() not found; "
- "falling back to find_module()")
- _warnings.warn(msg, ImportWarning)
- loader = finder.find_module(fullname)
- portions = []
- if loader is not None:
- return _bootstrap.spec_from_loader(fullname, loader)
- spec = _bootstrap.ModuleSpec(fullname, None)
- spec.submodule_search_locations = portions
- return spec
-
- @classmethod
def _get_spec(cls, fullname, path, target=None):
"""Find the loader or namespace_path for this module/package name."""
# If this ends up being a namespace package, namespace_path is
@@ -1549,10 +1492,7 @@ class PathFinder:
continue
finder = cls._path_importer_cache(entry)
if finder is not None:
- if hasattr(finder, 'find_spec'):
- spec = finder.find_spec(fullname, target)
- else:
- spec = cls._legacy_get_spec(fullname, finder)
+ spec = finder.find_spec(fullname, target)
if spec is None:
continue
if spec.loader is not None:
@@ -1594,22 +1534,6 @@ class PathFinder:
else:
return spec
- @classmethod
- def find_module(cls, fullname, path=None):
- """find the module on sys.path or 'path' based on sys.path_hooks and
- sys.path_importer_cache.
-
- This method is deprecated. Use find_spec() instead.
-
- """
- _warnings.warn("PathFinder.find_module() is deprecated and "
- "slated for removal in Python 3.12; use find_spec() instead",
- DeprecationWarning)
- spec = cls.find_spec(fullname, path)
- if spec is None:
- return None
- return spec.loader
-
@staticmethod
def find_distributions(*args, **kwargs):
"""
@@ -1654,23 +1578,6 @@ class FileFinder:
"""Invalidate the directory mtime."""
self._path_mtime = -1
- find_module = _find_module_shim
-
- def find_loader(self, fullname):
- """Try to find a loader for the specified module, or the namespace
- package portions. Returns (loader, list-of-portions).
-
- This method is deprecated. Use find_spec() instead.
-
- """
- _warnings.warn("FileFinder.find_loader() is deprecated and "
- "slated for removal in Python 3.12; use find_spec() instead",
- DeprecationWarning)
- spec = self.find_spec(fullname)
- if spec is None:
- return None, []
- return spec.loader, spec.submodule_search_locations or []
-
def _get_spec(self, loader_class, fullname, path, smsl, target):
loader = loader_class(fullname, path)
return spec_from_file_location(fullname, path, loader=loader,
diff --git a/Lib/importlib/abc.py b/Lib/importlib/abc.py
index 8fa9a0f..b56fa94 100644
--- a/Lib/importlib/abc.py
+++ b/Lib/importlib/abc.py
@@ -19,7 +19,7 @@ from .resources import abc as _resources_abc
__all__ = [
- 'Loader', 'Finder', 'MetaPathFinder', 'PathEntryFinder',
+ 'Loader', 'MetaPathFinder', 'PathEntryFinder',
'ResourceLoader', 'InspectLoader', 'ExecutionLoader',
'FileLoader', 'SourceLoader',
]
@@ -49,38 +49,6 @@ def _register(abstract_cls, *classes):
abstract_cls.register(frozen_cls)
-class Finder(metaclass=abc.ABCMeta):
-
- """Legacy abstract base class for import finders.
-
- It may be subclassed for compatibility with legacy third party
- reimplementations of the import system. Otherwise, finder
- implementations should derive from the more specific MetaPathFinder
- or PathEntryFinder ABCs.
-
- Deprecated since Python 3.3
- """
-
- def __init__(self):
- warnings.warn("the Finder ABC is deprecated and "
- "slated for removal in Python 3.12; use MetaPathFinder "
- "or PathEntryFinder instead",
- DeprecationWarning)
-
- @abc.abstractmethod
- def find_module(self, fullname, path=None):
- """An abstract method that should find a module.
- The fullname is a str and the optional path is a str or None.
- Returns a Loader object or None.
- """
- warnings.warn("importlib.abc.Finder along with its find_module() "
- "method are deprecated and "
- "slated for removal in Python 3.12; use "
- "MetaPathFinder.find_spec() or "
- "PathEntryFinder.find_spec() instead",
- DeprecationWarning)
-
-
class MetaPathFinder(metaclass=abc.ABCMeta):
"""Abstract base class for import finders on sys.meta_path."""
@@ -88,27 +56,6 @@ class MetaPathFinder(metaclass=abc.ABCMeta):
# We don't define find_spec() here since that would break
# hasattr checks we do to support backward compatibility.
- def find_module(self, fullname, path):
- """Return a loader for the module.
-
- If no module is found, return None. The fullname is a str and
- the path is a list of strings or None.
-
- This method is deprecated since Python 3.4 in favor of
- finder.find_spec(). If find_spec() exists then backwards-compatible
- functionality is provided for this method.
-
- """
- warnings.warn("MetaPathFinder.find_module() is deprecated since Python "
- "3.4 in favor of MetaPathFinder.find_spec() and is "
- "slated for removal in Python 3.12",
- DeprecationWarning,
- stacklevel=2)
- if not hasattr(self, 'find_spec'):
- return None
- found = self.find_spec(fullname, path)
- return found.loader if found is not None else None
-
def invalidate_caches(self):
"""An optional method for clearing the finder's cache, if any.
This method is used by importlib.invalidate_caches().
@@ -122,43 +69,6 @@ class PathEntryFinder(metaclass=abc.ABCMeta):
"""Abstract base class for path entry finders used by PathFinder."""
- # We don't define find_spec() here since that would break
- # hasattr checks we do to support backward compatibility.
-
- def find_loader(self, fullname):
- """Return (loader, namespace portion) for the path entry.
-
- The fullname is a str. The namespace portion is a sequence of
- path entries contributing to part of a namespace package. The
- sequence may be empty. If loader is not None, the portion will
- be ignored.
-
- The portion will be discarded if another path entry finder
- locates the module as a normal module or package.
-
- This method is deprecated since Python 3.4 in favor of
- finder.find_spec(). If find_spec() is provided than backwards-compatible
- functionality is provided.
- """
- warnings.warn("PathEntryFinder.find_loader() is deprecated since Python "
- "3.4 in favor of PathEntryFinder.find_spec() "
- "(available since 3.4)",
- DeprecationWarning,
- stacklevel=2)
- if not hasattr(self, 'find_spec'):
- return None, []
- found = self.find_spec(fullname)
- if found is not None:
- if not found.submodule_search_locations:
- portions = []
- else:
- portions = found.submodule_search_locations
- return found.loader, portions
- else:
- return None, []
-
- find_module = _bootstrap_external._find_module_shim
-
def invalidate_caches(self):
"""An optional method for clearing the finder's cache, if any.
This method is used by PathFinder.invalidate_caches().
diff --git a/Lib/pkgutil.py b/Lib/pkgutil.py
index fb977ea..f62eccb 100644
--- a/Lib/pkgutil.py
+++ b/Lib/pkgutil.py
@@ -23,20 +23,6 @@ ModuleInfo = namedtuple('ModuleInfo', 'module_finder name ispkg')
ModuleInfo.__doc__ = 'A namedtuple with minimal info about a module.'
-def _get_spec(finder, name):
- """Return the finder-specific module spec."""
- # Works with legacy finders.
- try:
- find_spec = finder.find_spec
- except AttributeError:
- loader = finder.find_module(name)
- if loader is None:
- return None
- return importlib.util.spec_from_loader(name, loader)
- else:
- return find_spec(name)
-
-
def read_code(stream):
# This helper is needed in order for the PEP 302 emulation to
# correctly handle compiled files
diff --git a/Lib/pydoc.py b/Lib/pydoc.py
index 1c3443f..b10a5da 100755
--- a/Lib/pydoc.py
+++ b/Lib/pydoc.py
@@ -2242,7 +2242,7 @@ class ModuleScanner:
callback(None, modname, '')
else:
try:
- spec = pkgutil._get_spec(importer, modname)
+ spec = importer.find_spec(modname)
except SyntaxError:
# raised by tests for bad coding cookies or BOM
continue
diff --git a/Lib/test/test_importlib/builtin/test_finder.py b/Lib/test/test_importlib/builtin/test_finder.py
index 81dc5a3..111c4af 100644
--- a/Lib/test/test_importlib/builtin/test_finder.py
+++ b/Lib/test/test_importlib/builtin/test_finder.py
@@ -43,38 +43,5 @@ class FindSpecTests(abc.FinderTests):
) = util.test_both(FindSpecTests, machinery=machinery)
-@unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module')
-class FinderTests(abc.FinderTests):
-
- """Test find_module() for built-in modules."""
-
- def test_module(self):
- # Common case.
- with util.uncache(util.BUILTINS.good_name):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- found = self.machinery.BuiltinImporter.find_module(util.BUILTINS.good_name)
- self.assertTrue(found)
- self.assertTrue(hasattr(found, 'load_module'))
-
- # Built-in modules cannot be a package.
- test_package = test_package_in_package = test_package_over_module = None
-
- # Built-in modules cannot be in a package.
- test_module_in_package = None
-
- def test_failure(self):
- assert 'importlib' not in sys.builtin_module_names
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- loader = self.machinery.BuiltinImporter.find_module('importlib')
- self.assertIsNone(loader)
-
-
-(Frozen_FinderTests,
- Source_FinderTests
- ) = util.test_both(FinderTests, machinery=machinery)
-
-
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_importlib/extension/test_path_hook.py b/Lib/test/test_importlib/extension/test_path_hook.py
index a0adc70..ec9644d 100644
--- a/Lib/test/test_importlib/extension/test_path_hook.py
+++ b/Lib/test/test_importlib/extension/test_path_hook.py
@@ -19,7 +19,7 @@ class PathHookTests:
def test_success(self):
# Path hook should handle a directory where a known extension module
# exists.
- self.assertTrue(hasattr(self.hook(util.EXTENSIONS.path), 'find_module'))
+ self.assertTrue(hasattr(self.hook(util.EXTENSIONS.path), 'find_spec'))
(Frozen_PathHooksTests,
diff --git a/Lib/test/test_importlib/fixtures.py b/Lib/test/test_importlib/fixtures.py
index a364a97..73e5da2 100644
--- a/Lib/test/test_importlib/fixtures.py
+++ b/Lib/test/test_importlib/fixtures.py
@@ -350,11 +350,6 @@ def DALS(str):
return textwrap.dedent(str).lstrip()
-class NullFinder:
- def find_module(self, name):
- pass
-
-
@requires_zlib()
class ZipFixtures:
root = 'test.test_importlib.data'
diff --git a/Lib/test/test_importlib/frozen/test_finder.py b/Lib/test/test_importlib/frozen/test_finder.py
index 0697556..469dcdb 100644
--- a/Lib/test/test_importlib/frozen/test_finder.py
+++ b/Lib/test/test_importlib/frozen/test_finder.py
@@ -182,45 +182,5 @@ class FindSpecTests(abc.FinderTests):
) = util.test_both(FindSpecTests, machinery=machinery)
-class FinderTests(abc.FinderTests):
-
- """Test finding frozen modules."""
-
- def find(self, name, path=None):
- finder = self.machinery.FrozenImporter
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- with import_helper.frozen_modules():
- return finder.find_module(name, path)
-
- def test_module(self):
- name = '__hello__'
- loader = self.find(name)
- self.assertTrue(hasattr(loader, 'load_module'))
-
- def test_package(self):
- loader = self.find('__phello__')
- self.assertTrue(hasattr(loader, 'load_module'))
-
- def test_module_in_package(self):
- loader = self.find('__phello__.spam', ['__phello__'])
- self.assertTrue(hasattr(loader, 'load_module'))
-
- # No frozen package within another package to test with.
- test_package_in_package = None
-
- # No easy way to test.
- test_package_over_module = None
-
- def test_failure(self):
- loader = self.find('<not real>')
- self.assertIsNone(loader)
-
-
-(Frozen_FinderTests,
- Source_FinderTests
- ) = util.test_both(FinderTests, machinery=machinery)
-
-
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_importlib/frozen/test_loader.py b/Lib/test/test_importlib/frozen/test_loader.py
index da1569e..4f1af45 100644
--- a/Lib/test/test_importlib/frozen/test_loader.py
+++ b/Lib/test/test_importlib/frozen/test_loader.py
@@ -125,88 +125,6 @@ class ExecModuleTests(abc.LoaderTests):
) = util.test_both(ExecModuleTests, machinery=machinery)
-class LoaderTests(abc.LoaderTests):
-
- def load_module(self, name):
- with fresh(name, oldapi=True):
- module = self.machinery.FrozenImporter.load_module(name)
- with captured_stdout() as stdout:
- module.main()
- return module, stdout
-
- def test_module(self):
- module, stdout = self.load_module('__hello__')
- filename = resolve_stdlib_file('__hello__')
- check = {'__name__': '__hello__',
- '__package__': '',
- '__loader__': self.machinery.FrozenImporter,
- '__file__': filename,
- }
- for attr, value in check.items():
- self.assertEqual(getattr(module, attr, None), value)
- self.assertEqual(stdout.getvalue(), 'Hello world!\n')
-
- def test_package(self):
- module, stdout = self.load_module('__phello__')
- filename = resolve_stdlib_file('__phello__', ispkg=True)
- pkgdir = os.path.dirname(filename)
- check = {'__name__': '__phello__',
- '__package__': '__phello__',
- '__path__': [pkgdir],
- '__loader__': self.machinery.FrozenImporter,
- '__file__': filename,
- }
- for attr, value in check.items():
- attr_value = getattr(module, attr, None)
- self.assertEqual(attr_value, value,
- "for __phello__.%s, %r != %r" %
- (attr, attr_value, value))
- self.assertEqual(stdout.getvalue(), 'Hello world!\n')
-
- def test_lacking_parent(self):
- with util.uncache('__phello__'):
- module, stdout = self.load_module('__phello__.spam')
- filename = resolve_stdlib_file('__phello__.spam')
- check = {'__name__': '__phello__.spam',
- '__package__': '__phello__',
- '__loader__': self.machinery.FrozenImporter,
- '__file__': filename,
- }
- for attr, value in check.items():
- attr_value = getattr(module, attr)
- self.assertEqual(attr_value, value,
- "for __phello__.spam.%s, %r != %r" %
- (attr, attr_value, value))
- self.assertEqual(stdout.getvalue(), 'Hello world!\n')
-
- def test_module_reuse(self):
- with fresh('__hello__', oldapi=True):
- module1 = self.machinery.FrozenImporter.load_module('__hello__')
- module2 = self.machinery.FrozenImporter.load_module('__hello__')
- with captured_stdout() as stdout:
- module1.main()
- module2.main()
- self.assertIs(module1, module2)
- self.assertEqual(stdout.getvalue(),
- 'Hello world!\nHello world!\n')
-
- # No way to trigger an error in a frozen module.
- test_state_after_failure = None
-
- def test_unloadable(self):
- with import_helper.frozen_modules():
- with deprecated():
- assert self.machinery.FrozenImporter.find_module('_not_real') is None
- with self.assertRaises(ImportError) as cm:
- self.load_module('_not_real')
- self.assertEqual(cm.exception.name, '_not_real')
-
-
-(Frozen_LoaderTests,
- Source_LoaderTests
- ) = util.test_both(LoaderTests, machinery=machinery)
-
-
class InspectLoaderTests:
"""Tests for the InspectLoader methods for FrozenImporter."""
diff --git a/Lib/test/test_importlib/import_/test___loader__.py b/Lib/test/test_importlib/import_/test___loader__.py
index eaf665a..a141639 100644
--- a/Lib/test/test_importlib/import_/test___loader__.py
+++ b/Lib/test/test_importlib/import_/test___loader__.py
@@ -33,48 +33,5 @@ class SpecLoaderAttributeTests:
) = util.test_both(SpecLoaderAttributeTests, __import__=util.__import__)
-class LoaderMock:
-
- def find_module(self, fullname, path=None):
- return self
-
- def load_module(self, fullname):
- sys.modules[fullname] = self.module
- return self.module
-
-
-class LoaderAttributeTests:
-
- def test___loader___missing(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- module = types.ModuleType('blah')
- try:
- del module.__loader__
- except AttributeError:
- pass
- loader = LoaderMock()
- loader.module = module
- with util.uncache('blah'), util.import_state(meta_path=[loader]):
- module = self.__import__('blah')
- self.assertEqual(loader, module.__loader__)
-
- def test___loader___is_None(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- module = types.ModuleType('blah')
- module.__loader__ = None
- loader = LoaderMock()
- loader.module = module
- with util.uncache('blah'), util.import_state(meta_path=[loader]):
- returned_module = self.__import__('blah')
- self.assertEqual(loader, module.__loader__)
-
-
-(Frozen_Tests,
- Source_Tests
- ) = util.test_both(LoaderAttributeTests, __import__=util.__import__)
-
-
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_importlib/import_/test___package__.py b/Lib/test/test_importlib/import_/test___package__.py
index ab1b35e..7130c99 100644
--- a/Lib/test/test_importlib/import_/test___package__.py
+++ b/Lib/test/test_importlib/import_/test___package__.py
@@ -95,25 +95,6 @@ class FakeSpec:
self.parent = parent
-class Using__package__PEP302(Using__package__):
- mock_modules = util.mock_modules
-
- def test_using___package__(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- super().test_using___package__()
-
- def test_spec_fallback(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- super().test_spec_fallback()
-
-
-(Frozen_UsingPackagePEP302,
- Source_UsingPackagePEP302
- ) = util.test_both(Using__package__PEP302, __import__=util.__import__)
-
-
class Using__package__PEP451(Using__package__):
mock_modules = util.mock_spec
@@ -162,23 +143,6 @@ class Setting__package__:
module = getattr(pkg, 'mod')
self.assertEqual(module.__package__, 'pkg')
-class Setting__package__PEP302(Setting__package__, unittest.TestCase):
- mock_modules = util.mock_modules
-
- def test_top_level(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- super().test_top_level()
-
- def test_package(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- super().test_package()
-
- def test_submodule(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- super().test_submodule()
class Setting__package__PEP451(Setting__package__, unittest.TestCase):
mock_modules = util.mock_spec
diff --git a/Lib/test/test_importlib/import_/test_api.py b/Lib/test/test_importlib/import_/test_api.py
index 0ee032b..d6ad590 100644
--- a/Lib/test/test_importlib/import_/test_api.py
+++ b/Lib/test/test_importlib/import_/test_api.py
@@ -29,11 +29,6 @@ class BadSpecFinderLoader:
class BadLoaderFinder:
@classmethod
- def find_module(cls, fullname, path):
- if fullname == SUBMOD_NAME:
- return cls
-
- @classmethod
def load_module(cls, fullname):
if fullname == SUBMOD_NAME:
raise ImportError('I cannot be loaded!')
diff --git a/Lib/test/test_importlib/import_/test_caching.py b/Lib/test/test_importlib/import_/test_caching.py
index 3ca765f..aedf0fd 100644
--- a/Lib/test/test_importlib/import_/test_caching.py
+++ b/Lib/test/test_importlib/import_/test_caching.py
@@ -52,12 +52,11 @@ class ImportlibUseCache(UseCache, unittest.TestCase):
__import__ = util.__import__['Source']
def create_mock(self, *names, return_=None):
- mock = util.mock_modules(*names)
- original_load = mock.load_module
- def load_module(self, fullname):
- original_load(fullname)
- return return_
- mock.load_module = MethodType(load_module, mock)
+ mock = util.mock_spec(*names)
+ original_spec = mock.find_spec
+ def find_spec(self, fullname, path, target=None):
+ return original_spec(fullname)
+ mock.find_spec = MethodType(find_spec, mock)
return mock
# __import__ inconsistent between loaders and built-in import when it comes
@@ -86,14 +85,12 @@ class ImportlibUseCache(UseCache, unittest.TestCase):
# See test_using_cache_after_loader() for reasoning.
def test_using_cache_for_fromlist(self):
# [from cache for fromlist]
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- with self.create_mock('pkg.__init__', 'pkg.module') as importer:
- with util.import_state(meta_path=[importer]):
- module = self.__import__('pkg', fromlist=['module'])
- self.assertTrue(hasattr(module, 'module'))
- self.assertEqual(id(module.module),
- id(sys.modules['pkg.module']))
+ with self.create_mock('pkg.__init__', 'pkg.module') as importer:
+ with util.import_state(meta_path=[importer]):
+ module = self.__import__('pkg', fromlist=['module'])
+ self.assertTrue(hasattr(module, 'module'))
+ self.assertEqual(id(module.module),
+ id(sys.modules['pkg.module']))
if __name__ == '__main__':
diff --git a/Lib/test/test_importlib/import_/test_meta_path.py b/Lib/test/test_importlib/import_/test_meta_path.py
index c8b898e..8689017 100644
--- a/Lib/test/test_importlib/import_/test_meta_path.py
+++ b/Lib/test/test_importlib/import_/test_meta_path.py
@@ -113,16 +113,6 @@ class CallSignoreSuppressImportWarning(CallSignature):
super().test_no_path()
-class CallSignaturePEP302(CallSignoreSuppressImportWarning):
- mock_modules = util.mock_modules
- finder_name = 'find_module'
-
-
-(Frozen_CallSignaturePEP302,
- Source_CallSignaturePEP302
- ) = util.test_both(CallSignaturePEP302, __import__=util.__import__)
-
-
class CallSignaturePEP451(CallSignature):
mock_modules = util.mock_spec
finder_name = 'find_spec'
diff --git a/Lib/test/test_importlib/import_/test_path.py b/Lib/test/test_importlib/import_/test_path.py
index de62084..89b52fb 100644
--- a/Lib/test/test_importlib/import_/test_path.py
+++ b/Lib/test/test_importlib/import_/test_path.py
@@ -116,46 +116,6 @@ class FinderTests:
if email is not missing:
sys.modules['email'] = email
- def test_finder_with_find_module(self):
- class TestFinder:
- def find_module(self, fullname):
- return self.to_return
- failing_finder = TestFinder()
- failing_finder.to_return = None
- path = 'testing path'
- with util.import_state(path_importer_cache={path: failing_finder}):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- self.assertIsNone(
- self.machinery.PathFinder.find_spec('whatever', [path]))
- success_finder = TestFinder()
- success_finder.to_return = __loader__
- with util.import_state(path_importer_cache={path: success_finder}):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- spec = self.machinery.PathFinder.find_spec('whatever', [path])
- self.assertEqual(spec.loader, __loader__)
-
- def test_finder_with_find_loader(self):
- class TestFinder:
- loader = None
- portions = []
- def find_loader(self, fullname):
- return self.loader, self.portions
- path = 'testing path'
- with util.import_state(path_importer_cache={path: TestFinder()}):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- self.assertIsNone(
- self.machinery.PathFinder.find_spec('whatever', [path]))
- success_finder = TestFinder()
- success_finder.loader = __loader__
- with util.import_state(path_importer_cache={path: success_finder}):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- spec = self.machinery.PathFinder.find_spec('whatever', [path])
- self.assertEqual(spec.loader, __loader__)
-
def test_finder_with_find_spec(self):
class TestFinder:
spec = None
@@ -228,9 +188,9 @@ class FinderTests:
class FindModuleTests(FinderTests):
def find(self, *args, **kwargs):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- return self.machinery.PathFinder.find_module(*args, **kwargs)
+ spec = self.machinery.PathFinder.find_spec(*args, **kwargs)
+ return None if spec is None else spec.loader
+
def check_found(self, found, importer):
self.assertIs(found, importer)
@@ -255,16 +215,14 @@ class FindSpecTests(FinderTests):
class PathEntryFinderTests:
def test_finder_with_failing_find_spec(self):
- # PathEntryFinder with find_module() defined should work.
- # Issue #20763.
class Finder:
- path_location = 'test_finder_with_find_module'
+ path_location = 'test_finder_with_find_spec'
def __init__(self, path):
if path != self.path_location:
raise ImportError
@staticmethod
- def find_module(fullname):
+ def find_spec(fullname, target=None):
return None
@@ -274,27 +232,6 @@ class PathEntryFinderTests:
warnings.simplefilter("ignore", ImportWarning)
self.machinery.PathFinder.find_spec('importlib')
- def test_finder_with_failing_find_module(self):
- # PathEntryFinder with find_module() defined should work.
- # Issue #20763.
- class Finder:
- path_location = 'test_finder_with_find_module'
- def __init__(self, path):
- if path != self.path_location:
- raise ImportError
-
- @staticmethod
- def find_module(fullname):
- return None
-
-
- with util.import_state(path=[Finder.path_location]+sys.path[:],
- path_hooks=[Finder]):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", ImportWarning)
- warnings.simplefilter("ignore", DeprecationWarning)
- self.machinery.PathFinder.find_module('importlib')
-
(Frozen_PEFTests,
Source_PEFTests
diff --git a/Lib/test/test_importlib/source/test_case_sensitivity.py b/Lib/test/test_importlib/source/test_case_sensitivity.py
index 9d47270..6a06313 100644
--- a/Lib/test/test_importlib/source/test_case_sensitivity.py
+++ b/Lib/test/test_importlib/source/test_case_sensitivity.py
@@ -63,19 +63,6 @@ class CaseSensitivityTest(util.CASEOKTestBase):
self.assertIn(self.name, insensitive.get_filename(self.name))
-class CaseSensitivityTestPEP302(CaseSensitivityTest):
- def find(self, finder):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- return finder.find_module(self.name)
-
-
-(Frozen_CaseSensitivityTestPEP302,
- Source_CaseSensitivityTestPEP302
- ) = util.test_both(CaseSensitivityTestPEP302, importlib=importlib,
- machinery=machinery)
-
-
class CaseSensitivityTestPEP451(CaseSensitivityTest):
def find(self, finder):
found = finder.find_spec(self.name)
diff --git a/Lib/test/test_importlib/source/test_finder.py b/Lib/test/test_importlib/source/test_finder.py
index bed9d56..12db7c7 100644
--- a/Lib/test/test_importlib/source/test_finder.py
+++ b/Lib/test/test_importlib/source/test_finder.py
@@ -120,7 +120,7 @@ class FinderTests(abc.FinderTests):
def test_failure(self):
with util.create_modules('blah') as mapping:
nothing = self.import_(mapping['.root'], 'sdfsadsadf')
- self.assertIsNone(nothing)
+ self.assertEqual(nothing, self.NOT_FOUND)
def test_empty_string_for_dir(self):
# The empty string from sys.path means to search in the cwd.
@@ -150,7 +150,7 @@ class FinderTests(abc.FinderTests):
found = self._find(finder, 'mod', loader_only=True)
self.assertIsNotNone(found)
found = self._find(finder, 'mod', loader_only=True)
- self.assertIsNone(found)
+ self.assertEqual(found, self.NOT_FOUND)
@unittest.skipUnless(sys.platform != 'win32',
'os.chmod() does not support the needed arguments under Windows')
@@ -196,10 +196,12 @@ class FinderTestsPEP420(FinderTests):
NOT_FOUND = (None, [])
def _find(self, finder, name, loader_only=False):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- loader_portions = finder.find_loader(name)
- return loader_portions[0] if loader_only else loader_portions
+ spec = finder.find_spec(name)
+ if spec is None:
+ return self.NOT_FOUND
+ if loader_only:
+ return spec.loader
+ return spec.loader, spec.submodule_search_locations
(Frozen_FinderTestsPEP420,
@@ -207,20 +209,5 @@ class FinderTestsPEP420(FinderTests):
) = util.test_both(FinderTestsPEP420, machinery=machinery)
-class FinderTestsPEP302(FinderTests):
-
- NOT_FOUND = None
-
- def _find(self, finder, name, loader_only=False):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- return finder.find_module(name)
-
-
-(Frozen_FinderTestsPEP302,
- Source_FinderTestsPEP302
- ) = util.test_both(FinderTestsPEP302, machinery=machinery)
-
-
if __name__ == '__main__':
unittest.main()
diff --git a/Lib/test/test_importlib/source/test_path_hook.py b/Lib/test/test_importlib/source/test_path_hook.py
index ead62f5..f274330 100644
--- a/Lib/test/test_importlib/source/test_path_hook.py
+++ b/Lib/test/test_importlib/source/test_path_hook.py
@@ -18,19 +18,10 @@ class PathHookTest:
self.assertTrue(hasattr(self.path_hook()(mapping['.root']),
'find_spec'))
- def test_success_legacy(self):
- with util.create_modules('dummy') as mapping:
- self.assertTrue(hasattr(self.path_hook()(mapping['.root']),
- 'find_module'))
-
def test_empty_string(self):
# The empty string represents the cwd.
self.assertTrue(hasattr(self.path_hook()(''), 'find_spec'))
- def test_empty_string_legacy(self):
- # The empty string represents the cwd.
- self.assertTrue(hasattr(self.path_hook()(''), 'find_module'))
-
(Frozen_PathHookTest,
Source_PathHooktest
diff --git a/Lib/test/test_importlib/test_abc.py b/Lib/test/test_importlib/test_abc.py
index 3c9149c..603125f 100644
--- a/Lib/test/test_importlib/test_abc.py
+++ b/Lib/test/test_importlib/test_abc.py
@@ -147,20 +147,13 @@ class ABCTestHarness:
class MetaPathFinder:
- def find_module(self, fullname, path):
- return super().find_module(fullname, path)
+ pass
class MetaPathFinderDefaultsTests(ABCTestHarness):
SPLIT = make_abc_subclasses(MetaPathFinder)
- def test_find_module(self):
- # Default should return None.
- with self.assertWarns(DeprecationWarning):
- found = self.ins.find_module('something', None)
- self.assertIsNone(found)
-
def test_invalidate_caches(self):
# Calling the method is a no-op.
self.ins.invalidate_caches()
@@ -173,22 +166,13 @@ class MetaPathFinderDefaultsTests(ABCTestHarness):
class PathEntryFinder:
- def find_loader(self, fullname):
- return super().find_loader(fullname)
+ pass
class PathEntryFinderDefaultsTests(ABCTestHarness):
SPLIT = make_abc_subclasses(PathEntryFinder)
- def test_find_loader(self):
- with self.assertWarns(DeprecationWarning):
- found = self.ins.find_loader('something')
- self.assertEqual(found, (None, []))
-
- def find_module(self):
- self.assertEqual(None, self.ins.find_module('something'))
-
def test_invalidate_caches(self):
# Should be a no-op.
self.ins.invalidate_caches()
@@ -201,8 +185,7 @@ class PathEntryFinderDefaultsTests(ABCTestHarness):
class Loader:
- def load_module(self, fullname):
- return super().load_module(fullname)
+ pass
class LoaderDefaultsTests(ABCTestHarness):
@@ -333,14 +316,6 @@ class MetaPathFinderFindModuleTests:
return MetaPathSpecFinder()
- def test_find_module(self):
- finder = self.finder(None)
- path = ['a', 'b', 'c']
- name = 'blah'
- with self.assertWarns(DeprecationWarning):
- found = finder.find_module(name, path)
- self.assertIsNone(found)
-
def test_find_spec_with_explicit_target(self):
loader = object()
spec = self.util.spec_from_loader('blah', loader)
@@ -370,53 +345,6 @@ class MetaPathFinderFindModuleTests:
) = test_util.test_both(MetaPathFinderFindModuleTests, abc=abc, util=util)
-##### PathEntryFinder concrete methods #########################################
-class PathEntryFinderFindLoaderTests:
-
- @classmethod
- def finder(cls, spec):
- class PathEntrySpecFinder(cls.abc.PathEntryFinder):
-
- def find_spec(self, fullname, target=None):
- self.called_for = fullname
- return spec
-
- return PathEntrySpecFinder()
-
- def test_no_spec(self):
- finder = self.finder(None)
- name = 'blah'
- with self.assertWarns(DeprecationWarning):
- found = finder.find_loader(name)
- self.assertIsNone(found[0])
- self.assertEqual([], found[1])
- self.assertEqual(name, finder.called_for)
-
- def test_spec_with_loader(self):
- loader = object()
- spec = self.util.spec_from_loader('blah', loader)
- finder = self.finder(spec)
- with self.assertWarns(DeprecationWarning):
- found = finder.find_loader('blah')
- self.assertIs(found[0], spec.loader)
-
- def test_spec_with_portions(self):
- spec = self.machinery.ModuleSpec('blah', None)
- paths = ['a', 'b', 'c']
- spec.submodule_search_locations = paths
- finder = self.finder(spec)
- with self.assertWarns(DeprecationWarning):
- found = finder.find_loader('blah')
- self.assertIsNone(found[0])
- self.assertEqual(paths, found[1])
-
-
-(Frozen_PEFFindLoaderTests,
- Source_PEFFindLoaderTests
- ) = test_util.test_both(PathEntryFinderFindLoaderTests, abc=abc, util=util,
- machinery=machinery)
-
-
##### Loader concrete methods ##################################################
class LoaderLoadModuleTests:
diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py
index b3a99dc..ecf2c47 100644
--- a/Lib/test/test_importlib/test_api.py
+++ b/Lib/test/test_importlib/test_api.py
@@ -95,7 +95,8 @@ class ImportModuleTests:
(Frozen_ImportModuleTests,
Source_ImportModuleTests
- ) = test_util.test_both(ImportModuleTests, init=init)
+ ) = test_util.test_both(
+ ImportModuleTests, init=init, util=util, machinery=machinery)
class FindLoaderTests:
@@ -103,29 +104,26 @@ class FindLoaderTests:
FakeMetaFinder = None
def test_sys_modules(self):
- # If a module with __loader__ is in sys.modules, then return it.
+ # If a module with __spec__.loader is in sys.modules, then return it.
name = 'some_mod'
with test_util.uncache(name):
module = types.ModuleType(name)
loader = 'a loader!'
- module.__loader__ = loader
+ module.__spec__ = self.machinery.ModuleSpec(name, loader)
sys.modules[name] = module
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- found = self.init.find_loader(name)
- self.assertEqual(loader, found)
+ spec = self.util.find_spec(name)
+ self.assertIsNotNone(spec)
+ self.assertEqual(spec.loader, loader)
def test_sys_modules_loader_is_None(self):
- # If sys.modules[name].__loader__ is None, raise ValueError.
+ # If sys.modules[name].__spec__.loader is None, raise ValueError.
name = 'some_mod'
with test_util.uncache(name):
module = types.ModuleType(name)
module.__loader__ = None
sys.modules[name] = module
with self.assertRaises(ValueError):
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- self.init.find_loader(name)
+ self.util.find_spec(name)
def test_sys_modules_loader_is_not_set(self):
# Should raise ValueError
@@ -134,24 +132,20 @@ class FindLoaderTests:
with test_util.uncache(name):
module = types.ModuleType(name)
try:
- del module.__loader__
+ del module.__spec__.loader
except AttributeError:
pass
sys.modules[name] = module
with self.assertRaises(ValueError):
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- self.init.find_loader(name)
+ self.util.find_spec(name)
def test_success(self):
# Return the loader found on sys.meta_path.
name = 'some_mod'
with test_util.uncache(name):
with test_util.import_state(meta_path=[self.FakeMetaFinder]):
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- warnings.simplefilter('ignore', ImportWarning)
- self.assertEqual((name, None), self.init.find_loader(name))
+ spec = self.util.find_spec(name)
+ self.assertEqual((name, (name, None)), (spec.name, spec.loader))
def test_success_path(self):
# Searching on a path should work.
@@ -159,17 +153,12 @@ class FindLoaderTests:
path = 'path to some place'
with test_util.uncache(name):
with test_util.import_state(meta_path=[self.FakeMetaFinder]):
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- warnings.simplefilter('ignore', ImportWarning)
- self.assertEqual((name, path),
- self.init.find_loader(name, path))
+ spec = self.util.find_spec(name, path)
+ self.assertEqual(name, spec.name)
def test_nothing(self):
# None is returned upon failure to find a loader.
- with warnings.catch_warnings():
- warnings.simplefilter('ignore', DeprecationWarning)
- self.assertIsNone(self.init.find_loader('nevergoingtofindthismodule'))
+ self.assertIsNone(self.util.find_spec('nevergoingtofindthismodule'))
class FindLoaderPEP451Tests(FindLoaderTests):
@@ -182,20 +171,8 @@ class FindLoaderPEP451Tests(FindLoaderTests):
(Frozen_FindLoaderPEP451Tests,
Source_FindLoaderPEP451Tests
- ) = test_util.test_both(FindLoaderPEP451Tests, init=init)
-
-
-class FindLoaderPEP302Tests(FindLoaderTests):
-
- class FakeMetaFinder:
- @staticmethod
- def find_module(name, path=None):
- return name, path
-
-
-(Frozen_FindLoaderPEP302Tests,
- Source_FindLoaderPEP302Tests
- ) = test_util.test_both(FindLoaderPEP302Tests, init=init)
+ ) = test_util.test_both(
+ FindLoaderPEP451Tests, init=init, util=util, machinery=machinery)
class ReloadTests:
@@ -380,7 +357,8 @@ class ReloadTests:
(Frozen_ReloadTests,
Source_ReloadTests
- ) = test_util.test_both(ReloadTests, init=init, util=util)
+ ) = test_util.test_both(
+ ReloadTests, init=init, util=util, machinery=machinery)
class InvalidateCacheTests:
@@ -390,8 +368,6 @@ class InvalidateCacheTests:
class InvalidatingNullFinder:
def __init__(self, *ignored):
self.called = False
- def find_module(self, *args):
- return None
def invalidate_caches(self):
self.called = True
@@ -416,7 +392,8 @@ class InvalidateCacheTests:
(Frozen_InvalidateCacheTests,
Source_InvalidateCacheTests
- ) = test_util.test_both(InvalidateCacheTests, init=init)
+ ) = test_util.test_both(
+ InvalidateCacheTests, init=init, util=util, machinery=machinery)
class FrozenImportlibTests(unittest.TestCase):
diff --git a/Lib/test/test_importlib/test_windows.py b/Lib/test/test_importlib/test_windows.py
index b7dfe86..40b8aa1 100644
--- a/Lib/test/test_importlib/test_windows.py
+++ b/Lib/test/test_importlib/test_windows.py
@@ -92,30 +92,16 @@ class WindowsRegistryFinderTests:
def test_find_spec_missing(self):
spec = self.machinery.WindowsRegistryFinder.find_spec('spam')
- self.assertIs(spec, None)
-
- def test_find_module_missing(self):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- loader = self.machinery.WindowsRegistryFinder.find_module('spam')
- self.assertIs(loader, None)
+ self.assertIsNone(spec)
def test_module_found(self):
with setup_module(self.machinery, self.test_module):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module)
spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
- self.assertIsNot(loader, None)
- self.assertIsNot(spec, None)
+ self.assertIsNotNone(spec)
def test_module_not_found(self):
with setup_module(self.machinery, self.test_module, path="."):
- with warnings.catch_warnings():
- warnings.simplefilter("ignore", DeprecationWarning)
- loader = self.machinery.WindowsRegistryFinder.find_module(self.test_module)
spec = self.machinery.WindowsRegistryFinder.find_spec(self.test_module)
- self.assertIsNone(loader)
self.assertIsNone(spec)
(Frozen_WindowsRegistryFinderTests,
diff --git a/Lib/test/test_importlib/util.py b/Lib/test/test_importlib/util.py
index e348733..c25be09 100644
--- a/Lib/test/test_importlib/util.py
+++ b/Lib/test/test_importlib/util.py
@@ -194,8 +194,7 @@ def import_state(**kwargs):
new_value = default
setattr(sys, attr, new_value)
if len(kwargs):
- raise ValueError(
- 'unrecognized arguments: {0}'.format(kwargs.keys()))
+ raise ValueError('unrecognized arguments: {}'.format(kwargs))
yield
finally:
for attr, value in originals.items():
@@ -243,30 +242,6 @@ class _ImporterMock:
self._uncache.__exit__(None, None, None)
-class mock_modules(_ImporterMock):
-
- """Importer mock using PEP 302 APIs."""
-
- def find_module(self, fullname, path=None):
- if fullname not in self.modules:
- return None
- else:
- return self
-
- def load_module(self, fullname):
- if fullname not in self.modules:
- raise ImportError
- else:
- sys.modules[fullname] = self.modules[fullname]
- if fullname in self.module_code:
- try:
- self.module_code[fullname]()
- except Exception:
- del sys.modules[fullname]
- raise
- return self.modules[fullname]
-
-
class mock_spec(_ImporterMock):
"""Importer mock using PEP 451 APIs."""
diff --git a/Lib/test/test_pkgutil.py b/Lib/test/test_pkgutil.py
index 4d9f5db..9026270 100644
--- a/Lib/test/test_pkgutil.py
+++ b/Lib/test/test_pkgutil.py
@@ -429,7 +429,7 @@ class ExtendPathTests(unittest.TestCase):
importers = list(iter_importers(fullname))
expected_importer = get_importer(pathitem)
for finder in importers:
- spec = pkgutil._get_spec(finder, fullname)
+ spec = finder.find_spec(fullname)
loader = spec.loader
try:
loader = loader.loader
@@ -441,7 +441,7 @@ class ExtendPathTests(unittest.TestCase):
self.assertEqual(finder, expected_importer)
self.assertIsInstance(loader,
importlib.machinery.SourceFileLoader)
- self.assertIsNone(pkgutil._get_spec(finder, pkgname))
+ self.assertIsNone(finder.find_spec(pkgname))
with self.assertRaises(ImportError):
list(iter_importers('invalid.module'))
@@ -535,12 +535,6 @@ class ImportlibMigrationTests(unittest.TestCase):
# PEP 302 emulation in this module is in the process of being
# deprecated in favour of importlib proper
- def check_deprecated(self):
- return check_warnings(
- ("This emulation is deprecated and slated for removal in "
- "Python 3.12; use 'importlib' instead",
- DeprecationWarning))
-
def test_get_loader_avoids_emulation(self):
with check_warnings() as w:
self.assertIsNotNone(pkgutil.get_loader("sys"))
diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py
index 52d43bd..14c1971 100644
--- a/Lib/test/test_zipimport.py
+++ b/Lib/test/test_zipimport.py
@@ -836,7 +836,6 @@ class BadFileZipImportTestCase(unittest.TestCase):
self.assertRaises(TypeError, z.get_source, None)
error = zipimport.ZipImportError
- self.assertIsNone(z.find_module('abc'))
self.assertIsNone(z.find_spec('abc'))
with warnings.catch_warnings():