Fred Drake | 3a0351c | 1998-04-04 07:23:21 +0000 | [diff] [blame] | 1 | \section{Built-in Module \module{struct}} |
Fred Drake | b91e934 | 1998-07-23 17:59:49 +0000 | [diff] [blame] | 2 | \declaremodule{builtin}{struct} |
| 3 | |
| 4 | \modulesynopsis{Interpret strings as packed binary data.} |
| 5 | |
Fred Drake | abdea22 | 1998-03-16 05:22:08 +0000 | [diff] [blame] | 6 | \indexii{C@\C{}}{structures} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 7 | |
| 8 | This module performs conversions between Python values and C |
| 9 | structs represented as Python strings. It uses \dfn{format strings} |
| 10 | (explained below) as compact descriptions of the lay-out of the C |
| 11 | structs and the intended conversion to/from Python values. |
| 12 | |
| 13 | The module defines the following exception and functions: |
| 14 | |
Fred Drake | 7ddd043 | 1998-03-08 07:44:13 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 16 | \begin{excdesc}{error} |
| 17 | Exception raised on various occasions; argument is a string |
| 18 | describing what is wrong. |
| 19 | \end{excdesc} |
| 20 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 21 | \begin{funcdesc}{pack}{fmt, v1, v2, {\rm \ldots}} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 22 | Return a string containing the values |
| 23 | \code{\var{v1}, \var{v2}, {\rm \ldots}} packed according to the given |
| 24 | format. The arguments must match the values required by the format |
| 25 | exactly. |
| 26 | \end{funcdesc} |
| 27 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 28 | \begin{funcdesc}{unpack}{fmt, string} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 29 | Unpack the string (presumably packed by \code{pack(\var{fmt}, {\rm \ldots})}) |
| 30 | according to the given format. The result is a tuple even if it |
| 31 | contains exactly one item. The string must contain exactly the |
| 32 | amount of data required by the format (i.e. \code{len(\var{string})} must |
| 33 | equal \code{calcsize(\var{fmt})}). |
| 34 | \end{funcdesc} |
| 35 | |
| 36 | \begin{funcdesc}{calcsize}{fmt} |
| 37 | Return the size of the struct (and hence of the string) |
| 38 | corresponding to the given format. |
| 39 | \end{funcdesc} |
| 40 | |
| 41 | Format characters have the following meaning; the conversion between C |
| 42 | and Python values should be obvious given their types: |
| 43 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 44 | \begin{tableiii}{c|l|l}{samp}{Format}{C Type}{Python} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 45 | \lineiii{x}{pad byte}{no value} |
| 46 | \lineiii{c}{char}{string of length 1} |
| 47 | \lineiii{b}{signed char}{integer} |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 48 | \lineiii{B}{unsigned char}{integer} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 49 | \lineiii{h}{short}{integer} |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 50 | \lineiii{H}{unsigned short}{integer} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 51 | \lineiii{i}{int}{integer} |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 52 | \lineiii{I}{unsigned int}{integer} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 53 | \lineiii{l}{long}{integer} |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 54 | \lineiii{L}{unsigned long}{integer} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 55 | \lineiii{f}{float}{float} |
| 56 | \lineiii{d}{double}{float} |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 57 | \lineiii{s}{char[]}{string} |
Fred Drake | cf0fb8b | 1998-07-23 21:18:25 +0000 | [diff] [blame] | 58 | \lineiii{p}{char[]}{string} |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 59 | \end{tableiii} |
| 60 | |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 61 | A format character may be preceded by an integral repeat count; e.g.\ |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 62 | the format string \code{'4h'} means exactly the same as \code{'hhhh'}. |
| 63 | |
Guido van Rossum | e20aef5 | 1997-08-26 20:39:54 +0000 | [diff] [blame] | 64 | Whitespace characters between formats are ignored; a count and its |
| 65 | format must not contain whitespace though. |
| 66 | |
Fred Drake | cf0fb8b | 1998-07-23 21:18:25 +0000 | [diff] [blame] | 67 | For the \character{s} format character, the count is interpreted as the |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 68 | size of the string, not a repeat count like for the other format |
| 69 | characters; e.g. \code{'10s'} means a single 10-byte string, while |
| 70 | \code{'10c'} means 10 characters. For packing, the string is |
| 71 | truncated or padded with null bytes as appropriate to make it fit. |
| 72 | For unpacking, the resulting string always has exactly the specified |
| 73 | number of bytes. As a special case, \code{'0s'} means a single, empty |
| 74 | string (while \code{'0c'} means 0 characters). |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 75 | |
Fred Drake | cf0fb8b | 1998-07-23 21:18:25 +0000 | [diff] [blame] | 76 | The \character{p} format character can be used to encode a Pascal |
| 77 | string. The first byte is the length of the stored string, with the |
| 78 | bytes of the string following. If count is given, it is used as the |
| 79 | total number of bytes used, including the length byte. If the string |
| 80 | passed in to \function{pack()} is too long, the stored representation |
| 81 | is truncated. If the string is too short, padding is used to ensure |
| 82 | that exactly enough bytes are used to satisfy the count. |
| 83 | |
| 84 | For the \character{I} and \character{L} format characters, the return |
Guido van Rossum | 6530717 | 1997-01-03 19:21:53 +0000 | [diff] [blame] | 85 | value is a Python long integer. |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 86 | |
| 87 | By default, C numbers are represented in the machine's native format |
| 88 | and byte order, and properly aligned by skipping pad bytes if |
| 89 | necessary (according to the rules used by the C compiler). |
| 90 | |
| 91 | Alternatively, the first character of the format string can be used to |
| 92 | indicate the byte order, size and alignment of the packed data, |
| 93 | according to the following table: |
| 94 | |
Fred Drake | ee60191 | 1998-04-11 20:53:03 +0000 | [diff] [blame] | 95 | \begin{tableiii}{c|l|l}{samp}{Character}{Byte order}{Size and alignment} |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 96 | \lineiii{@}{native}{native} |
| 97 | \lineiii{=}{native}{standard} |
| 98 | \lineiii{<}{little-endian}{standard} |
| 99 | \lineiii{>}{big-endian}{standard} |
| 100 | \lineiii{!}{network (= big-endian)}{standard} |
| 101 | \end{tableiii} |
| 102 | |
Fred Drake | cf0fb8b | 1998-07-23 21:18:25 +0000 | [diff] [blame] | 103 | If the first character is not one of these, \character{@} is assumed. |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 104 | |
| 105 | Native byte order is big-endian or little-endian, depending on the |
| 106 | host system (e.g. Motorola and Sun are big-endian; Intel and DEC are |
| 107 | little-endian). |
| 108 | |
| 109 | Native size and alignment are determined using the C compiler's sizeof |
| 110 | expression. This is always combined with native byte order. |
| 111 | |
| 112 | Standard size and alignment are as follows: no alignment is required |
| 113 | for any type (so you have to use pad bytes); short is 2 bytes; int and |
Guido van Rossum | dbadd55 | 1997-01-03 04:20:09 +0000 | [diff] [blame] | 114 | long are 4 bytes. Float and double are 32-bit and 64-bit IEEE floating |
| 115 | point numbers, respectively. |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 116 | |
Fred Drake | cf0fb8b | 1998-07-23 21:18:25 +0000 | [diff] [blame] | 117 | Note the difference between \character{@} and \character{=}: both use native |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 118 | byte order, but the size and alignment of the latter is standardized. |
| 119 | |
Fred Drake | cf0fb8b | 1998-07-23 21:18:25 +0000 | [diff] [blame] | 120 | The form \character{!} is available for those poor souls who claim they |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 121 | can't remember whether network byte order is big-endian or |
| 122 | little-endian. |
| 123 | |
| 124 | There is no way to indicate non-native byte order (i.e. force |
Fred Drake | cf0fb8b | 1998-07-23 21:18:25 +0000 | [diff] [blame] | 125 | byte-swapping); use the appropriate choice of \character{<} or |
| 126 | \character{>}. |
Guido van Rossum | 1254346 | 1996-12-31 02:22:14 +0000 | [diff] [blame] | 127 | |
| 128 | Examples (all using native byte order, size and alignment, on a |
| 129 | big-endian machine): |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 130 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 131 | \begin{verbatim} |
Guido van Rossum | dbadd55 | 1997-01-03 04:20:09 +0000 | [diff] [blame] | 132 | >>> from struct import * |
| 133 | >>> pack('hhl', 1, 2, 3) |
| 134 | '\000\001\000\002\000\000\000\003' |
| 135 | >>> unpack('hhl', '\000\001\000\002\000\000\000\003') |
| 136 | (1, 2, 3) |
| 137 | >>> calcsize('hhl') |
| 138 | 8 |
| 139 | >>> |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 140 | \end{verbatim} |
Guido van Rossum | e47da0a | 1997-07-17 16:34:52 +0000 | [diff] [blame] | 141 | % |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 142 | Hint: to align the end of a structure to the alignment requirement of |
| 143 | a particular type, end the format with the code for that type with a |
Guido van Rossum | 6c4f003 | 1995-03-07 10:14:09 +0000 | [diff] [blame] | 144 | repeat count of zero, e.g.\ the format \code{'llh0l'} specifies two |
Guido van Rossum | 5fdeeea | 1994-01-02 01:22:07 +0000 | [diff] [blame] | 145 | pad bytes at the end, assuming longs are aligned on 4-byte boundaries. |
Fred Drake | 7ddd043 | 1998-03-08 07:44:13 +0000 | [diff] [blame] | 146 | This only works when native size and alignment are in effect; |
| 147 | standard size and alignment does not enforce any alignment. |
| 148 | |
| 149 | \begin{seealso} |
| 150 | \seemodule{array}{packed binary storage of homogeneous data} |
| 151 | \end{seealso} |