blob: 9a1942da608e148eb133b1367f2c3224e2b040b0 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{struct} ---
Fred Drakeb68a1251999-08-24 20:16:29 +00002 Interpret strings as packed binary data}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{builtin}{struct}
4
5\modulesynopsis{Interpret strings as packed binary data.}
6
Fred Drakeb68a1251999-08-24 20:16:29 +00007\indexii{C}{structures}
8\indexiii{packing}{binary}{data}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00009
Fred Drakeb68a1251999-08-24 20:16:29 +000010This module performs conversions between Python values and C
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000011structs represented as Python strings. It uses \dfn{format strings}
Fred Drakeb68a1251999-08-24 20:16:29 +000012(explained below) as compact descriptions of the lay-out of the C
13structs and the intended conversion to/from Python values. This can
14be used in handling binary data stored in files or from network
15connections, among other sources.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000016
17The module defines the following exception and functions:
18
Fred Drake7ddd0431998-03-08 07:44:13 +000019
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000020\begin{excdesc}{error}
21 Exception raised on various occasions; argument is a string
22 describing what is wrong.
23\end{excdesc}
24
Fred Drake50b804d1998-11-30 22:14:58 +000025\begin{funcdesc}{pack}{fmt, v1, v2, \textrm{\ldots}}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000026 Return a string containing the values
Fred Drake50b804d1998-11-30 22:14:58 +000027 \code{\var{v1}, \var{v2}, \textrm{\ldots}} packed according to the given
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000028 format. The arguments must match the values required by the format
29 exactly.
30\end{funcdesc}
31
Fred Drakecce10901998-03-17 06:33:25 +000032\begin{funcdesc}{unpack}{fmt, string}
Fred Drake50b804d1998-11-30 22:14:58 +000033 Unpack the string (presumably packed by \code{pack(\var{fmt},
34 \textrm{\ldots})}) according to the given format. The result is a
35 tuple even if it contains exactly one item. The string must contain
36 exactly the amount of data required by the format (i.e.
37 \code{len(\var{string})} must equal \code{calcsize(\var{fmt})}).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000038\end{funcdesc}
39
40\begin{funcdesc}{calcsize}{fmt}
41 Return the size of the struct (and hence of the string)
42 corresponding to the given format.
43\end{funcdesc}
44
Fred Drake50b804d1998-11-30 22:14:58 +000045Format characters have the following meaning; the conversion between
Fred Drakeb68a1251999-08-24 20:16:29 +000046C and Python values should be obvious given their types:
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000047
Fred Drake38e5d272000-04-03 20:13:55 +000048\begin{tableiv}{c|l|l|c}{samp}{Format}{C Type}{Python}{Notes}
49 \lineiv{x}{pad byte}{no value}{}
50 \lineiv{c}{\ctype{char}}{string of length 1}{}
51 \lineiv{b}{\ctype{signed char}}{integer}{}
52 \lineiv{B}{\ctype{unsigned char}}{integer}{}
53 \lineiv{h}{\ctype{short}}{integer}{}
54 \lineiv{H}{\ctype{unsigned short}}{integer}{}
55 \lineiv{i}{\ctype{int}}{integer}{}
Tim Peters7b9542a2001-06-10 23:40:19 +000056 \lineiv{I}{\ctype{unsigned int}}{long}{}
Fred Drake38e5d272000-04-03 20:13:55 +000057 \lineiv{l}{\ctype{long}}{integer}{}
58 \lineiv{L}{\ctype{unsigned long}}{long}{}
Tim Peters7b9542a2001-06-10 23:40:19 +000059 \lineiv{q}{\ctype{long long}}{long}{(1)}
60 \lineiv{Q}{\ctype{unsigned long long}}{long}{(1)}
Fred Drake38e5d272000-04-03 20:13:55 +000061 \lineiv{f}{\ctype{float}}{float}{}
62 \lineiv{d}{\ctype{double}}{float}{}
63 \lineiv{s}{\ctype{char[]}}{string}{}
64 \lineiv{p}{\ctype{char[]}}{string}{}
65 \lineiv{P}{\ctype{void *}}{integer}{}
66\end{tableiv}
67
68\noindent
69Notes:
70
71\begin{description}
72\item[(1)]
Tim Peters7b9542a2001-06-10 23:40:19 +000073 The \character{q} and \character{Q} conversion codes are available in
74 native mode only if the platform C compiler supports C \ctype{long long},
75 or, on Windows, \ctype{__int64}.
Fred Drake38e5d272000-04-03 20:13:55 +000076\end{description}
77
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000078
Fred Drake50b804d1998-11-30 22:14:58 +000079A format character may be preceded by an integral repeat count;
80e.g.\ the format string \code{'4h'} means exactly the same as
81\code{'hhhh'}.
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000082
Guido van Rossume20aef51997-08-26 20:39:54 +000083Whitespace characters between formats are ignored; a count and its
84format must not contain whitespace though.
85
Fred Drakecf0fb8b1998-07-23 21:18:25 +000086For the \character{s} format character, the count is interpreted as the
Guido van Rossum12543461996-12-31 02:22:14 +000087size of the string, not a repeat count like for the other format
88characters; e.g. \code{'10s'} means a single 10-byte string, while
89\code{'10c'} means 10 characters. For packing, the string is
90truncated or padded with null bytes as appropriate to make it fit.
91For unpacking, the resulting string always has exactly the specified
92number of bytes. As a special case, \code{'0s'} means a single, empty
93string (while \code{'0c'} means 0 characters).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000094
Fred Drakecf0fb8b1998-07-23 21:18:25 +000095The \character{p} format character can be used to encode a Pascal
96string. The first byte is the length of the stored string, with the
97bytes of the string following. If count is given, it is used as the
98total number of bytes used, including the length byte. If the string
99passed in to \function{pack()} is too long, the stored representation
100is truncated. If the string is too short, padding is used to ensure
101that exactly enough bytes are used to satisfy the count.
102
103For the \character{I} and \character{L} format characters, the return
Guido van Rossum65307171997-01-03 19:21:53 +0000104value is a Python long integer.
Guido van Rossum12543461996-12-31 02:22:14 +0000105
Guido van Rossum6ac06b31998-09-21 14:44:34 +0000106For the \character{P} format character, the return value is a Python
107integer or long integer, depending on the size needed to hold a
Fred Drake50b804d1998-11-30 22:14:58 +0000108pointer when it has been cast to an integer type. A \NULL{} pointer will
109always be returned as the Python integer \code{0}. When packing pointer-sized
Guido van Rossum6ac06b31998-09-21 14:44:34 +0000110values, Python integer or long integer objects may be used. For
111example, the Alpha and Merced processors use 64-bit pointer values,
112meaning a Python long integer will be used to hold the pointer; other
113platforms use 32-bit pointers and will use a Python integer.
114
Fred Drakeb68a1251999-08-24 20:16:29 +0000115By default, C numbers are represented in the machine's native format
Guido van Rossum12543461996-12-31 02:22:14 +0000116and byte order, and properly aligned by skipping pad bytes if
Fred Drakeb68a1251999-08-24 20:16:29 +0000117necessary (according to the rules used by the C compiler).
Guido van Rossum12543461996-12-31 02:22:14 +0000118
119Alternatively, the first character of the format string can be used to
120indicate the byte order, size and alignment of the packed data,
121according to the following table:
122
Fred Drakeee601911998-04-11 20:53:03 +0000123\begin{tableiii}{c|l|l}{samp}{Character}{Byte order}{Size and alignment}
Guido van Rossum12543461996-12-31 02:22:14 +0000124 \lineiii{@}{native}{native}
125 \lineiii{=}{native}{standard}
126 \lineiii{<}{little-endian}{standard}
127 \lineiii{>}{big-endian}{standard}
128 \lineiii{!}{network (= big-endian)}{standard}
129\end{tableiii}
130
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000131If the first character is not one of these, \character{@} is assumed.
Guido van Rossum12543461996-12-31 02:22:14 +0000132
133Native byte order is big-endian or little-endian, depending on the
134host system (e.g. Motorola and Sun are big-endian; Intel and DEC are
135little-endian).
136
Fred Drakeb68a1251999-08-24 20:16:29 +0000137Native size and alignment are determined using the C compiler's
Fred Drake50b804d1998-11-30 22:14:58 +0000138\keyword{sizeof} expression. This is always combined with native byte
139order.
Guido van Rossum12543461996-12-31 02:22:14 +0000140
141Standard size and alignment are as follows: no alignment is required
Fred Drake50b804d1998-11-30 22:14:58 +0000142for any type (so you have to use pad bytes); \ctype{short} is 2 bytes;
143\ctype{int} and \ctype{long} are 4 bytes. \ctype{float} and
144\ctype{double} are 32-bit and 64-bit IEEE floating point numbers,
145respectively.
Guido van Rossum12543461996-12-31 02:22:14 +0000146
Fred Drake50b804d1998-11-30 22:14:58 +0000147Note the difference between \character{@} and \character{=}: both use
148native byte order, but the size and alignment of the latter is
149standardized.
Guido van Rossum12543461996-12-31 02:22:14 +0000150
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000151The form \character{!} is available for those poor souls who claim they
Guido van Rossum12543461996-12-31 02:22:14 +0000152can't remember whether network byte order is big-endian or
153little-endian.
154
155There is no way to indicate non-native byte order (i.e. force
Fred Drakecf0fb8b1998-07-23 21:18:25 +0000156byte-swapping); use the appropriate choice of \character{<} or
157\character{>}.
Guido van Rossum12543461996-12-31 02:22:14 +0000158
Guido van Rossum6ac06b31998-09-21 14:44:34 +0000159The \character{P} format character is only available for the native
160byte ordering (selected as the default or with the \character{@} byte
161order character). The byte order character \character{=} chooses to
162use little- or big-endian ordering based on the host system. The
163struct module does not interpret this as native ordering, so the
164\character{P} format is not available.
165
Guido van Rossum12543461996-12-31 02:22:14 +0000166Examples (all using native byte order, size and alignment, on a
167big-endian machine):
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000168
Fred Drake19479911998-02-13 06:58:54 +0000169\begin{verbatim}
Guido van Rossumdbadd551997-01-03 04:20:09 +0000170>>> from struct import *
171>>> pack('hhl', 1, 2, 3)
Ka-Ping Yeefa004ad2001-01-24 17:19:08 +0000172'\x00\x01\x00\x02\x00\x00\x00\x03'
173>>> unpack('hhl', '\x00\x01\x00\x02\x00\x00\x00\x03')
Guido van Rossumdbadd551997-01-03 04:20:09 +0000174(1, 2, 3)
175>>> calcsize('hhl')
1768
Fred Drake19479911998-02-13 06:58:54 +0000177\end{verbatim}
Fred Drake50b804d1998-11-30 22:14:58 +0000178
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000179Hint: to align the end of a structure to the alignment requirement of
180a particular type, end the format with the code for that type with a
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000181repeat count of zero, e.g.\ the format \code{'llh0l'} specifies two
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000182pad bytes at the end, assuming longs are aligned on 4-byte boundaries.
Fred Drake7ddd0431998-03-08 07:44:13 +0000183This only works when native size and alignment are in effect;
184standard size and alignment does not enforce any alignment.
185
186\begin{seealso}
Fred Drakeb68a1251999-08-24 20:16:29 +0000187 \seemodule{array}{Packed binary storage of homogeneous data.}
188 \seemodule{xdrlib}{Packing and unpacking of XDR data.}
Fred Drake7ddd0431998-03-08 07:44:13 +0000189\end{seealso}