Skip to content

Commit ab01d18

Browse files
authored
gh-142083: Document the 'w' type code in the array.array docstring (#158040)
Add a test asserting that the table lists exactly the supported type codes and that the minimum size it gives for each of them is one the implementation really meets.
1 parent a6a63b6 commit ab01d18

3 files changed

Lines changed: 24 additions & 0 deletions

File tree

‎Lib/test/test_array.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
import weakref
1313
import pickle
1414
import operator
15+
import re
1516
import struct
1617
import sys
1718

@@ -110,6 +111,25 @@ def test_typecodes(self):
110111
self.assertIsInstance(typecode, str)
111112
self.assertGreaterEqual(len(typecode), 1)
112113

114+
@support.cpython_only
115+
@support.requires_docstrings
116+
def test_typecodes_documented(self):
117+
# The type code table in the array.array docstring must list every
118+
# supported type code, and nothing else, and the minimum size it
119+
# gives for each of them must be one the implementation meets.
120+
row = re.compile(r"^ {4}'(\w+)' +\S.*? +(\d+)(?: \(see note\))?$")
121+
documented = {}
122+
for line in array.array.__doc__.splitlines():
123+
match = row.match(line)
124+
if match is not None:
125+
documented[match.group(1)] = int(match.group(2))
126+
127+
self.assertEqual(sorted(documented), sorted(array.typecodes))
128+
for typecode, minimum_size in documented.items():
129+
with self.subTest(typecode=typecode):
130+
self.assertGreaterEqual(array.array(typecode).itemsize,
131+
minimum_size)
132+
113133

114134
# Machine format codes.
115135
#
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
Document the ``'w'`` type code in the :class:`array.array` docstring, so
2+
that it lists the same type codes as :data:`array.typecodes` and the
3+
:mod:`array` module documentation.

‎Modules/arraymodule.c‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3109,6 +3109,7 @@ The following type codes are defined:\n\
31093109
Type code C Type Minimum size in bytes\n\
31103110
'b' signed integer 1\n\
31113111
'B' unsigned integer 1\n\
3112+
'w' Py_UCS4 4\n\
31123113
'h' signed integer 2\n\
31133114
'H' unsigned integer 2\n\
31143115
'i' signed integer 2\n\

0 commit comments

Comments
 (0)