blob: 19f674c99b73986c4f3c9552fde6b367b849549b [file] [log] [blame]
Guido van Rossum470be141995-03-17 16:07:09 +00001\section{Built-in Module \sectcode{struct}}
Guido van Rossume47da0a1997-07-17 16:34:52 +00002\label{module-struct}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +00003\bimodindex{struct}
4\indexii{C}{structures}
5
6This module performs conversions between Python values and C
7structs represented as Python strings. It uses \dfn{format strings}
8(explained below) as compact descriptions of the lay-out of the C
9structs and the intended conversion to/from Python values.
10
Guido van Rossumecde7811995-03-28 13:35:14 +000011See also built-in module \code{array}.
Fred Drake4f496cc1997-12-16 04:08:24 +000012\refbimodindex{array}
Guido van Rossumecde7811995-03-28 13:35:14 +000013
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000014The module defines the following exception and functions:
15
16\renewcommand{\indexsubitem}{(in module struct)}
17\begin{excdesc}{error}
18 Exception raised on various occasions; argument is a string
19 describing what is wrong.
20\end{excdesc}
21
22\begin{funcdesc}{pack}{fmt\, v1\, v2\, {\rm \ldots}}
23 Return a string containing the values
24 \code{\var{v1}, \var{v2}, {\rm \ldots}} packed according to the given
25 format. The arguments must match the values required by the format
26 exactly.
27\end{funcdesc}
28
29\begin{funcdesc}{unpack}{fmt\, string}
30 Unpack the string (presumably packed by \code{pack(\var{fmt}, {\rm \ldots})})
31 according to the given format. The result is a tuple even if it
32 contains exactly one item. The string must contain exactly the
33 amount of data required by the format (i.e. \code{len(\var{string})} must
34 equal \code{calcsize(\var{fmt})}).
35\end{funcdesc}
36
37\begin{funcdesc}{calcsize}{fmt}
38 Return the size of the struct (and hence of the string)
39 corresponding to the given format.
40\end{funcdesc}
41
42Format characters have the following meaning; the conversion between C
43and Python values should be obvious given their types:
44
45\begin{tableiii}{|c|l|l|}{samp}{Format}{C}{Python}
46 \lineiii{x}{pad byte}{no value}
47 \lineiii{c}{char}{string of length 1}
48 \lineiii{b}{signed char}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000049 \lineiii{B}{unsigned char}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000050 \lineiii{h}{short}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000051 \lineiii{H}{unsigned short}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000052 \lineiii{i}{int}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000053 \lineiii{I}{unsigned int}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000054 \lineiii{l}{long}{integer}
Guido van Rossum12543461996-12-31 02:22:14 +000055 \lineiii{L}{unsigned long}{integer}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000056 \lineiii{f}{float}{float}
57 \lineiii{d}{double}{float}
Guido van Rossum12543461996-12-31 02:22:14 +000058 \lineiii{s}{char[]}{string}
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000059\end{tableiii}
60
Guido van Rossum6c4f0031995-03-07 10:14:09 +000061A format character may be preceded by an integral repeat count; e.g.\
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000062the format string \code{'4h'} means exactly the same as \code{'hhhh'}.
63
Guido van Rossume20aef51997-08-26 20:39:54 +000064Whitespace characters between formats are ignored; a count and its
65format must not contain whitespace though.
66
Guido van Rossum12543461996-12-31 02:22:14 +000067For the \code{'s'} format character, the count is interpreted as the
68size of the string, not a repeat count like for the other format
69characters; e.g. \code{'10s'} means a single 10-byte string, while
70\code{'10c'} means 10 characters. For packing, the string is
71truncated or padded with null bytes as appropriate to make it fit.
72For unpacking, the resulting string always has exactly the specified
73number of bytes. As a special case, \code{'0s'} means a single, empty
74string (while \code{'0c'} means 0 characters).
Guido van Rossum5fdeeea1994-01-02 01:22:07 +000075
Guido van Rossum12543461996-12-31 02:22:14 +000076For the \code{'I'} and \code{'L'} format characters, the return
Guido van Rossum65307171997-01-03 19:21:53 +000077value is a Python long integer.
Guido van Rossum12543461996-12-31 02:22:14 +000078
79By default, C numbers are represented in the machine's native format
80and byte order, and properly aligned by skipping pad bytes if
81necessary (according to the rules used by the C compiler).
82
83Alternatively, the first character of the format string can be used to
84indicate the byte order, size and alignment of the packed data,
85according to the following table:
86
87\begin{tableiii}{|c|l|l|}{samp}{Character}{Byte order}{Size and alignment}
88 \lineiii{@}{native}{native}
89 \lineiii{=}{native}{standard}
90 \lineiii{<}{little-endian}{standard}
91 \lineiii{>}{big-endian}{standard}
92 \lineiii{!}{network (= big-endian)}{standard}
93\end{tableiii}
94
95If the first character is not one of these, \code{'@'} is assumed.
96
97Native byte order is big-endian or little-endian, depending on the
98host system (e.g. Motorola and Sun are big-endian; Intel and DEC are
99little-endian).
100
101Native size and alignment are determined using the C compiler's sizeof
102expression. This is always combined with native byte order.
103
104Standard size and alignment are as follows: no alignment is required
105for any type (so you have to use pad bytes); short is 2 bytes; int and
Guido van Rossumdbadd551997-01-03 04:20:09 +0000106long are 4 bytes. Float and double are 32-bit and 64-bit IEEE floating
107point numbers, respectively.
Guido van Rossum12543461996-12-31 02:22:14 +0000108
109Note the difference between \code{'@'} and \code{'='}: both use native
110byte order, but the size and alignment of the latter is standardized.
111
112The form \code{'!'} is available for those poor souls who claim they
113can't remember whether network byte order is big-endian or
114little-endian.
115
116There is no way to indicate non-native byte order (i.e. force
117byte-swapping); use the appropriate choice of \code{'<'} or
118\code{'>'}.
119
120Examples (all using native byte order, size and alignment, on a
121big-endian machine):
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000122
123\bcode\begin{verbatim}
Guido van Rossumdbadd551997-01-03 04:20:09 +0000124>>> from struct import *
125>>> pack('hhl', 1, 2, 3)
126'\000\001\000\002\000\000\000\003'
127>>> unpack('hhl', '\000\001\000\002\000\000\000\003')
128(1, 2, 3)
129>>> calcsize('hhl')
1308
131>>>
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000132\end{verbatim}\ecode
Guido van Rossume47da0a1997-07-17 16:34:52 +0000133%
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000134Hint: to align the end of a structure to the alignment requirement of
135a particular type, end the format with the code for that type with a
Guido van Rossum6c4f0031995-03-07 10:14:09 +0000136repeat count of zero, e.g.\ the format \code{'llh0l'} specifies two
Guido van Rossum5fdeeea1994-01-02 01:22:07 +0000137pad bytes at the end, assuming longs are aligned on 4-byte boundaries.
Guido van Rossum12543461996-12-31 02:22:14 +0000138(This only works when native size and alignment are in effect;
139standard size and alignment does not enforce any alignment.)