diff options
author | Brett Cannon <brett@python.org> | 2020-12-04 23:39:21 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-12-04 23:39:21 (GMT) |
commit | 2de5097ba4c50eba90df55696a7b2e74c93834d4 (patch) | |
tree | 7a3d54477997114ef27136e9c9da0e380f835a34 /Lib | |
parent | 79c1849b9e5b635bd36b13e1be9dc7cbc2bd6312 (diff) | |
download | cpython-2de5097ba4c50eba90df55696a7b2e74c93834d4.zip cpython-2de5097ba4c50eba90df55696a7b2e74c93834d4.tar.gz cpython-2de5097ba4c50eba90df55696a7b2e74c93834d4.tar.bz2 |
bpo-26131: Deprecate usage of load_module() (GH-23469)
Raise an ImportWarning when the import system falls back on load_module(). As for implementations of load_module(), raise a DeprecationWarning.
Diffstat (limited to 'Lib')
-rw-r--r-- | Lib/importlib/_abc.py | 1 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap.py | 24 | ||||
-rw-r--r-- | Lib/importlib/_bootstrap_external.py | 6 | ||||
-rw-r--r-- | Lib/test/test_importlib/builtin/test_loader.py | 5 | ||||
-rw-r--r-- | Lib/test/test_importlib/extension/test_loader.py | 44 | ||||
-rw-r--r-- | Lib/test/test_importlib/frozen/test_loader.py | 20 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test___loader__.py | 39 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test___package__.py | 25 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_api.py | 31 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_caching.py | 39 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_fromlist.py | 26 | ||||
-rw-r--r-- | Lib/test/test_importlib/import_/test_meta_path.py | 14 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_abc.py | 84 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_api.py | 16 | ||||
-rw-r--r-- | Lib/test/test_importlib/test_spec.py | 54 | ||||
-rw-r--r-- | Lib/test/test_venv.py | 11 | ||||
-rw-r--r-- | Lib/test/test_zipimport.py | 51 | ||||
-rw-r--r-- | Lib/zipimport.py | 6 |
18 files changed, 321 insertions, 175 deletions
diff --git a/Lib/importlib/_abc.py b/Lib/importlib/_abc.py index fb5ec72..7591946 100644 --- a/Lib/importlib/_abc.py +++ b/Lib/importlib/_abc.py @@ -35,6 +35,7 @@ class Loader(metaclass=abc.ABCMeta): """ if not hasattr(self, 'exec_module'): raise ImportError + # Warning implemented in _load_module_shim(). return _bootstrap._load_module_shim(self, fullname) def module_repr(self, module): diff --git a/Lib/importlib/_bootstrap.py b/Lib/importlib/_bootstrap.py index 9b7335b..e4f893c 100644 --- a/Lib/importlib/_bootstrap.py +++ b/Lib/importlib/_bootstrap.py @@ -20,6 +20,12 @@ work. One should use importlib as the public-facing version of this module. # reference any injected objects! This includes not only global code but also # anything specified at the class level. +def _object_name(obj): + try: + return obj.__qualname__ + except AttributeError: + return type(obj).__qualname__ + # Bootstrap-related code ###################################################### # Modules injected manually by _setup() @@ -272,6 +278,9 @@ def _load_module_shim(self, fullname): This method is deprecated. Use loader.exec_module instead. """ + msg = ("the load_module() method is deprecated and slated for removal in " + "Python 3.12; use exec_module() instead") + _warnings.warn(msg, DeprecationWarning) spec = spec_from_loader(fullname, self) if fullname in sys.modules: module = sys.modules[fullname] @@ -612,9 +621,9 @@ def _exec(spec, module): else: _init_module_attrs(spec, module, override=True) if not hasattr(spec.loader, 'exec_module'): - # (issue19713) Once BuiltinImporter and ExtensionFileLoader - # have exec_module() implemented, we can add a deprecation - # warning here. + msg = (f"{_object_name(spec.loader)}.exec_module() not found; " + "falling back to load_module()") + _warnings.warn(msg, ImportWarning) spec.loader.load_module(name) else: spec.loader.exec_module(module) @@ -627,9 +636,8 @@ def _exec(spec, module): def _load_backward_compatible(spec): - # (issue19713) Once BuiltinImporter and ExtensionFileLoader - # have exec_module() implemented, we can add a deprecation - # warning here. + # It is assumed that all callers have been warned about using load_module() + # appropriately before calling this function. try: spec.loader.load_module(spec.name) except: @@ -668,6 +676,9 @@ def _load_unlocked(spec): if spec.loader is not None: # Not a namespace package. if not hasattr(spec.loader, 'exec_module'): + msg = (f"{_object_name(spec.loader)}.exec_module() not found; " + "falling back to load_module()") + _warnings.warn(msg, ImportWarning) return _load_backward_compatible(spec) module = module_from_spec(spec) @@ -851,6 +862,7 @@ class FrozenImporter: This method is deprecated. Use exec_module() instead. """ + # Warning about deprecation implemented in _load_module_shim(). return _load_module_shim(cls, fullname) @classmethod diff --git a/Lib/importlib/_bootstrap_external.py b/Lib/importlib/_bootstrap_external.py index b8dd128..d9e44df 100644 --- a/Lib/importlib/_bootstrap_external.py +++ b/Lib/importlib/_bootstrap_external.py @@ -832,7 +832,8 @@ class _LoaderBasics: _bootstrap._call_with_frames_removed(exec, code, module.__dict__) def load_module(self, fullname): - """This module is deprecated.""" + """This method is deprecated.""" + # Warning implemented in _load_module_shim(). return _bootstrap._load_module_shim(self, fullname) @@ -1007,7 +1008,7 @@ class FileLoader: """ # The only reason for this method is for the name check. # Issue #14857: Avoid the zero-argument form of super so the implementation - # of that form can be updated without breaking the frozen module + # of that form can be updated without breaking the frozen module. return super(FileLoader, self).load_module(fullname) @_check_name @@ -1253,6 +1254,7 @@ class _NamespaceLoader: # The import system never calls this method. _bootstrap._verbose_message('namespace module loaded with path {!r}', self._path) + # Warning implemented in _load_module_shim(). return _bootstrap._load_module_shim(self, fullname) diff --git a/Lib/test/test_importlib/builtin/test_loader.py b/Lib/test/test_importlib/builtin/test_loader.py index b1349ec..f6b6d97 100644 --- a/Lib/test/test_importlib/builtin/test_loader.py +++ b/Lib/test/test_importlib/builtin/test_loader.py @@ -6,6 +6,7 @@ machinery = util.import_importlib('importlib.machinery') import sys import types import unittest +import warnings @unittest.skipIf(util.BUILTINS.good_name is None, 'no reasonable builtin module') class LoaderTests(abc.LoaderTests): @@ -24,7 +25,9 @@ class LoaderTests(abc.LoaderTests): self.assertIn(module.__name__, sys.modules) def load_module(self, name): - return self.machinery.BuiltinImporter.load_module(name) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + return self.machinery.BuiltinImporter.load_module(name) def test_module(self): # Common case. diff --git a/Lib/test/test_importlib/extension/test_loader.py b/Lib/test/test_importlib/extension/test_loader.py index abd612f..22cf2da 100644 --- a/Lib/test/test_importlib/extension/test_loader.py +++ b/Lib/test/test_importlib/extension/test_loader.py @@ -1,3 +1,4 @@ +from warnings import catch_warnings from .. import abc from .. import util @@ -7,6 +8,7 @@ import os.path import sys import types import unittest +import warnings import importlib.util import importlib from test.support.script_helper import assert_python_failure @@ -20,14 +22,18 @@ class LoaderTests(abc.LoaderTests): util.EXTENSIONS.file_path) def load_module(self, fullname): - return self.loader.load_module(fullname) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + return self.loader.load_module(fullname) def test_load_module_API(self): # Test the default argument for load_module(). - self.loader.load_module() - self.loader.load_module(None) - with self.assertRaises(ImportError): - self.load_module('XXX') + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + self.loader.load_module() + self.loader.load_module(None) + with self.assertRaises(ImportError): + self.load_module('XXX') def test_equality(self): other = self.machinery.ExtensionFileLoader(util.EXTENSIONS.name, @@ -94,6 +100,21 @@ class MultiPhaseExtensionModuleTests(abc.LoaderTests): self.loader = self.machinery.ExtensionFileLoader( self.name, self.spec.origin) + def load_module(self): + '''Load the module from the test extension''' + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + return self.loader.load_module(self.name) + + def load_module_by_name(self, fullname): + '''Load a module from the test extension by name''' + origin = self.spec.origin + loader = self.machinery.ExtensionFileLoader(fullname, origin) + spec = importlib.util.spec_from_loader(fullname, loader) + module = importlib.util.module_from_spec(spec) + loader.exec_module(module) + return module + # No extension module as __init__ available for testing. test_package = None @@ -157,19 +178,6 @@ class MultiPhaseExtensionModuleTests(abc.LoaderTests): with self.assertRaises(SystemError): module.call_state_registration_func(2) - def load_module(self): - '''Load the module from the test extension''' - return self.loader.load_module(self.name) - - def load_module_by_name(self, fullname): - '''Load a module from the test extension by name''' - origin = self.spec.origin - loader = self.machinery.ExtensionFileLoader(fullname, origin) - spec = importlib.util.spec_from_loader(fullname, loader) - module = importlib.util.module_from_spec(spec) - loader.exec_module(module) - return module - def test_load_submodule(self): '''Test loading a simulated submodule''' module = self.load_module_by_name('pkg.' + self.name) diff --git a/Lib/test/test_importlib/frozen/test_loader.py b/Lib/test/test_importlib/frozen/test_loader.py index 29ecff1..8eaffa7 100644 --- a/Lib/test/test_importlib/frozen/test_loader.py +++ b/Lib/test/test_importlib/frozen/test_loader.py @@ -161,19 +161,23 @@ class LoaderTests(abc.LoaderTests): "<module '__hello__' (frozen)>") def test_module_repr_indirect(self): - with util.uncache('__hello__'), captured_stdout(): - module = self.machinery.FrozenImporter.load_module('__hello__') - self.assertEqual(repr(module), - "<module '__hello__' (frozen)>") + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + with util.uncache('__hello__'), captured_stdout(): + module = self.machinery.FrozenImporter.load_module('__hello__') + self.assertEqual(repr(module), + "<module '__hello__' (frozen)>") # No way to trigger an error in a frozen module. test_state_after_failure = None def test_unloadable(self): - assert self.machinery.FrozenImporter.find_module('_not_real') is None - with self.assertRaises(ImportError) as cm: - self.machinery.FrozenImporter.load_module('_not_real') - self.assertEqual(cm.exception.name, '_not_real') + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + assert self.machinery.FrozenImporter.find_module('_not_real') is None + with self.assertRaises(ImportError) as cm: + self.machinery.FrozenImporter.load_module('_not_real') + self.assertEqual(cm.exception.name, '_not_real') (Frozen_LoaderTests, diff --git a/Lib/test/test_importlib/import_/test___loader__.py b/Lib/test/test_importlib/import_/test___loader__.py index 4b18093..ecd83c6 100644 --- a/Lib/test/test_importlib/import_/test___loader__.py +++ b/Lib/test/test_importlib/import_/test___loader__.py @@ -2,6 +2,7 @@ from importlib import machinery import sys import types import unittest +import warnings from .. import util @@ -45,25 +46,29 @@ class LoaderMock: class LoaderAttributeTests: def test___loader___missing(self): - 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__) + 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): - 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__) + 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, diff --git a/Lib/test/test_importlib/import_/test___package__.py b/Lib/test/test_importlib/import_/test___package__.py index 761b256..4a2b34e 100644 --- a/Lib/test/test_importlib/import_/test___package__.py +++ b/Lib/test/test_importlib/import_/test___package__.py @@ -98,6 +98,16 @@ class FakeSpec: 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 @@ -155,6 +165,21 @@ class Setting__package__: 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 0cd9de4..35c2697 100644 --- a/Lib/test/test_importlib/import_/test_api.py +++ b/Lib/test/test_importlib/import_/test_api.py @@ -4,6 +4,7 @@ from importlib import machinery import sys import types import unittest +import warnings PKG_NAME = 'fine' SUBMOD_NAME = 'fine.bogus' @@ -100,6 +101,36 @@ class APITest: class OldAPITests(APITest): bad_finder_loader = BadLoaderFinder + def test_raises_ModuleNotFoundError(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + super().test_raises_ModuleNotFoundError() + + def test_name_requires_rparition(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + super().test_name_requires_rparition() + + def test_negative_level(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + super().test_negative_level() + + def test_nonexistent_fromlist_entry(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + super().test_nonexistent_fromlist_entry() + + def test_fromlist_load_error_propagates(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + super().test_fromlist_load_error_propagates + + def test_blocked_fromlist(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + super().test_blocked_fromlist() + (Frozen_OldAPITests, Source_OldAPITests diff --git a/Lib/test/test_importlib/import_/test_caching.py b/Lib/test/test_importlib/import_/test_caching.py index 8079add..0f987b2 100644 --- a/Lib/test/test_importlib/import_/test_caching.py +++ b/Lib/test/test_importlib/import_/test_caching.py @@ -3,6 +3,7 @@ from .. import util import sys from types import MethodType import unittest +import warnings class UseCache: @@ -63,30 +64,36 @@ class ImportlibUseCache(UseCache, unittest.TestCase): # to when to use the module in sys.modules and when not to. def test_using_cache_after_loader(self): # [from cache on return] - with self.create_mock('module') as mock: - with util.import_state(meta_path=[mock]): - module = self.__import__('module') - self.assertEqual(id(module), id(sys.modules['module'])) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + with self.create_mock('module') as mock: + with util.import_state(meta_path=[mock]): + module = self.__import__('module') + self.assertEqual(id(module), id(sys.modules['module'])) # See test_using_cache_after_loader() for reasoning. def test_using_cache_for_assigning_to_attribute(self): # [from cache to attribute] - with self.create_mock('pkg.__init__', 'pkg.module') as importer: - with util.import_state(meta_path=[importer]): - module = self.__import__('pkg.module') - self.assertTrue(hasattr(module, 'module')) - self.assertEqual(id(module.module), - id(sys.modules['pkg.module'])) + 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.module') + self.assertTrue(hasattr(module, 'module')) + self.assertEqual(id(module.module), + id(sys.modules['pkg.module'])) # See test_using_cache_after_loader() for reasoning. def test_using_cache_for_fromlist(self): # [from cache for fromlist] - 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 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'])) if __name__ == '__main__': diff --git a/Lib/test/test_importlib/import_/test_fromlist.py b/Lib/test/test_importlib/import_/test_fromlist.py index 018c172..deb2171 100644 --- a/Lib/test/test_importlib/import_/test_fromlist.py +++ b/Lib/test/test_importlib/import_/test_fromlist.py @@ -24,7 +24,7 @@ class ReturnValue: def test_return_from_from_import(self): # [from return] - with util.mock_modules('pkg.__init__', 'pkg.module')as importer: + with util.mock_spec('pkg.__init__', 'pkg.module')as importer: with util.import_state(meta_path=[importer]): module = self.__import__('pkg.module', fromlist=['attr']) self.assertEqual(module.__name__, 'pkg.module') @@ -52,14 +52,14 @@ class HandlingFromlist: def test_object(self): # [object case] - with util.mock_modules('module') as importer: + with util.mock_spec('module') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('module', fromlist=['attr']) self.assertEqual(module.__name__, 'module') def test_nonexistent_object(self): # [bad object] - with util.mock_modules('module') as importer: + with util.mock_spec('module') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('module', fromlist=['non_existent']) self.assertEqual(module.__name__, 'module') @@ -67,7 +67,7 @@ class HandlingFromlist: def test_module_from_package(self): # [module] - with util.mock_modules('pkg.__init__', 'pkg.module') as importer: + with util.mock_spec('pkg.__init__', 'pkg.module') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('pkg', fromlist=['module']) self.assertEqual(module.__name__, 'pkg') @@ -75,7 +75,7 @@ class HandlingFromlist: self.assertEqual(module.module.__name__, 'pkg.module') def test_nonexistent_from_package(self): - with util.mock_modules('pkg.__init__') as importer: + with util.mock_spec('pkg.__init__') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('pkg', fromlist=['non_existent']) self.assertEqual(module.__name__, 'pkg') @@ -87,7 +87,7 @@ class HandlingFromlist: # ModuleNotFoundError propagate. def module_code(): import i_do_not_exist - with util.mock_modules('pkg.__init__', 'pkg.mod', + with util.mock_spec('pkg.__init__', 'pkg.mod', module_code={'pkg.mod': module_code}) as importer: with util.import_state(meta_path=[importer]): with self.assertRaises(ModuleNotFoundError) as exc: @@ -95,14 +95,14 @@ class HandlingFromlist: self.assertEqual('i_do_not_exist', exc.exception.name) def test_empty_string(self): - with util.mock_modules('pkg.__init__', 'pkg.mod') as importer: + with util.mock_spec('pkg.__init__', 'pkg.mod') as importer: with util.import_state(meta_path=[importer]): module = self.__import__('pkg.mod', fromlist=['']) self.assertEqual(module.__name__, 'pkg.mod') def basic_star_test(self, fromlist=['*']): # [using *] - with util.mock_modules('pkg.__init__', 'pkg.module') as mock: + with util.mock_spec('pkg.__init__', 'pkg.module') as mock: with util.import_state(meta_path=[mock]): mock['pkg'].__all__ = ['module'] module = self.__import__('pkg', fromlist=fromlist) @@ -119,7 +119,7 @@ class HandlingFromlist: def test_star_with_others(self): # [using * with others] - context = util.mock_modules('pkg.__init__', 'pkg.module1', 'pkg.module2') + context = util.mock_spec('pkg.__init__', 'pkg.module1', 'pkg.module2') with context as mock: with util.import_state(meta_path=[mock]): mock['pkg'].__all__ = ['module1'] @@ -131,7 +131,7 @@ class HandlingFromlist: self.assertEqual(module.module2.__name__, 'pkg.module2') def test_nonexistent_in_all(self): - with util.mock_modules('pkg.__init__') as importer: + with util.mock_spec('pkg.__init__') as importer: with util.import_state(meta_path=[importer]): importer['pkg'].__all__ = ['non_existent'] module = self.__import__('pkg', fromlist=['*']) @@ -139,7 +139,7 @@ class HandlingFromlist: self.assertFalse(hasattr(module, 'non_existent')) def test_star_in_all(self): - with util.mock_modules('pkg.__init__') as importer: + with util.mock_spec('pkg.__init__') as importer: with util.import_state(meta_path=[importer]): importer['pkg'].__all__ = ['*'] module = self.__import__('pkg', fromlist=['*']) @@ -147,7 +147,7 @@ class HandlingFromlist: self.assertFalse(hasattr(module, '*')) def test_invalid_type(self): - with util.mock_modules('pkg.__init__') as importer: + with util.mock_spec('pkg.__init__') as importer: with util.import_state(meta_path=[importer]), \ warnings.catch_warnings(): warnings.simplefilter('error', BytesWarning) @@ -157,7 +157,7 @@ class HandlingFromlist: self.__import__('pkg', fromlist=iter([b'attr'])) def test_invalid_type_in_all(self): - with util.mock_modules('pkg.__init__') as importer: + with util.mock_spec('pkg.__init__') as importer: with util.import_state(meta_path=[importer]), \ warnings.catch_warnings(): warnings.simplefilter('error', BytesWarning) diff --git a/Lib/test/test_importlib/import_/test_meta_path.py b/Lib/test/test_importlib/import_/test_meta_path.py index 5a41e89..5730119 100644 --- a/Lib/test/test_importlib/import_/test_meta_path.py +++ b/Lib/test/test_importlib/import_/test_meta_path.py @@ -100,8 +100,20 @@ class CallSignature: self.assertEqual(args[0], mod_name) self.assertIs(args[1], path) +class CallSignoreSuppressImportWarning(CallSignature): -class CallSignaturePEP302(CallSignature): + def test_no_path(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + super().test_no_path() + + def test_with_path(self): + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + super().test_no_path() + + +class CallSignaturePEP302(CallSignoreSuppressImportWarning): mock_modules = util.mock_modules finder_name = 'find_module' diff --git a/Lib/test/test_importlib/test_abc.py b/Lib/test/test_importlib/test_abc.py index 605738f..d8b9fc8 100644 --- a/Lib/test/test_importlib/test_abc.py +++ b/Lib/test/test_importlib/test_abc.py @@ -458,32 +458,36 @@ class LoaderLoadModuleTests: return SpecLoader() def test_fresh(self): - loader = self.loader() - name = 'blah' - with test_util.uncache(name): - loader.load_module(name) - module = loader.found - self.assertIs(sys.modules[name], module) - self.assertEqual(loader, module.__loader__) - self.assertEqual(loader, module.__spec__.loader) - self.assertEqual(name, module.__name__) - self.assertEqual(name, module.__spec__.name) - self.assertIsNotNone(module.__path__) - self.assertIsNotNone(module.__path__, - module.__spec__.submodule_search_locations) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + loader = self.loader() + name = 'blah' + with test_util.uncache(name): + loader.load_module(name) + module = loader.found + self.assertIs(sys.modules[name], module) + self.assertEqual(loader, module.__loader__) + self.assertEqual(loader, module.__spec__.loader) + self.assertEqual(name, module.__name__) + self.assertEqual(name, module.__spec__.name) + self.assertIsNotNone(module.__path__) + self.assertIsNotNone(module.__path__, + module.__spec__.submodule_search_locations) def test_reload(self): - name = 'blah' - loader = self.loader() - module = types.ModuleType(name) - module.__spec__ = self.util.spec_from_loader(name, loader) - module.__loader__ = loader - with test_util.uncache(name): - sys.modules[name] = module - loader.load_module(name) - found = loader.found - self.assertIs(found, sys.modules[name]) - self.assertIs(module, sys.modules[name]) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + name = 'blah' + loader = self.loader() + module = types.ModuleType(name) + module.__spec__ = self.util.spec_from_loader(name, loader) + module.__loader__ = loader + with test_util.uncache(name): + sys.modules[name] = module + loader.load_module(name) + found = loader.found + self.assertIs(found, sys.modules[name]) + self.assertIs(module, sys.modules[name]) (Frozen_LoaderLoadModuleTests, @@ -837,25 +841,29 @@ class SourceOnlyLoaderTests(SourceLoaderTestHarness): # Loading a module should set __name__, __loader__, __package__, # __path__ (for packages), __file__, and __cached__. # The module should also be put into sys.modules. - with test_util.uncache(self.name): - with warnings.catch_warnings(): - warnings.simplefilter('ignore', DeprecationWarning) - module = self.loader.load_module(self.name) - self.verify_module(module) - self.assertEqual(module.__path__, [os.path.dirname(self.path)]) - self.assertIn(self.name, sys.modules) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + with test_util.uncache(self.name): + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = self.loader.load_module(self.name) + self.verify_module(module) + self.assertEqual(module.__path__, [os.path.dirname(self.path)]) + self.assertIn(self.name, sys.modules) def test_package_settings(self): # __package__ needs to be set, while __path__ is set on if the module # is a package. # Testing the values for a package are covered by test_load_module. - self.setUp(is_package=False) - with test_util.uncache(self.name): - with warnings.catch_warnings(): - warnings.simplefilter('ignore', DeprecationWarning) - module = self.loader.load_module(self.name) - self.verify_module(module) - self.assertFalse(hasattr(module, '__path__')) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + self.setUp(is_package=False) + with test_util.uncache(self.name): + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + module = self.loader.load_module(self.name) + self.verify_module(module) + self.assertFalse(hasattr(module, '__path__')) def test_get_source_encoding(self): # Source is considered encoded in UTF-8 by default unless otherwise diff --git a/Lib/test/test_importlib/test_api.py b/Lib/test/test_importlib/test_api.py index fd60634..3f06a10 100644 --- a/Lib/test/test_importlib/test_api.py +++ b/Lib/test/test_importlib/test_api.py @@ -20,7 +20,7 @@ class ImportModuleTests: def test_module_import(self): # Test importing a top-level module. - with test_util.mock_modules('top_level') as mock: + with test_util.mock_spec('top_level') as mock: with test_util.import_state(meta_path=[mock]): module = self.init.import_module('top_level') self.assertEqual(module.__name__, 'top_level') @@ -30,7 +30,7 @@ class ImportModuleTests: pkg_name = 'pkg' pkg_long_name = '{0}.__init__'.format(pkg_name) name = '{0}.mod'.format(pkg_name) - with test_util.mock_modules(pkg_long_name, name) as mock: + with test_util.mock_spec(pkg_long_name, name) as mock: with test_util.import_state(meta_path=[mock]): module = self.init.import_module(name) self.assertEqual(module.__name__, name) @@ -42,7 +42,7 @@ class ImportModuleTests: module_name = 'mod' absolute_name = '{0}.{1}'.format(pkg_name, module_name) relative_name = '.{0}'.format(module_name) - with test_util.mock_modules(pkg_long_name, absolute_name) as mock: + with test_util.mock_spec(pkg_long_name, absolute_name) as mock: with test_util.import_state(meta_path=[mock]): self.init.import_module(pkg_name) module = self.init.import_module(relative_name, pkg_name) @@ -50,7 +50,7 @@ class ImportModuleTests: def test_deep_relative_package_import(self): modules = ['a.__init__', 'a.b.__init__', 'a.c'] - with test_util.mock_modules(*modules) as mock: + with test_util.mock_spec(*modules) as mock: with test_util.import_state(meta_path=[mock]): self.init.import_module('a') self.init.import_module('a.b') @@ -63,7 +63,7 @@ class ImportModuleTests: pkg_name = 'pkg' pkg_long_name = '{0}.__init__'.format(pkg_name) name = '{0}.mod'.format(pkg_name) - with test_util.mock_modules(pkg_long_name, name) as mock: + with test_util.mock_spec(pkg_long_name, name) as mock: with test_util.import_state(meta_path=[mock]): self.init.import_module(pkg_name) module = self.init.import_module(name, pkg_name) @@ -88,7 +88,7 @@ class ImportModuleTests: b_load_count += 1 code = {'a': load_a, 'a.b': load_b} modules = ['a.__init__', 'a.b'] - with test_util.mock_modules(*modules, module_code=code) as mock: + with test_util.mock_spec(*modules, module_code=code) as mock: with test_util.import_state(meta_path=[mock]): self.init.import_module('a.b') self.assertEqual(b_load_count, 1) @@ -212,8 +212,8 @@ class ReloadTests: module = type(sys)('top_level') module.spam = 3 sys.modules['top_level'] = module - mock = test_util.mock_modules('top_level', - module_code={'top_level': code}) + mock = test_util.mock_spec('top_level', + module_code={'top_level': code}) with mock: with test_util.import_state(meta_path=[mock]): module = self.init.import_module('top_level') diff --git a/Lib/test/test_importlib/test_spec.py b/Lib/test/test_importlib/test_spec.py index eed90f2..b57eb6c 100644 --- a/Lib/test/test_importlib/test_spec.py +++ b/Lib/test/test_importlib/test_spec.py @@ -303,32 +303,38 @@ class ModuleSpecMethodsTests: self.assertNotIn(self.spec.name, sys.modules) def test_load_legacy(self): - self.spec.loader = LegacyLoader() - with CleanImport(self.spec.name): - loaded = self.bootstrap._load(self.spec) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + self.spec.loader = LegacyLoader() + with CleanImport(self.spec.name): + loaded = self.bootstrap._load(self.spec) - self.assertEqual(loaded.ham, -1) + self.assertEqual(loaded.ham, -1) def test_load_legacy_attributes(self): - self.spec.loader = LegacyLoader() - with CleanImport(self.spec.name): - loaded = self.bootstrap._load(self.spec) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + self.spec.loader = LegacyLoader() + with CleanImport(self.spec.name): + loaded = self.bootstrap._load(self.spec) - self.assertIs(loaded.__loader__, self.spec.loader) - self.assertEqual(loaded.__package__, self.spec.parent) - self.assertIs(loaded.__spec__, self.spec) + self.assertIs(loaded.__loader__, self.spec.loader) + self.assertEqual(loaded.__package__, self.spec.parent) + self.assertIs(loaded.__spec__, self.spec) def test_load_legacy_attributes_immutable(self): module = object() - class ImmutableLoader(TestLoader): - def load_module(self, name): - sys.modules[name] = module - return module - self.spec.loader = ImmutableLoader() - with CleanImport(self.spec.name): - loaded = self.bootstrap._load(self.spec) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + class ImmutableLoader(TestLoader): + def load_module(self, name): + sys.modules[name] = module + return module + self.spec.loader = ImmutableLoader() + with CleanImport(self.spec.name): + loaded = self.bootstrap._load(self.spec) - self.assertIs(sys.modules[self.spec.name], module) + self.assertIs(sys.modules[self.spec.name], module) # reload() @@ -382,11 +388,13 @@ class ModuleSpecMethodsTests: self.assertFalse(hasattr(loaded, '__cached__')) def test_reload_legacy(self): - self.spec.loader = LegacyLoader() - with CleanImport(self.spec.name): - loaded = self.bootstrap._load(self.spec) - reloaded = self.bootstrap._exec(self.spec, loaded) - installed = sys.modules[self.spec.name] + with warnings.catch_warnings(): + warnings.simplefilter("ignore", ImportWarning) + self.spec.loader = LegacyLoader() + with CleanImport(self.spec.name): + loaded = self.bootstrap._load(self.spec) + reloaded = self.bootstrap._exec(self.spec, loaded) + installed = sys.modules[self.spec.name] self.assertEqual(loaded.ham, -1) self.assertIs(reloaded, loaded) diff --git a/Lib/test/test_venv.py b/Lib/test/test_venv.py index 5bb62cd..098ba17 100644 --- a/Lib/test/test_venv.py +++ b/Lib/test/test_venv.py @@ -446,7 +446,7 @@ class EnsurePipTest(BaseTest): # pip's cross-version compatibility may trigger deprecation # warnings in current versions of Python. Ensure related # environment settings don't cause venv to fail. - envvars["PYTHONWARNINGS"] = "e" + envvars["PYTHONWARNINGS"] = "ignore" # ensurepip is different enough from a normal pip invocation # that we want to ensure it ignores the normal pip environment # variable settings. We set PIP_NO_INSTALL here specifically @@ -485,7 +485,8 @@ class EnsurePipTest(BaseTest): # Ensure pip is available in the virtual environment envpy = os.path.join(os.path.realpath(self.env_dir), self.bindir, self.exe) # Ignore DeprecationWarning since pip code is not part of Python - out, err = check_output([envpy, '-W', 'ignore::DeprecationWarning', '-I', + out, err = check_output([envpy, '-W', 'ignore::DeprecationWarning', + '-W', 'ignore::ImportWarning', '-I', '-m', 'pip', '--version']) # We force everything to text, so unittest gives the detailed diff # if we get unexpected results @@ -501,8 +502,12 @@ class EnsurePipTest(BaseTest): # Check the private uninstall command provided for the Windows # installers works (at least in a virtual environment) with EnvironmentVarGuard() as envvars: + # It seems ensurepip._uninstall calls subprocesses which do not + # inherit the interpreter settings. + envvars["PYTHONWARNINGS"] = "ignore" out, err = check_output([envpy, - '-W', 'ignore::DeprecationWarning', '-I', + '-W', 'ignore::DeprecationWarning', + '-W', 'ignore::ImportWarning', '-I', '-m', 'ensurepip._uninstall']) # We force everything to text, so unittest gives the detailed diff # if we get unexpected results diff --git a/Lib/test/test_zipimport.py b/Lib/test/test_zipimport.py index be0a198..6dea2b1 100644 --- a/Lib/test/test_zipimport.py +++ b/Lib/test/test_zipimport.py @@ -7,6 +7,7 @@ import struct import time import unittest import unittest.mock +import warnings from test import support from test.support import import_helper @@ -453,15 +454,17 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase): self.assertTrue(zi.is_package(TESTPACK)) # PEP 302 - find_mod = zi.find_module('spam') - self.assertIsNotNone(find_mod) - self.assertIsInstance(find_mod, zipimport.zipimporter) - self.assertFalse(find_mod.is_package('spam')) - load_mod = find_mod.load_module('spam') - self.assertEqual(find_mod.get_filename('spam'), load_mod.__file__) - - mod = zi.load_module(TESTPACK) - self.assertEqual(zi.get_filename(TESTPACK), mod.__file__) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + find_mod = zi.find_module('spam') + self.assertIsNotNone(find_mod) + self.assertIsInstance(find_mod, zipimport.zipimporter) + self.assertFalse(find_mod.is_package('spam')) + load_mod = find_mod.load_module('spam') + self.assertEqual(find_mod.get_filename('spam'), load_mod.__file__) + + mod = zi.load_module(TESTPACK) + self.assertEqual(zi.get_filename(TESTPACK), mod.__file__) # PEP 451 spec = zi.find_spec('spam') @@ -522,8 +525,10 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase): self.assertEqual(zi.prefix, packdir) self.assertTrue(zi.is_package(TESTPACK2)) # PEP 302 - mod = zi.load_module(TESTPACK2) - self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + mod = zi.load_module(TESTPACK2) + self.assertEqual(zi.get_filename(TESTPACK2), mod.__file__) # PEP 451 spec = zi.find_spec(TESTPACK2) mod = importlib.util.module_from_spec(spec) @@ -536,13 +541,15 @@ class UncompressedZipImportTestCase(ImportHooksBaseTestCase): pkg_path = TEMP_ZIP + os.sep + packdir + TESTPACK2 zi2 = zipimport.zipimporter(pkg_path) # PEP 302 - find_mod_dotted = zi2.find_module(TESTMOD) - self.assertIsNotNone(find_mod_dotted) - self.assertIsInstance(find_mod_dotted, zipimport.zipimporter) - self.assertFalse(zi2.is_package(TESTMOD)) - load_mod = find_mod_dotted.load_module(TESTMOD) - self.assertEqual( - find_mod_dotted.get_filename(TESTMOD), load_mod.__file__) + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + find_mod_dotted = zi2.find_module(TESTMOD) + self.assertIsNotNone(find_mod_dotted) + self.assertIsInstance(find_mod_dotted, zipimport.zipimporter) + self.assertFalse(zi2.is_package(TESTMOD)) + load_mod = find_mod_dotted.load_module(TESTMOD) + self.assertEqual( + find_mod_dotted.get_filename(TESTMOD), load_mod.__file__) # PEP 451 spec = zi2.find_spec(TESTMOD) @@ -778,10 +785,12 @@ class BadFileZipImportTestCase(unittest.TestCase): z = zipimport.zipimporter(TESTMOD) try: + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + self.assertRaises(TypeError, z.load_module, None) self.assertRaises(TypeError, z.find_module, None) self.assertRaises(TypeError, z.find_spec, None) self.assertRaises(TypeError, z.exec_module, None) - self.assertRaises(TypeError, z.load_module, None) self.assertRaises(TypeError, z.is_package, None) self.assertRaises(TypeError, z.get_code, None) self.assertRaises(TypeError, z.get_data, None) @@ -791,7 +800,9 @@ class BadFileZipImportTestCase(unittest.TestCase): self.assertIsNone(z.find_module('abc')) self.assertIsNone(z.find_spec('abc')) - self.assertRaises(error, z.load_module, 'abc') + with warnings.catch_warnings(): + warnings.simplefilter("ignore", DeprecationWarning) + self.assertRaises(error, z.load_module, 'abc') self.assertRaises(error, z.get_code, 'abc') self.assertRaises(OSError, z.get_data, 'abc') self.assertRaises(error, z.get_source, 'abc') diff --git a/Lib/zipimport.py b/Lib/zipimport.py index 2e5188a..02e4fd3 100644 --- a/Lib/zipimport.py +++ b/Lib/zipimport.py @@ -22,6 +22,7 @@ import _io # for open import marshal # for loads import sys # for modules import time # for mktime +import _warnings # For warn() __all__ = ['ZipImportError', 'zipimporter'] @@ -268,8 +269,11 @@ class zipimporter(_bootstrap_external._LoaderBasics): fully qualified (dotted) module name. It returns the imported module, or raises ZipImportError if it wasn't found. - Deprecated since Python 3.10. use exec_module() instead. + Deprecated since Python 3.10. Use exec_module() instead. """ + msg = ("zipimport.zipimporter.load_module() is deprecated and slated for " + "removal in Python 3.12; use exec_module() instead") + _warnings.warn(msg, DeprecationWarning) code, ispackage, modpath = _get_module_code(self, fullname) mod = sys.modules.get(fullname) if mod is None or not isinstance(mod, _module_type): |