Change array.typecodes type to tuple

This commit is contained in:
Victor Stinner 2026-05-02 12:30:40 +02:00
parent fe496e997f
commit cf16b041f5
5 changed files with 44 additions and 9 deletions

View file

@ -109,7 +109,10 @@ The module defines the following item:
.. data:: typecodes
A string with all available type codes.
A tuple with all available type codes.
.. versionchanged:: next
The type changed from :str:`str` to :class:`tuple`.
The module defines the following type:

View file

@ -680,12 +680,17 @@ array
* Support the :c:expr:`float complex` and :c:expr:`double complex` C types:
formatting characters ``'F'``/``'Zd'`` and ``'D'``/``'Zd'`` respectively.
(Contributed by Sergey B Kirpichev in :gh:`146151`.)
(Contributed by Sergey B Kirpichev and Victor Stinner in :gh:`146151`
and :gh:`148675`.)
* Support half-floats (16-bit IEEE 754 binary interchange format): formatting
character ``'e'``.
(Contributed by Sergey B Kirpichev in :gh:`146238`.)
* The :data:`array.typecodes` type changed from :str:`str` to :class:`tuple`
to support type codes longer than 1 character (``Zf`` and ``Zd``).
(Contributed by Victor Stinner in :gh:`148675`.)
base64
------

View file

@ -85,6 +85,12 @@ def __index__(self):
with self.assertRaises(TypeError):
a.fromlist(lst)
def test_typecodes(self):
self.assertIsInstance(array.typecodes, tuple)
for typecode in array.typecodes:
self.assertIsInstance(typecode, str)
self.assertGreaterEqual(len(typecode), 1)
# Machine format codes.
#
@ -1237,6 +1243,9 @@ def test_free_after_iterating(self):
support.check_free_after_iterating(self, reversed, array.array,
(self.typecode,))
def test_known_typecode(self):
self.assertIn(self.typecode, array.typecodes)
class StringTest(BaseTest):
def test_setitem(self):

View file

@ -0,0 +1,3 @@
The :data:`array.typecodes` type changed from :str:`str` to :class:`tuple`
to support type codes longer than 1 character (``Zf`` and ``Zd``). Patch by
Victor Stinner.

View file

@ -3493,7 +3493,6 @@ static int
array_modexec(PyObject *m)
{
array_state *state = get_array_state(m);
char buffer[Py_ARRAY_LENGTH(descriptors)*2], *p;
PyObject *typecodes;
const struct arraydescr *descr;
@ -3532,13 +3531,29 @@ array_modexec(PyObject *m)
return -1;
}
p = buffer;
for (descr = descriptors; descr->typecode != NULL; descr++) {
strcpy(p, descr->typecode);
p += strlen(descr->typecode);
typecodes = PyList_New(0);
if (typecodes == NULL) {
return -1;
}
typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
if (PyModule_Add(m, "typecodes", typecodes) < 0) {
for (descr = descriptors; descr->typecode != NULL; descr++) {
PyObject *typecode = PyUnicode_DecodeASCII(descr->typecode, strlen(descr->typecode), NULL);
if (typecode == NULL) {
Py_DECREF(typecodes);
return -1;
}
int res = PyList_Append(typecodes, typecode);
Py_DECREF(typecode);
if (res < 0) {
Py_DECREF(typecodes);
return -1;
}
}
PyObject *tuple = PyList_AsTuple(typecodes);
Py_DECREF(typecodes);
if (tuple == NULL) {
return -1;
}
if (PyModule_Add(m, "typecodes", tuple) < 0) {
return -1;
}