Skip to content

Commit 93323d4

Browse files
committed
gh-149402: don't assume single-character type codes (struct/array/ctypes)
In the struct docs, section "Format Characters" was renamed to "Type Codes".
1 parent 5fcab14 commit 93323d4

3 files changed

Lines changed: 20 additions & 21 deletions

File tree

Doc/library/array.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ This module defines an object type which can compactly represent an array of
1212
basic values: characters, integers, floating-point numbers, complex numbers. Arrays are mutable :term:`sequence`
1313
types and behave very much like lists, except that the type of objects stored in
1414
them is constrained. The type is specified at object creation time by using a
15-
:dfn:`type code`, which is a single character. The following type codes are
15+
:dfn:`type code`. The following type codes are
1616
defined:
1717

1818
+-----------+--------------------+-------------------+-----------------------+-------+

Doc/library/ctypes.rst

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,8 +2572,7 @@ Fundamental data types
25722572

25732573
.. attribute:: _type_
25742574

2575-
Class attribute that contains an internal type code, as a
2576-
single-character string.
2575+
Class attribute that contains an internal type code.
25772576
See :ref:`ctypes-fundamental-data-types` for a summary.
25782577

25792578
Types marked \* in the summary may be (or always are) aliases of a

Doc/library/struct.rst

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ Format Strings
111111
--------------
112112

113113
Format strings describe the data layout when
114-
packing and unpacking data. They are built up from :ref:`format characters<format-characters>`,
114+
packing and unpacking data. They are built up from :ref:`type codes<type-codes>`,
115115
which specify the type of data being packed/unpacked. In addition,
116116
special characters control the :ref:`byte order, size and alignment<struct-alignment>`.
117117
Each format string consists of an optional prefix character which
@@ -183,8 +183,8 @@ Use :data:`sys.byteorder` to check the endianness of your system.
183183
Native size and alignment are determined using the C compiler's
184184
``sizeof`` expression. This is always combined with native byte order.
185185

186-
Standard size depends only on the format character; see the table in
187-
the :ref:`format-characters` section.
186+
Standard size depends only on the type code; see the table in
187+
the :ref:`type-codes` section.
188188

189189
Note the difference between ``'@'`` and ``'='``: both use native byte order, but
190190
the size and alignment of the latter is standardized.
@@ -208,12 +208,12 @@ Notes:
208208
count of zero. See :ref:`struct-examples`.
209209

210210

211-
.. _format-characters:
211+
.. _type-codes:
212212

213-
Format Characters
214-
^^^^^^^^^^^^^^^^^
213+
Type Codes
214+
^^^^^^^^^^
215215

216-
Format characters have the following meaning; the conversion between C and
216+
Type codes (or format codes) have the following meaning; the conversion between C and
217217
Python values should be obvious given their types. The 'Standard size' column
218218
refers to the size of the packed value in bytes when using standard size; that
219219
is, when the format string starts with one of ``'<'``, ``'>'``, ``'!'`` or
@@ -324,7 +324,7 @@ Notes:
324324
format used by the platform.
325325

326326
(5)
327-
The ``'P'`` format character is only available for the native byte ordering
327+
The ``'P'`` type code is only available for the native byte ordering
328328
(selected as the default or with the ``'@'`` byte order character). The byte
329329
order character ``'='`` chooses to use little- or big-endian ordering based
330330
on the host system. The struct module does not interpret this as native
@@ -346,22 +346,22 @@ Notes:
346346
When packing, ``'x'`` inserts one NUL byte.
347347

