Skip to content

Commit 6ac7e54

Browse files
authored
[3.13] gh-153210: Fix array module import crash under a memory pressure (GH-153238) (#153251)
(cherry picked from commit 12ed8b1)
1 parent 68f68fb commit 6ac7e54

3 files changed

Lines changed: 19 additions & 8 deletions

File tree

Lib/test/test_array.py

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
import collections.abc
66
import unittest
77
from test import support
8-
from test.support import import_helper
8+
from test.support import import_helper, script_helper
99
from test.support import os_helper
1010
from test.support import _2G
1111
from test.support import subTests
@@ -45,6 +45,23 @@ def test_bad_constructor(self):
4545
self.assertRaises(TypeError, array.array, 'xx')
4646
self.assertRaises(ValueError, array.array, 'x')
4747

48+
@support.cpython_only
49+
def test_does_not_crash_on_broken_imports(self):
50+
# gh-153210
51+
code = """if 1:
52+
import collections.abc
53+
54+
del collections.abc.MutableSequence
55+
56+
try:
57+
import array # it used to crash before
58+
except AttributeError:
59+
pass
60+
else:
61+
raise AssertionError('AttributeError was not raised')
62+
"""
63+
script_helper.assert_python_ok('-c', code)
64+
4865
@support.cpython_only
4966
def test_disallow_instantiation(self):
5067
my_array = array.array("I")
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fix crash on :mod:`array` import under a memory pressure.

Modules/arraymodule.c

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3262,22 +3262,15 @@ array_modexec(PyObject *m)
32623262
CREATE_TYPE(m, state->ArrayIterType, &arrayiter_spec);
32633263
Py_SET_TYPE(state->ArrayIterType, &PyType_Type);
32643264

3265-
if (PyModule_AddObjectRef(m, "ArrayType",
3266-
(PyObject *)state->ArrayType) < 0) {
3267-
return -1;
3268-
}
3269-
32703265
PyObject *mutablesequence = _PyImport_GetModuleAttrString(
32713266
"collections.abc", "MutableSequence");
32723267
if (!mutablesequence) {
3273-
Py_DECREF((PyObject *)state->ArrayType);
32743268
return -1;
32753269
}
32763270
PyObject *res = PyObject_CallMethod(mutablesequence, "register", "O",
32773271
(PyObject *)state->ArrayType);
32783272
Py_DECREF(mutablesequence);
32793273
if (!res) {
3280-
Py_DECREF((PyObject *)state->ArrayType);
32813274
return -1;
32823275
}
32833276
Py_DECREF(res);

0 commit comments

Comments
 (0)