summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_importlib
diff options
context:
space:
mode:
authorBrett Cannon <brett@python.org>2020-12-04 23:39:21 (GMT)
committerGitHub <noreply@github.com>2020-12-04 23:39:21 (GMT)
commit2de5097ba4c50eba90df55696a7b2e74c93834d4 (patch)
tree7a3d54477997114ef27136e9c9da0e380f835a34 /Lib/test/test_importlib
parent79c1849b9e5b635bd36b13e1be9dc7cbc2bd6312 (diff)
downloadcpython-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/test/test_importlib')
-rw-r--r--Lib/test/test_importlib/builtin/test_loader.py5
-rw-r--r--Lib/test/test_importlib/extension/test_loader.py44
-rw-r--r--Lib/test/test_importlib/frozen/test_loader.py20
-rw-r--r--Lib/test/test_importlib/import_/test___loader__.py39
-rw-r--r--Lib/test/test_importlib/import_/test___package__.py25
-rw-r--r--Lib/test/test_importlib/import_/test_api.py31
-rw-r--r--Lib/test/test_importlib/import_/test_caching.py39
-rw-r--r--Lib/test/test_importlib/import_/test_fromlist.py26
-rw-r--r--Lib/test/test_importlib/import_/test_meta_path.py14
-rw-r--r--Lib/test/test_importlib/test_abc.py84
-rw-r--r--Lib/test/test_importlib/test_api.py16
-rw-r--r--Lib/test/test_importlib/test_spec.py54
12 files changed, 254 insertions, 143 deletions
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)