bpo-29727: Register array.array as a MutableSequence by pablogsal · Pull Request #21338 · python/cpython · GitHub
Skip to content
Merged
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
66 changes: 66 additions & 0 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Register :class:`array.array` as a
:class:`~collections.abc.MutableSequence`. Patch by Pablo Galindo.
20 changes: 20 additions & 0 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -2989,6 +2989,26 @@ array_modexec(PyObject *m)
Py_DECREF((PyObject *)&Arraytype);
return -1;
}

PyObject *abc_mod = PyImport_ImportModule("collections.abc");
if (!abc_mod) {
Py_DECREF((PyObject *)&Arraytype);
return -1;
}
PyObject *mutablesequence = PyObject_GetAttrString(abc_mod, "MutableSequence");
Py_DECREF(abc_mod);
if (!mutablesequence) {
Py_DECREF((PyObject *)&Arraytype);
return -1;
}
PyObject *res = PyObject_CallMethod(mutablesequence, "register", "O", (PyObject *)&Arraytype);

@brandtbucher brandtbucher Jul 5, 2020

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, @brandtbucher. The reason I originally did not add _Py_IDENTIFIER is so we don't add more to the global/static state so it does not make future sub interpreters isolation harder, especially given that this code will run only at module import time.

Py_DECREF(mutablesequence);
if (!res) {
Py_DECREF((PyObject *)&Arraytype);
return -1;
}
Py_DECREF(res);

Py_INCREF((PyObject *)&Arraytype);
if (PyModule_AddObject(m, "array", (PyObject *)&Arraytype) < 0) {
Py_DECREF((PyObject *)&Arraytype);
Expand Down