https://github.com/python/cpython/commit/12ed8b16feb3502cea427ff846e9d0b3d7fef721
commit: 12ed8b16feb3502cea427ff846e9d0b3d7fef721
branch: main
author: sobolevn <[email protected]>
committer: sobolevn <[email protected]>
date: 2026-07-07T11:25:30+03:00
summary:

gh-153210: Fix `array` module import crash under a memory pressure (#153238)

files:
A Misc/NEWS.d/next/Library/2026-07-07-02-26-43.gh-issue-153210.LUXIcm.rst
M Lib/test/test_array.py
M Modules/arraymodule.c

diff --git a/Lib/test/test_array.py b/Lib/test/test_array.py
index 23d9f3c9667d86..27bd63bde08eb8 100755
--- a/Lib/test/test_array.py
+++ b/Lib/test/test_array.py
@@ -5,7 +5,7 @@
 import collections.abc
 import unittest
 from test import support
-from test.support import import_helper
+from test.support import import_helper, script_helper
 from test.support import os_helper
 from test.support import _2G
 from test.support import subTests
@@ -42,6 +42,23 @@ def test_bad_constructor(self):
         self.assertRaises(ValueError, array.array, 'x')
         self.assertRaises(ValueError, array.array, 'Z')
 
+    @support.cpython_only
+    def test_does_not_crash_on_broken_imports(self):
+        # gh-153210
+        code = """if 1:
+            import collections.abc
+
+            del collections.abc.MutableSequence
+
+            try:
+                import array  # it used to crash before
+            except AttributeError:
+                pass
+            else:
+                raise AssertionError('AttributeError was not raised')
+        """
+        script_helper.assert_python_ok('-c', code)
+
     @support.cpython_only
     def test_disallow_instantiation(self):
         my_array = array.array("I")
diff --git 
a/Misc/NEWS.d/next/Library/2026-07-07-02-26-43.gh-issue-153210.LUXIcm.rst 
b/Misc/NEWS.d/next/Library/2026-07-07-02-26-43.gh-issue-153210.LUXIcm.rst
new file mode 100644
index 00000000000000..d5d4b7c1b8c0ed
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2026-07-07-02-26-43.gh-issue-153210.LUXIcm.rst
@@ -0,0 +1 @@
+Fix crash on :mod:`array` import under a memory pressure.
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c
index 9f613927be6315..45aad5d6fe2191 100644
--- a/Modules/arraymodule.c
+++ b/Modules/arraymodule.c
@@ -3401,22 +3401,15 @@ array_modexec(PyObject *m)
     CREATE_TYPE(m, state->ArrayIterType, &arrayiter_spec);
     Py_SET_TYPE(state->ArrayIterType, &PyType_Type);
 
-    if (PyModule_AddObjectRef(m, "ArrayType",
-                              (PyObject *)state->ArrayType) < 0) {
-        return -1;
-    }
-
     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);

_______________________________________________
Python-checkins mailing list -- [email protected]
To unsubscribe send an email to [email protected]
https://mail.python.org/mailman3//lists/python-checkins.python.org
Member address: [email protected]

Reply via email to