Skip to content

Commit cfd56f2

Browse files
committed
Issue #8469: Reorder struct module sections for clarity; other minor tweaks.
1 parent 7a70b2c commit cfd56f2

1 file changed

Lines changed: 79 additions & 75 deletions

File tree

Doc/library/struct.rst

Lines changed: 79 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,84 @@ Format Strings
7777
--------------
7878

7979
Format strings are the mechanism used to specify the expected layout when
80-
packing and unpacking data. They are built up from format characters, which
81-
specify the type of data being packed/unpacked. In addition, there are
82-
special characters for controlling the byte order, size, and alignment.
80+
packing and unpacking data. They are built up from :ref:`format-characters`,
81+
which specify the type of data being packed/unpacked. In addition, there are
82+
special characters for controlling the :ref:`struct-alignment`.
83+
84+
85+
.. _struct-alignment:
86+
87+
Byte Order, Size, and Alignment
88+
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
89+
90+
By default, C types are represented in the machine's native format and byte
91+
order, and properly aligned by skipping pad bytes if necessary (according to the
92+
rules used by the C compiler).
93+
94+
Alternatively, the first character of the format string can be used to indicate
95+
the byte order, size and alignment of the packed data, according to the
96+
following table:
97+
98+
+-----------+------------------------+--------------------+
99+
| Character | Byte order | Size and alignment |
100+
+===========+========================+====================+
101+
| ``@`` | native | native |
102+
+-----------+------------------------+--------------------+
103+
| ``=`` | native | standard |
104+
+-----------+------------------------+--------------------+
105+
| ``<`` | little-endian | standard |
106+
+-----------+------------------------+--------------------+
107+
| ``>`` | big-endian | standard |
108+
+-----------+------------------------+--------------------+
109+
| ``!`` | network (= big-endian) | standard |
110+
+-----------+------------------------+--------------------+
111+
112+
If the first character is not one of these, ``'@'`` is assumed.
113+
114+
Native byte order is big-endian or little-endian, depending on the host
115+
system. For example, Intel x86 and AMD64 (x86-64) are little-endian;
116+
Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature
117+
switchable endianness (bi-endian). Use ``sys.byteorder`` to check the
118+
endianness of your system.
119+
120+
Native size and alignment are determined using the C compiler's
121+
``sizeof`` expression. This is always combined with native byte order.
122+
123+
Standard size and alignment are as follows: no alignment is required for any
124+
type (so you have to use pad bytes); :ctype:`short` is 2 bytes; :ctype:`int` and
125+
:ctype:`long` are 4 bytes; :ctype:`long long` (:ctype:`__int64` on Windows) is 8
126+
bytes; :ctype:`float` and :ctype:`double` are 32-bit and 64-bit IEEE floating
127+
point numbers, respectively. :ctype:`_Bool` is 1 byte.
128+
129+
Note the difference between ``'@'`` and ``'='``: both use native byte order, but
130+
the size and alignment of the latter is standardized.
131+
132+
The form ``'!'`` is available for those poor souls who claim they can't remember
133+
whether network byte order is big-endian or little-endian.
134+
135+
There is no way to indicate non-native byte order (force byte-swapping); use the
136+
appropriate choice of ``'<'`` or ``'>'``.
137+
138+
The ``'P'`` format character is only available for the native byte ordering
139+
(selected as the default or with the ``'@'`` byte order character). The byte
140+
order character ``'='`` chooses to use little- or big-endian ordering based on
141+
the host system. The struct module does not interpret this as native ordering,
142+
so the ``'P'`` format is not available.
143+
144+
Notes:
145+
146+
(1) Padding is only automatically added between successive structure members.
147+
No padding is added at the beginning or the end of the encoded struct.
148+
149+
(2) No padding is added when using non-native size and alignment, e.g.
150+
with '<', '>', '=', and '!'.
151+
152+
(3) To align the end of a structure to the alignment requirement of a
153+
particular type, end the format with the code for that type with a repeat
154+
count of zero. See :ref:`struct-examples`.
155+
156+
157+
.. _format-characters:
83158

