Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
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
Prev Previous commit
Next Next commit
Change array.typecodes type to tuple
  • Loading branch information
vstinner committed May 2, 2026
commit cf16b041f5d9a27dbd02bd002ad38ecab563d376
5 changes: 4 additions & 1 deletion Doc/library/array.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
7 changes: 6 additions & 1 deletion Doc/whatsnew/3.15.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Comment thread
vstinner marked this conversation as resolved.
Outdated
(Contributed by Sergey B Kirpichev in :gh:`146151`.)
(Contributed by Sergey B Kirpichev and Victor Stinner in :gh:`146151`
and :gh:`148675`.)
Comment thread
vstinner marked this conversation as resolved.
Outdated

* 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
------
Expand Down
9 changes: 9 additions & 0 deletions Lib/test/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
#
Expand Down Expand Up @@ -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):
Expand Down
Original file line number Diff line number Diff line change
@@ -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.
27 changes: 21 additions & 6 deletions Modules/arraymodule.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -3532,13 +3531,29 @@ array_modexec(PyObject *m)
return -1;
}

p = buffer;
typecodes = PyList_New(0);
if (typecodes == NULL) {
return -1;
}
for (descr = descriptors; descr->typecode != NULL; descr++) {
strcpy(p, descr->typecode);
p += strlen(descr->typecode);
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;
}
typecodes = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
if (PyModule_Add(m, "typecodes", typecodes) < 0) {
if (PyModule_Add(m, "typecodes", tuple) < 0) {
return -1;
}

Expand Down