Skip to content

Commit af18572

Browse files
authored
GH-65961: Stop setting __cached__ on modules (GH-142165)
1 parent a26c831 commit af18572

File tree

33 files changed

+93
-236
lines changed

33 files changed

+93
-236
lines changed

Doc/c-api/import.rst

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -129,8 +129,7 @@ Importing Modules
129129
of :class:`~importlib.machinery.SourceFileLoader` otherwise.
130130
131131
The module's :attr:`~module.__file__` attribute will be set to the code
132-
object's :attr:`~codeobject.co_filename`. If applicable,
133-
:attr:`~module.__cached__` will also be set.
132+
object's :attr:`~codeobject.co_filename`.
134133
135134
This function will reload the module if it was already imported. See
136135
:c:func:`PyImport_ReloadModule` for the intended way to reload a module.
@@ -142,10 +141,13 @@ Importing Modules
142141
:c:func:`PyImport_ExecCodeModuleWithPathnames`.
143142
144143
.. versionchanged:: 3.12
145-
The setting of :attr:`~module.__cached__` and :attr:`~module.__loader__`
144+
The setting of ``__cached__`` and :attr:`~module.__loader__`
146145
is deprecated. See :class:`~importlib.machinery.ModuleSpec` for
147146
alternatives.
148147
148+
.. versionchanged:: 3.15
149+
``__cached__`` is no longer set.
150+
149151
150152
.. c:function:: PyObject* PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
151153
@@ -157,16 +159,19 @@ Importing Modules
157159
158160
.. c:function:: PyObject* PyImport_ExecCodeModuleObject(PyObject *name, PyObject *co, PyObject *pathname, PyObject *cpathname)
159161
160-
Like :c:func:`PyImport_ExecCodeModuleEx`, but the :attr:`~module.__cached__`
161-
attribute of the module object is set to *cpathname* if it is
162-
non-``NULL``. Of the three functions, this is the preferred one to use.
162+
Like :c:func:`PyImport_ExecCodeModuleEx`, but the path to any compiled file
163+
via *cpathname* is used appropriately when non-``NULL``. Of the three
164+
functions, this is the preferred one to use.
163165
164166
.. versionadded:: 3.3
165167
166168
.. versionchanged:: 3.12
167-
Setting :attr:`~module.__cached__` is deprecated. See
169+
Setting ``__cached__`` is deprecated. See
168170
:class:`~importlib.machinery.ModuleSpec` for alternatives.
169171
172+
.. versionchanged:: 3.15
173+
``__cached__`` no longer set.
174+
170175
171176
.. c:function:: PyObject* PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, const char *pathname, const char *cpathname)
172177

Doc/deprecations/pending-removal-in-3.15.rst

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ Pending removal in Python 3.15
33

44
* The import system:
55

6-
* Setting :attr:`~module.__cached__` on a module while
6+
* Setting ``__cached__`` on a module while
77
failing to set :attr:`__spec__.cached <importlib.machinery.ModuleSpec.cached>`
8-
is deprecated. In Python 3.15, :attr:`!__cached__` will cease to be set or
8+
is deprecated. In Python 3.15, ``__cached__`` will cease to be set or
99
take into consideration by the import system or standard library. (:gh:`97879`)
1010

1111
* Setting :attr:`~module.__package__` on a module while

