blob: 1077437a4ae9e670cdc9037b491393db71b18761 [file] [log] [blame]
Thomas Wouters477c8d52006-05-27 19:21:47 +00001"""
2Functions to convert between Python values and C structs.
3Python strings are used to hold the data representing the C struct
4and also as format strings to describe the layout of data in the C struct.
5
6The optional first format char indicates byte order, size and alignment:
7 @: native order, size & alignment (default)
8 =: native order, std. size & alignment
9 <: little-endian, std. size & alignment
10 >: big-endian, std. size & alignment
11 !: same as >
12
13The remaining chars indicate types of args and must match exactly;
14these can be preceded by a decimal repeat count:
15 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;
16 h:short; H:unsigned short; i:int; I:unsigned int;
17 l:long; L:unsigned long; f:float; d:double.
18Special cases (preceding decimal count indicates length):
19 s:string (array of char); p: pascal string (with count byte).
20Special case (only available in native format):
21 P:an integer type that is wide enough to hold a pointer.
22Special case (not in native mode unless 'long long' in platform C):
23 q:long long; Q:unsigned long long
24Whitespace between formats is ignored.
25
26The variable struct.error is an exception raised on errors.
27"""
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000028
29# XXX Move the bytes and str8 casts into the _struct module
30
31__version__ = '3.0'
32
Thomas Wouters477c8d52006-05-27 19:21:47 +000033
34from _struct import Struct, error
35
36_MAXCACHE = 100
37_cache = {}
38
39def _compile(fmt):
40 # Internal: compile struct pattern
41 if len(_cache) >= _MAXCACHE:
42 _cache.clear()
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000043 s = Struct(str8(fmt))
Thomas Wouters477c8d52006-05-27 19:21:47 +000044 _cache[fmt] = s
45 return s
46
47def calcsize(fmt):
48 """
49 Return size of C struct described by format string fmt.
50 See struct.__doc__ for more on format strings.
51 """
52 try:
53 o = _cache[fmt]
54 except KeyError:
55 o = _compile(fmt)
56 return o.size
57
58def pack(fmt, *args):
59 """
60 Return string containing values v1, v2, ... packed according to fmt.
61 See struct.__doc__ for more on format strings.
62 """
63 try:
64 o = _cache[fmt]
65 except KeyError:
66 o = _compile(fmt)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000067 return bytes(o.pack(*args))
Thomas Wouters477c8d52006-05-27 19:21:47 +000068
Thomas Wouters73e5a5b2006-06-08 15:35:45 +000069def pack_into(fmt, buf, offset, *args):
Thomas Wouters477c8d52006-05-27 19:21:47 +000070 """
Thomas Wouters0e3f5912006-08-11 14:57:12 +000071 Pack the values v1, v2, ... according to fmt, write
Thomas Wouters477c8d52006-05-27 19:21:47 +000072 the packed bytes into the writable buffer buf starting at offset.
73 See struct.__doc__ for more on format strings.
74 """
75 try:
76 o = _cache[fmt]
77 except KeyError:
78 o = _compile(fmt)
Guido van Rossum2e6a4b32007-05-04 19:56:22 +000079 return bytes(o.pack_into(buf, offset, *args))
Thomas Wouters477c8d52006-05-27 19:21:47 +000080
81def unpack(fmt, s):
82 """
83 Unpack the string, containing packed C structure data, according
84 to fmt. Requires len(string)==calcsize(fmt).
85 See struct.__doc__ for more on format strings.
86 """
87 try:
88 o = _cache[fmt]
89 except KeyError:
90 o = _compile(fmt)
91 return o.unpack(s)
92
93def unpack_from(fmt, buf, offset=0):
94 """
95 Unpack the buffer, containing packed C structure data, according to
96 fmt starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).
97 See struct.__doc__ for more on format strings.
98 """
99 try:
100 o = _cache[fmt]
101 except KeyError:
102 o = _compile(fmt)
103 return o.unpack_from(buf, offset)