Skip to content

Commit 46b17f7

Browse files
committed
rephrase
1 parent 07352a3 commit 46b17f7

3 files changed

Lines changed: 4 additions & 32 deletions

File tree

‎Doc/library/importlib.rst‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1373,15 +1373,13 @@ an :term:`importer`.
13731373

13741374
.. versionchanged:: 3.16
13751375
Reading a lazily-loaded module's :attr:`~module.__name__` and
1376-
:attr:`~module.__file__` attributes no longer triggers the load. Until
1376+
:attr:`~module.__file__` attributes no longer triggers the load. Until
13771377
the module is loaded they act as aliases for the
13781378
:attr:`~importlib.machinery.ModuleSpec.name` and
13791379
:attr:`~importlib.machinery.ModuleSpec.origin` of its
13801380
:attr:`~module.__spec__`, and assigning to them updates the spec
1381-
instead. This keeps introspection tools such as
1382-
:func:`inspect.getmodule` -- used indirectly by, for example,
1383-
:func:`inspect.getframeinfo` -- from forcing every lazily-loaded module
1384-
in :data:`sys.modules` to be imported (:gh:`139669`).
1381+
instead. This avoids the unintentional loading caused by
1382+
introspection tools like :func:`inspect.getframeinfo` (:gh:`139669`).
13851383

13861384
.. classmethod:: factory(loader)
13871385

‎Lib/importlib/util.py‎

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,8 @@ def __setattr__(self, attr, value):
242242

243243
def __delattr__(self, attr):
244244
"""Trigger the load and then perform the deletion."""
245-
# Reading __name__/__file__ aliases the spec and no longer triggers
246-
# the load, so access __spec__ to force it. This also resets
247-
# __class__ to the real module type, avoiding infinite recursion in
248-
# the delattr() below.
249245
self.__getattribute__('__spec__')
246+
# Goes into ModuleType.__delattr__
250247
delattr(self, attr)
251248

252249

‎Lib/test/test_importlib/test_lazy.py‎

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -228,29 +228,6 @@ def __delattr__(self, name):
228228
with self.assertRaises(AttributeError):
229229
del module.CONSTANT
230230

231-
def test_name_file_alias_spec(self):
232-
# gh-139669: __name__/__file__ are aliases for __spec__.name and
233-
# __spec__.origin. Reading them returns the spec values and does not
234-
# trigger the load; writing them flows through to the spec.
235-
loader = TestingImporter()
236-
module = self.new_module(loader=loader)
237-
spec = object.__getattribute__(module, '__spec__')
238-
spec.origin = '/some/where.py'
239-
self.assertEqual(module.__name__, spec.name)
240-
self.assertEqual(module.__file__, '/some/where.py')
241-
# Neither read triggered the load.
242-
self.assertIsNone(loader.loaded)
243-
self.assertEqual(0, loader.load_count)
244-
# Writes flow through to the spec, still without loading.
245-
module.__name__ = 'renamed'
246-
module.__file__ = '/other.py'
247-
self.assertEqual(spec.name, 'renamed')
248-
self.assertEqual(spec.origin, '/other.py')
249-
self.assertEqual(module.__name__, 'renamed')
250-
self.assertEqual(module.__file__, '/other.py')
251-
self.assertIsNone(loader.loaded)
252-
self.assertIsInstance(module, util._LazyModule)
253-
254231
def test_inspect_does_not_trigger_lazy_load(self):
255232
# gh-139669: introspecting an unrelated frame iterates over
256233
# sys.modules and must not force lazy modules to be loaded.

0 commit comments

Comments
 (0)