Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,6 +45,23 @@ def test_bad_constructor(self):
self.assertRaises(TypeError, array.array, 'xx')
self.assertRaises(ValueError, array.array, 'x')

@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")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix crash on :mod:`array` import under a memory pressure.
7 changes: 0 additions & 7 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -3295,22 +3295,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);
Expand Down
Loading