]> git.ipfire.org Git - thirdparty/Python/cpython.git/commitdiff
[3.15] gh-153908: Fix data race in `itertools.count.__repr__` (GH-153917) (GH-153951)
authorMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>
Sat, 18 Jul 2026 15:18:04 +0000 (17:18 +0200)
committerGitHub <noreply@github.com>
Sat, 18 Jul 2026 15:18:04 +0000 (15:18 +0000)
(cherry picked from commit 5200f1192428becab0c31db98e7baa97f3c3c3e2)

Co-authored-by: John <me@joh.ng>
Misc/NEWS.d/next/Library/2026-07-18-11-16-41.gh-issue-153908.82FiGk.rst [new file with mode: 0644]
Modules/itertoolsmodule.c

diff --git a/Misc/NEWS.d/next/Library/2026-07-18-11-16-41.gh-issue-153908.82FiGk.rst b/Misc/NEWS.d/next/Library/2026-07-18-11-16-41.gh-issue-153908.82FiGk.rst
new file mode 100644 (file)
index 0000000..6ee943a
--- /dev/null
@@ -0,0 +1 @@
+Fix data race when calling :func:`repr` on :class:`itertools.count` under the :term:`free-threaded build`.
index 72bfab1abaf9caed3b0a5de271b554978934b1be..c0023c839ca7fe3c3e2a36da0e36d9b8d08821bc 100644 (file)
@@ -3675,9 +3675,11 @@ static PyObject *
 count_repr(PyObject *op)
 {
     countobject *lz = countobject_CAST(op);
-    if (lz->long_cnt == NULL)
+    if (lz->long_cnt == NULL) {
+        Py_ssize_t cnt = FT_ATOMIC_LOAD_SSIZE_RELAXED(lz->cnt);
         return PyUnicode_FromFormat("%s(%zd)",
-                                    _PyType_Name(Py_TYPE(lz)), lz->cnt);
+                                    _PyType_Name(Py_TYPE(lz)), cnt);
+    }
 
     if (PyLong_Check(lz->long_step)) {
         long step = PyLong_AsLong(lz->long_step);