https://github.com/python/cpython/commit/dfeefbe8ead0e370cc701643d9909f84bd29f171
commit: dfeefbe8ead0e370cc701643d9909f84bd29f171
branch: main
author: Brett Cannon <[email protected]>
committer: brettcannon <[email protected]>
date: 2026-01-08T22:08:03Z
summary:
GH-139686: Revert "gh-139686: Make reloading a lazy module no-op (GH-139857)"
(#143584)
This reverts commits 57db12514ac686f0a752ec8fe1c08b6daa0c6219 and
0a97941245f1dda6d838f9aaf0512104e5253929.
files:
A Misc/NEWS.d/next/Library/2026-01-08-13-41-58.gh-issue-139686.S_nzkl.rst
M Doc/library/importlib.rst
M Lib/importlib/__init__.py
M Lib/test/test_importlib/test_lazy.py
M Misc/NEWS.d/3.15.0a3.rst
diff --git a/Doc/library/importlib.rst b/Doc/library/importlib.rst
index e4b5b6831fa0b2..c5ea78c1683761 100644
--- a/Doc/library/importlib.rst
+++ b/Doc/library/importlib.rst
@@ -210,12 +210,6 @@ Functions
:exc:`ModuleNotFoundError` is raised when the module being reloaded
lacks
a :class:`~importlib.machinery.ModuleSpec`.
- .. versionchanged:: 3.15
- If *module* is a lazy module that has not yet been materialized (i.e.,
- loaded via :class:`importlib.util.LazyLoader` and not yet accessed),
- calling :func:`reload` is a no-op and returns the module unchanged.
- This prevents the reload from unintentionally triggering the lazy load.
-
.. warning::
This function is not thread-safe. Calling it from multiple threads can
result
in unexpected behavior. It's recommended to use the
:class:`threading.Lock`
diff --git a/Lib/importlib/__init__.py b/Lib/importlib/__init__.py
index 694fea806f7944..a7d57561ead046 100644
--- a/Lib/importlib/__init__.py
+++ b/Lib/importlib/__init__.py
@@ -97,11 +97,6 @@ def reload(module):
The module must have been successfully imported before.
"""
- # If a LazyModule has not yet been materialized, reload is a no-op.
- if importlib_util := sys.modules.get('importlib.util'):
- if lazy_module_type := getattr(importlib_util, '_LazyModule', None):
- if isinstance(module, lazy_module_type):
- return module
try:
name = module.__spec__.name
except AttributeError:
diff --git a/Lib/test/test_importlib/test_lazy.py
b/Lib/test/test_importlib/test_lazy.py
index c6b26ad75b97f9..e48fad8898f0ef 100644
--- a/Lib/test/test_importlib/test_lazy.py
+++ b/Lib/test/test_importlib/test_lazy.py
@@ -10,9 +10,6 @@
from test.support import threading_helper
from test.test_importlib import util as test_util
-# Make sure sys.modules[util] is in sync with the import.
-# That is needed as other tests may reload util.
-sys.modules['importlib.util'] = util
class CollectInit:
@@ -195,7 +192,7 @@ def test_lazy_self_referential_modules(self):
sys.modules['json'] = module
loader.exec_module(module)
- # Trigger load with attribute lookup, ensure expected behavior.
+ # Trigger load with attribute lookup, ensure expected behavior
test_load = module.loads('{}')
self.assertEqual(test_load, {})
@@ -227,26 +224,6 @@ def __delattr__(self, name):
with self.assertRaises(AttributeError):
del module.CONSTANT
- def test_reload(self):
- # Reloading a lazy module that hasn't been materialized is a no-op.
- module = self.new_module()
- sys.modules[TestingImporter.module_name] = module
-
- # Change the source code to add a new attribute.
- TestingImporter.source_code = 'attr = 42\nnew_attr = 123\n__name__ =
{!r}'.format(TestingImporter.mutated_name)
- self.assertIsInstance(module, util._LazyModule)
-
- # Reload the module (should be a no-op since not materialized).
- reloaded = importlib.reload(module)
- self.assertIs(reloaded, module)
- self.assertIsInstance(module, util._LazyModule)
-
- # Access the new attribute (should trigger materialization, and
new_attr should exist).
- self.assertEqual(module.attr, 42)
- self.assertNotIsInstance(module, util._LazyModule)
- self.assertTrue(hasattr(module, 'new_attr'))
- self.assertEqual(module.new_attr, 123)
-
if __name__ == '__main__':
unittest.main()
diff --git a/Misc/NEWS.d/3.15.0a3.rst b/Misc/NEWS.d/3.15.0a3.rst
index 7d52b3d0c80c55..e493c3570847fd 100644
--- a/Misc/NEWS.d/3.15.0a3.rst
+++ b/Misc/NEWS.d/3.15.0a3.rst
@@ -843,15 +843,6 @@ for :term:`stdlib` modules.
..
-.. date: 2025-10-09-15-46-18
-.. gh-issue: 139686
-.. nonce: XwIZB2
-.. section: Library
-
-Make importlib.reload no-op for lazy modules.
-
-..
-
.. date: 2025-09-09-13-00-42
.. gh-issue: 138697
.. nonce: QVwJw_
diff --git
a/Misc/NEWS.d/next/Library/2026-01-08-13-41-58.gh-issue-139686.S_nzkl.rst
b/Misc/NEWS.d/next/Library/2026-01-08-13-41-58.gh-issue-139686.S_nzkl.rst
new file mode 100644
index 00000000000000..6d21a48613465f
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-01-08-13-41-58.gh-issue-139686.S_nzkl.rst
@@ -0,0 +1,3 @@
+Revert 0a97941245f1dda6d838f9aaf0512104e5253929 and
+57db12514ac686f0a752ec8fe1c08b6daa0c6219 which made importlib.reload a no-op
+for lazy modules; caused Buildbot failures.
_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]