From eb3d9e200aff536aa7b180c5ef154a9e25fc53ac Mon Sep 17 00:00:00 2001 From: Steve Stagg Date: Tue, 7 Jul 2026 00:19:47 +0100 Subject: [PATCH 1/3] gh-153210: Do not DECREF ArryaType in array_modexc because it's initialized on the array mod state, so GC / module cleanup will do this. (Ownership belongs to the state struct) --- Lib/test/test_array.py | 19 +++++++++++++++++++ Modules/arraymodule.c | 2 -- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 23d9f3c9667d868..998358ff23cae86 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -9,11 +9,13 @@ from test.support import os_helper from test.support import _2G from test.support import subTests +from test.support.script_helper import assert_python_ok import weakref import pickle import operator import struct import sys +import textwrap import array from array import _array_reconstructor as array_reconstructor @@ -35,6 +37,23 @@ def test_array_is_sequence(self): self.assertIsInstance(array.array("B"), collections.abc.MutableSequence) self.assertIsInstance(array.array("B"), collections.abc.Reversible) + @support.cpython_only + def test_module_init_mutablesequence_register_failure(self): + # gh-153210: Check that when collections.abc.MutableSequence is unavailable + # The refcount of ArrayType is decremented correctly. + # This test only catches the issue in a debug build. + script = textwrap.dedent(""" + import collections.abc + del collections.abc.MutableSequence + try: + import array + except AttributeError: + pass + else: + raise SystemExit("import array should have failed") + """) + assert_python_ok("-c", script) + def test_bad_constructor(self): self.assertRaises(TypeError, array.array) self.assertRaises(TypeError, array.array, spam=42) diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 9f613927be63159..528d1b287612fef 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -3409,14 +3409,12 @@ array_modexec(PyObject *m) PyObject *mutablesequence = PyImport_ImportModuleAttrString( "collections.abc", "MutableSequence"); if (!mutablesequence) { - Py_DECREF((PyObject *)state->ArrayType); return -1; } PyObject *res = PyObject_CallMethod(mutablesequence, "register", "O", (PyObject *)state->ArrayType); Py_DECREF(mutablesequence); if (!res) { - Py_DECREF((PyObject *)state->ArrayType); return -1; } Py_DECREF(res); From a8da27b604cebaa3fb4fba7610c06bbc013ef2f9 Mon Sep 17 00:00:00 2001 From: Steve Stagg Date: Tue, 7 Jul 2026 00:26:10 +0100 Subject: [PATCH 2/3] Add blurb --- .../next/Library/2026-07-07-00-26-06.gh-issue-153210.HRwj-j.rst | 2 ++ 1 file changed, 2 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-07-07-00-26-06.gh-issue-153210.HRwj-j.rst diff --git a/Misc/NEWS.d/next/Library/2026-07-07-00-26-06.gh-issue-153210.HRwj-j.rst b/Misc/NEWS.d/next/Library/2026-07-07-00-26-06.gh-issue-153210.HRwj-j.rst new file mode 100644 index 000000000000000..ef3279fee8061e6 --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-07-07-00-26-06.gh-issue-153210.HRwj-j.rst @@ -0,0 +1,2 @@ +:mod:`array`: Fix incorrect handling of reference counts when errors occur +during module import From 84c0da3e09b60d3b2888b8a9b433b31ba2b85069 Mon Sep 17 00:00:00 2001 From: Steve Stagg Date: Tue, 7 Jul 2026 00:33:24 +0100 Subject: [PATCH 3/3] Tighten up the inline script in the test a bit --- Lib/test/test_array.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py index 998358ff23cae86..122957dc58f6c15 100755 --- a/Lib/test/test_array.py +++ b/Lib/test/test_array.py @@ -49,8 +49,6 @@ def test_module_init_mutablesequence_register_failure(self): import array except AttributeError: pass - else: - raise SystemExit("import array should have failed") """) assert_python_ok("-c", script)