Skip to content

Commit 22d53a9

Browse files
[3.14] gh-148529: Minor improvements of the struct module documentation (GH-148565) (GH-149063)
* Document that 's' and 'p' accept bytes and bytearray. * Fix some footnotes. * Clarify that "string" is a byte string. * Fix the module docstring. (cherry picked from commit 3e5a3cb) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent 2459faa commit 22d53a9

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

Doc/library/struct.rst

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -227,32 +227,32 @@ platform-dependent.
227227
+--------+--------------------------+--------------------+----------------+------------+
228228
| ``c`` | :c:expr:`char` | bytes of length 1 | 1 | |
229229
+--------+--------------------------+--------------------+----------------+------------+
230-
| ``b`` | :c:expr:`signed char` | integer | 1 | \(1), \(2) |
230+
| ``b`` | :c:expr:`signed char` | int | 1 | \(2) |
231231
+--------+--------------------------+--------------------+----------------+------------+
232-
| ``B`` | :c:expr:`unsigned char` | integer | 1 | \(2) |
232+
| ``B`` | :c:expr:`unsigned char` | int | 1 | \(2) |
233233
+--------+--------------------------+--------------------+----------------+------------+
234234
| ``?`` | :c:expr:`_Bool` | bool | 1 | \(1) |
235235
+--------+--------------------------+--------------------+----------------+------------+
236-
| ``h`` | :c:expr:`short` | integer | 2 | \(2) |
236+
| ``h`` | :c:expr:`short` | int | 2 | \(2) |
237237
+--------+--------------------------+--------------------+----------------+------------+
238-
| ``H`` | :c:expr:`unsigned short` | integer | 2 | \(2) |
238+
| ``H`` | :c:expr:`unsigned short` | int | 2 | \(2) |
239239
+--------+--------------------------+--------------------+----------------+------------+
240-
| ``i`` | :c:expr:`int` | integer | 4 | \(2) |
240+
| ``i`` | :c:expr:`int` | int | 4 | \(2) |
241241
+--------+--------------------------+--------------------+----------------+------------+
242-
| ``I`` | :c:expr:`unsigned int` | integer | 4 | \(2) |
242+
| ``I`` | :c:expr:`unsigned int` | int | 4 | \(2) |
243243
+--------+--------------------------+--------------------+----------------+------------+
244-
| ``l`` | :c:expr:`long` | integer | 4 | \(2) |
244+
| ``l`` | :c:expr:`long` | int | 4 | \(2) |
245245
+--------+--------------------------+--------------------+----------------+------------+
246-
| ``L`` | :c:expr:`unsigned long` | integer | 4 | \(2) |
246+
| ``L`` | :c:expr:`unsigned long` | int | 4 | \(2) |
247247
+--------+--------------------------+--------------------+----------------+------------+
248-
| ``q`` | :c:expr:`long long` | integer | 8 | \(2) |
248+
| ``q`` | :c:expr:`long long` | int | 8 | \(2) |
249249
+--------+--------------------------+--------------------+----------------+------------+
250-
| ``Q`` | :c:expr:`unsigned long | integer | 8 | \(2) |
250+
| ``Q`` | :c:expr:`unsigned long | int | 8 | \(2) |
251251
| | long` | | | |
252252
+--------+--------------------------+--------------------+----------------+------------+
253-
| ``n`` | :c:type:`ssize_t` | integer | | \(3) |
253+
| ``n`` | :c:type:`ssize_t` | int | | \(2), \(3) |
254254
+--------+--------------------------+--------------------+----------------+------------+
255-
| ``N`` | :c:type:`size_t` | integer | | \(3) |
255+
| ``N`` | :c:type:`size_t` | int | | \(2), \(3) |
256256
+--------+--------------------------+--------------------+----------------+------------+
257257
| ``e`` | :c:expr:`_Float16` | float | 2 | \(4), \(6) |
258258
+--------+--------------------------+--------------------+----------------+------------+
@@ -268,7 +268,7 @@ platform-dependent.
268268
+--------+--------------------------+--------------------+----------------+------------+
269269
| ``p`` | :c:expr:`char[]` | bytes | | \(8) |
270270
+--------+--------------------------+--------------------+----------------+------------+
271-
| ``P`` | :c:expr:`void \*` | integer | | \(5) |
271+
| ``P`` | :c:expr:`void \*` | int | | \(2), \(5) |
272272
+--------+--------------------------+--------------------+----------------+------------+
273273

274274
.. versionchanged:: 3.3
@@ -342,27 +342,31 @@ Notes:
342342
The ``'p'`` format character encodes a "Pascal string", meaning a short
343343
variable-length string stored in a *fixed number of bytes*, given by the count.
344344
The first byte stored is the length of the string, or 255, whichever is
345-
smaller. The bytes of the string follow. If the string passed in to
345+
smaller. The bytes of the string follow. If the byte string passed in to
346346
:func:`pack` is too long (longer than the count minus 1), only the leading
347-
``count-1`` bytes of the string are stored. If the string is shorter than
347+
``count-1`` bytes of the string are stored. If the byte string is shorter than
348348
``count-1``, it is padded with null bytes so that exactly count bytes in all
349349
are used. Note that for :func:`unpack`, the ``'p'`` format character consumes
350-
``count`` bytes, but that the string returned can never contain more than 255
350+
``count`` bytes, but that the :class:`!bytes` object returned can never contain more than 255
351351
bytes.
352+
When packing, arguments of types :class:`bytes` and :class:`bytearray`
353+
are accepted.
352354

353355
(9)
354356
For the ``'s'`` format character, the count is interpreted as the length of the
355-
bytes, not a repeat count like for the other format characters; for example,
357+
byte string, not a repeat count like for the other format characters; for example,
356358
``'10s'`` means a single 10-byte string mapping to or from a single
357359
Python byte string, while ``'10c'`` means 10
358360
separate one byte character elements (e.g., ``cccccccccc``) mapping
359361
to or from ten different Python byte objects. (See :ref:`struct-examples`
360362
for a concrete demonstration of the difference.)
361-
If a count is not given, it defaults to 1. For packing, the string is
363+
If a count is not given, it defaults to 1. For packing, the byte string is
362364
truncated or padded with null bytes as appropriate to make it fit. For
363-
unpacking, the resulting bytes object always has exactly the specified number
364-
of bytes. As a special case, ``'0s'`` means a single, empty string (while
365+
unpacking, the resulting :class:`!bytes` object always has exactly the specified number
366+
of bytes. As a special case, ``'0s'`` means a single, empty byte string (while
365367
``'0c'`` means 0 characters).
368+
When packing, arguments of types :class:`bytes` and :class:`bytearray`
369+
are accepted.
366370

367371
(10)
368372
For the ``'F'`` and ``'D'`` format characters, the packed representation uses

Modules/_struct.c

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* struct module -- pack values into and (out of) bytes objects */
22

33
/* New version supporting byte order, alignment and size options,
4-
character strings, and unsigned numbers */
4+
byte strings, and unsigned numbers */
55

66
#ifndef Py_BUILD_CORE_BUILTIN
77
# define Py_BUILD_CORE_MODULE 1
@@ -2158,7 +2158,7 @@ Struct_iter_unpack_impl(PyStructObject *self, PyObject *buffer)
21582158
*
21592159
* Takes a struct object, a tuple of arguments, and offset in that tuple of
21602160
* argument for where to start processing the arguments for packing, and a
2161-
* character buffer for writing the packed string. The caller must insure
2161+
* character buffer for writing the packed data. The caller must ensure
21622162
* that the buffer may contain the required length for packing the arguments.
21632163
* 0 is returned on success, 1 is returned if there is an error.
21642164
*
@@ -2699,8 +2699,8 @@ static struct PyMethodDef module_functions[] = {
26992699

27002700
PyDoc_STRVAR(module_doc,
27012701
"Functions to convert between Python values and C structs.\n\
2702-
Python bytes objects are used to hold the data representing the C struct\n\
2703-
and also as format strings (explained below) to describe the layout of data\n\
2702+
Python bytes objects are used to hold the data representing the C struct.\n\
2703+
The format string (explained below) describes the layout of data\n\
27042704
in the C struct.\n\
27052705
\n\
27062706
The optional first format char indicates byte order, size and alignment:\n\
@@ -2710,19 +2710,18 @@ The optional first format char indicates byte order, size and alignment:\n\
27102710
>: big-endian, std. size & alignment\n\
27112711
!: same as >\n\
27122712
\n\
2713-
The remaining chars indicate types of args and must match exactly;\n\
2713+
The remaining characters indicate types of args and must match exactly;\n\
27142714
these can be preceded by a decimal repeat count:\n\
2715-
x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
2716-
?:_Bool; h:short; H:unsigned short; i:int; I:unsigned int;\n\
2717-
l:long; L:unsigned long; f:float; d:double; e:half-float.\n\
2718-
F:float complex; D:double complex.\n\
2715+
x: pad byte (no data); c: char; b: signed byte; B: unsigned byte;\n\
2716+
?: _Bool; h: short; H: unsigned short; i: int; I: unsigned int;\n\
2717+
l: long; L: unsigned long; q: long long; Q: unsigned long long;\n\
2718+
f: float; d: double; e: half-float;\n\
2719+
F: float complex; D: double complex.\n\
27192720
Special cases (preceding decimal count indicates length):\n\
2720-
s:string (array of char); p: pascal string (with count byte).\n\
2721+
s: byte string (array of char); p: Pascal string (with count byte).\n\
27212722
Special cases (only available in native format):\n\
2722-
n:ssize_t; N:size_t;\n\
2723-
P:an integer type that is wide enough to hold a pointer.\n\
2724-
Special case (not in native mode unless 'long long' in platform C):\n\
2725-
q:long long; Q:unsigned long long\n\
2723+
n: ssize_t; N: size_t;\n\
2724+
P: an integer type that is wide enough to hold a pointer.\n\
27262725
Whitespace between formats is ignored.\n\
27272726
\n\
27282727
The variable struct.error is an exception raised on errors.\n");

0 commit comments

Comments
 (0)