From f13e3bd39f5c6c8001f3c20c6902ae4b92e71307 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 22 Oct 2025 20:52:48 +0100 Subject: [PATCH 1/3] Commit --- .../Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst | 2 ++ Modules/arraymodule.c | 3 +++ 2 files changed, 5 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst diff --git a/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst b/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst new file mode 100644 index 00000000000000..6c2640db5a1258 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst @@ -0,0 +1,2 @@ +Fix memory leak in :func:`array.array` when creating arrays from empty +:class:`str`. diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index d97cf7af767ca3..729e085c19f006 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -2833,6 +2833,9 @@ array_new(PyTypeObject *type, PyObject *args, PyObject *kwds) Py_SET_SIZE(self, n); self->allocated = n; } + else { + PyMem_Free(ustr); + } } else { // c == 'w' Py_ssize_t n = PyUnicode_GET_LENGTH(initial); From 9d5fb14b80147bc53c62454bd5c4ce86c5fe026d Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Wed, 22 Oct 2025 20:55:32 +0100 Subject: [PATCH 2/3] Fix blurb ref --- .../next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst b/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst index 6c2640db5a1258..276f7b7684905d 100644 --- a/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst +++ b/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst @@ -1,2 +1,2 @@ -Fix memory leak in :func:`array.array` when creating arrays from empty +Fix memory leak in :class:`array.array` when creating arrays from empty :class:`str`. From d2d307ac795acef93dd2647c13610b1293d3f1e6 Mon Sep 17 00:00:00 2001 From: Stan Ulbrych Date: Thu, 23 Oct 2025 08:00:07 +0100 Subject: [PATCH 3/3] Fix blurb and add test --- Lib/test/test_array.py | 8 ++++++++ .../2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst | 4 ++-- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 58ea89c4fac833..83b3c978da3581 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -1255,6 +1255,14 @@ def test_typecode_u_deprecation(self): with self.assertWarns(DeprecationWarning): array.array("u") + def test_empty_string_mem_leak_gh140474(self): + with warnings.catch_warnings(): + warnings.simplefilter('ignore', DeprecationWarning) + for _ in range(1000): + a = array.array('u', '') + self.assertEqual(len(a), 0) + self.assertEqual(a.typecode, 'u') + class UCS4Test(UnicodeTest): typecode = 'w' diff --git a/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst b/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst index 276f7b7684905d..aca4e68b1e5e49 100644 --- a/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst +++ b/Misc/NEWS.d/next/Library/2025-10-22-20-52-13.gh-issue-140474.xIWlip.rst @@ -1,2 +1,2 @@ -Fix memory leak in :class:`array.array` when creating arrays from empty -:class:`str`. +Fix memory leak in :class:`array.array` when creating arrays from an empty +:class:`str` and the ``u`` type code.