diff options
author | Brett Cannon <brett@python.org> | 2021-03-30 15:43:03 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-03-30 15:43:03 (GMT) |
commit | a7ff6df60c05e1b69fca743573b1e118bebf121d (patch) | |
tree | 7dae896ba674245645b5ca46c02cc387a43d35a0 /Lib | |
parent | cf35e05f89bb008d6f4553f9875e0fe87fc02406 (diff) | |
download | cpython-a7ff6df60c05e1b69fca743573b1e118bebf121d.zip cpython-a7ff6df60c05e1b69fca743573b1e118bebf121d.tar.gz cpython-a7ff6df60c05e1b69fca743573b1e118bebf121d.tar.bz2 |
bpo-42134: Raise ImportWarning when calling find_module() in the import system (GH-25044)
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_bootstrap.py | 5 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap_external.py | 3 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_path.py | 16 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_api.py | 2 |
4 files changed, 20 insertions, 6 deletions
diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index d5acb65..ab52e77 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -903,8 +903,9 @@ def _resolve_name(name, package, level): def _find_spec_legacy(finder, name, path): - # This would be a good place for a DeprecationWarning if - # we ended up going that route. + 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 diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index dac881f..bf7c268 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -1324,6 +1324,9 @@ class PathFinder: if hasattr(finder, 'find_loader'): 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: diff --git a/Lib/test/test_importlib/import_/test_path.py b/Lib/test/test_importlib/import_/test_path.py index 18c81dd..c51aee2 100644 --- a/Lib/test/test_importlib/import_/test_path.py +++ b/Lib/test/test_importlib/import_/test_path.py @@ -123,12 +123,16 @@ class FinderTests: failing_finder.to_return = None path = 'testing path' with util.import_state(path_importer_cache={path: failing_finder}): - self.assertIsNone( + 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}): - spec = self.machinery.PathFinder.find_spec('whatever', [path]) + 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): @@ -248,7 +252,9 @@ class PathEntryFinderTests: with util.import_state(path=[Finder.path_location]+sys.path[:], path_hooks=[Finder]): - self.machinery.PathFinder.find_spec('importlib') + with warnings.catch_warnings(): + 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. @@ -266,7 +272,9 @@ class PathEntryFinderTests: with util.import_state(path=[Finder.path_location]+sys.path[:], path_hooks=[Finder]): - self.machinery.PathFinder.find_module('importlib') + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + self.machinery.PathFinder.find_module('importlib') (Frozen_PEFTests, diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index 3f06a10..384ae9c 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -151,6 +151,7 @@ class FindLoaderTests: 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)) def test_success_path(self): @@ -161,6 +162,7 @@ class FindLoaderTests: 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)) |