348348
(8)
349-
The ``'p'`` format character encodes a "Pascal string", meaning a short
349+
The ``'p'`` type code encodes a "Pascal string", meaning a short
350350
variable-length string stored in a *fixed number of bytes*, given by the count.
351351
The first byte stored is the length of the string, or 255, whichever is
352352
smaller. The bytes of the string follow. If the byte string passed in to
353353
:func:`pack` is too long (longer than the count minus 1), only the leading
354354
``count-1`` bytes of the string are stored. If the byte string is shorter than
355355
``count-1``, it is padded with null bytes so that exactly count bytes in all
356-
are used. Note that for :func:`unpack`, the ``'p'`` format character consumes
356+
are used. Note that for :func:`unpack`, the ``'p'`` type code consumes
357357
``count`` bytes, but that the :class:`!bytes` object returned can never contain more than 255
358358
bytes.
359359
When packing, arguments of types :class:`bytes` and :class:`bytearray`
360360
are accepted.
361361

362362
(9)
363-
For the ``'s'`` format character, the count is interpreted as the length of the
364-
byte string, not a repeat count like for the other format characters; for example,
363+
For the ``'s'`` type code, the count is interpreted as the length of the
364+
byte string, not a repeat count like for the other type codes; for example,
365365
``'10s'`` means a single 10-byte string mapping to or from a single
366366
Python byte string, while ``'10c'`` means 10
367367
separate one byte character elements (e.g., ``cccccccccc``) mapping
@@ -376,7 +376,7 @@ Notes:
376376
are accepted.
377377

378378
(10)
379-
For the ``'F'`` and ``'D'`` format characters, the packed representation uses
379+
For the ``'F'`` and ``'D'`` type codes, the packed representation uses
380380
the IEEE 754 binary32 and binary64 format for components of the complex
381381
number, regardless of the floating-point format used by the platform.
382382
Note that complex types (``F``/``Zf`` and ``D``/``Zd``) are available unconditionally,
@@ -385,7 +385,7 @@ Notes:
385385
two-element C array containing, respectively, the real and imaginary parts.
386386

387387

388-
A format character may be preceded by an integral repeat count. For example,
388+
A type code may be preceded by an integral repeat count. For example,
389389
the format string ``'4h'`` means exactly the same as ``'hhhh'``.
390390

391391
Whitespace characters between formats are ignored; a count and its format must
@@ -402,7 +402,7 @@ then :exc:`struct.error` is raised.
402402

403403
.. index:: single: ? (question mark); in struct format strings
404404

405-
For the ``'?'`` format character, the return value is either :const:`True` or
405+
For the ``'?'`` type code, the return value is either :const:`True` or
406406
:const:`False`. When packing, the truth value of the argument object is used.
407407
Either 0 or 1 in the native or standard bool representation will be packed, and
408408
any non-zero value will be ``True`` when unpacking.
@@ -457,7 +457,7 @@ the result in a named tuple::
457457
>>> Student._make(unpack('<10sHHb', record))
458458
Student(name=b'raymond ', serialnum=4658, school=264, gradelevel=8)
459459

460-
The ordering of format characters may have an impact on size in native
460+
The ordering of type codes may have an impact on size in native
461461
mode since padding is implicit. In standard mode, the user is
462462
responsible for inserting any desired padding.
463463
Note in
@@ -515,7 +515,7 @@ When constructing format strings which mimic native layouts, the
515515
compiler and machine architecture determine byte ordering and padding.
516516
In such cases, the ``@`` format character should be used to specify
517517
native byte ordering and data sizes. Internal pad bytes are normally inserted
518-
automatically. It is possible that a zero-repeat format code will be
518+
automatically. It is possible that a zero-repeat type code will be
519519
needed at the end of a format string to round up to the correct
520520
byte boundary for proper alignment of consecutive chunks of data.
521521

@@ -534,7 +534,7 @@ code solves that problem::
534534
>>> calcsize('@llh0l')
535535
24
536536

537-
The ``'x'`` format code can be used to specify the repeat, but for
537+
The ``'x'`` type code can be used to specify the repeat, but for
538538
native formats it is better to use a zero-repeat format like ``'0l'``.
539539

540540
By default, native byte ordering and alignment is used, but it is

0 commit comments

Comments
 (0)