Doc/howto/gdb_helpers.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ enabled::
136136
at Objects/unicodeobject.c:551
137137
#7 0x0000000000440d94 in PyUnicodeUCS2_FromString (u=0x5c2b8d "__lltrace__") at Objects/unicodeobject.c:569
138138
#8 0x0000000000584abd in PyDict_GetItemString (v=
139-
{'Yuck': <type at remote 0xad4730>, '__builtins__': <module at remote 0x7ffff7fd5ee8>, '__file__': 'Lib/test/crashers/nasty_eq_vs_dict.py', '__package__': None, 'y': <Yuck(i=0) at remote 0xaacd80>, 'dict': {0: 0, 1: 1, 2: 2, 3: 3}, '__cached__': None, '__name__': '__main__', 'z': <Yuck(i=0) at remote 0xaace60>, '__doc__': None}, key=
139+
{'Yuck': <type at remote 0xad4730>, '__builtins__': <module at remote 0x7ffff7fd5ee8>, '__file__': 'Lib/test/crashers/nasty_eq_vs_dict.py', '__package__': None, 'y': <Yuck(i=0) at remote 0xaacd80>, 'dict': {0: 0, 1: 1, 2: 2, 3: 3}, '__name__': '__main__', 'z': <Yuck(i=0) at remote 0xaace60>, '__doc__': None}, key=
140140
0x5c2b8d "__lltrace__") at Objects/dictobject.c:2171
141141

142142
Notice how the dictionary argument to ``PyDict_GetItemString`` is displayed

