blob: 093cd7b9e1be12580ea507e87f55a06da8330b1f [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001
2/* struct module -- pack values into and (out of) strings */
3
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00004/* New version supporting byte order, alignment and size options,
5 character strings, and unsigned numbers */
6
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00007#include "Python.h"
8#include <ctype.h>
9
10PyDoc_STRVAR(struct__doc__,
11"Functions to convert between Python values and C structs.\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000012Python strings are used to hold the data representing the C struct\n\
13and also as format strings to describe the layout of data in the C struct.\n\
14\n\
Tim Petersbe800852001-06-11 16:45:33 +000015The optional first format char indicates byte order, size and alignment:\n\
16 @: native order, size & alignment (default)\n\
17 =: native order, std. size & alignment\n\
18 <: little-endian, std. size & alignment\n\
19 >: big-endian, std. size & alignment\n\
20 !: same as >\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000021\n\
22The remaining chars indicate types of args and must match exactly;\n\
23these can be preceded by a decimal repeat count:\n\
24 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
25 h:short; H:unsigned short; i:int; I:unsigned int;\n\
26 l:long; L:unsigned long; f:float; d:double.\n\
27Special cases (preceding decimal count indicates length):\n\
Tim Peters7b9542a2001-06-10 23:40:19 +000028 s:string (array of char); p: pascal string (with count byte).\n\
Guido van Rossum78694d91998-09-18 14:14:13 +000029Special case (only available in native format):\n\
30 P:an integer type that is wide enough to hold a pointer.\n\
Tim Peters7b9542a2001-06-10 23:40:19 +000031Special case (not in native mode unless 'long long' in platform C):\n\
32 q:long long; Q:unsigned long long\n\
Guido van Rossum414fd481997-12-19 04:24:24 +000033Whitespace between formats is ignored.\n\
34\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000035The variable struct.error is an exception raised on errors.");
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000036
37
38/* Exception */
39
Barry Warsaw30695fa1996-12-12 23:32:31 +000040static PyObject *StructError;
Guido van Rossum02975121992-08-17 08:55:12 +000041
42
43/* Define various structs to figure out the alignments of types */
44
Jack Jansen971e1df1995-02-02 14:29:10 +000045#ifdef __MWERKS__
46/*
47** XXXX We have a problem here. There are no unique alignment rules
Tim Peters2d4e0772001-06-11 16:57:33 +000048** on the PowerPC mac.
Jack Jansen971e1df1995-02-02 14:29:10 +000049*/
50#ifdef __powerc
51#pragma options align=mac68k
52#endif
53#endif /* __MWERKS__ */
54
Guido van Rossume2ae77b2001-10-24 20:42:55 +000055typedef struct { char c; short x; } st_short;
56typedef struct { char c; int x; } st_int;
57typedef struct { char c; long x; } st_long;
58typedef struct { char c; float x; } st_float;
59typedef struct { char c; double x; } st_double;
60typedef struct { char c; void *x; } st_void_p;
Guido van Rossum02975121992-08-17 08:55:12 +000061
Guido van Rossume2ae77b2001-10-24 20:42:55 +000062#define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
63#define INT_ALIGN (sizeof(st_int) - sizeof(int))
64#define LONG_ALIGN (sizeof(st_long) - sizeof(long))
65#define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
66#define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
67#define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
Guido van Rossum02975121992-08-17 08:55:12 +000068
Tim Peters7b9542a2001-06-10 23:40:19 +000069/* We can't support q and Q in native mode unless the compiler does;
70 in std mode, they're 8 bytes on all platforms. */
71#ifdef HAVE_LONG_LONG
72typedef struct { char c; LONG_LONG x; } s_long_long;
73#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(LONG_LONG))
Tim Peters7b9542a2001-06-10 23:40:19 +000074#endif
75
Martin v. Löwis2af72d52000-09-15 08:10:33 +000076#define STRINGIFY(x) #x
77
Jack Jansen971e1df1995-02-02 14:29:10 +000078#ifdef __powerc
79#pragma options align=reset
80#endif
81
Tim Peters7a3bfc32001-06-12 01:22:22 +000082/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
83
84static PyObject *
85get_pylong(PyObject *v)
86{
87 PyNumberMethods *m;
88
89 assert(v != NULL);
90 if (PyInt_Check(v))
91 return PyLong_FromLong(PyInt_AS_LONG(v));
92 if (PyLong_Check(v)) {
93 Py_INCREF(v);
94 return v;
95 }
96 m = v->ob_type->tp_as_number;
97 if (m != NULL && m->nb_long != NULL) {
98 v = m->nb_long(v);
99 if (v == NULL)
100 return NULL;
101 if (PyLong_Check(v))
102 return v;
103 Py_DECREF(v);
104 }
105 PyErr_SetString(StructError,
106 "cannot convert argument to long");
107 return NULL;
108}
109
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000110/* Helper routine to get a Python integer and raise the appropriate error
111 if it isn't one */
112
113static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000114get_long(PyObject *v, long *p)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000115{
116 long x = PyInt_AsLong(v);
117 if (x == -1 && PyErr_Occurred()) {
Fred Draked3dbb381998-05-28 04:35:49 +0000118 if (PyErr_ExceptionMatches(PyExc_TypeError))
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000119 PyErr_SetString(StructError,
120 "required argument is not an integer");
121 return -1;
122 }
123 *p = x;
124 return 0;
125}
126
127
Guido van Rossum60c50611996-12-31 16:29:52 +0000128/* Same, but handling unsigned long */
129
130static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000131get_ulong(PyObject *v, unsigned long *p)
Guido van Rossum60c50611996-12-31 16:29:52 +0000132{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000133 if (PyLong_Check(v)) {
134 unsigned long x = PyLong_AsUnsignedLong(v);
135 if (x == (unsigned long)(-1) && PyErr_Occurred())
Guido van Rossum60c50611996-12-31 16:29:52 +0000136 return -1;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000137 *p = x;
138 return 0;
Guido van Rossum60c50611996-12-31 16:29:52 +0000139 }
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000140 else {
141 return get_long(v, (long *)p);
142 }
Guido van Rossum60c50611996-12-31 16:29:52 +0000143}
144
Tim Peters7b9542a2001-06-10 23:40:19 +0000145#ifdef HAVE_LONG_LONG
146
147/* Same, but handling native long long. */
148
149static int
150get_longlong(PyObject *v, LONG_LONG *p)
151{
152 LONG_LONG x;
Tim Peters7b9542a2001-06-10 23:40:19 +0000153
Tim Peters7a3bfc32001-06-12 01:22:22 +0000154 v = get_pylong(v);
155 if (v == NULL)
156 return -1;
Tim Peters7b9542a2001-06-10 23:40:19 +0000157 assert(PyLong_Check(v));
158 x = PyLong_AsLongLong(v);
Tim Peters7a3bfc32001-06-12 01:22:22 +0000159 Py_DECREF(v);
Tim Peters7b9542a2001-06-10 23:40:19 +0000160 if (x == (LONG_LONG)-1 && PyErr_Occurred())
161 return -1;
162 *p = x;
163 return 0;
164}
165
166/* Same, but handling native unsigned long long. */
167
168static int
169get_ulonglong(PyObject *v, unsigned LONG_LONG *p)
170{
171 unsigned LONG_LONG x;
Tim Peters7b9542a2001-06-10 23:40:19 +0000172
Tim Peters7a3bfc32001-06-12 01:22:22 +0000173 v = get_pylong(v);
174 if (v == NULL)
175 return -1;
Tim Peters7b9542a2001-06-10 23:40:19 +0000176 assert(PyLong_Check(v));
177 x = PyLong_AsUnsignedLongLong(v);
Tim Peters7a3bfc32001-06-12 01:22:22 +0000178 Py_DECREF(v);
Tim Peters7b9542a2001-06-10 23:40:19 +0000179 if (x == (unsigned LONG_LONG)-1 && PyErr_Occurred())
180 return -1;
181 *p = x;
182 return 0;
183}
184
185#endif
Guido van Rossum60c50611996-12-31 16:29:52 +0000186
Guido van Rossum74679b41997-01-02 22:21:36 +0000187/* Floating point helpers */
188
189/* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
190 Point Arithmetic). See the following URL:
191 http://www.psc.edu/general/software/packages/ieee/ieee.html */
192
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000193/* XXX Inf/NaN are not handled quite right (but underflow is!) */
Guido van Rossum74679b41997-01-02 22:21:36 +0000194
195static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000196pack_float(double x, /* The number to pack */
197 char *p, /* Where to pack the high order byte */
198 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000199{
200 int s;
201 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000202 double f;
203 long fbits;
Guido van Rossum74679b41997-01-02 22:21:36 +0000204
205 if (x < 0) {
206 s = 1;
207 x = -x;
208 }
209 else
210 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000211
212 f = frexp(x, &e);
213
214 /* Normalize f to be in the range [1.0, 2.0) */
215 if (0.5 <= f && f < 1.0) {
216 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000217 e--;
218 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000219 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000220 e = 0;
221 }
222 else {
223 PyErr_SetString(PyExc_SystemError,
224 "frexp() result out of range");
225 return -1;
226 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000227
228 if (e >= 128) {
229 /* XXX 128 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000230 PyErr_SetString(PyExc_OverflowError,
231 "float too large to pack with f format");
232 return -1;
233 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000234 else if (e < -126) {
235 /* Gradual underflow */
236 f = ldexp(f, 126 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000237 e = 0;
238 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000239 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000240 e += 127;
241 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000242 }
243
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000244 f *= 8388608.0; /* 2**23 */
245 fbits = (long) floor(f + 0.5); /* Round */
246
Guido van Rossum74679b41997-01-02 22:21:36 +0000247 /* First byte */
248 *p = (s<<7) | (e>>1);
249 p += incr;
250
251 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000252 *p = (char) (((e&1)<<7) | (fbits>>16));
Guido van Rossum74679b41997-01-02 22:21:36 +0000253 p += incr;
254
255 /* Third byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000256 *p = (fbits>>8) & 0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000257 p += incr;
258
259 /* Fourth byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000260 *p = fbits&0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000261
262 /* Done */
263 return 0;
264}
265
266static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000267pack_double(double x, /* The number to pack */
268 char *p, /* Where to pack the high order byte */
269 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000270{
271 int s;
272 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000273 double f;
Guido van Rossum74679b41997-01-02 22:21:36 +0000274 long fhi, flo;
275
276 if (x < 0) {
277 s = 1;
278 x = -x;
279 }
280 else
281 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000282
283 f = frexp(x, &e);
284
285 /* Normalize f to be in the range [1.0, 2.0) */
286 if (0.5 <= f && f < 1.0) {
287 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000288 e--;
289 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000290 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000291 e = 0;
292 }
293 else {
294 PyErr_SetString(PyExc_SystemError,
295 "frexp() result out of range");
296 return -1;
297 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000298
299 if (e >= 1024) {
300 /* XXX 1024 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000301 PyErr_SetString(PyExc_OverflowError,
302 "float too large to pack with d format");
303 return -1;
304 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000305 else if (e < -1022) {
306 /* Gradual underflow */
307 f = ldexp(f, 1022 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000308 e = 0;
309 }
Guido van Rossum8f3c8121997-11-04 17:12:33 +0000310 else if (!(e == 0 && f == 0.0)) {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000311 e += 1023;
312 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000313 }
314
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000315 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
316 f *= 268435456.0; /* 2**28 */
317 fhi = (long) floor(f); /* Truncate */
318 f -= (double)fhi;
319 f *= 16777216.0; /* 2**24 */
320 flo = (long) floor(f + 0.5); /* Round */
321
Guido van Rossum74679b41997-01-02 22:21:36 +0000322 /* First byte */
323 *p = (s<<7) | (e>>4);
324 p += incr;
325
326 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000327 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum74679b41997-01-02 22:21:36 +0000328 p += incr;
329
330 /* Third byte */
331 *p = (fhi>>16) & 0xFF;
332 p += incr;
333
334 /* Fourth byte */
335 *p = (fhi>>8) & 0xFF;
336 p += incr;
337
338 /* Fifth byte */
339 *p = fhi & 0xFF;
340 p += incr;
341
342 /* Sixth byte */
343 *p = (flo>>16) & 0xFF;
344 p += incr;
345
346 /* Seventh byte */
347 *p = (flo>>8) & 0xFF;
348 p += incr;
349
350 /* Eighth byte */
351 *p = flo & 0xFF;
352 p += incr;
353
354 /* Done */
355 return 0;
356}
357
358static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000359unpack_float(const char *p, /* Where the high order byte is */
360 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000361{
362 int s;
363 int e;
364 long f;
365 double x;
366
367 /* First byte */
368 s = (*p>>7) & 1;
369 e = (*p & 0x7F) << 1;
370 p += incr;
371
372 /* Second byte */
373 e |= (*p>>7) & 1;
374 f = (*p & 0x7F) << 16;
375 p += incr;
376
377 /* Third byte */
378 f |= (*p & 0xFF) << 8;
379 p += incr;
380
381 /* Fourth byte */
382 f |= *p & 0xFF;
383
384 x = (double)f / 8388608.0;
385
386 /* XXX This sadly ignores Inf/NaN issues */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000387 if (e == 0)
388 e = -126;
389 else {
390 x += 1.0;
391 e -= 127;
392 }
393 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000394
395 if (s)
396 x = -x;
397
398 return PyFloat_FromDouble(x);
399}
400
401static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000402unpack_double(const char *p, /* Where the high order byte is */
403 int incr) /* 1 for big-endian; -1 for little-endian */
Guido van Rossum74679b41997-01-02 22:21:36 +0000404{
405 int s;
406 int e;
407 long fhi, flo;
408 double x;
409
410 /* First byte */
411 s = (*p>>7) & 1;
412 e = (*p & 0x7F) << 4;
413 p += incr;
414
415 /* Second byte */
416 e |= (*p>>4) & 0xF;
417 fhi = (*p & 0xF) << 24;
418 p += incr;
419
420 /* Third byte */
421 fhi |= (*p & 0xFF) << 16;
422 p += incr;
423
424 /* Fourth byte */
425 fhi |= (*p & 0xFF) << 8;
426 p += incr;
427
428 /* Fifth byte */
429 fhi |= *p & 0xFF;
430 p += incr;
431
432 /* Sixth byte */
433 flo = (*p & 0xFF) << 16;
434 p += incr;
435
436 /* Seventh byte */
437 flo |= (*p & 0xFF) << 8;
438 p += incr;
439
440 /* Eighth byte */
441 flo |= *p & 0xFF;
442 p += incr;
443
444 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
445 x /= 268435456.0; /* 2**28 */
446
447 /* XXX This sadly ignores Inf/NaN */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000448 if (e == 0)
449 e = -1022;
450 else {
451 x += 1.0;
452 e -= 1023;
453 }
454 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000455
456 if (s)
457 x = -x;
458
459 return PyFloat_FromDouble(x);
460}
461
462
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000463/* The translation function for each format character is table driven */
464
465typedef struct _formatdef {
466 char format;
467 int size;
468 int alignment;
Tim Petersdbd9ba62000-07-09 03:09:57 +0000469 PyObject* (*unpack)(const char *,
470 const struct _formatdef *);
471 int (*pack)(char *, PyObject *,
472 const struct _formatdef *);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000473} formatdef;
474
Tim Peters7b9542a2001-06-10 23:40:19 +0000475/* A large number of small routines follow, with names of the form
476
477 [bln][up]_TYPE
478
479 [bln] distiguishes among big-endian, little-endian and native.
480 [pu] distiguishes between pack (to struct) and unpack (from struct).
481 TYPE is one of char, byte, ubyte, etc.
482*/
483
Tim Peters7a3bfc32001-06-12 01:22:22 +0000484/* Native mode routines. ****************************************************/
Tim Peters7b9542a2001-06-10 23:40:19 +0000485
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000486static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000487nu_char(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000488{
489 return PyString_FromStringAndSize(p, 1);
490}
491
492static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000493nu_byte(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000494{
495 return PyInt_FromLong((long) *(signed char *)p);
496}
497
498static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000499nu_ubyte(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000500{
501 return PyInt_FromLong((long) *(unsigned char *)p);
502}
503
504static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000505nu_short(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000506{
507 return PyInt_FromLong((long) *(short *)p);
508}
509
510static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000511nu_ushort(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000512{
513 return PyInt_FromLong((long) *(unsigned short *)p);
514}
515
516static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000517nu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000518{
519 return PyInt_FromLong((long) *(int *)p);
520}
521
522static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000523nu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000524{
525 unsigned int x = *(unsigned int *)p;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000526 return PyLong_FromUnsignedLong((unsigned long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000527}
528
529static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000530nu_long(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000531{
532 return PyInt_FromLong(*(long *)p);
533}
534
535static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000536nu_ulong(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000537{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000538 return PyLong_FromUnsignedLong(*(unsigned long *)p);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000539}
540
Tim Peters7b9542a2001-06-10 23:40:19 +0000541/* Native mode doesn't support q or Q unless the platform C supports
542 long long (or, on Windows, __int64). */
543
544#ifdef HAVE_LONG_LONG
545
546static PyObject *
547nu_longlong(const char *p, const formatdef *f)
548{
Tim Peters3dac5592001-07-18 20:47:31 +0000549 /* p may not be properly aligned */
550 LONG_LONG x;
551 memcpy(&x, p, sizeof(LONG_LONG));
552 return PyLong_FromLongLong(x);
Tim Peters7b9542a2001-06-10 23:40:19 +0000553}
554
555static PyObject *
556nu_ulonglong(const char *p, const formatdef *f)
557{
Tim Peters3dac5592001-07-18 20:47:31 +0000558 /* p may not be properly aligned */
559 unsigned LONG_LONG x;
560 memcpy(&x, p, sizeof(unsigned LONG_LONG));
561 return PyLong_FromUnsignedLongLong(x);
Tim Peters7b9542a2001-06-10 23:40:19 +0000562}
Tim Peters7b9542a2001-06-10 23:40:19 +0000563#endif
564
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000565static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000566nu_float(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000567{
568 float x;
569 memcpy((char *)&x, p, sizeof(float));
570 return PyFloat_FromDouble((double)x);
571}
572
573static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000574nu_double(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000575{
576 double x;
577 memcpy((char *)&x, p, sizeof(double));
578 return PyFloat_FromDouble(x);
579}
580
Guido van Rossum78694d91998-09-18 14:14:13 +0000581static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000582nu_void_p(const char *p, const formatdef *f)
Guido van Rossum78694d91998-09-18 14:14:13 +0000583{
584 return PyLong_FromVoidPtr(*(void **)p);
585}
586
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000587static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000588np_byte(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000589{
590 long x;
591 if (get_long(v, &x) < 0)
592 return -1;
Martin v. Löwis66de5492000-09-15 07:31:57 +0000593 if (x < -128 || x > 127){
594 PyErr_SetString(StructError,
595 "byte format requires -128<=number<=127");
596 return -1;
597 }
598 *p = (char)x;
599 return 0;
600}
601
602static int
603np_ubyte(char *p, PyObject *v, const formatdef *f)
604{
605 long x;
606 if (get_long(v, &x) < 0)
607 return -1;
608 if (x < 0 || x > 255){
609 PyErr_SetString(StructError,
610 "ubyte format requires 0<=number<=255");
611 return -1;
612 }
Guido van Rossum7844e381997-04-11 20:44:04 +0000613 *p = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000614 return 0;
615}
616
617static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000618np_char(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000619{
620 if (!PyString_Check(v) || PyString_Size(v) != 1) {
621 PyErr_SetString(StructError,
622 "char format require string of length 1");
623 return -1;
624 }
625 *p = *PyString_AsString(v);
626 return 0;
627}
628
629static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000630np_short(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000631{
632 long x;
633 if (get_long(v, &x) < 0)
634 return -1;
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000635 if (x < SHRT_MIN || x > SHRT_MAX){
Martin v. Löwis66de5492000-09-15 07:31:57 +0000636 PyErr_SetString(StructError,
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000637 "short format requires " STRINGIFY(SHRT_MIN)
638 "<=number<=" STRINGIFY(SHRT_MAX));
Martin v. Löwis66de5492000-09-15 07:31:57 +0000639 return -1;
640 }
Guido van Rossum7844e381997-04-11 20:44:04 +0000641 * (short *)p = (short)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000642 return 0;
643}
644
645static int
Martin v. Löwis66de5492000-09-15 07:31:57 +0000646np_ushort(char *p, PyObject *v, const formatdef *f)
647{
648 long x;
649 if (get_long(v, &x) < 0)
650 return -1;
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000651 if (x < 0 || x > USHRT_MAX){
Martin v. Löwis66de5492000-09-15 07:31:57 +0000652 PyErr_SetString(StructError,
Martin v. Löwis2af72d52000-09-15 08:10:33 +0000653 "short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
Martin v. Löwis66de5492000-09-15 07:31:57 +0000654 return -1;
655 }
656 * (unsigned short *)p = (unsigned short)x;
657 return 0;
658}
659
660static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000661np_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000662{
663 long x;
664 if (get_long(v, &x) < 0)
665 return -1;
666 * (int *)p = x;
667 return 0;
668}
669
670static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000671np_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000672{
673 unsigned long x;
674 if (get_ulong(v, &x) < 0)
675 return -1;
676 * (unsigned int *)p = x;
677 return 0;
678}
679
680static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000681np_long(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000682{
683 long x;
684 if (get_long(v, &x) < 0)
685 return -1;
686 * (long *)p = x;
687 return 0;
688}
689
690static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000691np_ulong(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000692{
693 unsigned long x;
694 if (get_ulong(v, &x) < 0)
695 return -1;
696 * (unsigned long *)p = x;
697 return 0;
698}
699
Tim Peters7b9542a2001-06-10 23:40:19 +0000700#ifdef HAVE_LONG_LONG
701
702static int
703np_longlong(char *p, PyObject *v, const formatdef *f)
704{
705 LONG_LONG x;
706 if (get_longlong(v, &x) < 0)
707 return -1;
Tim Peters3dac5592001-07-18 20:47:31 +0000708 memcpy(p, &x, sizeof(LONG_LONG));
Tim Peters7b9542a2001-06-10 23:40:19 +0000709 return 0;
710}
711
712static int
713np_ulonglong(char *p, PyObject *v, const formatdef *f)
714{
715 unsigned LONG_LONG x;
716 if (get_ulonglong(v, &x) < 0)
717 return -1;
Tim Peters3dac5592001-07-18 20:47:31 +0000718 memcpy(p, &x, sizeof(unsigned LONG_LONG));
Tim Peters7b9542a2001-06-10 23:40:19 +0000719 return 0;
720}
Tim Peters7b9542a2001-06-10 23:40:19 +0000721#endif
722
Guido van Rossum60c50611996-12-31 16:29:52 +0000723static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000724np_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000725{
726 float x = (float)PyFloat_AsDouble(v);
727 if (x == -1 && PyErr_Occurred()) {
728 PyErr_SetString(StructError,
729 "required argument is not a float");
730 return -1;
731 }
732 memcpy(p, (char *)&x, sizeof(float));
733 return 0;
734}
735
736static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000737np_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000738{
739 double x = PyFloat_AsDouble(v);
740 if (x == -1 && PyErr_Occurred()) {
741 PyErr_SetString(StructError,
742 "required argument is not a float");
743 return -1;
744 }
745 memcpy(p, (char *)&x, sizeof(double));
746 return 0;
747}
748
Guido van Rossum78694d91998-09-18 14:14:13 +0000749static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000750np_void_p(char *p, PyObject *v, const formatdef *f)
Guido van Rossum78694d91998-09-18 14:14:13 +0000751{
752 void *x = PyLong_AsVoidPtr(v);
753 if (x == NULL && PyErr_Occurred()) {
754 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
755 if (PyErr_ExceptionMatches(PyExc_TypeError))
756 PyErr_SetString(StructError,
757 "required argument is not an integer");
758 return -1;
759 }
760 *(void **)p = x;
761 return 0;
762}
763
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000764static formatdef native_table[] = {
765 {'x', sizeof(char), 0, NULL},
766 {'b', sizeof(char), 0, nu_byte, np_byte},
Martin v. Löwis66de5492000-09-15 07:31:57 +0000767 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000768 {'c', sizeof(char), 0, nu_char, np_char},
769 {'s', sizeof(char), 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000770 {'p', sizeof(char), 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000771 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
Martin v. Löwis66de5492000-09-15 07:31:57 +0000772 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000773 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000774 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000775 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
Guido van Rossum60c50611996-12-31 16:29:52 +0000776 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000777 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
778 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
Guido van Rossum78694d91998-09-18 14:14:13 +0000779 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Tim Peters7b9542a2001-06-10 23:40:19 +0000780#ifdef HAVE_LONG_LONG
781 {'q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
782 {'Q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
Tim Peters7b9542a2001-06-10 23:40:19 +0000783#endif
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000784 {0}
785};
786
Tim Peters7a3bfc32001-06-12 01:22:22 +0000787/* Big-endian routines. *****************************************************/
788
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000789static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000790bu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000791{
792 long x = 0;
793 int i = f->size;
794 do {
795 x = (x<<8) | (*p++ & 0xFF);
796 } while (--i > 0);
Tim Petersf0e717b2001-04-08 23:39:38 +0000797 /* Extend the sign bit. */
798 if (SIZEOF_LONG > f->size)
799 x |= -(x & (1L << (8*f->size - 1)));
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000800 return PyInt_FromLong(x);
801}
802
803static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000804bu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000805{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000806 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000807 int i = f->size;
808 do {
809 x = (x<<8) | (*p++ & 0xFF);
810 } while (--i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000811 if (f->size >= 4)
812 return PyLong_FromUnsignedLong(x);
813 else
814 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000815}
816
Guido van Rossum74679b41997-01-02 22:21:36 +0000817static PyObject *
Tim Peters7a3bfc32001-06-12 01:22:22 +0000818bu_longlong(const char *p, const formatdef *f)
819{
820 return _PyLong_FromByteArray((const unsigned char *)p,
821 8,
822 0, /* little-endian */
823 1 /* signed */);
824}
825
826static PyObject *
827bu_ulonglong(const char *p, const formatdef *f)
828{
829 return _PyLong_FromByteArray((const unsigned char *)p,
830 8,
831 0, /* little-endian */
832 0 /* signed */);
833}
834
835static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000836bu_float(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000837{
838 return unpack_float(p, 1);
839}
840
841static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000842bu_double(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000843{
844 return unpack_double(p, 1);
845}
846
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000847static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000848bp_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000849{
850 long x;
851 int i;
852 if (get_long(v, &x) < 0)
853 return -1;
854 i = f->size;
855 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000856 p[--i] = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000857 x >>= 8;
858 } while (i > 0);
859 return 0;
860}
861
Guido van Rossum60c50611996-12-31 16:29:52 +0000862static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000863bp_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +0000864{
865 unsigned long x;
866 int i;
867 if (get_ulong(v, &x) < 0)
868 return -1;
869 i = f->size;
870 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000871 p[--i] = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000872 x >>= 8;
873 } while (i > 0);
874 return 0;
875}
876
Guido van Rossum74679b41997-01-02 22:21:36 +0000877static int
Tim Peters7a3bfc32001-06-12 01:22:22 +0000878bp_longlong(char *p, PyObject *v, const formatdef *f)
879{
880 int res;
881 v = get_pylong(v);
Tim Petersda9c5b32001-06-13 01:26:35 +0000882 if (v == NULL)
883 return -1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000884 res = _PyLong_AsByteArray((PyLongObject *)v,
885 (unsigned char *)p,
886 8,
887 0, /* little_endian */
888 1 /* signed */);
889 Py_DECREF(v);
890 return res;
891}
892
893static int
894bp_ulonglong(char *p, PyObject *v, const formatdef *f)
895{
896 int res;
897 v = get_pylong(v);
Tim Petersda9c5b32001-06-13 01:26:35 +0000898 if (v == NULL)
899 return -1;
Tim Peters7a3bfc32001-06-12 01:22:22 +0000900 res = _PyLong_AsByteArray((PyLongObject *)v,
901 (unsigned char *)p,
902 8,
903 0, /* little_endian */
904 0 /* signed */);
905 Py_DECREF(v);
906 return res;
907}
908
909static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000910bp_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000911{
912 double x = PyFloat_AsDouble(v);
913 if (x == -1 && PyErr_Occurred()) {
914 PyErr_SetString(StructError,
915 "required argument is not a float");
916 return -1;
917 }
918 return pack_float(x, p, 1);
919}
920
921static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000922bp_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +0000923{
924 double x = PyFloat_AsDouble(v);
925 if (x == -1 && PyErr_Occurred()) {
926 PyErr_SetString(StructError,
927 "required argument is not a float");
928 return -1;
929 }
930 return pack_double(x, p, 1);
931}
932
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000933static formatdef bigendian_table[] = {
934 {'x', 1, 0, NULL},
935 {'b', 1, 0, bu_int, bp_int},
936 {'B', 1, 0, bu_uint, bp_int},
937 {'c', 1, 0, nu_char, np_char},
938 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +0000939 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000940 {'h', 2, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000941 {'H', 2, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000942 {'i', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000943 {'I', 4, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000944 {'l', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000945 {'L', 4, 0, bu_uint, bp_uint},
Tim Peters7a3bfc32001-06-12 01:22:22 +0000946 {'q', 8, 0, bu_longlong, bp_longlong},
947 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
Guido van Rossum74679b41997-01-02 22:21:36 +0000948 {'f', 4, 0, bu_float, bp_float},
949 {'d', 8, 0, bu_double, bp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000950 {0}
951};
952
Tim Peters7a3bfc32001-06-12 01:22:22 +0000953/* Little-endian routines. *****************************************************/
954
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000955static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000956lu_int(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000957{
958 long x = 0;
959 int i = f->size;
960 do {
961 x = (x<<8) | (p[--i] & 0xFF);
962 } while (i > 0);
Tim Petersf0e717b2001-04-08 23:39:38 +0000963 /* Extend the sign bit. */
964 if (SIZEOF_LONG > f->size)
965 x |= -(x & (1L << (8*f->size - 1)));
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000966 return PyInt_FromLong(x);
967}
968
969static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +0000970lu_uint(const char *p, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000971{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000972 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000973 int i = f->size;
974 do {
975 x = (x<<8) | (p[--i] & 0xFF);
976 } while (i > 0);
Guido van Rossum39ef2271998-06-29 04:00:40 +0000977 if (f->size >= 4)
978 return PyLong_FromUnsignedLong(x);
979 else
980 return PyInt_FromLong((long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000981}
982
Guido van Rossum74679b41997-01-02 22:21:36 +0000983static PyObject *
Tim Peters7a3bfc32001-06-12 01:22:22 +0000984lu_longlong(const char *p, const formatdef *f)
985{
986 return _PyLong_FromByteArray((const unsigned char *)p,
987 8,
988 1, /* little-endian */
989 1 /* signed */);
990}
991
992static PyObject *
993lu_ulonglong(const char *p, const formatdef *f)
994{
995 return _PyLong_FromByteArray((const unsigned char *)p,
996 8,
997 1, /* little-endian */
998 0 /* signed */);
999}
1000
1001static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001002lu_float(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001003{
1004 return unpack_float(p+3, -1);
1005}
1006
1007static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001008lu_double(const char *p, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001009{
1010 return unpack_double(p+7, -1);
1011}
1012
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001013static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001014lp_int(char *p, PyObject *v, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001015{
1016 long x;
1017 int i;
1018 if (get_long(v, &x) < 0)
1019 return -1;
1020 i = f->size;
1021 do {
Guido van Rossum7844e381997-04-11 20:44:04 +00001022 *p++ = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001023 x >>= 8;
1024 } while (--i > 0);
1025 return 0;
1026}
1027
Guido van Rossum60c50611996-12-31 16:29:52 +00001028static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001029lp_uint(char *p, PyObject *v, const formatdef *f)
Guido van Rossum60c50611996-12-31 16:29:52 +00001030{
1031 unsigned long x;
1032 int i;
1033 if (get_ulong(v, &x) < 0)
1034 return -1;
1035 i = f->size;
1036 do {
Guido van Rossum7844e381997-04-11 20:44:04 +00001037 *p++ = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +00001038 x >>= 8;
1039 } while (--i > 0);
1040 return 0;
1041}
1042
Guido van Rossum74679b41997-01-02 22:21:36 +00001043static int
Tim Peters7a3bfc32001-06-12 01:22:22 +00001044lp_longlong(char *p, PyObject *v, const formatdef *f)
1045{
1046 int res;
1047 v = get_pylong(v);
Tim Petersda9c5b32001-06-13 01:26:35 +00001048 if (v == NULL)
1049 return -1;
Tim Peters7a3bfc32001-06-12 01:22:22 +00001050 res = _PyLong_AsByteArray((PyLongObject*)v,
1051 (unsigned char *)p,
1052 8,
1053 1, /* little_endian */
1054 1 /* signed */);
1055 Py_DECREF(v);
1056 return res;
1057}
1058
1059static int
1060lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1061{
1062 int res;
1063 v = get_pylong(v);
Tim Petersda9c5b32001-06-13 01:26:35 +00001064 if (v == NULL)
1065 return -1;
Tim Peters7a3bfc32001-06-12 01:22:22 +00001066 res = _PyLong_AsByteArray((PyLongObject*)v,
1067 (unsigned char *)p,
1068 8,
1069 1, /* little_endian */
1070 0 /* signed */);
1071 Py_DECREF(v);
1072 return res;
1073}
1074
1075static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001076lp_float(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001077{
1078 double x = PyFloat_AsDouble(v);
1079 if (x == -1 && PyErr_Occurred()) {
1080 PyErr_SetString(StructError,
1081 "required argument is not a float");
1082 return -1;
1083 }
1084 return pack_float(x, p+3, -1);
1085}
1086
1087static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001088lp_double(char *p, PyObject *v, const formatdef *f)
Guido van Rossum74679b41997-01-02 22:21:36 +00001089{
1090 double x = PyFloat_AsDouble(v);
1091 if (x == -1 && PyErr_Occurred()) {
1092 PyErr_SetString(StructError,
1093 "required argument is not a float");
1094 return -1;
1095 }
1096 return pack_double(x, p+7, -1);
1097}
1098
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001099static formatdef lilendian_table[] = {
1100 {'x', 1, 0, NULL},
1101 {'b', 1, 0, lu_int, lp_int},
1102 {'B', 1, 0, lu_uint, lp_int},
1103 {'c', 1, 0, nu_char, np_char},
1104 {'s', 1, 0, NULL},
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001105 {'p', 1, 0, NULL},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001106 {'h', 2, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001107 {'H', 2, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001108 {'i', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001109 {'I', 4, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001110 {'l', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +00001111 {'L', 4, 0, lu_uint, lp_uint},
Tim Peters7a3bfc32001-06-12 01:22:22 +00001112 {'q', 8, 0, lu_longlong, lp_longlong},
1113 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
Guido van Rossum74679b41997-01-02 22:21:36 +00001114 {'f', 4, 0, lu_float, lp_float},
1115 {'d', 8, 0, lu_double, lp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001116 {0}
1117};
1118
1119
1120static const formatdef *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001121whichtable(char **pfmt)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001122{
1123 const char *fmt = (*pfmt)++; /* May be backed out of later */
1124 switch (*fmt) {
1125 case '<':
1126 return lilendian_table;
1127 case '>':
1128 case '!': /* Network byte order is big-endian */
1129 return bigendian_table;
1130 case '=': { /* Host byte order -- different from native in aligment! */
1131 int n = 1;
1132 char *p = (char *) &n;
1133 if (*p == 1)
1134 return lilendian_table;
1135 else
1136 return bigendian_table;
1137 }
1138 default:
1139 --*pfmt; /* Back out of pointer increment */
1140 /* Fall through */
1141 case '@':
1142 return native_table;
1143 }
1144}
1145
1146
1147/* Get the table entry for a format code */
1148
1149static const formatdef *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001150getentry(int c, const formatdef *f)
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001151{
1152 for (; f->format != '\0'; f++) {
1153 if (f->format == c) {
1154 return f;
1155 }
1156 }
1157 PyErr_SetString(StructError, "bad char in struct format");
1158 return NULL;
1159}
1160
1161
Guido van Rossum02975121992-08-17 08:55:12 +00001162/* Align a size according to a format code */
1163
1164static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001165align(int size, int c, const formatdef *e)
Guido van Rossum02975121992-08-17 08:55:12 +00001166{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001167 if (e->format == c) {
1168 if (e->alignment) {
1169 size = ((size + e->alignment - 1)
1170 / e->alignment)
1171 * e->alignment;
1172 }
Guido van Rossum02975121992-08-17 08:55:12 +00001173 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001174 return size;
Guido van Rossum02975121992-08-17 08:55:12 +00001175}
1176
1177
1178/* calculate the size of a format string */
1179
1180static int
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001181calcsize(const char *fmt, const formatdef *f)
Guido van Rossum02975121992-08-17 08:55:12 +00001182{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001183 const formatdef *e;
1184 const char *s;
Guido van Rossum02975121992-08-17 08:55:12 +00001185 char c;
1186 int size, num, itemsize, x;
1187
1188 s = fmt;
1189 size = 0;
1190 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001191 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001192 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001193 if ('0' <= c && c <= '9') {
1194 num = c - '0';
1195 while ('0' <= (c = *s++) && c <= '9') {
1196 x = num*10 + (c - '0');
1197 if (x/10 != num) {
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001198 PyErr_SetString(
1199 StructError,
1200 "overflow in item count");
Guido van Rossum02975121992-08-17 08:55:12 +00001201 return -1;
1202 }
1203 num = x;
1204 }
1205 if (c == '\0')
1206 break;
1207 }
1208 else
1209 num = 1;
Tim Peters2d4e0772001-06-11 16:57:33 +00001210
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001211 e = getentry(c, f);
1212 if (e == NULL)
Guido van Rossum02975121992-08-17 08:55:12 +00001213 return -1;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001214 itemsize = e->size;
1215 size = align(size, c, e);
Guido van Rossum02975121992-08-17 08:55:12 +00001216 x = num * itemsize;
1217 size += x;
1218 if (x/itemsize != num || size < 0) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001219 PyErr_SetString(StructError,
1220 "total struct size too long");
Guido van Rossum02975121992-08-17 08:55:12 +00001221 return -1;
1222 }
Guido van Rossum02975121992-08-17 08:55:12 +00001223 }
1224
1225 return size;
1226}
1227
1228
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001229PyDoc_STRVAR(calcsize__doc__,
1230"calcsize(fmt) -> int\n\
Guido van Rossum414fd481997-12-19 04:24:24 +00001231Return size of C struct described by format string fmt.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001232See struct.__doc__ for more on format strings.");
Guido van Rossum02975121992-08-17 08:55:12 +00001233
Barry Warsaw30695fa1996-12-12 23:32:31 +00001234static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001235struct_calcsize(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001236{
1237 char *fmt;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001238 const formatdef *f;
Guido van Rossum02975121992-08-17 08:55:12 +00001239 int size;
1240
Guido van Rossum43713e52000-02-29 13:59:29 +00001241 if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001242 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001243 f = whichtable(&fmt);
1244 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001245 if (size < 0)
1246 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001247 return PyInt_FromLong((long)size);
Guido van Rossum02975121992-08-17 08:55:12 +00001248}
1249
1250
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001251PyDoc_STRVAR(pack__doc__,
1252"pack(fmt, v1, v2, ...) -> string\n\
Guido van Rossum414fd481997-12-19 04:24:24 +00001253Return string containing values v1, v2, ... packed according to fmt.\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001254See struct.__doc__ for more on format strings.");
Guido van Rossum02975121992-08-17 08:55:12 +00001255
Barry Warsaw30695fa1996-12-12 23:32:31 +00001256static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001257struct_pack(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001258{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001259 const formatdef *f, *e;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001260 PyObject *format, *result, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001261 char *fmt;
1262 int size, num;
1263 int i, n;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001264 char *s, *res, *restart, *nres;
Guido van Rossum02975121992-08-17 08:55:12 +00001265 char c;
Guido van Rossum02975121992-08-17 08:55:12 +00001266
Barry Warsaw30695fa1996-12-12 23:32:31 +00001267 if (args == NULL || !PyTuple_Check(args) ||
1268 (n = PyTuple_Size(args)) < 1)
1269 {
Tim Peters2d4e0772001-06-11 16:57:33 +00001270 PyErr_SetString(PyExc_TypeError,
Fred Drake137507e2000-06-01 02:02:46 +00001271 "struct.pack requires at least one argument");
Guido van Rossum02975121992-08-17 08:55:12 +00001272 return NULL;
1273 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001274 format = PyTuple_GetItem(args, 0);
Neal Norwitz187ae562002-04-02 18:17:57 +00001275 fmt = PyString_AsString(format);
1276 if (!fmt)
Guido van Rossum02975121992-08-17 08:55:12 +00001277 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001278 f = whichtable(&fmt);
1279 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001280 if (size < 0)
1281 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001282 result = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum02975121992-08-17 08:55:12 +00001283 if (result == NULL)
1284 return NULL;
1285
1286 s = fmt;
1287 i = 1;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001288 res = restart = PyString_AsString(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001289
1290 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001291 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001292 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001293 if ('0' <= c && c <= '9') {
1294 num = c - '0';
1295 while ('0' <= (c = *s++) && c <= '9')
1296 num = num*10 + (c - '0');
1297 if (c == '\0')
1298 break;
1299 }
1300 else
1301 num = 1;
1302
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001303 e = getentry(c, f);
1304 if (e == NULL)
1305 goto fail;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001306 nres = restart + align((int)(res-restart), c, e);
1307 /* Fill padd bytes with zeros */
1308 while (res < nres)
1309 *res++ = '\0';
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001310 if (num == 0 && c != 's')
1311 continue;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001312 do {
1313 if (c == 'x') {
1314 /* doesn't consume arguments */
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001315 memset(res, '\0', num);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001316 res += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001317 break;
Guido van Rossum02975121992-08-17 08:55:12 +00001318 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001319 if (i >= n) {
1320 PyErr_SetString(StructError,
1321 "insufficient arguments to pack");
1322 goto fail;
1323 }
1324 v = PyTuple_GetItem(args, i++);
1325 if (v == NULL)
1326 goto fail;
1327 if (c == 's') {
1328 /* num is string size, not repeat count */
1329 int n;
1330 if (!PyString_Check(v)) {
1331 PyErr_SetString(StructError,
1332 "argument for 's' must be a string");
1333 goto fail;
1334 }
1335 n = PyString_Size(v);
1336 if (n > num)
1337 n = num;
1338 if (n > 0)
1339 memcpy(res, PyString_AsString(v), n);
1340 if (n < num)
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001341 memset(res+n, '\0', num-n);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001342 res += num;
1343 break;
1344 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001345 else if (c == 'p') {
1346 /* num is string size + 1,
1347 to fit in the count byte */
1348 int n;
1349 num--; /* now num is max string size */
1350 if (!PyString_Check(v)) {
1351 PyErr_SetString(StructError,
1352 "argument for 'p' must be a string");
1353 goto fail;
1354 }
1355 n = PyString_Size(v);
1356 if (n > num)
1357 n = num;
1358 if (n > 0)
1359 memcpy(res+1, PyString_AsString(v), n);
1360 if (n < num)
1361 /* no real need, just to be nice */
1362 memset(res+1+n, '\0', num-n);
Tim Peters0891ac02001-09-15 02:35:15 +00001363 if (n > 255)
1364 n = 255;
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001365 *res++ = n; /* store the length byte */
1366 res += num;
1367 break;
1368 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001369 else {
1370 if (e->pack(res, v, e) < 0)
1371 goto fail;
1372 res += e->size;
1373 }
1374 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001375 }
1376
1377 if (i < n) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001378 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001379 "too many arguments for pack format");
Guido van Rossum02975121992-08-17 08:55:12 +00001380 goto fail;
1381 }
1382
1383 return result;
1384
1385 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001386 Py_DECREF(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001387 return NULL;
1388}
1389
1390
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001391PyDoc_STRVAR(unpack__doc__,
1392"unpack(fmt, string) -> (v1, v2, ...)\n\
Guido van Rossum9897f0f1997-12-21 06:46:20 +00001393Unpack the string, containing packed C structure data, according\n\
1394to fmt. Requires len(string)==calcsize(fmt).\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001395See struct.__doc__ for more on format strings.");
Guido van Rossum02975121992-08-17 08:55:12 +00001396
Barry Warsaw30695fa1996-12-12 23:32:31 +00001397static PyObject *
Peter Schneider-Kamp41c36ff2000-07-10 12:29:26 +00001398struct_unpack(PyObject *self, PyObject *args)
Guido van Rossum02975121992-08-17 08:55:12 +00001399{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001400 const formatdef *f, *e;
Guido van Rossum02975121992-08-17 08:55:12 +00001401 char *str, *start, *fmt, *s;
1402 char c;
Barry Warsawb9a781e1997-01-03 00:26:28 +00001403 int len, size, num;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001404 PyObject *res, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001405
Guido van Rossum43713e52000-02-29 13:59:29 +00001406 if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
Guido van Rossum02975121992-08-17 08:55:12 +00001407 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001408 f = whichtable(&fmt);
1409 size = calcsize(fmt, f);
1410 if (size < 0)
1411 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +00001412 if (size != len) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001413 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001414 "unpack str size does not match format");
Guido van Rossum02975121992-08-17 08:55:12 +00001415 return NULL;
1416 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001417 res = PyList_New(0);
Guido van Rossum02975121992-08-17 08:55:12 +00001418 if (res == NULL)
1419 return NULL;
1420 str = start;
1421 s = fmt;
1422 while ((c = *s++) != '\0') {
Guido van Rossum730806d1998-04-10 22:27:42 +00001423 if (isspace((int)c))
Guido van Rossume20aef51997-08-26 20:39:54 +00001424 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001425 if ('0' <= c && c <= '9') {
1426 num = c - '0';
1427 while ('0' <= (c = *s++) && c <= '9')
1428 num = num*10 + (c - '0');
1429 if (c == '\0')
1430 break;
1431 }
1432 else
1433 num = 1;
1434
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001435 e = getentry(c, f);
1436 if (e == NULL)
1437 goto fail;
1438 str = start + align((int)(str-start), c, e);
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001439 if (num == 0 && c != 's')
1440 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001441
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001442 do {
1443 if (c == 'x') {
1444 str += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001445 break;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001446 }
1447 if (c == 's') {
1448 /* num is string size, not repeat count */
1449 v = PyString_FromStringAndSize(str, num);
1450 if (v == NULL)
1451 goto fail;
1452 str += num;
1453 num = 0;
1454 }
Guido van Rossum9eb671f1997-09-05 07:08:39 +00001455 else if (c == 'p') {
1456 /* num is string buffer size,
1457 not repeat count */
1458 int n = *(unsigned char*)str;
1459 /* first byte (unsigned) is string size */
1460 if (n >= num)
1461 n = num-1;
1462 v = PyString_FromStringAndSize(str+1, n);
1463 if (v == NULL)
1464 goto fail;
1465 str += num;
1466 num = 0;
1467 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001468 else {
1469 v = e->unpack(str, e);
1470 if (v == NULL)
1471 goto fail;
1472 str += e->size;
Guido van Rossum02975121992-08-17 08:55:12 +00001473 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001474 if (v == NULL || PyList_Append(res, v) < 0)
Guido van Rossum02975121992-08-17 08:55:12 +00001475 goto fail;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001476 Py_DECREF(v);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001477 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001478 }
1479
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001480 v = PyList_AsTuple(res);
1481 Py_DECREF(res);
1482 return v;
Guido van Rossum02975121992-08-17 08:55:12 +00001483
1484 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001485 Py_DECREF(res);
Guido van Rossum02975121992-08-17 08:55:12 +00001486 return NULL;
1487}
1488
Guido van Rossum90ddb7b1992-08-19 16:44:15 +00001489
Guido van Rossum02975121992-08-17 08:55:12 +00001490/* List of functions */
1491
Barry Warsaw30695fa1996-12-12 23:32:31 +00001492static PyMethodDef struct_methods[] = {
Guido van Rossum414fd481997-12-19 04:24:24 +00001493 {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__},
1494 {"pack", struct_pack, METH_VARARGS, pack__doc__},
1495 {"unpack", struct_unpack, METH_VARARGS, unpack__doc__},
Guido van Rossum02975121992-08-17 08:55:12 +00001496 {NULL, NULL} /* sentinel */
1497};
1498
1499
1500/* Module initialization */
1501
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001502PyMODINIT_FUNC
Thomas Woutersf3f33dc2000-07-21 06:00:07 +00001503initstruct(void)
Guido van Rossum02975121992-08-17 08:55:12 +00001504{
Fred Drake78f6c862002-02-14 07:11:23 +00001505 PyObject *m;
Guido van Rossum02975121992-08-17 08:55:12 +00001506
1507 /* Create the module and add the functions */
Guido van Rossum414fd481997-12-19 04:24:24 +00001508 m = Py_InitModule4("struct", struct_methods, struct__doc__,
1509 (PyObject*)NULL, PYTHON_API_VERSION);
Guido van Rossum02975121992-08-17 08:55:12 +00001510
1511 /* Add some symbolic constants to the module */
Fred Drake78f6c862002-02-14 07:11:23 +00001512 if (StructError == NULL) {
1513 StructError = PyErr_NewException("struct.error", NULL, NULL);
1514 if (StructError == NULL)
1515 return;
1516 }
1517 Py_INCREF(StructError);
Fred Drake2eeec9b2002-02-14 07:16:30 +00001518 PyModule_AddObject(m, "error", StructError);
Guido van Rossum02975121992-08-17 08:55:12 +00001519}