Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Doc/about.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and now maintained as an independent project.
.. _reStructuredText: https://docutils.sourceforge.io/rst.html
.. _Sphinx: https://www.sphinx-doc.org/

.. In the online version of these documents, you can submit comments and suggest
.. In the online version of this documentation, you can submit comments and suggest
changes directly on the documentation pages.

Development of the documentation and its toolchain is an entirely volunteer
Expand Down
4 changes: 2 additions & 2 deletions Doc/c-api/import.rst
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ Importing Modules
alternatives.

.. versionchanged:: 3.15
``__cached__`` is no longer set.
The ``__cached__`` attribute is no longer set.


.. c:function:: PyObject* PyImport_ExecCodeModuleEx(const char *name, PyObject *co, const char *pathname)
Expand All @@ -170,7 +170,7 @@ Importing Modules
:class:`~importlib.machinery.ModuleSpec` for alternatives.

.. versionchanged:: 3.15
``__cached__`` no longer set.
The ``__cached__`` attribute no longer set.


.. c:function:: PyObject* PyImport_ExecCodeModuleWithPathnames(const char *name, PyObject *co, const char *pathname, const char *cpathname)
Expand Down
4 changes: 2 additions & 2 deletions Doc/library/runpy.rst
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ The :mod:`!runpy` module provides two functions:
:class:`~importlib.machinery.ModuleSpec` for alternatives.

.. versionchanged:: 3.15
``__cached__`` is no longer set.
The global variable ``__cached__`` is no longer set.

.. function:: run_path(path_name, init_globals=None, run_name=None)

Expand Down Expand Up @@ -175,7 +175,7 @@ The :mod:`!runpy` module provides two functions:
``__package__`` are deprecated.

.. versionchanged:: 3.15
``__cached__`` is no longer set.
The global variable ``__cached__`` is no longer set.

.. seealso::

Expand Down
11 changes: 3 additions & 8 deletions Doc/reference/datamodel.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1099,14 +1099,9 @@ this approach.
:ref:`import system <importsystem>` may opt to leave it unset if it
has no semantic meaning (for example, a module loaded from a database).

.. deprecated-removed:: 3.13 3.15
Setting ``__cached__`` on a module while failing to set
:attr:`!__spec__.cached` is deprecated. In Python 3.15,
``__cached__`` will cease to be set or taken into consideration by
the import system or standard library.

.. versionchanged:: 3.15
``__cached__`` is no longer set.
.. versionchanged:: 3.15
The ``__cached__`` attribute is no longer set on modules or taken into
consideration by the import system or standard library.

Other writable attributes on module objects
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
Expand Down
2 changes: 1 addition & 1 deletion Doc/tools/templates/indexsidebar.html
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<h3>{% trans %}Download{% endtrans %}</h3>
<p><a href="{{ pathto('download') }}">{% trans %}Download these documents{% endtrans %}</a></p>
<p><a href="{{ pathto('download') }}">{% trans %}Download the documentation{% endtrans %}</a></p>
<h3>{% trans %}Docs by version{% endtrans %}</h3>
<ul>
{# _docs_by_version.html is overwritten by build_docs.py for non-EOL versions #}
Expand Down
8 changes: 8 additions & 0 deletions Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,14 @@ Other language changes
other, as regular dynamic extensions do.
(Contributed by Stefano Rivera in :gh:`122931`.)

* The ``__cached__`` attribute on modules, which was deprecated since version
3.13, is no longer set or taken into consideration by the import system or
standard library.
Use :attr:`__spec__.cached <importlib.machinery.ModuleSpec.cached>` instead.
(Contributed by Brett Cannon in :gh:`97879`)

Note that the :attr:`~module.__loader__` and :attr:`~module.__package__`
attributes are also deprecated and scheduled for removal.


Default interactive shell
Expand Down
21 changes: 21 additions & 0 deletions Lib/test/test_free_threading/test_gc.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import threading
from threading import Thread
import time
from unittest import TestCase
import gc

Expand Down Expand Up @@ -94,6 +95,26 @@ def evil():
thread.start()
thread.join()

def test_gc_callbacks_race_with_mutation(self):
def collect():
b.wait()
while not stop.is_set():
gc.collect()

def mutate():
b.wait()
while not stop.is_set():
gc.callbacks[:] = [lambda *_: _ for _ in range(16)]
time.sleep(0)
gc.callbacks.clear()

threads = [threading.Thread(target=f) for f in (collect, mutate) * 4]
b = threading.Barrier(len(threads) + 1)
stop = threading.Event()

with threading_helper.start_threads(threads, stop.set):
b.wait()
time.sleep(0.2)
def test_set_threshold(self):
# GH-148613: Setting the GC threshold from another thread could cause a
# race between the `gc_should_collect` and `gc_set_threshold` functions.
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix race conditions in ``invoke_gc_callback`` iterating ``gc.callbacks``
in free-threading mode.
38 changes: 21 additions & 17 deletions Python/gc_free_threading.c
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#include "pycore_initconfig.h" // _PyStatus_NO_MEMORY()
#include "pycore_interp.h" // PyInterpreterState.gc
#include "pycore_interpframe.h" // _PyFrame_GetLocalsArray()
#include "pycore_list.h" // _PyList_GetItemRef()
#include "pycore_object_alloc.h" // _PyObject_MallocWithType()
#include "pycore_pystate.h" // _PyThreadState_GET()
#include "pycore_tstate.h" // _PyThreadStateImpl
Expand Down Expand Up @@ -1940,33 +1941,36 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,

/* The local variable cannot be rebound, check it for sanity */
assert(PyList_CheckExact(gcstate->callbacks));
PyObject *info = NULL;
if (PyList_GET_SIZE(gcstate->callbacks) != 0) {
info = Py_BuildValue("{sisnsnsnsd}",
"generation", generation,
"collected", collected,
"uncollectable", uncollectable,
"candidates", candidates,
"duration", duration);
if (info == NULL) {
PyErr_FormatUnraisable("Exception ignored while "
"invoking gc callbacks");
return;
}
if (PyList_GET_SIZE(gcstate->callbacks) == 0) {
return;
}

PyObject *info = Py_BuildValue("{sisnsnsnsd}",
"generation", generation,
"collected", collected,
"uncollectable", uncollectable,
"candidates", candidates,
"duration", duration);
if (info == NULL) {
PyErr_FormatUnraisable("Exception ignored while "
"invoking gc callbacks");
return;
}

PyObject *phase_obj = PyUnicode_FromString(phase);
if (phase_obj == NULL) {
Py_XDECREF(info);
Py_DECREF(info);
PyErr_FormatUnraisable("Exception ignored while "
"invoking gc callbacks");
return;
}

PyObject *stack[] = {phase_obj, info};
for (Py_ssize_t i=0; i<PyList_GET_SIZE(gcstate->callbacks); i++) {
PyObject *r, *cb = PyList_GET_ITEM(gcstate->callbacks, i);
Py_INCREF(cb); /* make sure cb doesn't go away */
PyObject *r, *cb = _PyList_GetItemRef((PyListObject *)gcstate->callbacks, i);
if (cb == NULL) {
break;
}
r = PyObject_Vectorcall(cb, stack, 2, NULL);
if (r == NULL) {
PyErr_FormatUnraisable("Exception ignored while "
Expand All @@ -1978,7 +1982,7 @@ invoke_gc_callback(PyThreadState *tstate, const char *phase,
Py_DECREF(cb);
}
Py_DECREF(phase_obj);
Py_XDECREF(info);
Py_DECREF(info);
assert(!_PyErr_Occurred(tstate));
}

Expand Down
Loading