blob: cc3017b5fcb3f13b4f077c75e14fcc585291aabd [file] [log] [blame]
Benjamin Peterson4ae19462008-07-31 15:03:40 +00001:mod:`struct` --- Interpret bytes as packed binary data
Georg Brandl7f01a132009-09-16 15:58:14 +00002=======================================================
Georg Brandl116aa622007-08-15 14:28:22 +00003
4.. module:: struct
Benjamin Peterson4ae19462008-07-31 15:03:40 +00005 :synopsis: Interpret bytes as packed binary data.
Georg Brandl116aa622007-08-15 14:28:22 +00006
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/struct.py`
8
Georg Brandl116aa622007-08-15 14:28:22 +00009.. index::
10 pair: C; structures
11 triple: packing; binary; data
12
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040013--------------
14
Georg Brandl116aa622007-08-15 14:28:22 +000015This module performs conversions between Python values and C structs represented
Mark Dickinson6abf1822010-04-12 21:00:59 +000016as Python :class:`bytes` objects. This can be used in handling binary data
17stored in files or from network connections, among other sources. It uses
18:ref:`struct-format-strings` as compact descriptions of the layout of the C
19structs and the intended conversion to/from Python values.
20
21.. note::
22
23 By default, the result of packing a given C struct includes pad bytes in
24 order to maintain proper alignment for the C types involved; similarly,
25 alignment is taken into account when unpacking. This behavior is chosen so
26 that the bytes of a packed struct correspond exactly to the layout in memory
Mark Dickinsoncb532f12010-06-15 08:42:37 +000027 of the corresponding C struct. To handle platform-independent data formats
Senthil Kumaran916bd382010-10-15 12:55:19 +000028 or omit implicit pad bytes, use ``standard`` size and alignment instead of
29 ``native`` size and alignment: see :ref:`struct-alignment` for details.
Mark Dickinson6abf1822010-04-12 21:00:59 +000030
Georg Brandlf30132f2014-10-31 09:46:41 +010031Several :mod:`struct` functions (and methods of :class:`Struct`) take a *buffer*
32argument. This refers to objects that implement the :ref:`bufferobjects` and
33provide either a readable or read-writable buffer. The most common types used
34for that purpose are :class:`bytes` and :class:`bytearray`, but many other types
35that can be viewed as an array of bytes implement the buffer protocol, so that
36they can be read/filled without additional copying from a :class:`bytes` object.
37
38
Mark Dickinson6abf1822010-04-12 21:00:59 +000039Functions and Exceptions
40------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000041
42The module defines the following exception and functions:
43
44
45.. exception:: error
46
Mark Dickinson6abf1822010-04-12 21:00:59 +000047 Exception raised on various occasions; argument is a string describing what
48 is wrong.
Georg Brandl116aa622007-08-15 14:28:22 +000049
50
51.. function:: pack(fmt, v1, v2, ...)
52
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000053 Return a bytes object containing the values *v1*, *v2*, ... packed according
54 to the format string *fmt*. The arguments must match the values required by
55 the format exactly.
Georg Brandl116aa622007-08-15 14:28:22 +000056
57
58.. function:: pack_into(fmt, buffer, offset, v1, v2, ...)
59
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000060 Pack the values *v1*, *v2*, ... according to the format string *fmt* and
61 write the packed bytes into the writable buffer *buffer* starting at
Georg Brandlf30132f2014-10-31 09:46:41 +010062 position *offset*. Note that *offset* is a required argument.
Georg Brandl116aa622007-08-15 14:28:22 +000063
Georg Brandl116aa622007-08-15 14:28:22 +000064
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000065.. function:: unpack(fmt, buffer)
Georg Brandl116aa622007-08-15 14:28:22 +000066
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000067 Unpack from the buffer *buffer* (presumably packed by ``pack(fmt, ...)``)
68 according to the format string *fmt*. The result is a tuple even if it
Martin Panterb0309912016-04-15 23:03:54 +000069 contains exactly one item. The buffer's size in bytes must match the
70 size required by the format, as reflected by :func:`calcsize`.
Georg Brandl116aa622007-08-15 14:28:22 +000071
72
Georg Brandl7f01a132009-09-16 15:58:14 +000073.. function:: unpack_from(fmt, buffer, offset=0)
Georg Brandl116aa622007-08-15 14:28:22 +000074
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000075 Unpack from *buffer* starting at position *offset*, according to the format
76 string *fmt*. The result is a tuple even if it contains exactly one
Martin Panterb0309912016-04-15 23:03:54 +000077 item. The buffer's size in bytes, minus *offset*, must be at least
78 the size required by the format, as reflected by :func:`calcsize`.
Georg Brandl116aa622007-08-15 14:28:22 +000079
Georg Brandl116aa622007-08-15 14:28:22 +000080
Antoine Pitrou9f146812013-04-27 00:20:04 +020081.. function:: iter_unpack(fmt, buffer)
82
83 Iteratively unpack from the buffer *buffer* according to the format
84 string *fmt*. This function returns an iterator which will read
85 equally-sized chunks from the buffer until all its contents have been
Martin Panterb0309912016-04-15 23:03:54 +000086 consumed. The buffer's size in bytes must be a multiple of the size
87 required by the format, as reflected by :func:`calcsize`.
Antoine Pitrou9f146812013-04-27 00:20:04 +020088
89 Each iteration yields a tuple as specified by the format string.
90
91 .. versionadded:: 3.4
92
93
Georg Brandl116aa622007-08-15 14:28:22 +000094.. function:: calcsize(fmt)
95
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000096 Return the size of the struct (and hence of the bytes object produced by
97 ``pack(fmt, ...)``) corresponding to the format string *fmt*.
Georg Brandl116aa622007-08-15 14:28:22 +000098
Mark Dickinson6abf1822010-04-12 21:00:59 +000099.. _struct-format-strings:
100
101Format Strings
102--------------
103
104Format strings are the mechanism used to specify the expected layout when
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000105packing and unpacking data. They are built up from :ref:`format-characters`,
106which specify the type of data being packed/unpacked. In addition, there are
107special characters for controlling the :ref:`struct-alignment`.
108
109
110.. _struct-alignment:
111
112Byte Order, Size, and Alignment
113^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
114
115By default, C types are represented in the machine's native format and byte
116order, and properly aligned by skipping pad bytes if necessary (according to the
117rules used by the C compiler).
118
119Alternatively, the first character of the format string can be used to indicate
120the byte order, size and alignment of the packed data, according to the
121following table:
122
Mark Dickinsoncb532f12010-06-15 08:42:37 +0000123+-----------+------------------------+----------+-----------+
124| Character | Byte order | Size | Alignment |
125+===========+========================+==========+===========+
126| ``@`` | native | native | native |
127+-----------+------------------------+----------+-----------+
128| ``=`` | native | standard | none |
129+-----------+------------------------+----------+-----------+
130| ``<`` | little-endian | standard | none |
131+-----------+------------------------+----------+-----------+
132| ``>`` | big-endian | standard | none |
133+-----------+------------------------+----------+-----------+
134| ``!`` | network (= big-endian) | standard | none |
135+-----------+------------------------+----------+-----------+
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000136
137If the first character is not one of these, ``'@'`` is assumed.
138
139Native byte order is big-endian or little-endian, depending on the host
140system. For example, Intel x86 and AMD64 (x86-64) are little-endian;
141Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature
142switchable endianness (bi-endian). Use ``sys.byteorder`` to check the
143endianness of your system.
144
145Native size and alignment are determined using the C compiler's
146``sizeof`` expression. This is always combined with native byte order.
147
Mark Dickinsoncb532f12010-06-15 08:42:37 +0000148Standard size depends only on the format character; see the table in
149the :ref:`format-characters` section.
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000150
151Note the difference between ``'@'`` and ``'='``: both use native byte order, but
152the size and alignment of the latter is standardized.
153
154The form ``'!'`` is available for those poor souls who claim they can't remember
155whether network byte order is big-endian or little-endian.
156
157There is no way to indicate non-native byte order (force byte-swapping); use the
158appropriate choice of ``'<'`` or ``'>'``.
159
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000160Notes:
161
162(1) Padding is only automatically added between successive structure members.
163 No padding is added at the beginning or the end of the encoded struct.
164
165(2) No padding is added when using non-native size and alignment, e.g.
166 with '<', '>', '=', and '!'.
167
168(3) To align the end of a structure to the alignment requirement of a
169 particular type, end the format with the code for that type with a repeat
170 count of zero. See :ref:`struct-examples`.
171
172
173.. _format-characters:
Mark Dickinson6abf1822010-04-12 21:00:59 +0000174
175Format Characters
176^^^^^^^^^^^^^^^^^
177
Georg Brandl116aa622007-08-15 14:28:22 +0000178Format characters have the following meaning; the conversion between C and
Mark Dickinson719e4e32010-06-29 20:10:42 +0000179Python values should be obvious given their types. The 'Standard size' column
180refers to the size of the packed value in bytes when using standard size; that
181is, when the format string starts with one of ``'<'``, ``'>'``, ``'!'`` or
182``'='``. When using native size, the size of the packed value is
183platform-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +0000184
Georg Brandl60203b42010-10-06 10:11:56 +0000185+--------+--------------------------+--------------------+----------------+------------+
186| Format | C Type | Python type | Standard size | Notes |
187+========+==========================+====================+================+============+
188| ``x`` | pad byte | no value | | |
189+--------+--------------------------+--------------------+----------------+------------+
190| ``c`` | :c:type:`char` | bytes of length 1 | 1 | |
191+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000192| ``b`` | :c:type:`signed char` | integer | 1 | \(1),\(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000193+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000194| ``B`` | :c:type:`unsigned char` | integer | 1 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000195+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000196| ``?`` | :c:type:`_Bool` | bool | 1 | \(1) |
Georg Brandl60203b42010-10-06 10:11:56 +0000197+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000198| ``h`` | :c:type:`short` | integer | 2 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000199+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000200| ``H`` | :c:type:`unsigned short` | integer | 2 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000201+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000202| ``i`` | :c:type:`int` | integer | 4 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000203+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000204| ``I`` | :c:type:`unsigned int` | integer | 4 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000205+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000206| ``l`` | :c:type:`long` | integer | 4 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000207+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000208| ``L`` | :c:type:`unsigned long` | integer | 4 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000209+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000210| ``q`` | :c:type:`long long` | integer | 8 | \(2), \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000211+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000212| ``Q`` | :c:type:`unsigned long | integer | 8 | \(2), \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000213| | long` | | | |
214+--------+--------------------------+--------------------+----------------+------------+
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200215| ``n`` | :c:type:`ssize_t` | integer | | \(4) |
Georg Brandl60203b42010-10-06 10:11:56 +0000216+--------+--------------------------+--------------------+----------------+------------+
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200217| ``N`` | :c:type:`size_t` | integer | | \(4) |
218+--------+--------------------------+--------------------+----------------+------------+
Mark Dickinson7c4e4092016-09-03 17:21:29 +0100219| ``e`` | \(7) | float | 2 | \(5) |
220+--------+--------------------------+--------------------+----------------+------------+
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200221| ``f`` | :c:type:`float` | float | 4 | \(5) |
222+--------+--------------------------+--------------------+----------------+------------+
223| ``d`` | :c:type:`double` | float | 8 | \(5) |
Georg Brandl60203b42010-10-06 10:11:56 +0000224+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000225| ``s`` | :c:type:`char[]` | bytes | | |
Georg Brandl60203b42010-10-06 10:11:56 +0000226+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000227| ``p`` | :c:type:`char[]` | bytes | | |
Georg Brandl60203b42010-10-06 10:11:56 +0000228+--------+--------------------------+--------------------+----------------+------------+
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200229| ``P`` | :c:type:`void \*` | integer | | \(6) |
Georg Brandl60203b42010-10-06 10:11:56 +0000230+--------+--------------------------+--------------------+----------------+------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000231
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200232.. versionchanged:: 3.3
233 Added support for the ``'n'`` and ``'N'`` formats.
234
Yury Selivanov3479b5f2016-11-10 13:25:26 -0500235.. versionchanged:: 3.6
236 Added support for the ``'e'`` format.
237
238
Georg Brandl116aa622007-08-15 14:28:22 +0000239Notes:
240
241(1)
Georg Brandl60203b42010-10-06 10:11:56 +0000242 The ``'?'`` conversion code corresponds to the :c:type:`_Bool` type defined by
243 C99. If this type is not available, it is simulated using a :c:type:`char`. In
Georg Brandl116aa622007-08-15 14:28:22 +0000244 standard mode, it is always represented by one byte.
245
Victor Stinnerda9ec992010-12-28 13:26:42 +0000246(2)
Georg Brandl116aa622007-08-15 14:28:22 +0000247 The ``'q'`` and ``'Q'`` conversion codes are available in native mode only if
Georg Brandl60203b42010-10-06 10:11:56 +0000248 the platform C compiler supports C :c:type:`long long`, or, on Windows,
249 :c:type:`__int64`. They are always available in standard modes.
Georg Brandl116aa622007-08-15 14:28:22 +0000250
Victor Stinnerda9ec992010-12-28 13:26:42 +0000251(3)
Mark Dickinsonc5935772010-04-03 15:54:36 +0000252 When attempting to pack a non-integer using any of the integer conversion
253 codes, if the non-integer has a :meth:`__index__` method then that method is
254 called to convert the argument to an integer before packing.
255
256 .. versionchanged:: 3.2
257 Use of the :meth:`__index__` method for non-integers is new in 3.2.
258
Victor Stinnerda9ec992010-12-28 13:26:42 +0000259(4)
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200260 The ``'n'`` and ``'N'`` conversion codes are only available for the native
261 size (selected as the default or with the ``'@'`` byte order character).
262 For the standard size, you can use whichever of the other integer formats
263 fits your application.
264
265(5)
Mark Dickinson7c4e4092016-09-03 17:21:29 +0100266 For the ``'f'``, ``'d'`` and ``'e'`` conversion codes, the packed
267 representation uses the IEEE 754 binary32, binary64 or binary16 format (for
268 ``'f'``, ``'d'`` or ``'e'`` respectively), regardless of the floating-point
269 format used by the platform.
Mark Dickinsoncb532f12010-06-15 08:42:37 +0000270
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200271(6)
Mark Dickinsoncb532f12010-06-15 08:42:37 +0000272 The ``'P'`` format character is only available for the native byte ordering
273 (selected as the default or with the ``'@'`` byte order character). The byte
274 order character ``'='`` chooses to use little- or big-endian ordering based
275 on the host system. The struct module does not interpret this as native
276 ordering, so the ``'P'`` format is not available.
277
Mark Dickinson7c4e4092016-09-03 17:21:29 +0100278(7)
279 The IEEE 754 binary16 "half precision" type was introduced in the 2008
280 revision of the `IEEE 754 standard <ieee 754 standard_>`_. It has a sign
281 bit, a 5-bit exponent and 11-bit precision (with 10 bits explicitly stored),
282 and can represent numbers between approximately ``6.1e-05`` and ``6.5e+04``
283 at full precision. This type is not widely supported by C compilers: on a
284 typical machine, an unsigned short can be used for storage, but not for math
285 operations. See the Wikipedia page on the `half-precision floating-point
286 format <half precision format_>`_ for more information.
287
Mark Dickinsonc5935772010-04-03 15:54:36 +0000288
Georg Brandl116aa622007-08-15 14:28:22 +0000289A format character may be preceded by an integral repeat count. For example,
290the format string ``'4h'`` means exactly the same as ``'hhhh'``.
291
292Whitespace characters between formats are ignored; a count and its format must
293not contain whitespace though.
294
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000295For the ``'s'`` format character, the count is interpreted as the length of the
296bytes, not a repeat count like for the other format characters; for example,
Georg Brandl116aa622007-08-15 14:28:22 +0000297``'10s'`` means a single 10-byte string, while ``'10c'`` means 10 characters.
Senthil Kumaranad3882a2011-07-17 17:29:17 +0800298If a count is not given, it defaults to 1. For packing, the string is
299truncated or padded with null bytes as appropriate to make it fit. For
300unpacking, the resulting bytes object always has exactly the specified number
301of bytes. As a special case, ``'0s'`` means a single, empty string (while
302``'0c'`` means 0 characters).
Georg Brandl116aa622007-08-15 14:28:22 +0000303
Mark Dickinsonb40b9472009-03-29 16:58:21 +0000304When packing a value ``x`` using one of the integer formats (``'b'``,
305``'B'``, ``'h'``, ``'H'``, ``'i'``, ``'I'``, ``'l'``, ``'L'``,
306``'q'``, ``'Q'``), if ``x`` is outside the valid range for that format
307then :exc:`struct.error` is raised.
308
309.. versionchanged:: 3.1
310 In 3.0, some of the integer formats wrapped out-of-range values and
311 raised :exc:`DeprecationWarning` instead of :exc:`struct.error`.
312
Georg Brandl116aa622007-08-15 14:28:22 +0000313The ``'p'`` format character encodes a "Pascal string", meaning a short
Georg Brandl93eb42e2010-07-10 10:23:40 +0000314variable-length string stored in a *fixed number of bytes*, given by the count.
315The first byte stored is the length of the string, or 255, whichever is
316smaller. The bytes of the string follow. If the string passed in to
317:func:`pack` is too long (longer than the count minus 1), only the leading
318``count-1`` bytes of the string are stored. If the string is shorter than
319``count-1``, it is padded with null bytes so that exactly count bytes in all
320are used. Note that for :func:`unpack`, the ``'p'`` format character consumes
321``count`` bytes, but that the string returned can never contain more than 255
322bytes.
Georg Brandl116aa622007-08-15 14:28:22 +0000323
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000324For the ``'?'`` format character, the return value is either :const:`True` or
Georg Brandl116aa622007-08-15 14:28:22 +0000325:const:`False`. When packing, the truth value of the argument object is used.
326Either 0 or 1 in the native or standard bool representation will be packed, and
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200327any non-zero value will be ``True`` when unpacking.
Georg Brandl116aa622007-08-15 14:28:22 +0000328
Mark Dickinson6abf1822010-04-12 21:00:59 +0000329
Mark Dickinson6abf1822010-04-12 21:00:59 +0000330
331.. _struct-examples:
332
333Examples
334^^^^^^^^
335
336.. note::
337 All examples assume a native byte order, size, and alignment with a
338 big-endian machine.
339
340A basic example of packing/unpacking three integers::
Georg Brandl116aa622007-08-15 14:28:22 +0000341
342 >>> from struct import *
343 >>> pack('hhl', 1, 2, 3)
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000344 b'\x00\x01\x00\x02\x00\x00\x00\x03'
345 >>> unpack('hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
Georg Brandl116aa622007-08-15 14:28:22 +0000346 (1, 2, 3)
347 >>> calcsize('hhl')
348 8
349
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000350Unpacked fields can be named by assigning them to variables or by wrapping
351the result in a named tuple::
352
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000353 >>> record = b'raymond \x32\x12\x08\x01\x08'
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000354 >>> name, serialnum, school, gradelevel = unpack('<10sHHb', record)
355
356 >>> from collections import namedtuple
357 >>> Student = namedtuple('Student', 'name serialnum school gradelevel')
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000358 >>> Student._make(unpack('<10sHHb', record))
359 Student(name=b'raymond ', serialnum=4658, school=264, gradelevel=8)
Georg Brandl116aa622007-08-15 14:28:22 +0000360
Mark Dickinson6abf1822010-04-12 21:00:59 +0000361The ordering of format characters may have an impact on size since the padding
362needed to satisfy alignment requirements is different::
363
Victor Stinnerda9ec992010-12-28 13:26:42 +0000364 >>> pack('ci', b'*', 0x12131415)
Mark Dickinson6abf1822010-04-12 21:00:59 +0000365 b'*\x00\x00\x00\x12\x13\x14\x15'
Victor Stinnerda9ec992010-12-28 13:26:42 +0000366 >>> pack('ic', 0x12131415, b'*')
Mark Dickinson6abf1822010-04-12 21:00:59 +0000367 b'\x12\x13\x14\x15*'
368 >>> calcsize('ci')
369 8
370 >>> calcsize('ic')
371 5
372
373The following format ``'llh0l'`` specifies two pad bytes at the end, assuming
374longs are aligned on 4-byte boundaries::
375
376 >>> pack('llh0l', 1, 2, 3)
377 b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00'
378
379This only works when native size and alignment are in effect; standard size and
380alignment does not enforce any alignment.
381
382
Georg Brandl116aa622007-08-15 14:28:22 +0000383.. seealso::
384
385 Module :mod:`array`
386 Packed binary storage of homogeneous data.
387
388 Module :mod:`xdrlib`
389 Packing and unpacking of XDR data.
390
391
392.. _struct-objects:
393
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000394Classes
Mark Dickinson6abf1822010-04-12 21:00:59 +0000395-------
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397The :mod:`struct` module also defines the following type:
398
399
400.. class:: Struct(format)
401
Mark Dickinson6abf1822010-04-12 21:00:59 +0000402 Return a new Struct object which writes and reads binary data according to
403 the format string *format*. Creating a Struct object once and calling its
404 methods is more efficient than calling the :mod:`struct` functions with the
405 same format since the format string only needs to be compiled once.
Georg Brandl116aa622007-08-15 14:28:22 +0000406
Georg Brandl116aa622007-08-15 14:28:22 +0000407
Benjamin Petersone41251e2008-04-25 01:59:09 +0000408 Compiled Struct objects support the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000409
Benjamin Petersone41251e2008-04-25 01:59:09 +0000410 .. method:: pack(v1, v2, ...)
Georg Brandl116aa622007-08-15 14:28:22 +0000411
Benjamin Petersone41251e2008-04-25 01:59:09 +0000412 Identical to the :func:`pack` function, using the compiled format.
Martin Panterb0309912016-04-15 23:03:54 +0000413 (``len(result)`` will equal :attr:`size`.)
Georg Brandl116aa622007-08-15 14:28:22 +0000414
415
Benjamin Petersone41251e2008-04-25 01:59:09 +0000416 .. method:: pack_into(buffer, offset, v1, v2, ...)
Georg Brandl116aa622007-08-15 14:28:22 +0000417
Benjamin Petersone41251e2008-04-25 01:59:09 +0000418 Identical to the :func:`pack_into` function, using the compiled format.
Georg Brandl116aa622007-08-15 14:28:22 +0000419
420
Mark Dickinsonfdb99f12010-06-12 16:30:53 +0000421 .. method:: unpack(buffer)
Georg Brandl116aa622007-08-15 14:28:22 +0000422
Benjamin Petersone41251e2008-04-25 01:59:09 +0000423 Identical to the :func:`unpack` function, using the compiled format.
Martin Panterb0309912016-04-15 23:03:54 +0000424 The buffer's size in bytes must equal :attr:`size`.
Georg Brandl116aa622007-08-15 14:28:22 +0000425
426
Georg Brandl7f01a132009-09-16 15:58:14 +0000427 .. method:: unpack_from(buffer, offset=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000428
Benjamin Petersone41251e2008-04-25 01:59:09 +0000429 Identical to the :func:`unpack_from` function, using the compiled format.
Martin Panterb0309912016-04-15 23:03:54 +0000430 The buffer's size in bytes, minus *offset*, must be at least
431 :attr:`size`.
Georg Brandl116aa622007-08-15 14:28:22 +0000432
433
Antoine Pitrou9f146812013-04-27 00:20:04 +0200434 .. method:: iter_unpack(buffer)
435
436 Identical to the :func:`iter_unpack` function, using the compiled format.
Martin Panterb0309912016-04-15 23:03:54 +0000437 The buffer's size in bytes must be a multiple of :attr:`size`.
Antoine Pitrou9f146812013-04-27 00:20:04 +0200438
439 .. versionadded:: 3.4
440
Benjamin Petersone41251e2008-04-25 01:59:09 +0000441 .. attribute:: format
Georg Brandl116aa622007-08-15 14:28:22 +0000442
Benjamin Petersone41251e2008-04-25 01:59:09 +0000443 The format string used to construct this Struct object.
Georg Brandl116aa622007-08-15 14:28:22 +0000444
Benjamin Petersone41251e2008-04-25 01:59:09 +0000445 .. attribute:: size
Guido van Rossum04110fb2007-08-24 16:32:05 +0000446
Mark Dickinsonfdb99f12010-06-12 16:30:53 +0000447 The calculated size of the struct (and hence of the bytes object produced
448 by the :meth:`pack` method) corresponding to :attr:`format`.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000449
Mark Dickinson7c4e4092016-09-03 17:21:29 +0100450
451.. _half precision format: https://en.wikipedia.org/wiki/Half-precision_floating-point_format
452
453.. _ieee 754 standard: https://en.wikipedia.org/wiki/IEEE_floating_point#IEEE_754-2008