Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit cb532f1

Browse files
committed
Merged revisions 81992 via svnmerge from
svn+ssh://[email protected]/python/trunk ........ r81992 | mark.dickinson | 2010-06-15 09:33:03 +0100 (Tue, 15 Jun 2010) | 3 lines Issue #8469: Further clarifications and improvements to struct module documentation. Thanks Mads Kiilerich. ........
1 parent 4507190 commit cb532f1

1 file changed

Lines changed: 33 additions & 30 deletions

File tree

Doc/library/struct.rst

Lines changed: 33 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,9 @@ structs and the intended conversion to/from Python values.
2020
order to maintain proper alignment for the C types involved; similarly,
2121
alignment is taken into account when unpacking. This behavior is chosen so
2222
that the bytes of a packed struct correspond exactly to the layout in memory
23-
of the corresponding C struct. To omit pad bytes, use `standard` size and
24-
alignment instead of `native` size and alignment: see :ref:`struct-alignment`
25-
for details.
23+
of the corresponding C struct. To handle platform-independent data formats
24+
or omit implicit pad bytes, use `standard` size and alignment instead of
25+
`native` size and alignment: see :ref:`struct-alignment` for details.
2626

2727
Functions and Exceptions
2828
------------------------
@@ -95,19 +95,19 @@ Alternatively, the first character of the format string can be used to indicate
9595
the byte order, size and alignment of the packed data, according to the
9696
following table:
9797

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-
+-----------+------------------------+--------------------+
98+
+-----------+------------------------+----------+-----------+
99+
| Character | Byte order | Size | Alignment |
100+
+===========+========================+==========+===========+
101+
| ``@`` | native | native | native |
102+
+-----------+------------------------+----------+-----------+
103+
| ``=`` | native | standard | none |
104+
+-----------+------------------------+----------+-----------+
105+
| ``<`` | little-endian | standard | none |
106+
+-----------+------------------------+----------+-----------+
107+
| ``>`` | big-endian | standard | none |
108+
+-----------+------------------------+----------+-----------+
109+
| ``!`` | network (= big-endian) | standard | none |
110+
+-----------+------------------------+----------+-----------+
111111

112112
If the first character is not one of these, ``'@'`` is assumed.
113113

@@ -120,11 +120,8 @@ endianness of your system.
120120
Native size and alignment are determined using the C compiler's
121121
``sizeof`` expression. This is always combined with native byte order.
122122

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.
123+
Standard size depends only on the format character; see the table in
124+
the :ref:`format-characters` section.
128125

129126
Note the difference between ``'@'`` and ``'='``: both use native byte order, but
130127
the size and alignment of the latter is standardized.
@@ -135,12 +132,6 @@ whether network byte order is big-endian or little-endian.
135132
There is no way to indicate non-native byte order (force byte-swapping); use the
136133
appropriate choice of ``'<'`` or ``'>'``.
137134

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-
144135
Notes:
145136

146137
(1) Padding is only automatically added between successive structure members.
@@ -192,15 +183,15 @@ Python values should be obvious given their types:
192183
| ``Q`` | :ctype:`unsigned long | integer | 8 | \(3), \(4) |
193184
| | long` | | | |
194185
+--------+-------------------------+--------------------+----------------+------------+
195-
| ``f`` | :ctype:`float` | float | 4 | |
186+
| ``f`` | :ctype:`float` | float | 4 | \(5) |
196187
+--------+-------------------------+--------------------+----------------+------------+
197-
| ``d`` | :ctype:`double` | float | 8 | |
188+
| ``d`` | :ctype:`double` | float | 8 | \(5) |
198189
+--------+-------------------------+--------------------+----------------+------------+
199190
| ``s`` | :ctype:`char[]` | bytes | | \(1) |
200191
+--------+-------------------------+--------------------+----------------+------------+
201192
| ``p`` | :ctype:`char[]` | bytes | | \(1) |
202193
+--------+-------------------------+--------------------+----------------+------------+
203-
| ``P`` | :ctype:`void \*` | integer | | |
194+
| ``P`` | :ctype:`void \*` | integer | | \(6) |
204195
+--------+-------------------------+--------------------+----------------+------------+
205196

206197
Notes:
@@ -228,6 +219,18 @@ Notes:
228219
.. versionchanged:: 3.2
229220
Use of the :meth:`__index__` method for non-integers is new in 3.2.
230221

222+
(5)
223+
For the ``'f'`` and ``'d'`` conversion codes, the packed representation uses
224+
the IEEE 754 binary32 (for ``'f'``) or binary64 (for ``'d'``) format,
225+
regardless of the floating-point format used by the platform.
226+
227+
(6)
228+
The ``'P'`` format character is only available for the native byte ordering
229+
(selected as the default or with the ``'@'`` byte order character). The byte
230+
order character ``'='`` chooses to use little- or big-endian ordering based
231+
on the host system. The struct module does not interpret this as native
232+
ordering, so the ``'P'`` format is not available.
233+
231234

232235
A format character may be preceded by an integral repeat count. For example,
233236
the format string ``'4h'`` means exactly the same as ``'hhhh'``.

0 commit comments

Comments
 (0)