From f5d81ba2be7711ca8db738e7cf14e85b8be7d575 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 17 Jun 2024 00:23:58 +0200 Subject: [PATCH 1/4] Make reversed thread-safe --- Objects/enumobject.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/Objects/enumobject.c b/Objects/enumobject.c index 556666779d8522..668166c3a2dd20 100644 --- a/Objects/enumobject.c +++ b/Objects/enumobject.c @@ -2,6 +2,7 @@ #include "Python.h" #include "pycore_call.h" // _PyObject_CallNoArgs() +#include "pycore_critical_section.h" // Py_BEGIN_CRITICAL_SECTION() #include "pycore_long.h" // _PyLong_GetOne() #include "pycore_modsupport.h" // _PyArg_NoKwnames() #include "pycore_object.h" // _PyObject_GC_TRACK() @@ -431,16 +432,20 @@ reversed_next(reversedobject *ro) PyObject *item; Py_ssize_t index = ro->index; + Py_BEGIN_CRITICAL_SECTION(ro); if (index >= 0) { item = PySequence_GetItem(ro->seq, index); if (item != NULL) { ro->index--; + Py_EXIT_CRITICAL_SECTION(); return item; } if (PyErr_ExceptionMatches(PyExc_IndexError) || PyErr_ExceptionMatches(PyExc_StopIteration)) PyErr_Clear(); } + Py_END_CRITICAL_SECTION(); + ro->index = -1; Py_CLEAR(ro->seq); return NULL; From 40018a179253487c2f1cc426051c84beb21e817f Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 17 Jun 2024 00:24:23 +0200 Subject: [PATCH 2/4] Add Py_EXIT_CRITICAL_SECTION --- Include/internal/pycore_critical_section.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/Include/internal/pycore_critical_section.h b/Include/internal/pycore_critical_section.h index 7bebcdb67c7169..b3d15cc1b856b9 100644 --- a/Include/internal/pycore_critical_section.h +++ b/Include/internal/pycore_critical_section.h @@ -98,6 +98,8 @@ extern "C" { # define Py_END_CRITICAL_SECTION() \ _PyCriticalSection_End(&_cs); \ } +# define Py_EXIT_CRITICAL_SECTION() \ + _PyCriticalSection_End(&_cs); # define Py_BEGIN_CRITICAL_SECTION2(a, b) \ { \ @@ -156,6 +158,7 @@ extern "C" { # define Py_BEGIN_CRITICAL_SECTION(op) # define Py_END_CRITICAL_SECTION() # define Py_BEGIN_CRITICAL_SECTION2(a, b) +# define Py_EXIT_CRITICAL_SECTION() # define Py_END_CRITICAL_SECTION2() # define Py_BEGIN_CRITICAL_SECTION_SEQUENCE_FAST(original) # define Py_END_CRITICAL_SECTION_SEQUENCE_FAST() From 726f6caadef1d1fe911f5b128241e1f0715f6ad5 Mon Sep 17 00:00:00 2001 From: "blurb-it[bot]" <43283697+blurb-it[bot]@users.noreply.github.com> Date: Sun, 16 Jun 2024 22:42:50 +0000 Subject: [PATCH 3/4] =?UTF-8?q?=F0=9F=93=9C=F0=9F=A4=96=20Added=20by=20blu?= =?UTF-8?q?rb=5Fit.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst | 1 + 1 file changed, 1 insertion(+) create mode 100644 Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst new file mode 100644 index 00000000000000..2f8b6e31c07c83 --- /dev/null +++ b/Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst @@ -0,0 +1 @@ +Make reversed thread-safe. From 8c82d5ef83a1d72b5fbecf0f4c9e89fe776922c0 Mon Sep 17 00:00:00 2001 From: Pieter Eendebak Date: Mon, 17 Jun 2024 12:01:51 +0200 Subject: [PATCH 4/4] Update Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst Co-authored-by: Nikita Sobolev --- .../2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst b/Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst index 2f8b6e31c07c83..76e018eeefbcfa 100644 --- a/Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst +++ b/Misc/NEWS.d/next/Core and Builtins/2024-06-16-22-42-47.gh-issue-120608._-YtWX.rst @@ -1 +1 @@ -Make reversed thread-safe. +Make :func:`reversed` thread-safe.