Skip to content

Commit eb3d9e2

Browse files
committed
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)
1 parent ef4d3f3 commit eb3d9e2

2 files changed

Lines changed: 19 additions & 2 deletions

File tree

Lib/test/test_array.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,13 @@
99
from test.support import os_helper
1010
from test.support import _2G
1111
from test.support import subTests
12+
from test.support.script_helper import assert_python_ok
1213
import weakref
1314
import pickle
1415
import operator
1516
import struct
1617
import sys
18+
import textwrap
1719

1820
import array
1921
from array import _array_reconstructor as array_reconstructor
@@ -35,6 +37,23 @@ def test_array_is_sequence(self):
3537
self.assertIsInstance(array.array("B"), collections.abc.MutableSequence)
3638
self.assertIsInstance(array.array("B"), collections.abc.Reversible)
3739

40+
@support.cpython_only
41+
def test_module_init_mutablesequence_register_failure(self):
42+
# gh-153210: Check that when collections.abc.MutableSequence is unavailable
43+
# The refcount of ArrayType is decremented correctly.
44+
# This test only catches the issue in a debug build.
45+
script = textwrap.dedent("""
46+
import collections.abc
47+
del collections.abc.MutableSequence
48+
try:
49+
import array
50+
except AttributeError:
51+
pass
52+
else:
53+
raise SystemExit("import array should have failed")
54+
""")
55+
assert_python_ok("-c", script)
56+
3857
def test_bad_constructor(self):
3958
self.assertRaises(TypeError, array.array)
4059
self.assertRaises(TypeError, array.array, spam=42)

Modules/arraymodule.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3409,14 +3409,12 @@ array_modexec(PyObject *m)
34093409
PyObject *mutablesequence = PyImport_ImportModuleAttrString(
34103410
"collections.abc", "MutableSequence");
34113411
if (!mutablesequence) {
3412-
Py_DECREF((PyObject *)state->ArrayType);
34133412
return -1;
34143413
}
34153414
PyObject *res = PyObject_CallMethod(mutablesequence, "register", "O",
34163415
(PyObject *)state->ArrayType);
34173416
Py_DECREF(mutablesequence);
34183417
if (!res) {
3419-
Py_DECREF((PyObject *)state->ArrayType);
34203418
return -1;
34213419
}
34223420
Py_DECREF(res);

0 commit comments

Comments
 (0)