blob: ec2e1beb03cd3bbe3b9f1736a63c5eb141bffe95 [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
7.. index::
8 pair: C; structures
9 triple: packing; binary; data
10
11This module performs conversions between Python values and C structs represented
Mark Dickinson6abf1822010-04-12 21:00:59 +000012as Python :class:`bytes` objects. This can be used in handling binary data
13stored in files or from network connections, among other sources. It uses
14:ref:`struct-format-strings` as compact descriptions of the layout of the C
15structs and the intended conversion to/from Python values.
16
17.. note::
18
19 By default, the result of packing a given C struct includes pad bytes in
20 order to maintain proper alignment for the C types involved; similarly,
21 alignment is taken into account when unpacking. This behavior is chosen so
22 that the bytes of a packed struct correspond exactly to the layout in memory
Mark Dickinsoncb532f12010-06-15 08:42:37 +000023 of the corresponding C struct. To handle platform-independent data formats
Senthil Kumaran916bd382010-10-15 12:55:19 +000024 or omit implicit pad bytes, use ``standard`` size and alignment instead of
25 ``native`` size and alignment: see :ref:`struct-alignment` for details.
Mark Dickinson6abf1822010-04-12 21:00:59 +000026
27Functions and Exceptions
28------------------------
Georg Brandl116aa622007-08-15 14:28:22 +000029
30The module defines the following exception and functions:
31
32
33.. exception:: error
34
Mark Dickinson6abf1822010-04-12 21:00:59 +000035 Exception raised on various occasions; argument is a string describing what
36 is wrong.
Georg Brandl116aa622007-08-15 14:28:22 +000037
38
39.. function:: pack(fmt, v1, v2, ...)
40
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000041 Return a bytes object containing the values *v1*, *v2*, ... packed according
42 to the format string *fmt*. The arguments must match the values required by
43 the format exactly.
Georg Brandl116aa622007-08-15 14:28:22 +000044
45
46.. function:: pack_into(fmt, buffer, offset, v1, v2, ...)
47
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000048 Pack the values *v1*, *v2*, ... according to the format string *fmt* and
49 write the packed bytes into the writable buffer *buffer* starting at
50 position *offset*. Note that *offset* is a required argument.
Georg Brandl116aa622007-08-15 14:28:22 +000051
Georg Brandl116aa622007-08-15 14:28:22 +000052
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000053.. function:: unpack(fmt, buffer)
Georg Brandl116aa622007-08-15 14:28:22 +000054
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000055 Unpack from the buffer *buffer* (presumably packed by ``pack(fmt, ...)``)
56 according to the format string *fmt*. The result is a tuple even if it
57 contains exactly one item. The buffer must contain exactly the amount of
58 data required by the format (``len(bytes)`` must equal ``calcsize(fmt)``).
Georg Brandl116aa622007-08-15 14:28:22 +000059
60
Georg Brandl7f01a132009-09-16 15:58:14 +000061.. function:: unpack_from(fmt, buffer, offset=0)
Georg Brandl116aa622007-08-15 14:28:22 +000062
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000063 Unpack from *buffer* starting at position *offset*, according to the format
64 string *fmt*. The result is a tuple even if it contains exactly one
65 item. *buffer* must contain at least the amount of data required by the
66 format (``len(buffer[offset:])`` must be at least ``calcsize(fmt)``).
Georg Brandl116aa622007-08-15 14:28:22 +000067
Georg Brandl116aa622007-08-15 14:28:22 +000068
Antoine Pitrou9f146812013-04-27 00:20:04 +020069.. function:: iter_unpack(fmt, buffer)
70
71 Iteratively unpack from the buffer *buffer* according to the format
72 string *fmt*. This function returns an iterator which will read
73 equally-sized chunks from the buffer until all its contents have been
74 consumed. The buffer's size in bytes must be a multiple of the amount
75 of data required by the format, as reflected by :func:`calcsize`.
76
77 Each iteration yields a tuple as specified by the format string.
78
79 .. versionadded:: 3.4
80
81
Georg Brandl116aa622007-08-15 14:28:22 +000082.. function:: calcsize(fmt)
83
Mark Dickinsonfdb99f12010-06-12 16:30:53 +000084 Return the size of the struct (and hence of the bytes object produced by
85 ``pack(fmt, ...)``) corresponding to the format string *fmt*.
Georg Brandl116aa622007-08-15 14:28:22 +000086
Mark Dickinson6abf1822010-04-12 21:00:59 +000087.. _struct-format-strings:
88
89Format Strings
90--------------
91
92Format strings are the mechanism used to specify the expected layout when
Mark Dickinsoncfd56f22010-06-12 18:37:54 +000093packing and unpacking data. They are built up from :ref:`format-characters`,
94which specify the type of data being packed/unpacked. In addition, there are
95special characters for controlling the :ref:`struct-alignment`.
96
97
98.. _struct-alignment:
99
100Byte Order, Size, and Alignment
101^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
102
103By default, C types are represented in the machine's native format and byte
104order, and properly aligned by skipping pad bytes if necessary (according to the
105rules used by the C compiler).
106
107Alternatively, the first character of the format string can be used to indicate
108the byte order, size and alignment of the packed data, according to the
109following table:
110
Mark Dickinsoncb532f12010-06-15 08:42:37 +0000111+-----------+------------------------+----------+-----------+
112| Character | Byte order | Size | Alignment |
113+===========+========================+==========+===========+
114| ``@`` | native | native | native |
115+-----------+------------------------+----------+-----------+
116| ``=`` | native | standard | none |
117+-----------+------------------------+----------+-----------+
118| ``<`` | little-endian | standard | none |
119+-----------+------------------------+----------+-----------+
120| ``>`` | big-endian | standard | none |
121+-----------+------------------------+----------+-----------+
122| ``!`` | network (= big-endian) | standard | none |
123+-----------+------------------------+----------+-----------+
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000124
125If the first character is not one of these, ``'@'`` is assumed.
126
127Native byte order is big-endian or little-endian, depending on the host
128system. For example, Intel x86 and AMD64 (x86-64) are little-endian;
129Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature
130switchable endianness (bi-endian). Use ``sys.byteorder`` to check the
131endianness of your system.
132
133Native size and alignment are determined using the C compiler's
134``sizeof`` expression. This is always combined with native byte order.
135
Mark Dickinsoncb532f12010-06-15 08:42:37 +0000136Standard size depends only on the format character; see the table in
137the :ref:`format-characters` section.
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000138
139Note the difference between ``'@'`` and ``'='``: both use native byte order, but
140the size and alignment of the latter is standardized.
141
142The form ``'!'`` is available for those poor souls who claim they can't remember
143whether network byte order is big-endian or little-endian.
144
145There is no way to indicate non-native byte order (force byte-swapping); use the
146appropriate choice of ``'<'`` or ``'>'``.
147
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000148Notes:
149
150(1) Padding is only automatically added between successive structure members.
151 No padding is added at the beginning or the end of the encoded struct.
152
153(2) No padding is added when using non-native size and alignment, e.g.
154 with '<', '>', '=', and '!'.
155
156(3) To align the end of a structure to the alignment requirement of a
157 particular type, end the format with the code for that type with a repeat
158 count of zero. See :ref:`struct-examples`.
159
160
161.. _format-characters:
Mark Dickinson6abf1822010-04-12 21:00:59 +0000162
163Format Characters
164^^^^^^^^^^^^^^^^^
165
Georg Brandl116aa622007-08-15 14:28:22 +0000166Format characters have the following meaning; the conversion between C and
Mark Dickinson719e4e32010-06-29 20:10:42 +0000167Python values should be obvious given their types. The 'Standard size' column
168refers to the size of the packed value in bytes when using standard size; that
169is, when the format string starts with one of ``'<'``, ``'>'``, ``'!'`` or
170``'='``. When using native size, the size of the packed value is
171platform-dependent.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
Georg Brandl60203b42010-10-06 10:11:56 +0000173+--------+--------------------------+--------------------+----------------+------------+
174| Format | C Type | Python type | Standard size | Notes |
175+========+==========================+====================+================+============+
176| ``x`` | pad byte | no value | | |
177+--------+--------------------------+--------------------+----------------+------------+
178| ``c`` | :c:type:`char` | bytes of length 1 | 1 | |
179+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000180| ``b`` | :c:type:`signed char` | integer | 1 | \(1),\(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000181+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000182| ``B`` | :c:type:`unsigned char` | integer | 1 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000183+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000184| ``?`` | :c:type:`_Bool` | bool | 1 | \(1) |
Georg Brandl60203b42010-10-06 10:11:56 +0000185+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000186| ``h`` | :c:type:`short` | integer | 2 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000187+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000188| ``H`` | :c:type:`unsigned short` | integer | 2 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000189+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000190| ``i`` | :c:type:`int` | integer | 4 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000191+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000192| ``I`` | :c:type:`unsigned int` | integer | 4 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000193+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000194| ``l`` | :c:type:`long` | integer | 4 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000195+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000196| ``L`` | :c:type:`unsigned long` | integer | 4 | \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000197+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000198| ``q`` | :c:type:`long long` | integer | 8 | \(2), \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000199+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000200| ``Q`` | :c:type:`unsigned long | integer | 8 | \(2), \(3) |
Georg Brandl60203b42010-10-06 10:11:56 +0000201| | long` | | | |
202+--------+--------------------------+--------------------+----------------+------------+
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200203| ``n`` | :c:type:`ssize_t` | integer | | \(4) |
Georg Brandl60203b42010-10-06 10:11:56 +0000204+--------+--------------------------+--------------------+----------------+------------+
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200205| ``N`` | :c:type:`size_t` | integer | | \(4) |
206+--------+--------------------------+--------------------+----------------+------------+
207| ``f`` | :c:type:`float` | float | 4 | \(5) |
208+--------+--------------------------+--------------------+----------------+------------+
209| ``d`` | :c:type:`double` | float | 8 | \(5) |
Georg Brandl60203b42010-10-06 10:11:56 +0000210+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000211| ``s`` | :c:type:`char[]` | bytes | | |
Georg Brandl60203b42010-10-06 10:11:56 +0000212+--------+--------------------------+--------------------+----------------+------------+
Victor Stinnerda9ec992010-12-28 13:26:42 +0000213| ``p`` | :c:type:`char[]` | bytes | | |
Georg Brandl60203b42010-10-06 10:11:56 +0000214+--------+--------------------------+--------------------+----------------+------------+
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200215| ``P`` | :c:type:`void \*` | integer | | \(6) |
Georg Brandl60203b42010-10-06 10:11:56 +0000216+--------+--------------------------+--------------------+----------------+------------+
Georg Brandl116aa622007-08-15 14:28:22 +0000217
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200218.. versionchanged:: 3.3
219 Added support for the ``'n'`` and ``'N'`` formats.
220
Georg Brandl116aa622007-08-15 14:28:22 +0000221Notes:
222
223(1)
Georg Brandl60203b42010-10-06 10:11:56 +0000224 The ``'?'`` conversion code corresponds to the :c:type:`_Bool` type defined by
225 C99. If this type is not available, it is simulated using a :c:type:`char`. In
Georg Brandl116aa622007-08-15 14:28:22 +0000226 standard mode, it is always represented by one byte.
227
Victor Stinnerda9ec992010-12-28 13:26:42 +0000228(2)
Georg Brandl116aa622007-08-15 14:28:22 +0000229 The ``'q'`` and ``'Q'`` conversion codes are available in native mode only if
Georg Brandl60203b42010-10-06 10:11:56 +0000230 the platform C compiler supports C :c:type:`long long`, or, on Windows,
231 :c:type:`__int64`. They are always available in standard modes.
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Victor Stinnerda9ec992010-12-28 13:26:42 +0000233(3)
Mark Dickinsonc5935772010-04-03 15:54:36 +0000234 When attempting to pack a non-integer using any of the integer conversion
235 codes, if the non-integer has a :meth:`__index__` method then that method is
236 called to convert the argument to an integer before packing.
237
238 .. versionchanged:: 3.2
239 Use of the :meth:`__index__` method for non-integers is new in 3.2.
240
Victor Stinnerda9ec992010-12-28 13:26:42 +0000241(4)
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200242 The ``'n'`` and ``'N'`` conversion codes are only available for the native
243 size (selected as the default or with the ``'@'`` byte order character).
244 For the standard size, you can use whichever of the other integer formats
245 fits your application.
246
247(5)
Mark Dickinsoncb532f12010-06-15 08:42:37 +0000248 For the ``'f'`` and ``'d'`` conversion codes, the packed representation uses
249 the IEEE 754 binary32 (for ``'f'``) or binary64 (for ``'d'``) format,
250 regardless of the floating-point format used by the platform.
251
Antoine Pitrou45d9c912011-10-06 15:27:40 +0200252(6)
Mark Dickinsoncb532f12010-06-15 08:42:37 +0000253 The ``'P'`` format character is only available for the native byte ordering
254 (selected as the default or with the ``'@'`` byte order character). The byte
255 order character ``'='`` chooses to use little- or big-endian ordering based
256 on the host system. The struct module does not interpret this as native
257 ordering, so the ``'P'`` format is not available.
258
Mark Dickinsonc5935772010-04-03 15:54:36 +0000259
Georg Brandl116aa622007-08-15 14:28:22 +0000260A format character may be preceded by an integral repeat count. For example,
261the format string ``'4h'`` means exactly the same as ``'hhhh'``.
262
263Whitespace characters between formats are ignored; a count and its format must
264not contain whitespace though.
265
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000266For the ``'s'`` format character, the count is interpreted as the length of the
267bytes, not a repeat count like for the other format characters; for example,
Georg Brandl116aa622007-08-15 14:28:22 +0000268``'10s'`` means a single 10-byte string, while ``'10c'`` means 10 characters.
Senthil Kumaranad3882a2011-07-17 17:29:17 +0800269If a count is not given, it defaults to 1. For packing, the string is
270truncated or padded with null bytes as appropriate to make it fit. For
271unpacking, the resulting bytes object always has exactly the specified number
272of bytes. As a special case, ``'0s'`` means a single, empty string (while
273``'0c'`` means 0 characters).
Georg Brandl116aa622007-08-15 14:28:22 +0000274
Mark Dickinsonb40b9472009-03-29 16:58:21 +0000275When packing a value ``x`` using one of the integer formats (``'b'``,
276``'B'``, ``'h'``, ``'H'``, ``'i'``, ``'I'``, ``'l'``, ``'L'``,
277``'q'``, ``'Q'``), if ``x`` is outside the valid range for that format
278then :exc:`struct.error` is raised.
279
280.. versionchanged:: 3.1
281 In 3.0, some of the integer formats wrapped out-of-range values and
282 raised :exc:`DeprecationWarning` instead of :exc:`struct.error`.
283
Georg Brandl116aa622007-08-15 14:28:22 +0000284The ``'p'`` format character encodes a "Pascal string", meaning a short
Georg Brandl93eb42e2010-07-10 10:23:40 +0000285variable-length string stored in a *fixed number of bytes*, given by the count.
286The first byte stored is the length of the string, or 255, whichever is
287smaller. The bytes of the string follow. If the string passed in to
288:func:`pack` is too long (longer than the count minus 1), only the leading
289``count-1`` bytes of the string are stored. If the string is shorter than
290``count-1``, it is padded with null bytes so that exactly count bytes in all
291are used. Note that for :func:`unpack`, the ``'p'`` format character consumes
292``count`` bytes, but that the string returned can never contain more than 255
293bytes.
Georg Brandl116aa622007-08-15 14:28:22 +0000294
Christian Heimesdd15f6c2008-03-16 00:07:10 +0000295For the ``'?'`` format character, the return value is either :const:`True` or
Georg Brandl116aa622007-08-15 14:28:22 +0000296:const:`False`. When packing, the truth value of the argument object is used.
297Either 0 or 1 in the native or standard bool representation will be packed, and
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200298any non-zero value will be ``True`` when unpacking.
Georg Brandl116aa622007-08-15 14:28:22 +0000299
Mark Dickinson6abf1822010-04-12 21:00:59 +0000300
Mark Dickinson6abf1822010-04-12 21:00:59 +0000301
302.. _struct-examples:
303
304Examples
305^^^^^^^^
306
307.. note::
308 All examples assume a native byte order, size, and alignment with a
309 big-endian machine.
310
311A basic example of packing/unpacking three integers::
Georg Brandl116aa622007-08-15 14:28:22 +0000312
313 >>> from struct import *
314 >>> pack('hhl', 1, 2, 3)
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000315 b'\x00\x01\x00\x02\x00\x00\x00\x03'
316 >>> unpack('hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03')
Georg Brandl116aa622007-08-15 14:28:22 +0000317 (1, 2, 3)
318 >>> calcsize('hhl')
319 8
320
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000321Unpacked fields can be named by assigning them to variables or by wrapping
322the result in a named tuple::
323
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000324 >>> record = b'raymond \x32\x12\x08\x01\x08'
Benjamin Peterson2b7411d2008-05-26 17:36:47 +0000325 >>> name, serialnum, school, gradelevel = unpack('<10sHHb', record)
326
327 >>> from collections import namedtuple
328 >>> Student = namedtuple('Student', 'name serialnum school gradelevel')
Benjamin Peterson4ae19462008-07-31 15:03:40 +0000329 >>> Student._make(unpack('<10sHHb', record))
330 Student(name=b'raymond ', serialnum=4658, school=264, gradelevel=8)
Georg Brandl116aa622007-08-15 14:28:22 +0000331
Mark Dickinson6abf1822010-04-12 21:00:59 +0000332The ordering of format characters may have an impact on size since the padding
333needed to satisfy alignment requirements is different::
334
Victor Stinnerda9ec992010-12-28 13:26:42 +0000335 >>> pack('ci', b'*', 0x12131415)
Mark Dickinson6abf1822010-04-12 21:00:59 +0000336 b'*\x00\x00\x00\x12\x13\x14\x15'
Victor Stinnerda9ec992010-12-28 13:26:42 +0000337 >>> pack('ic', 0x12131415, b'*')
Mark Dickinson6abf1822010-04-12 21:00:59 +0000338 b'\x12\x13\x14\x15*'
339 >>> calcsize('ci')
340 8
341 >>> calcsize('ic')
342 5
343
344The following format ``'llh0l'`` specifies two pad bytes at the end, assuming
345longs are aligned on 4-byte boundaries::
346
347 >>> pack('llh0l', 1, 2, 3)
348 b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00'
349
350This only works when native size and alignment are in effect; standard size and
351alignment does not enforce any alignment.
352
353
Georg Brandl116aa622007-08-15 14:28:22 +0000354.. seealso::
355
356 Module :mod:`array`
357 Packed binary storage of homogeneous data.
358
359 Module :mod:`xdrlib`
360 Packing and unpacking of XDR data.
361
362
363.. _struct-objects:
364
Mark Dickinsoncfd56f22010-06-12 18:37:54 +0000365Classes
Mark Dickinson6abf1822010-04-12 21:00:59 +0000366-------
Georg Brandl116aa622007-08-15 14:28:22 +0000367
368The :mod:`struct` module also defines the following type:
369
370
371.. class:: Struct(format)
372
Mark Dickinson6abf1822010-04-12 21:00:59 +0000373 Return a new Struct object which writes and reads binary data according to
374 the format string *format*. Creating a Struct object once and calling its
375 methods is more efficient than calling the :mod:`struct` functions with the
376 same format since the format string only needs to be compiled once.
Georg Brandl116aa622007-08-15 14:28:22 +0000377
Georg Brandl116aa622007-08-15 14:28:22 +0000378
Benjamin Petersone41251e2008-04-25 01:59:09 +0000379 Compiled Struct objects support the following methods and attributes:
Georg Brandl116aa622007-08-15 14:28:22 +0000380
Benjamin Petersone41251e2008-04-25 01:59:09 +0000381 .. method:: pack(v1, v2, ...)
Georg Brandl116aa622007-08-15 14:28:22 +0000382
Benjamin Petersone41251e2008-04-25 01:59:09 +0000383 Identical to the :func:`pack` function, using the compiled format.
384 (``len(result)`` will equal :attr:`self.size`.)
Georg Brandl116aa622007-08-15 14:28:22 +0000385
386
Benjamin Petersone41251e2008-04-25 01:59:09 +0000387 .. method:: pack_into(buffer, offset, v1, v2, ...)
Georg Brandl116aa622007-08-15 14:28:22 +0000388
Benjamin Petersone41251e2008-04-25 01:59:09 +0000389 Identical to the :func:`pack_into` function, using the compiled format.
Georg Brandl116aa622007-08-15 14:28:22 +0000390
391
Mark Dickinsonfdb99f12010-06-12 16:30:53 +0000392 .. method:: unpack(buffer)
Georg Brandl116aa622007-08-15 14:28:22 +0000393
Benjamin Petersone41251e2008-04-25 01:59:09 +0000394 Identical to the :func:`unpack` function, using the compiled format.
Mark Dickinsonfdb99f12010-06-12 16:30:53 +0000395 (``len(buffer)`` must equal :attr:`self.size`).
Georg Brandl116aa622007-08-15 14:28:22 +0000396
397
Georg Brandl7f01a132009-09-16 15:58:14 +0000398 .. method:: unpack_from(buffer, offset=0)
Georg Brandl116aa622007-08-15 14:28:22 +0000399
Benjamin Petersone41251e2008-04-25 01:59:09 +0000400 Identical to the :func:`unpack_from` function, using the compiled format.
401 (``len(buffer[offset:])`` must be at least :attr:`self.size`).
Georg Brandl116aa622007-08-15 14:28:22 +0000402
403
Antoine Pitrou9f146812013-04-27 00:20:04 +0200404 .. method:: iter_unpack(buffer)
405
406 Identical to the :func:`iter_unpack` function, using the compiled format.
407 (``len(buffer)`` must be a multiple of :attr:`self.size`).
408
409 .. versionadded:: 3.4
410
Benjamin Petersone41251e2008-04-25 01:59:09 +0000411 .. attribute:: format
Georg Brandl116aa622007-08-15 14:28:22 +0000412
Benjamin Petersone41251e2008-04-25 01:59:09 +0000413 The format string used to construct this Struct object.
Georg Brandl116aa622007-08-15 14:28:22 +0000414
Benjamin Petersone41251e2008-04-25 01:59:09 +0000415 .. attribute:: size
Guido van Rossum04110fb2007-08-24 16:32:05 +0000416
Mark Dickinsonfdb99f12010-06-12 16:30:53 +0000417 The calculated size of the struct (and hence of the bytes object produced
418 by the :meth:`pack` method) corresponding to :attr:`format`.
Guido van Rossum04110fb2007-08-24 16:32:05 +0000419