84159
Format Characters
85160
^^^^^^^^^^^^^^^^^
@@ -196,77 +271,6 @@ Either 0 or 1 in the native or standard bool representation will be packed, and
196271
any non-zero value will be True when unpacking.
197272

198273

199-
.. _struct-alignment:
200-
201-
Byte Order, Size, and Alignment
202-
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
203-
204-
By default, C types are represented in the machine's native format and byte
205-
order, and properly aligned by skipping pad bytes if necessary (according to the
206-
rules used by the C compiler).
207-
208-
Alternatively, the first character of the format string can be used to indicate
209-
the byte order, size and alignment of the packed data, according to the
210-
following table:
211-
212-
+-----------+------------------------+--------------------+
213-
| Character | Byte order | Size and alignment |
214-
+===========+========================+====================+
215-
| ``@`` | native | native |
216-
+-----------+------------------------+--------------------+
217-
| ``=`` | native | standard |
218-
+-----------+------------------------+--------------------+
219-
| ``<`` | little-endian | standard |
220-
+-----------+------------------------+--------------------+
221-
| ``>`` | big-endian | standard |
222-
+-----------+------------------------+--------------------+
223-
| ``!`` | network (= big-endian) | standard |
224-
+-----------+------------------------+--------------------+
225-
226-
If the first character is not one of these, ``'@'`` is assumed.
227-
228-
Native byte order is big-endian or little-endian, depending on the host
229-
system. For example, Intel x86 and AMD64 (x86-64) are little-endian;
230-
Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature
231-
switchable endianness (bi-endian). Use ``sys.byteorder`` to check the
232-
endianness of your system.
233-
234-
Native size and alignment are determined using the C compiler's
235-
``sizeof`` expression. This is always combined with native byte order.
236-
237-
Standard size and alignment are as follows: no alignment is required for any
238-
type (so you have to use pad bytes); :ctype:`short` is 2 bytes; :ctype:`int` and
239-
:ctype:`long` are 4 bytes; :ctype:`long long` (:ctype:`__int64` on Windows) is 8
240-
bytes; :ctype:`float` and :ctype:`double` are 32-bit and 64-bit IEEE floating
241-
point numbers, respectively. :ctype:`_Bool` is 1 byte.
242-
243-
Note the difference between ``'@'`` and ``'='``: both use native byte order, but
244-
the size and alignment of the latter is standardized.
245-
246-
The form ``'!'`` is available for those poor souls who claim they can't remember
247-
whether network byte order is big-endian or little-endian.
248-
249-
There is no way to indicate non-native byte order (force byte-swapping); use the
250-
appropriate choice of ``'<'`` or ``'>'``.
251-
252-
The ``'P'`` format character is only available for the native byte ordering
253-
(selected as the default or with the ``'@'`` byte order character). The byte
254-
order character ``'='`` chooses to use little- or big-endian ordering based on
255-
the host system. The struct module does not interpret this as native ordering,
256-
so the ``'P'`` format is not available.
257-
258-
Notes:
259-
260-
(1) Padding is only automatically added between successive structure members.
261-
No padding is added at the beginning or the end of the encoded struct.
262-
263-
(2) No padding is added when using non-native size and alignment, e.g.
264-
with '<', '>', '=', and '!'.
265-
266-
(3) To align the end of a structure to the alignment requirement of a
267-
particular type, end the format with the code for that type with a repeat
268-
count of zero. See :ref:`struct-examples`.
269-
270274

271275
.. _struct-examples:
272276

@@ -331,7 +335,7 @@ alignment does not enforce any alignment.
331335

332336
.. _struct-objects:
333337

334-
Objects
338+
Classes
335339
-------
336340

337341
The :mod:`struct` module also defines the following type:

0 commit comments

Comments
 (0)