Skip to content

Commit f8b771b

Browse files
committed
Revert "gh-139686: Make reloading a lazy module no-op (GH-139857)"
This reverts commit 57db125.
1 parent e2f15ae commit f8b771b

File tree

3 files changed

+1
-35
lines changed

3 files changed

+1
-35
lines changed

Doc/library/importlib.rst

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -210,12 +210,6 @@ Functions
210210
:exc:`ModuleNotFoundError` is raised when the module being reloaded lacks
211211
a :class:`~importlib.machinery.ModuleSpec`.
212212

213-
.. versionchanged:: 3.15
214-
If *module* is a lazy module that has not yet been materialized (i.e.,
215-
loaded via :class:`importlib.util.LazyLoader` and not yet accessed),
216-
calling :func:`reload` is a no-op and returns the module unchanged.
217-
This prevents the reload from unintentionally triggering the lazy load.
218-
219213
.. warning::
220214
This function is not thread-safe. Calling it from multiple threads can result
221215
in unexpected behavior. It's recommended to use the :class:`threading.Lock`

Lib/importlib/__init__.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -97,11 +97,6 @@ def reload(module):
9797
The module must have been successfully imported before.
9898
9999
"""
100-
# If a LazyModule has not yet been materialized, reload is a no-op.
101-
if importlib_util := sys.modules.get('importlib.util'):
102-
if lazy_module_type := getattr(importlib_util, '_LazyModule', None):
103-
if isinstance(module, lazy_module_type):
104-
return module
105100
try:
106101
name = module.__spec__.name
107102
except AttributeError:

Lib/test/test_importlib/test_lazy.py

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@
1010
from test.support import threading_helper
1111
from test.test_importlib import util as test_util
1212

13-
# Make sure sys.modules[util] is in sync with the import.
14-
# That is needed as other tests may reload util.
15-
sys.modules['importlib.util'] = util
1613

1714
class CollectInit:
1815

@@ -195,7 +192,7 @@ def test_lazy_self_referential_modules(self):
195192
sys.modules['json'] = module
196193
loader.exec_module(module)
197194

198-
# Trigger load with attribute lookup, ensure expected behavior.
195+
# Trigger load with attribute lookup, ensure expected behavior
199196
test_load = module.loads('{}')
200197
self.assertEqual(test_load, {})
201198

@@ -227,26 +224,6 @@ def __delattr__(self, name):
227224
with self.assertRaises(AttributeError):
228225
del module.CONSTANT
229226

230-
def test_reload(self):
231-
# Reloading a lazy module that hasn't been materialized is a no-op.
232-
module = self.new_module()
233-
sys.modules[TestingImporter.module_name] = module
234-
235-
# Change the source code to add a new attribute.
236-
TestingImporter.source_code = 'attr = 42\nnew_attr = 123\n__name__ = {!r}'.format(TestingImporter.mutated_name)
237-
self.assertIsInstance(module, util._LazyModule)
238-
239-
# Reload the module (should be a no-op since not materialized).
240-
reloaded = importlib.reload(module)
241-
self.assertIs(reloaded, module)
242-
self.assertIsInstance(module, util._LazyModule)
243-
244-
# Access the new attribute (should trigger materialization, and new_attr should exist).
245-
self.assertEqual(module.attr, 42)
246-
self.assertNotIsInstance(module, util._LazyModule)
247-
self.assertTrue(hasattr(module, 'new_attr'))
248-
self.assertEqual(module.new_attr, 123)
249-
250227

251228
if __name__ == '__main__':
252229
unittest.main()

0 commit comments

Comments
 (0)