From e7e439bd1dbc9fa5fea983358f7581c588045b73 Mon Sep 17 00:00:00 2001 From: Ilya Egorov <0x42005e1f@gmail.com> Date: Fri, 2 Oct 2026 15:53:41 +0400 Subject: [PATCH 1/3] gh-158592: Reallocate the list's array when deleting by index --- Objects/listobject.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Objects/listobject.c b/Objects/listobject.c index 81eb3e1102159b5..3f0c51904dec2dd 100644 --- a/Objects/listobject.c +++ b/Objects/listobject.c @@ -1148,7 +1148,7 @@ list_ass_item_lock_held(PyListObject *a, Py_ssize_t i, PyObject *v) for (Py_ssize_t idx = i; idx < size - 1; idx++) { FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[idx], a->ob_item[idx + 1]); } - Py_SET_SIZE(a, size - 1); + list_resize(a, size - 1); // NB: shrinking a list can't fail } else { FT_ATOMIC_STORE_PTR_RELEASE(a->ob_item[i], Py_NewRef(v)); From 093ab9bacf1353b79d7d8a00e6850aaccbb3a2b5 Mon Sep 17 00:00:00 2001 From: Ilya Egorov <0x42005e1f@gmail.com> Date: Fri, 2 Oct 2026 16:20:39 +0400 Subject: [PATCH 2/3] Add a NEWS entry --- .../2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 Misc/NEWS.d/next/Core_and_Builtins/2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst new file mode 100644 index 000000000000000..150e7d4e70c5f68 --- /dev/null +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst @@ -0,0 +1,3 @@ +Fix deletion by index from a :class:`list` (``del list[index]``), which did +not reallocate the underlying array and thus did not free memory (a +regression made in 3.13.0a5). From 6f68a64cd7d055a84f407c643bd2c9f74fdb79bc Mon Sep 17 00:00:00 2001 From: Ilya Egorov <0x42005e1f@gmail.com> Date: Sat, 3 Oct 2026 19:57:28 +0400 Subject: [PATCH 3/3] Apply the suggested change MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com> --- .../2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst index 150e7d4e70c5f68..a2c8cdd2233c31d 100644 --- a/Misc/NEWS.d/next/Core_and_Builtins/2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst +++ b/Misc/NEWS.d/next/Core_and_Builtins/2026-10-02-16-19-35.gh-issue-158592.V6W21J.rst @@ -1,3 +1,2 @@ -Fix deletion by index from a :class:`list` (``del list[index]``), which did -not reallocate the underlying array and thus did not free memory (a -regression made in 3.13.0a5). +Avoid holding memory much longer than needed after deleting +an item from a list via :keyword:`del`.