Benjamin Peterson | 4ae1946 | 2008-07-31 15:03:40 +0000 | [diff] [blame] | 1 | :mod:`struct` --- Interpret bytes as packed binary data |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 2 | ======================================================= |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 3 | |
| 4 | .. module:: struct |
Benjamin Peterson | 4ae1946 | 2008-07-31 15:03:40 +0000 | [diff] [blame] | 5 | :synopsis: Interpret bytes as packed binary data. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 6 | |
| 7 | .. index:: |
| 8 | pair: C; structures |
| 9 | triple: packing; binary; data |
| 10 | |
| 11 | This module performs conversions between Python values and C structs represented |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 12 | as Python :class:`bytes` objects. This can be used in handling binary data |
| 13 | stored 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 |
| 15 | structs 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 Dickinson | cb532f1 | 2010-06-15 08:42:37 +0000 | [diff] [blame] | 23 | of the corresponding C struct. To handle platform-independent data formats |
Senthil Kumaran | 916bd38 | 2010-10-15 12:55:19 +0000 | [diff] [blame] | 24 | or omit implicit pad bytes, use ``standard`` size and alignment instead of |
| 25 | ``native`` size and alignment: see :ref:`struct-alignment` for details. |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 26 | |
Georg Brandl | f30132f | 2014-10-31 09:46:41 +0100 | [diff] [blame] | 27 | Several :mod:`struct` functions (and methods of :class:`Struct`) take a *buffer* |
| 28 | argument. This refers to objects that implement the :ref:`bufferobjects` and |
| 29 | provide either a readable or read-writable buffer. The most common types used |
| 30 | for that purpose are :class:`bytes` and :class:`bytearray`, but many other types |
| 31 | that can be viewed as an array of bytes implement the buffer protocol, so that |
| 32 | they can be read/filled without additional copying from a :class:`bytes` object. |
| 33 | |
| 34 | |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 35 | Functions and Exceptions |
| 36 | ------------------------ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 37 | |
| 38 | The module defines the following exception and functions: |
| 39 | |
| 40 | |
| 41 | .. exception:: error |
| 42 | |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 43 | Exception raised on various occasions; argument is a string describing what |
| 44 | is wrong. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 45 | |
| 46 | |
| 47 | .. function:: pack(fmt, v1, v2, ...) |
| 48 | |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 49 | Return a bytes object containing the values *v1*, *v2*, ... packed according |
| 50 | to the format string *fmt*. The arguments must match the values required by |
| 51 | the format exactly. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 52 | |
| 53 | |
| 54 | .. function:: pack_into(fmt, buffer, offset, v1, v2, ...) |
| 55 | |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 56 | Pack the values *v1*, *v2*, ... according to the format string *fmt* and |
| 57 | write the packed bytes into the writable buffer *buffer* starting at |
Georg Brandl | f30132f | 2014-10-31 09:46:41 +0100 | [diff] [blame] | 58 | position *offset*. Note that *offset* is a required argument. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 59 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 60 | |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 61 | .. function:: unpack(fmt, buffer) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 62 | |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 63 | Unpack from the buffer *buffer* (presumably packed by ``pack(fmt, ...)``) |
| 64 | according to the format string *fmt*. The result is a tuple even if it |
| 65 | contains exactly one item. The buffer must contain exactly the amount of |
| 66 | data required by the format (``len(bytes)`` must equal ``calcsize(fmt)``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 67 | |
| 68 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 69 | .. function:: unpack_from(fmt, buffer, offset=0) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 71 | Unpack from *buffer* starting at position *offset*, according to the format |
| 72 | string *fmt*. The result is a tuple even if it contains exactly one |
| 73 | item. *buffer* must contain at least the amount of data required by the |
| 74 | format (``len(buffer[offset:])`` must be at least ``calcsize(fmt)``). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 75 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 76 | |
Antoine Pitrou | 9f14681 | 2013-04-27 00:20:04 +0200 | [diff] [blame] | 77 | .. function:: iter_unpack(fmt, buffer) |
| 78 | |
| 79 | Iteratively unpack from the buffer *buffer* according to the format |
| 80 | string *fmt*. This function returns an iterator which will read |
| 81 | equally-sized chunks from the buffer until all its contents have been |
| 82 | consumed. The buffer's size in bytes must be a multiple of the amount |
| 83 | of data required by the format, as reflected by :func:`calcsize`. |
| 84 | |
| 85 | Each iteration yields a tuple as specified by the format string. |
| 86 | |
| 87 | .. versionadded:: 3.4 |
| 88 | |
| 89 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 90 | .. function:: calcsize(fmt) |
| 91 | |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 92 | Return the size of the struct (and hence of the bytes object produced by |
| 93 | ``pack(fmt, ...)``) corresponding to the format string *fmt*. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 94 | |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 95 | .. _struct-format-strings: |
| 96 | |
| 97 | Format Strings |
| 98 | -------------- |
| 99 | |
| 100 | Format strings are the mechanism used to specify the expected layout when |
Mark Dickinson | cfd56f2 | 2010-06-12 18:37:54 +0000 | [diff] [blame] | 101 | packing and unpacking data. They are built up from :ref:`format-characters`, |
| 102 | which specify the type of data being packed/unpacked. In addition, there are |
| 103 | special characters for controlling the :ref:`struct-alignment`. |
| 104 | |
| 105 | |
| 106 | .. _struct-alignment: |
| 107 | |
| 108 | Byte Order, Size, and Alignment |
| 109 | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ |
| 110 | |
| 111 | By default, C types are represented in the machine's native format and byte |
| 112 | order, and properly aligned by skipping pad bytes if necessary (according to the |
| 113 | rules used by the C compiler). |
| 114 | |
| 115 | Alternatively, the first character of the format string can be used to indicate |
| 116 | the byte order, size and alignment of the packed data, according to the |
| 117 | following table: |
| 118 | |
Mark Dickinson | cb532f1 | 2010-06-15 08:42:37 +0000 | [diff] [blame] | 119 | +-----------+------------------------+----------+-----------+ |
| 120 | | Character | Byte order | Size | Alignment | |
| 121 | +===========+========================+==========+===========+ |
| 122 | | ``@`` | native | native | native | |
| 123 | +-----------+------------------------+----------+-----------+ |
| 124 | | ``=`` | native | standard | none | |
| 125 | +-----------+------------------------+----------+-----------+ |
| 126 | | ``<`` | little-endian | standard | none | |
| 127 | +-----------+------------------------+----------+-----------+ |
| 128 | | ``>`` | big-endian | standard | none | |
| 129 | +-----------+------------------------+----------+-----------+ |
| 130 | | ``!`` | network (= big-endian) | standard | none | |
| 131 | +-----------+------------------------+----------+-----------+ |
Mark Dickinson | cfd56f2 | 2010-06-12 18:37:54 +0000 | [diff] [blame] | 132 | |
| 133 | If the first character is not one of these, ``'@'`` is assumed. |
| 134 | |
| 135 | Native byte order is big-endian or little-endian, depending on the host |
| 136 | system. For example, Intel x86 and AMD64 (x86-64) are little-endian; |
| 137 | Motorola 68000 and PowerPC G5 are big-endian; ARM and Intel Itanium feature |
| 138 | switchable endianness (bi-endian). Use ``sys.byteorder`` to check the |
| 139 | endianness of your system. |
| 140 | |
| 141 | Native size and alignment are determined using the C compiler's |
| 142 | ``sizeof`` expression. This is always combined with native byte order. |
| 143 | |
Mark Dickinson | cb532f1 | 2010-06-15 08:42:37 +0000 | [diff] [blame] | 144 | Standard size depends only on the format character; see the table in |
| 145 | the :ref:`format-characters` section. |
Mark Dickinson | cfd56f2 | 2010-06-12 18:37:54 +0000 | [diff] [blame] | 146 | |
| 147 | Note the difference between ``'@'`` and ``'='``: both use native byte order, but |
| 148 | the size and alignment of the latter is standardized. |
| 149 | |
| 150 | The form ``'!'`` is available for those poor souls who claim they can't remember |
| 151 | whether network byte order is big-endian or little-endian. |
| 152 | |
| 153 | There is no way to indicate non-native byte order (force byte-swapping); use the |
| 154 | appropriate choice of ``'<'`` or ``'>'``. |
| 155 | |
Mark Dickinson | cfd56f2 | 2010-06-12 18:37:54 +0000 | [diff] [blame] | 156 | Notes: |
| 157 | |
| 158 | (1) Padding is only automatically added between successive structure members. |
| 159 | No padding is added at the beginning or the end of the encoded struct. |
| 160 | |
| 161 | (2) No padding is added when using non-native size and alignment, e.g. |
| 162 | with '<', '>', '=', and '!'. |
| 163 | |
| 164 | (3) To align the end of a structure to the alignment requirement of a |
| 165 | particular type, end the format with the code for that type with a repeat |
| 166 | count of zero. See :ref:`struct-examples`. |
| 167 | |
| 168 | |
| 169 | .. _format-characters: |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 170 | |
| 171 | Format Characters |
| 172 | ^^^^^^^^^^^^^^^^^ |
| 173 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 174 | Format characters have the following meaning; the conversion between C and |
Mark Dickinson | 719e4e3 | 2010-06-29 20:10:42 +0000 | [diff] [blame] | 175 | Python values should be obvious given their types. The 'Standard size' column |
| 176 | refers to the size of the packed value in bytes when using standard size; that |
| 177 | is, when the format string starts with one of ``'<'``, ``'>'``, ``'!'`` or |
| 178 | ``'='``. When using native size, the size of the packed value is |
| 179 | platform-dependent. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 180 | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 181 | +--------+--------------------------+--------------------+----------------+------------+ |
| 182 | | Format | C Type | Python type | Standard size | Notes | |
| 183 | +========+==========================+====================+================+============+ |
| 184 | | ``x`` | pad byte | no value | | | |
| 185 | +--------+--------------------------+--------------------+----------------+------------+ |
| 186 | | ``c`` | :c:type:`char` | bytes of length 1 | 1 | | |
| 187 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 188 | | ``b`` | :c:type:`signed char` | integer | 1 | \(1),\(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 189 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 190 | | ``B`` | :c:type:`unsigned char` | integer | 1 | \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 191 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 192 | | ``?`` | :c:type:`_Bool` | bool | 1 | \(1) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 193 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 194 | | ``h`` | :c:type:`short` | integer | 2 | \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 195 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 196 | | ``H`` | :c:type:`unsigned short` | integer | 2 | \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 197 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 198 | | ``i`` | :c:type:`int` | integer | 4 | \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 199 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 200 | | ``I`` | :c:type:`unsigned int` | integer | 4 | \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 201 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 202 | | ``l`` | :c:type:`long` | integer | 4 | \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 203 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 204 | | ``L`` | :c:type:`unsigned long` | integer | 4 | \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 205 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 206 | | ``q`` | :c:type:`long long` | integer | 8 | \(2), \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 207 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 208 | | ``Q`` | :c:type:`unsigned long | integer | 8 | \(2), \(3) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 209 | | | long` | | | | |
| 210 | +--------+--------------------------+--------------------+----------------+------------+ |
Antoine Pitrou | 45d9c91 | 2011-10-06 15:27:40 +0200 | [diff] [blame] | 211 | | ``n`` | :c:type:`ssize_t` | integer | | \(4) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 212 | +--------+--------------------------+--------------------+----------------+------------+ |
Antoine Pitrou | 45d9c91 | 2011-10-06 15:27:40 +0200 | [diff] [blame] | 213 | | ``N`` | :c:type:`size_t` | integer | | \(4) | |
| 214 | +--------+--------------------------+--------------------+----------------+------------+ |
| 215 | | ``f`` | :c:type:`float` | float | 4 | \(5) | |
| 216 | +--------+--------------------------+--------------------+----------------+------------+ |
| 217 | | ``d`` | :c:type:`double` | float | 8 | \(5) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 218 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 219 | | ``s`` | :c:type:`char[]` | bytes | | | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 220 | +--------+--------------------------+--------------------+----------------+------------+ |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 221 | | ``p`` | :c:type:`char[]` | bytes | | | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 222 | +--------+--------------------------+--------------------+----------------+------------+ |
Antoine Pitrou | 45d9c91 | 2011-10-06 15:27:40 +0200 | [diff] [blame] | 223 | | ``P`` | :c:type:`void \*` | integer | | \(6) | |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 224 | +--------+--------------------------+--------------------+----------------+------------+ |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 225 | |
Antoine Pitrou | 45d9c91 | 2011-10-06 15:27:40 +0200 | [diff] [blame] | 226 | .. versionchanged:: 3.3 |
| 227 | Added support for the ``'n'`` and ``'N'`` formats. |
| 228 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 229 | Notes: |
| 230 | |
| 231 | (1) |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 232 | The ``'?'`` conversion code corresponds to the :c:type:`_Bool` type defined by |
| 233 | C99. If this type is not available, it is simulated using a :c:type:`char`. In |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 234 | standard mode, it is always represented by one byte. |
| 235 | |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 236 | (2) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 237 | The ``'q'`` and ``'Q'`` conversion codes are available in native mode only if |
Georg Brandl | 60203b4 | 2010-10-06 10:11:56 +0000 | [diff] [blame] | 238 | the platform C compiler supports C :c:type:`long long`, or, on Windows, |
| 239 | :c:type:`__int64`. They are always available in standard modes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 240 | |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 241 | (3) |
Mark Dickinson | c593577 | 2010-04-03 15:54:36 +0000 | [diff] [blame] | 242 | When attempting to pack a non-integer using any of the integer conversion |
| 243 | codes, if the non-integer has a :meth:`__index__` method then that method is |
| 244 | called to convert the argument to an integer before packing. |
| 245 | |
| 246 | .. versionchanged:: 3.2 |
| 247 | Use of the :meth:`__index__` method for non-integers is new in 3.2. |
| 248 | |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 249 | (4) |
Antoine Pitrou | 45d9c91 | 2011-10-06 15:27:40 +0200 | [diff] [blame] | 250 | The ``'n'`` and ``'N'`` conversion codes are only available for the native |
| 251 | size (selected as the default or with the ``'@'`` byte order character). |
| 252 | For the standard size, you can use whichever of the other integer formats |
| 253 | fits your application. |
| 254 | |
| 255 | (5) |
Mark Dickinson | cb532f1 | 2010-06-15 08:42:37 +0000 | [diff] [blame] | 256 | For the ``'f'`` and ``'d'`` conversion codes, the packed representation uses |
| 257 | the IEEE 754 binary32 (for ``'f'``) or binary64 (for ``'d'``) format, |
| 258 | regardless of the floating-point format used by the platform. |
| 259 | |
Antoine Pitrou | 45d9c91 | 2011-10-06 15:27:40 +0200 | [diff] [blame] | 260 | (6) |
Mark Dickinson | cb532f1 | 2010-06-15 08:42:37 +0000 | [diff] [blame] | 261 | The ``'P'`` format character is only available for the native byte ordering |
| 262 | (selected as the default or with the ``'@'`` byte order character). The byte |
| 263 | order character ``'='`` chooses to use little- or big-endian ordering based |
| 264 | on the host system. The struct module does not interpret this as native |
| 265 | ordering, so the ``'P'`` format is not available. |
| 266 | |
Mark Dickinson | c593577 | 2010-04-03 15:54:36 +0000 | [diff] [blame] | 267 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 268 | A format character may be preceded by an integral repeat count. For example, |
| 269 | the format string ``'4h'`` means exactly the same as ``'hhhh'``. |
| 270 | |
| 271 | Whitespace characters between formats are ignored; a count and its format must |
| 272 | not contain whitespace though. |
| 273 | |
Benjamin Peterson | 4ae1946 | 2008-07-31 15:03:40 +0000 | [diff] [blame] | 274 | For the ``'s'`` format character, the count is interpreted as the length of the |
| 275 | bytes, not a repeat count like for the other format characters; for example, |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 276 | ``'10s'`` means a single 10-byte string, while ``'10c'`` means 10 characters. |
Senthil Kumaran | ad3882a | 2011-07-17 17:29:17 +0800 | [diff] [blame] | 277 | If a count is not given, it defaults to 1. For packing, the string is |
| 278 | truncated or padded with null bytes as appropriate to make it fit. For |
| 279 | unpacking, the resulting bytes object always has exactly the specified number |
| 280 | of bytes. As a special case, ``'0s'`` means a single, empty string (while |
| 281 | ``'0c'`` means 0 characters). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 282 | |
Mark Dickinson | b40b947 | 2009-03-29 16:58:21 +0000 | [diff] [blame] | 283 | When packing a value ``x`` using one of the integer formats (``'b'``, |
| 284 | ``'B'``, ``'h'``, ``'H'``, ``'i'``, ``'I'``, ``'l'``, ``'L'``, |
| 285 | ``'q'``, ``'Q'``), if ``x`` is outside the valid range for that format |
| 286 | then :exc:`struct.error` is raised. |
| 287 | |
| 288 | .. versionchanged:: 3.1 |
| 289 | In 3.0, some of the integer formats wrapped out-of-range values and |
| 290 | raised :exc:`DeprecationWarning` instead of :exc:`struct.error`. |
| 291 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 292 | The ``'p'`` format character encodes a "Pascal string", meaning a short |
Georg Brandl | 93eb42e | 2010-07-10 10:23:40 +0000 | [diff] [blame] | 293 | variable-length string stored in a *fixed number of bytes*, given by the count. |
| 294 | The first byte stored is the length of the string, or 255, whichever is |
| 295 | smaller. The bytes of the string follow. If the string passed in to |
| 296 | :func:`pack` is too long (longer than the count minus 1), only the leading |
| 297 | ``count-1`` bytes of the string are stored. If the string is shorter than |
| 298 | ``count-1``, it is padded with null bytes so that exactly count bytes in all |
| 299 | are used. Note that for :func:`unpack`, the ``'p'`` format character consumes |
| 300 | ``count`` bytes, but that the string returned can never contain more than 255 |
| 301 | bytes. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 302 | |
Christian Heimes | dd15f6c | 2008-03-16 00:07:10 +0000 | [diff] [blame] | 303 | For the ``'?'`` format character, the return value is either :const:`True` or |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 304 | :const:`False`. When packing, the truth value of the argument object is used. |
| 305 | Either 0 or 1 in the native or standard bool representation will be packed, and |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 306 | any non-zero value will be ``True`` when unpacking. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 307 | |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 308 | |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 309 | |
| 310 | .. _struct-examples: |
| 311 | |
| 312 | Examples |
| 313 | ^^^^^^^^ |
| 314 | |
| 315 | .. note:: |
| 316 | All examples assume a native byte order, size, and alignment with a |
| 317 | big-endian machine. |
| 318 | |
| 319 | A basic example of packing/unpacking three integers:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | |
| 321 | >>> from struct import * |
| 322 | >>> pack('hhl', 1, 2, 3) |
Benjamin Peterson | 4ae1946 | 2008-07-31 15:03:40 +0000 | [diff] [blame] | 323 | b'\x00\x01\x00\x02\x00\x00\x00\x03' |
| 324 | >>> unpack('hhl', b'\x00\x01\x00\x02\x00\x00\x00\x03') |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 325 | (1, 2, 3) |
| 326 | >>> calcsize('hhl') |
| 327 | 8 |
| 328 | |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 329 | Unpacked fields can be named by assigning them to variables or by wrapping |
| 330 | the result in a named tuple:: |
| 331 | |
Benjamin Peterson | 4ae1946 | 2008-07-31 15:03:40 +0000 | [diff] [blame] | 332 | >>> record = b'raymond \x32\x12\x08\x01\x08' |
Benjamin Peterson | 2b7411d | 2008-05-26 17:36:47 +0000 | [diff] [blame] | 333 | >>> name, serialnum, school, gradelevel = unpack('<10sHHb', record) |
| 334 | |
| 335 | >>> from collections import namedtuple |
| 336 | >>> Student = namedtuple('Student', 'name serialnum school gradelevel') |
Benjamin Peterson | 4ae1946 | 2008-07-31 15:03:40 +0000 | [diff] [blame] | 337 | >>> Student._make(unpack('<10sHHb', record)) |
| 338 | Student(name=b'raymond ', serialnum=4658, school=264, gradelevel=8) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 339 | |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 340 | The ordering of format characters may have an impact on size since the padding |
| 341 | needed to satisfy alignment requirements is different:: |
| 342 | |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 343 | >>> pack('ci', b'*', 0x12131415) |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 344 | b'*\x00\x00\x00\x12\x13\x14\x15' |
Victor Stinner | da9ec99 | 2010-12-28 13:26:42 +0000 | [diff] [blame] | 345 | >>> pack('ic', 0x12131415, b'*') |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 346 | b'\x12\x13\x14\x15*' |
| 347 | >>> calcsize('ci') |
| 348 | 8 |
| 349 | >>> calcsize('ic') |
| 350 | 5 |
| 351 | |
| 352 | The following format ``'llh0l'`` specifies two pad bytes at the end, assuming |
| 353 | longs are aligned on 4-byte boundaries:: |
| 354 | |
| 355 | >>> pack('llh0l', 1, 2, 3) |
| 356 | b'\x00\x00\x00\x01\x00\x00\x00\x02\x00\x03\x00\x00' |
| 357 | |
| 358 | This only works when native size and alignment are in effect; standard size and |
| 359 | alignment does not enforce any alignment. |
| 360 | |
| 361 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 362 | .. seealso:: |
| 363 | |
| 364 | Module :mod:`array` |
| 365 | Packed binary storage of homogeneous data. |
| 366 | |
| 367 | Module :mod:`xdrlib` |
| 368 | Packing and unpacking of XDR data. |
| 369 | |
| 370 | |
| 371 | .. _struct-objects: |
| 372 | |
Mark Dickinson | cfd56f2 | 2010-06-12 18:37:54 +0000 | [diff] [blame] | 373 | Classes |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 374 | ------- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 375 | |
| 376 | The :mod:`struct` module also defines the following type: |
| 377 | |
| 378 | |
| 379 | .. class:: Struct(format) |
| 380 | |
Mark Dickinson | 6abf182 | 2010-04-12 21:00:59 +0000 | [diff] [blame] | 381 | Return a new Struct object which writes and reads binary data according to |
| 382 | the format string *format*. Creating a Struct object once and calling its |
| 383 | methods is more efficient than calling the :mod:`struct` functions with the |
| 384 | same format since the format string only needs to be compiled once. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 385 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 386 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 387 | Compiled Struct objects support the following methods and attributes: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 388 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 389 | .. method:: pack(v1, v2, ...) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 390 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 391 | Identical to the :func:`pack` function, using the compiled format. |
| 392 | (``len(result)`` will equal :attr:`self.size`.) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 393 | |
| 394 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 395 | .. method:: pack_into(buffer, offset, v1, v2, ...) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 396 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 397 | Identical to the :func:`pack_into` function, using the compiled format. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 398 | |
| 399 | |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 400 | .. method:: unpack(buffer) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 401 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 402 | Identical to the :func:`unpack` function, using the compiled format. |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 403 | (``len(buffer)`` must equal :attr:`self.size`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 404 | |
| 405 | |
Georg Brandl | 7f01a13 | 2009-09-16 15:58:14 +0000 | [diff] [blame] | 406 | .. method:: unpack_from(buffer, offset=0) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 407 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 408 | Identical to the :func:`unpack_from` function, using the compiled format. |
| 409 | (``len(buffer[offset:])`` must be at least :attr:`self.size`). |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 410 | |
| 411 | |
Antoine Pitrou | 9f14681 | 2013-04-27 00:20:04 +0200 | [diff] [blame] | 412 | .. method:: iter_unpack(buffer) |
| 413 | |
| 414 | Identical to the :func:`iter_unpack` function, using the compiled format. |
| 415 | (``len(buffer)`` must be a multiple of :attr:`self.size`). |
| 416 | |
| 417 | .. versionadded:: 3.4 |
| 418 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 419 | .. attribute:: format |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 420 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 421 | The format string used to construct this Struct object. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 422 | |
Benjamin Peterson | e41251e | 2008-04-25 01:59:09 +0000 | [diff] [blame] | 423 | .. attribute:: size |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 424 | |
Mark Dickinson | fdb99f1 | 2010-06-12 16:30:53 +0000 | [diff] [blame] | 425 | The calculated size of the struct (and hence of the bytes object produced |
| 426 | by the :meth:`pack` method) corresponding to :attr:`format`. |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 427 | |