Doc/library/functions.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -526,7 +526,7 @@ are always available. They are listed here in alphabetical order.
526526
>>> dir() # show the names in the module namespace # doctest: +SKIP
527527
['__builtins__', '__name__', 'struct']
528528
>>> dir(struct) # show the names in the struct module # doctest: +SKIP
529-
['Struct', '__all__', '__builtins__', '__cached__', '__doc__', '__file__',
529+
['Struct', '__all__', '__builtins__', '__doc__', '__file__',
530530
'__initializing__', '__loader__', '__name__', '__package__',
531531
'_clearcache', 'calcsize', 'error', 'pack', 'pack_into',
532532
'unpack', 'unpack_from']

Doc/library/importlib.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1197,8 +1197,7 @@ find and load modules.
11971197

11981198
.. attribute:: cached
11991199

1200-
The filename of a compiled version of the module's code
1201-
(see :attr:`module.__cached__`).
1200+
The filename of a compiled version of the module's code.
12021201
The :term:`finder` should always set this attribute but it may be ``None``
12031202
for modules that do not need compiled code stored.
12041203

Doc/library/runpy.rst

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@ The :mod:`runpy` module provides two functions:
5050
overridden by :func:`run_module`.
5151

5252
The special global variables ``__name__``, ``__spec__``, ``__file__``,
53-
``__cached__``, ``__loader__`` and ``__package__`` are set in the globals
54-
dictionary before the module code is executed. (Note that this is a
55-
minimal set of variables - other variables may be set implicitly as an
56-
interpreter implementation detail.)
53+
``__loader__`` and ``__package__`` are set in the globals dictionary before
54+
the module code is executed. (Note that this is a minimal set of variables -
55+
other variables may be set implicitly as an interpreter implementation
56+
detail.)
5757

5858
``__name__`` is set to *run_name* if this optional argument is not
5959
:const:`None`, to ``mod_name + '.__main__'`` if the named module is a
@@ -63,7 +63,7 @@ The :mod:`runpy` module provides two functions:
6363
module (that is, ``__spec__.name`` will always be *mod_name* or
6464
``mod_name + '.__main__'``, never *run_name*).
6565

66-
``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` are
66+
``__file__``, ``__loader__`` and ``__package__`` are
6767
:ref:`set as normal <import-mod-attrs>` based on the module spec.
6868

6969
If the argument *alter_sys* is supplied and evaluates to :const:`True`,
@@ -98,6 +98,9 @@ The :mod:`runpy` module provides two functions:
9898
``__package__`` are deprecated. See
9999
:class:`~importlib.machinery.ModuleSpec` for alternatives.
100100

101+
.. versionchanged:: 3.15
102+
``__cached__`` is no longer set.
103+
101104
.. function:: run_path(path_name, init_globals=None, run_name=None)
102105

103106
.. index::
@@ -125,23 +128,23 @@ The :mod:`runpy` module provides two functions:
125128
overridden by :func:`run_path`.
126129

127130
The special global variables ``__name__``, ``__spec__``, ``__file__``,
128-
``__cached__``, ``__loader__`` and ``__package__`` are set in the globals
129-
dictionary before the module code is executed. (Note that this is a
130-
minimal set of variables - other variables may be set implicitly as an
131-
interpreter implementation detail.)
131+
``__loader__`` and ``__package__`` are set in the globals dictionary before
132+
the module code is executed. (Note that this is a minimal set of variables -
133+
other variables may be set implicitly as an interpreter implementation
134+
detail.)
132135

133136
``__name__`` is set to *run_name* if this optional argument is not
134137
:const:`None` and to ``'<run_path>'`` otherwise.
135138

136139
If *file_path* directly references a script file (whether as source
137140
or as precompiled byte code), then ``__file__`` will be set to
138-
*file_path*, and ``__spec__``, ``__cached__``, ``__loader__`` and
141+
*file_path*, and ``__spec__``, ``__loader__`` and
139142
``__package__`` will all be set to :const:`None`.
140143

141144
If *file_path* is a reference to a valid :data:`sys.path` entry, then
142145
``__spec__`` will be set appropriately for the imported :mod:`__main__`
143146
module (that is, ``__spec__.name`` will always be ``__main__``).
144-
``__file__``, ``__cached__``, ``__loader__`` and ``__package__`` will be
147+
``__file__``, ``__loader__`` and ``__package__`` will be
145148
:ref:`set as normal <import-mod-attrs>` based on the module spec.
146149

147150
A number of alterations are also made to the :mod:`sys` module. Firstly,
@@ -173,6 +176,9 @@ The :mod:`runpy` module provides two functions:
173176
The setting of ``__cached__``, ``__loader__``, and
174177
``__package__`` are deprecated.
175178

179+
.. versionchanged:: 3.15
180+
``__cached__`` is no longer set.
181+
176182
.. seealso::
177183

178184
:pep:`338` -- Executing modules as scripts

Doc/reference/datamodel.rst

Lines changed: 11 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,6 @@ Attribute assignment updates the module's namespace dictionary, e.g.,
895895
single: __loader__ (module attribute)
896896
single: __path__ (module attribute)
897897
single: __file__ (module attribute)
898-
single: __cached__ (module attribute)
899898
single: __doc__ (module attribute)
900899
single: __annotations__ (module attribute)
901900
single: __annotate__ (module attribute)
@@ -1044,43 +1043,28 @@ this approach.
10441043
instead of :attr:`!module.__path__`.
10451044

10461045
.. attribute:: module.__file__
1047-
.. attribute:: module.__cached__
10481046

1049-
:attr:`!__file__` and :attr:`!__cached__` are both optional attributes that
1047+
:attr:`!__file__` is an optional attribute that
10501048
may or may not be set. Both attributes should be a :class:`str` when they
10511049
are available.
10521050

1053-
:attr:`!__file__` indicates the pathname of the file from which the module
1054-
was loaded (if loaded from a file), or the pathname of the shared library
1055-
file for extension modules loaded dynamically from a shared library.
1056-
It might be missing for certain types of modules, such as C modules that are
1057-
statically linked into the interpreter, and the
1051+
An optional attribute, :attr:`!__file__` indicates the pathname of the file
1052+
from which the module was loaded (if loaded from a file), or the pathname of
1053+
the shared library file for extension modules loaded dynamically from a
1054+
shared library. It might be missing for certain types of modules, such as C
1055+
modules that are statically linked into the interpreter, and the
10581056
:ref:`import system <importsystem>` may opt to leave it unset if it
10591057
has no semantic meaning (for example, a module loaded from a database).
10601058

1061-
If :attr:`!__file__` is set then the :attr:`!__cached__` attribute might
1062-
also be set, which is the path to any compiled version of
1063-
the code (for example, a byte-compiled file). The file does not need to exist
1064-
to set this attribute; the path can simply point to where the
1065-
compiled file *would* exist (see :pep:`3147`).
1066-
1067-
Note that :attr:`!__cached__` may be set even if :attr:`!__file__` is not
1068-
set. However, that scenario is quite atypical. Ultimately, the
1069-
:term:`loader` is what makes use of the module spec provided by the
1070-
:term:`finder` (from which :attr:`!__file__` and :attr:`!__cached__` are
1071-
derived). So if a loader can load from a cached module but otherwise does
1072-
not load from a file, that atypical scenario may be appropriate.
1073-
1074-
It is **strongly** recommended that you use
1075-
:attr:`module.__spec__.cached <importlib.machinery.ModuleSpec.cached>`
1076-
instead of :attr:`!module.__cached__`.
1077-
10781059
.. deprecated-removed:: 3.13 3.15
1079-
Setting :attr:`!__cached__` on a module while failing to set
1060+
Setting ``__cached__`` on a module while failing to set
10801061
:attr:`!__spec__.cached` is deprecated. In Python 3.15,
1081-
:attr:`!__cached__` will cease to be set or taken into consideration by
1062+
``__cached__`` will cease to be set or taken into consideration by
10821063
the import system or standard library.
10831064

1065+
.. versionchanged:: 3.15
1066+
``__cached__`` is no longer set.
1067+
10841068
Other writable attributes on module objects
10851069
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
10861070

Doc/whatsnew/3.12.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1337,7 +1337,7 @@ Deprecated
13371337
it was :exc:`ImportWarning`).
13381338
(Contributed by Brett Cannon in :gh:`65961`.)
13391339

1340-
* Setting :attr:`~module.__package__` or :attr:`~module.__cached__` on a
1340+
* Setting :attr:`~module.__package__` or ``__cached__`` on a
13411341
module is deprecated, and will cease to be set or taken into consideration by
13421342
the import system in Python 3.14. (Contributed by Brett Cannon in :gh:`65961`.)
13431343

Doc/whatsnew/3.2.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ cluttering source directories, the *pyc* files are now collected in a
312312
Aside from the filenames and target directories, the new scheme has a few
313313
aspects that are visible to the programmer:
314314

315-
* Imported modules now have a :attr:`~module.__cached__` attribute which stores
315+
* Imported modules now have a ``__cached__`` attribute which stores
316316
the name of the actual file that was imported:
317317

318318
>>> import collections

Lib/importlib/_bootstrap.py

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -565,8 +565,7 @@ class ModuleSpec:
565565
`has_location` indicates that a spec's "origin" reflects a location.
566566
When this is True, `__file__` attribute of the module is set.
567567
568-
`cached` is the location of the cached bytecode file, if any. It
569-
corresponds to the `__cached__` attribute.
568+
`cached` is the location of the cached bytecode file, if any.
570569
571570
`submodule_search_locations` is the sequence of path entries to
572571
search when importing submodules. If set, is_package should be
@@ -699,18 +698,14 @@ def _spec_from_module(module, loader=None, origin=None):
699698
origin = getattr(loader, '_ORIGIN', None)
700699
if not origin and location is not None:
701700
origin = location
702-
try:
703-
cached = module.__cached__
704-
except AttributeError:
705-
cached = None
706701
try:
707702
submodule_search_locations = list(module.__path__)
708703
except AttributeError:
709704
submodule_search_locations = None
710705

711706
spec = ModuleSpec(name, loader, origin=origin)
712707
spec._set_fileattr = False if location is None else (origin == location)
713-
spec.cached = cached
708+
spec.cached = None
714709
spec.submodule_search_locations = submodule_search_locations
715710
return spec
716711

@@ -770,20 +765,14 @@ def _init_module_attrs(spec, module, *, override=False):
770765
module.__path__ = spec.submodule_search_locations
771766
except AttributeError:
772767
pass
773-
# __file__/__cached__
768+
# __file__
774769
if spec.has_location:
775770
if override or getattr(module, '__file__', None) is None:
776771
try:
777772
module.__file__ = spec.origin
778773
except AttributeError:
779774
pass
780775

781-
if override or getattr(module, '__cached__', None) is None:
782-
if spec.cached is not None:
783-
try:
784-
module.__cached__ = spec.cached
785-
except AttributeError:
786-
pass
787776
return module
788777

789778

0 commit comments

Comments
 (0)