Skip to content

Commit e0c3bd7

Browse files
committed
Issue python#18864: Don't try and use unittest as a testing module for
built-in loading; leads to a reload scenario where attributes get set which are wrong after the test.
1 parent a24348c commit e0c3bd7

File tree

1 file changed

+18
-6
lines changed

1 file changed

+18
-6
lines changed

Lib/test/test_importlib/builtin/test_loader.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,18 @@ def test_unloadable(self):
6363
def test_already_imported(self):
6464
# Using the name of a module already imported but not a built-in should
6565
# still fail.
66-
assert hasattr(unittest, '__file__') # Not a built-in.
66+
module_name = 'builtin_reload_test'
67+
assert module_name not in sys.builtin_module_names
68+
with util.uncache(module_name):
69+
module = types.ModuleType(module_name)
70+
spec = self.machinery.ModuleSpec(module_name,
71+
self.machinery.BuiltinImporter,
72+
origin='built-in')
73+
module.__spec__ = spec
74+
sys.modules[module_name] = module
6775
with self.assertRaises(ImportError) as cm:
68-
self.load_spec('unittest')
69-
self.assertEqual(cm.exception.name, 'unittest')
76+
self.machinery.BuiltinImporter.exec_module(module)
77+
self.assertEqual(cm.exception.name, module_name)
7078

7179

7280
Frozen_ExecModTests, Source_ExecModTests = util.test_both(ExecModTests,
@@ -120,10 +128,14 @@ def test_unloadable(self):
120128
def test_already_imported(self):
121129
# Using the name of a module already imported but not a built-in should
122130
# still fail.
123-
assert hasattr(unittest, '__file__') # Not a built-in.
131+
module_name = 'builtin_reload_test'
132+
assert module_name not in sys.builtin_module_names
133+
with util.uncache(module_name):
134+
module = types.ModuleType(module_name)
135+
sys.modules[module_name] = module
124136
with self.assertRaises(ImportError) as cm:
125-
self.load_module('unittest')
126-
self.assertEqual(cm.exception.name, 'unittest')
137+
self.load_module(module_name)
138+
self.assertEqual(cm.exception.name, module_name)
127139

128140

129141
Frozen_LoaderTests, Source_LoaderTests = util.test_both(LoaderTests,

0 commit comments

Comments
 (0)