blob: dbba9b48f20eb44c99ac266b88a43b643aa280c4 [file] [log] [blame]
Guido van Rossum02975121992-08-17 08:55:12 +00001/***********************************************************
Guido van Rossum524b5881995-01-04 19:10:35 +00002Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam,
3The Netherlands.
Guido van Rossum02975121992-08-17 08:55:12 +00004
5 All Rights Reserved
6
Guido van Rossumd266eb41996-10-25 14:44:06 +00007Permission to use, copy, modify, and distribute this software and its
8documentation for any purpose and without fee is hereby granted,
Guido van Rossum02975121992-08-17 08:55:12 +00009provided that the above copyright notice appear in all copies and that
Guido van Rossumd266eb41996-10-25 14:44:06 +000010both that copyright notice and this permission notice appear in
Guido van Rossum02975121992-08-17 08:55:12 +000011supporting documentation, and that the names of Stichting Mathematisch
Guido van Rossumd266eb41996-10-25 14:44:06 +000012Centrum or CWI or Corporation for National Research Initiatives or
13CNRI not be used in advertising or publicity pertaining to
14distribution of the software without specific, written prior
15permission.
Guido van Rossum02975121992-08-17 08:55:12 +000016
Guido van Rossumd266eb41996-10-25 14:44:06 +000017While CWI is the initial source for this software, a modified version
18is made available by the Corporation for National Research Initiatives
19(CNRI) at the Internet address ftp://ftp.python.org.
20
21STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH
22REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF
23MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH
24CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL
25DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR
26PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
27TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
28PERFORMANCE OF THIS SOFTWARE.
Guido van Rossum02975121992-08-17 08:55:12 +000029
30******************************************************************/
31
32/* struct module -- pack values into and (out of) strings */
33
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000034/* New version supporting byte order, alignment and size options,
35 character strings, and unsigned numbers */
36
Barry Warsaw30695fa1996-12-12 23:32:31 +000037#include "Python.h"
Guido van Rossum74679b41997-01-02 22:21:36 +000038#include "mymath.h"
Guido van Rossum02975121992-08-17 08:55:12 +000039
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000040#include <limits.h>
41
42
43/* Exception */
44
Barry Warsaw30695fa1996-12-12 23:32:31 +000045static PyObject *StructError;
Guido van Rossum02975121992-08-17 08:55:12 +000046
47
48/* Define various structs to figure out the alignments of types */
49
Jack Jansen971e1df1995-02-02 14:29:10 +000050#ifdef __MWERKS__
51/*
52** XXXX We have a problem here. There are no unique alignment rules
53** on the PowerPC mac.
54*/
55#ifdef __powerc
56#pragma options align=mac68k
57#endif
58#endif /* __MWERKS__ */
59
Guido van Rossum02975121992-08-17 08:55:12 +000060typedef struct { char c; short x; } s_short;
61typedef struct { char c; int x; } s_int;
62typedef struct { char c; long x; } s_long;
63typedef struct { char c; float x; } s_float;
64typedef struct { char c; double x; } s_double;
65
66#define SHORT_ALIGN (sizeof(s_short) - sizeof(short))
67#define INT_ALIGN (sizeof(s_int) - sizeof(int))
68#define LONG_ALIGN (sizeof(s_long) - sizeof(long))
69#define FLOAT_ALIGN (sizeof(s_float) - sizeof(float))
70#define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double))
71
Jack Jansen971e1df1995-02-02 14:29:10 +000072#ifdef __powerc
73#pragma options align=reset
74#endif
75
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +000076/* Helper routine to get a Python integer and raise the appropriate error
77 if it isn't one */
78
79static int
80get_long(v, p)
81 PyObject *v;
82 long *p;
83{
84 long x = PyInt_AsLong(v);
85 if (x == -1 && PyErr_Occurred()) {
86 if (PyErr_Occurred() == PyExc_TypeError)
87 PyErr_SetString(StructError,
88 "required argument is not an integer");
89 return -1;
90 }
91 *p = x;
92 return 0;
93}
94
95
Guido van Rossum60c50611996-12-31 16:29:52 +000096/* Same, but handling unsigned long */
97
98static int
99get_ulong(v, p)
100 PyObject *v;
101 unsigned long *p;
102{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000103 if (PyLong_Check(v)) {
104 unsigned long x = PyLong_AsUnsignedLong(v);
105 if (x == (unsigned long)(-1) && PyErr_Occurred())
Guido van Rossum60c50611996-12-31 16:29:52 +0000106 return -1;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000107 *p = x;
108 return 0;
Guido van Rossum60c50611996-12-31 16:29:52 +0000109 }
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000110 else {
111 return get_long(v, (long *)p);
112 }
Guido van Rossum60c50611996-12-31 16:29:52 +0000113}
114
115
Guido van Rossum74679b41997-01-02 22:21:36 +0000116/* Floating point helpers */
117
118/* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
119 Point Arithmetic). See the following URL:
120 http://www.psc.edu/general/software/packages/ieee/ieee.html */
121
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000122/* XXX Inf/NaN are not handled quite right (but underflow is!) */
Guido van Rossum74679b41997-01-02 22:21:36 +0000123
124static int
125pack_float(x, p, incr)
126 double x; /* The number to pack */
127 char *p; /* Where to pack the high order byte */
128 int incr; /* 1 for big-endian; -1 for little-endian */
129{
130 int s;
131 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000132 double f;
133 long fbits;
Guido van Rossum74679b41997-01-02 22:21:36 +0000134
135 if (x < 0) {
136 s = 1;
137 x = -x;
138 }
139 else
140 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000141
142 f = frexp(x, &e);
143
144 /* Normalize f to be in the range [1.0, 2.0) */
145 if (0.5 <= f && f < 1.0) {
146 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000147 e--;
148 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000149 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000150 e = 0;
151 }
152 else {
153 PyErr_SetString(PyExc_SystemError,
154 "frexp() result out of range");
155 return -1;
156 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000157
158 if (e >= 128) {
159 /* XXX 128 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000160 PyErr_SetString(PyExc_OverflowError,
161 "float too large to pack with f format");
162 return -1;
163 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000164 else if (e < -126) {
165 /* Gradual underflow */
166 f = ldexp(f, 126 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000167 e = 0;
168 }
Guido van Rossum74679b41997-01-02 22:21:36 +0000169 else {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000170 e += 127;
171 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000172 }
173
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000174 f *= 8388608.0; /* 2**23 */
175 fbits = (long) floor(f + 0.5); /* Round */
176
Guido van Rossum74679b41997-01-02 22:21:36 +0000177 /* First byte */
178 *p = (s<<7) | (e>>1);
179 p += incr;
180
181 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000182 *p = (char) (((e&1)<<7) | (fbits>>16));
Guido van Rossum74679b41997-01-02 22:21:36 +0000183 p += incr;
184
185 /* Third byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000186 *p = (fbits>>8) & 0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000187 p += incr;
188
189 /* Fourth byte */
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000190 *p = fbits&0xFF;
Guido van Rossum74679b41997-01-02 22:21:36 +0000191
192 /* Done */
193 return 0;
194}
195
196static int
197pack_double(x, p, incr)
198 double x; /* The number to pack */
199 char *p; /* Where to pack the high order byte */
200 int incr; /* 1 for big-endian; -1 for little-endian */
201{
202 int s;
203 int e;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000204 double f;
Guido van Rossum74679b41997-01-02 22:21:36 +0000205 long fhi, flo;
206
207 if (x < 0) {
208 s = 1;
209 x = -x;
210 }
211 else
212 s = 0;
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000213
214 f = frexp(x, &e);
215
216 /* Normalize f to be in the range [1.0, 2.0) */
217 if (0.5 <= f && f < 1.0) {
218 f *= 2.0;
Guido van Rossum74679b41997-01-02 22:21:36 +0000219 e--;
220 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000221 else if (f == 0.0) {
Guido van Rossum74679b41997-01-02 22:21:36 +0000222 e = 0;
223 }
224 else {
225 PyErr_SetString(PyExc_SystemError,
226 "frexp() result out of range");
227 return -1;
228 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000229
230 if (e >= 1024) {
231 /* XXX 1024 itself is reserved for Inf/NaN */
Guido van Rossum74679b41997-01-02 22:21:36 +0000232 PyErr_SetString(PyExc_OverflowError,
233 "float too large to pack with d format");
234 return -1;
235 }
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000236 else if (e < -1022) {
237 /* Gradual underflow */
238 f = ldexp(f, 1022 + e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000239 e = 0;
240 }
Guido van Rossum74679b41997-01-02 22:21:36 +0000241 else {
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000242 e += 1023;
243 f -= 1.0; /* Get rid of leading 1 */
Guido van Rossum74679b41997-01-02 22:21:36 +0000244 }
245
Guido van Rossum4ccc5311997-01-02 23:23:20 +0000246 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
247 f *= 268435456.0; /* 2**28 */
248 fhi = (long) floor(f); /* Truncate */
249 f -= (double)fhi;
250 f *= 16777216.0; /* 2**24 */
251 flo = (long) floor(f + 0.5); /* Round */
252
Guido van Rossum74679b41997-01-02 22:21:36 +0000253 /* First byte */
254 *p = (s<<7) | (e>>4);
255 p += incr;
256
257 /* Second byte */
Guido van Rossum7844e381997-04-11 20:44:04 +0000258 *p = (char) (((e&0xF)<<4) | (fhi>>24));
Guido van Rossum74679b41997-01-02 22:21:36 +0000259 p += incr;
260
261 /* Third byte */
262 *p = (fhi>>16) & 0xFF;
263 p += incr;
264
265 /* Fourth byte */
266 *p = (fhi>>8) & 0xFF;
267 p += incr;
268
269 /* Fifth byte */
270 *p = fhi & 0xFF;
271 p += incr;
272
273 /* Sixth byte */
274 *p = (flo>>16) & 0xFF;
275 p += incr;
276
277 /* Seventh byte */
278 *p = (flo>>8) & 0xFF;
279 p += incr;
280
281 /* Eighth byte */
282 *p = flo & 0xFF;
283 p += incr;
284
285 /* Done */
286 return 0;
287}
288
289static PyObject *
290unpack_float(p, incr)
291 char *p; /* Where the high order byte is */
292 int incr; /* 1 for big-endian; -1 for little-endian */
293{
294 int s;
295 int e;
296 long f;
297 double x;
298
299 /* First byte */
300 s = (*p>>7) & 1;
301 e = (*p & 0x7F) << 1;
302 p += incr;
303
304 /* Second byte */
305 e |= (*p>>7) & 1;
306 f = (*p & 0x7F) << 16;
307 p += incr;
308
309 /* Third byte */
310 f |= (*p & 0xFF) << 8;
311 p += incr;
312
313 /* Fourth byte */
314 f |= *p & 0xFF;
315
316 x = (double)f / 8388608.0;
317
318 /* XXX This sadly ignores Inf/NaN issues */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000319 if (e == 0)
320 e = -126;
321 else {
322 x += 1.0;
323 e -= 127;
324 }
325 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000326
327 if (s)
328 x = -x;
329
330 return PyFloat_FromDouble(x);
331}
332
333static PyObject *
334unpack_double(p, incr)
335 char *p; /* Where the high order byte is */
336 int incr; /* 1 for big-endian; -1 for little-endian */
337{
338 int s;
339 int e;
340 long fhi, flo;
341 double x;
342
343 /* First byte */
344 s = (*p>>7) & 1;
345 e = (*p & 0x7F) << 4;
346 p += incr;
347
348 /* Second byte */
349 e |= (*p>>4) & 0xF;
350 fhi = (*p & 0xF) << 24;
351 p += incr;
352
353 /* Third byte */
354 fhi |= (*p & 0xFF) << 16;
355 p += incr;
356
357 /* Fourth byte */
358 fhi |= (*p & 0xFF) << 8;
359 p += incr;
360
361 /* Fifth byte */
362 fhi |= *p & 0xFF;
363 p += incr;
364
365 /* Sixth byte */
366 flo = (*p & 0xFF) << 16;
367 p += incr;
368
369 /* Seventh byte */
370 flo |= (*p & 0xFF) << 8;
371 p += incr;
372
373 /* Eighth byte */
374 flo |= *p & 0xFF;
375 p += incr;
376
377 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
378 x /= 268435456.0; /* 2**28 */
379
380 /* XXX This sadly ignores Inf/NaN */
Guido van Rossum07ef6551997-01-02 22:31:07 +0000381 if (e == 0)
382 e = -1022;
383 else {
384 x += 1.0;
385 e -= 1023;
386 }
387 x = ldexp(x, e);
Guido van Rossum74679b41997-01-02 22:21:36 +0000388
389 if (s)
390 x = -x;
391
392 return PyFloat_FromDouble(x);
393}
394
395
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000396/* The translation function for each format character is table driven */
397
398typedef struct _formatdef {
399 char format;
400 int size;
401 int alignment;
402 PyObject* (*unpack) Py_PROTO((const char *,
403 const struct _formatdef *));
404 int (*pack) Py_PROTO((char *,
405 PyObject *,
406 const struct _formatdef *));
407} formatdef;
408
409static PyObject *
410nu_char(p, f)
411 const char *p;
412 const formatdef *f;
413{
414 return PyString_FromStringAndSize(p, 1);
415}
416
417static PyObject *
418nu_byte(p, f)
419 const char *p;
420 const formatdef *f;
421{
422 return PyInt_FromLong((long) *(signed char *)p);
423}
424
425static PyObject *
426nu_ubyte(p, f)
427 const char *p;
428 const formatdef *f;
429{
430 return PyInt_FromLong((long) *(unsigned char *)p);
431}
432
433static PyObject *
434nu_short(p, f)
435 const char *p;
436 const formatdef *f;
437{
438 return PyInt_FromLong((long) *(short *)p);
439}
440
441static PyObject *
442nu_ushort(p, f)
443 const char *p;
444 const formatdef *f;
445{
446 return PyInt_FromLong((long) *(unsigned short *)p);
447}
448
449static PyObject *
450nu_int(p, f)
451 const char *p;
452 const formatdef *f;
453{
454 return PyInt_FromLong((long) *(int *)p);
455}
456
457static PyObject *
458nu_uint(p, f)
459 const char *p;
460 const formatdef *f;
461{
462 unsigned int x = *(unsigned int *)p;
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000463 return PyLong_FromUnsignedLong((unsigned long)x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000464}
465
466static PyObject *
467nu_long(p, f)
468 const char *p;
469 const formatdef *f;
470{
471 return PyInt_FromLong(*(long *)p);
472}
473
474static PyObject *
475nu_ulong(p, f)
476 const char *p;
477 const formatdef *f;
478{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000479 return PyLong_FromUnsignedLong(*(unsigned long *)p);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000480}
481
482static PyObject *
483nu_float(p, f)
484 const char *p;
485 const formatdef *f;
486{
487 float x;
488 memcpy((char *)&x, p, sizeof(float));
489 return PyFloat_FromDouble((double)x);
490}
491
492static PyObject *
493nu_double(p, f)
494 const char *p;
495 const formatdef *f;
496{
497 double x;
498 memcpy((char *)&x, p, sizeof(double));
499 return PyFloat_FromDouble(x);
500}
501
502static int
503np_byte(p, v, f)
504 char *p;
505 PyObject *v;
506 const formatdef *f;
507{
508 long x;
509 if (get_long(v, &x) < 0)
510 return -1;
Guido van Rossum7844e381997-04-11 20:44:04 +0000511 *p = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000512 return 0;
513}
514
515static int
516np_char(p, v, f)
517 char *p;
518 PyObject *v;
519 const formatdef *f;
520{
521 if (!PyString_Check(v) || PyString_Size(v) != 1) {
522 PyErr_SetString(StructError,
523 "char format require string of length 1");
524 return -1;
525 }
526 *p = *PyString_AsString(v);
527 return 0;
528}
529
530static int
531np_short(p, v, f)
532 char *p;
533 PyObject *v;
534 const formatdef *f;
535{
536 long x;
537 if (get_long(v, &x) < 0)
538 return -1;
Guido van Rossum7844e381997-04-11 20:44:04 +0000539 * (short *)p = (short)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000540 return 0;
541}
542
543static int
544np_int(p, v, f)
545 char *p;
546 PyObject *v;
547 const formatdef *f;
548{
549 long x;
550 if (get_long(v, &x) < 0)
551 return -1;
552 * (int *)p = x;
553 return 0;
554}
555
556static int
Guido van Rossum60c50611996-12-31 16:29:52 +0000557np_uint(p, v, f)
558 char *p;
559 PyObject *v;
560 const formatdef *f;
561{
562 unsigned long x;
563 if (get_ulong(v, &x) < 0)
564 return -1;
565 * (unsigned int *)p = x;
566 return 0;
567}
568
569static int
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000570np_long(p, v, f)
571 char *p;
572 PyObject *v;
573 const formatdef *f;
574{
575 long x;
576 if (get_long(v, &x) < 0)
577 return -1;
578 * (long *)p = x;
579 return 0;
580}
581
582static int
Guido van Rossum60c50611996-12-31 16:29:52 +0000583np_ulong(p, v, f)
584 char *p;
585 PyObject *v;
586 const formatdef *f;
587{
588 unsigned long x;
589 if (get_ulong(v, &x) < 0)
590 return -1;
591 * (unsigned long *)p = x;
592 return 0;
593}
594
595static int
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000596np_float(p, v, f)
597 char *p;
598 PyObject *v;
599 const formatdef *f;
600{
601 float x = (float)PyFloat_AsDouble(v);
602 if (x == -1 && PyErr_Occurred()) {
603 PyErr_SetString(StructError,
604 "required argument is not a float");
605 return -1;
606 }
607 memcpy(p, (char *)&x, sizeof(float));
608 return 0;
609}
610
611static int
612np_double(p, v, f)
613 char *p;
614 PyObject *v;
615 const formatdef *f;
616{
617 double x = PyFloat_AsDouble(v);
618 if (x == -1 && PyErr_Occurred()) {
619 PyErr_SetString(StructError,
620 "required argument is not a float");
621 return -1;
622 }
623 memcpy(p, (char *)&x, sizeof(double));
624 return 0;
625}
626
627static formatdef native_table[] = {
628 {'x', sizeof(char), 0, NULL},
629 {'b', sizeof(char), 0, nu_byte, np_byte},
630 {'B', sizeof(char), 0, nu_ubyte, np_byte},
631 {'c', sizeof(char), 0, nu_char, np_char},
632 {'s', sizeof(char), 0, NULL},
633 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
634 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_short},
635 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000636 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000637 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
Guido van Rossum60c50611996-12-31 16:29:52 +0000638 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000639 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
640 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
641 {0}
642};
643
644static PyObject *
645bu_int(p, f)
646 const char *p;
647 const formatdef *f;
648{
649 long x = 0;
650 int i = f->size;
651 do {
652 x = (x<<8) | (*p++ & 0xFF);
653 } while (--i > 0);
654 i = 8*(sizeof(long) - f->size);
655 if (i) {
656 x <<= i;
657 x >>= i;
658 }
659 return PyInt_FromLong(x);
660}
661
662static PyObject *
663bu_uint(p, f)
664 const char *p;
665 const formatdef *f;
666{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000667 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000668 int i = f->size;
669 do {
670 x = (x<<8) | (*p++ & 0xFF);
671 } while (--i > 0);
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000672 return PyLong_FromUnsignedLong(x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000673}
674
Guido van Rossum74679b41997-01-02 22:21:36 +0000675static PyObject *
676bu_float(p, f)
677 const char *p;
678 const formatdef *f;
679{
680 return unpack_float(p, 1);
681}
682
683static PyObject *
684bu_double(p, f)
685 const char *p;
686 const formatdef *f;
687{
688 return unpack_double(p, 1);
689}
690
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000691static int
692bp_int(p, v, f)
693 char *p;
694 PyObject *v;
695 const formatdef *f;
696{
697 long x;
698 int i;
699 if (get_long(v, &x) < 0)
700 return -1;
701 i = f->size;
702 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000703 p[--i] = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000704 x >>= 8;
705 } while (i > 0);
706 return 0;
707}
708
Guido van Rossum60c50611996-12-31 16:29:52 +0000709static int
710bp_uint(p, v, f)
711 char *p;
712 PyObject *v;
713 const formatdef *f;
714{
715 unsigned long x;
716 int i;
717 if (get_ulong(v, &x) < 0)
718 return -1;
719 i = f->size;
720 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000721 p[--i] = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000722 x >>= 8;
723 } while (i > 0);
724 return 0;
725}
726
Guido van Rossum74679b41997-01-02 22:21:36 +0000727static int
728bp_float(p, v, f)
729 char *p;
730 PyObject *v;
731 const formatdef *f;
732{
733 double x = PyFloat_AsDouble(v);
734 if (x == -1 && PyErr_Occurred()) {
735 PyErr_SetString(StructError,
736 "required argument is not a float");
737 return -1;
738 }
739 return pack_float(x, p, 1);
740}
741
742static int
743bp_double(p, v, f)
744 char *p;
745 PyObject *v;
746 const formatdef *f;
747{
748 double x = PyFloat_AsDouble(v);
749 if (x == -1 && PyErr_Occurred()) {
750 PyErr_SetString(StructError,
751 "required argument is not a float");
752 return -1;
753 }
754 return pack_double(x, p, 1);
755}
756
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000757static formatdef bigendian_table[] = {
758 {'x', 1, 0, NULL},
759 {'b', 1, 0, bu_int, bp_int},
760 {'B', 1, 0, bu_uint, bp_int},
761 {'c', 1, 0, nu_char, np_char},
762 {'s', 1, 0, NULL},
763 {'h', 2, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000764 {'H', 2, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000765 {'i', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000766 {'I', 4, 0, bu_uint, bp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000767 {'l', 4, 0, bu_int, bp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000768 {'L', 4, 0, bu_uint, bp_uint},
Guido van Rossum74679b41997-01-02 22:21:36 +0000769 {'f', 4, 0, bu_float, bp_float},
770 {'d', 8, 0, bu_double, bp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000771 {0}
772};
773
774static PyObject *
775lu_int(p, f)
776 const char *p;
777 const formatdef *f;
778{
779 long x = 0;
780 int i = f->size;
781 do {
782 x = (x<<8) | (p[--i] & 0xFF);
783 } while (i > 0);
784 i = 8*(sizeof(long) - f->size);
785 if (i) {
786 x <<= i;
787 x >>= i;
788 }
789 return PyInt_FromLong(x);
790}
791
792static PyObject *
793lu_uint(p, f)
794 const char *p;
795 const formatdef *f;
796{
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000797 unsigned long x = 0;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000798 int i = f->size;
799 do {
800 x = (x<<8) | (p[--i] & 0xFF);
801 } while (i > 0);
Guido van Rossum6c87eca1997-01-03 19:08:16 +0000802 return PyLong_FromUnsignedLong(x);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000803}
804
Guido van Rossum74679b41997-01-02 22:21:36 +0000805static PyObject *
806lu_float(p, f)
807 const char *p;
808 const formatdef *f;
809{
810 return unpack_float(p+3, -1);
811}
812
813static PyObject *
814lu_double(p, f)
815 const char *p;
816 const formatdef *f;
817{
818 return unpack_double(p+7, -1);
819}
820
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000821static int
822lp_int(p, v, f)
823 char *p;
824 PyObject *v;
825 const formatdef *f;
826{
827 long x;
828 int i;
829 if (get_long(v, &x) < 0)
830 return -1;
831 i = f->size;
832 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000833 *p++ = (char)x;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000834 x >>= 8;
835 } while (--i > 0);
836 return 0;
837}
838
Guido van Rossum60c50611996-12-31 16:29:52 +0000839static int
840lp_uint(p, v, f)
841 char *p;
842 PyObject *v;
843 const formatdef *f;
844{
845 unsigned long x;
846 int i;
847 if (get_ulong(v, &x) < 0)
848 return -1;
849 i = f->size;
850 do {
Guido van Rossum7844e381997-04-11 20:44:04 +0000851 *p++ = (char)x;
Guido van Rossum60c50611996-12-31 16:29:52 +0000852 x >>= 8;
853 } while (--i > 0);
854 return 0;
855}
856
Guido van Rossum74679b41997-01-02 22:21:36 +0000857static int
858lp_float(p, v, f)
859 char *p;
860 PyObject *v;
861 const formatdef *f;
862{
863 double x = PyFloat_AsDouble(v);
864 if (x == -1 && PyErr_Occurred()) {
865 PyErr_SetString(StructError,
866 "required argument is not a float");
867 return -1;
868 }
869 return pack_float(x, p+3, -1);
870}
871
872static int
873lp_double(p, v, f)
874 char *p;
875 PyObject *v;
876 const formatdef *f;
877{
878 double x = PyFloat_AsDouble(v);
879 if (x == -1 && PyErr_Occurred()) {
880 PyErr_SetString(StructError,
881 "required argument is not a float");
882 return -1;
883 }
884 return pack_double(x, p+7, -1);
885}
886
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000887static formatdef lilendian_table[] = {
888 {'x', 1, 0, NULL},
889 {'b', 1, 0, lu_int, lp_int},
890 {'B', 1, 0, lu_uint, lp_int},
891 {'c', 1, 0, nu_char, np_char},
892 {'s', 1, 0, NULL},
893 {'h', 2, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000894 {'H', 2, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000895 {'i', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000896 {'I', 4, 0, lu_uint, lp_uint},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000897 {'l', 4, 0, lu_int, lp_int},
Guido van Rossum60c50611996-12-31 16:29:52 +0000898 {'L', 4, 0, lu_uint, lp_uint},
Guido van Rossum74679b41997-01-02 22:21:36 +0000899 {'f', 4, 0, lu_float, lp_float},
900 {'d', 8, 0, lu_double, lp_double},
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000901 {0}
902};
903
904
905static const formatdef *
906whichtable(pfmt)
907 const char **pfmt;
908{
909 const char *fmt = (*pfmt)++; /* May be backed out of later */
910 switch (*fmt) {
911 case '<':
912 return lilendian_table;
913 case '>':
914 case '!': /* Network byte order is big-endian */
915 return bigendian_table;
916 case '=': { /* Host byte order -- different from native in aligment! */
917 int n = 1;
918 char *p = (char *) &n;
919 if (*p == 1)
920 return lilendian_table;
921 else
922 return bigendian_table;
923 }
924 default:
925 --*pfmt; /* Back out of pointer increment */
926 /* Fall through */
927 case '@':
928 return native_table;
929 }
930}
931
932
933/* Get the table entry for a format code */
934
935static const formatdef *
936getentry(c, f)
937 int c;
938 const formatdef *f;
939{
940 for (; f->format != '\0'; f++) {
941 if (f->format == c) {
942 return f;
943 }
944 }
945 PyErr_SetString(StructError, "bad char in struct format");
946 return NULL;
947}
948
949
Guido van Rossum02975121992-08-17 08:55:12 +0000950/* Align a size according to a format code */
951
952static int
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000953align(size, c, e)
Guido van Rossum02975121992-08-17 08:55:12 +0000954 int size;
955 int c;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000956 const formatdef *e;
Guido van Rossum02975121992-08-17 08:55:12 +0000957{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000958 if (e->format == c) {
959 if (e->alignment) {
960 size = ((size + e->alignment - 1)
961 / e->alignment)
962 * e->alignment;
963 }
Guido van Rossum02975121992-08-17 08:55:12 +0000964 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000965 return size;
Guido van Rossum02975121992-08-17 08:55:12 +0000966}
967
968
969/* calculate the size of a format string */
970
971static int
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000972calcsize(fmt, f)
973 const char *fmt;
974 const formatdef *f;
Guido van Rossum02975121992-08-17 08:55:12 +0000975{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000976 const formatdef *e;
977 const char *s;
Guido van Rossum02975121992-08-17 08:55:12 +0000978 char c;
979 int size, num, itemsize, x;
980
981 s = fmt;
982 size = 0;
983 while ((c = *s++) != '\0') {
984 if ('0' <= c && c <= '9') {
985 num = c - '0';
986 while ('0' <= (c = *s++) && c <= '9') {
987 x = num*10 + (c - '0');
988 if (x/10 != num) {
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +0000989 PyErr_SetString(
990 StructError,
991 "overflow in item count");
Guido van Rossum02975121992-08-17 08:55:12 +0000992 return -1;
993 }
994 num = x;
995 }
996 if (c == '\0')
997 break;
998 }
999 else
1000 num = 1;
1001
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001002 e = getentry(c, f);
1003 if (e == NULL)
Guido van Rossum02975121992-08-17 08:55:12 +00001004 return -1;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001005 itemsize = e->size;
1006 size = align(size, c, e);
Guido van Rossum02975121992-08-17 08:55:12 +00001007 x = num * itemsize;
1008 size += x;
1009 if (x/itemsize != num || size < 0) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001010 PyErr_SetString(StructError,
1011 "total struct size too long");
Guido van Rossum02975121992-08-17 08:55:12 +00001012 return -1;
1013 }
Guido van Rossum02975121992-08-17 08:55:12 +00001014 }
1015
1016 return size;
1017}
1018
1019
1020/* pack(fmt, v1, v2, ...) --> string */
1021
Barry Warsaw30695fa1996-12-12 23:32:31 +00001022static PyObject *
Guido van Rossum02975121992-08-17 08:55:12 +00001023struct_calcsize(self, args)
Barry Warsaw30695fa1996-12-12 23:32:31 +00001024 PyObject *self; /* Not used */
1025 PyObject *args;
Guido van Rossum02975121992-08-17 08:55:12 +00001026{
1027 char *fmt;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001028 const formatdef *f;
Guido van Rossum02975121992-08-17 08:55:12 +00001029 int size;
1030
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001031 if (!PyArg_ParseTuple(args, "s", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001032 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001033 f = whichtable(&fmt);
1034 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001035 if (size < 0)
1036 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001037 return PyInt_FromLong((long)size);
Guido van Rossum02975121992-08-17 08:55:12 +00001038}
1039
1040
1041/* pack(fmt, v1, v2, ...) --> string */
1042
Barry Warsaw30695fa1996-12-12 23:32:31 +00001043static PyObject *
Guido van Rossum02975121992-08-17 08:55:12 +00001044struct_pack(self, args)
Barry Warsaw30695fa1996-12-12 23:32:31 +00001045 PyObject *self; /* Not used */
1046 PyObject *args;
Guido van Rossum02975121992-08-17 08:55:12 +00001047{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001048 const formatdef *f, *e;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001049 PyObject *format, *result, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001050 char *fmt;
1051 int size, num;
1052 int i, n;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001053 char *s, *res, *restart, *nres;
Guido van Rossum02975121992-08-17 08:55:12 +00001054 char c;
Guido van Rossum02975121992-08-17 08:55:12 +00001055
Barry Warsaw30695fa1996-12-12 23:32:31 +00001056 if (args == NULL || !PyTuple_Check(args) ||
1057 (n = PyTuple_Size(args)) < 1)
1058 {
1059 PyErr_BadArgument();
Guido van Rossum02975121992-08-17 08:55:12 +00001060 return NULL;
1061 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001062 format = PyTuple_GetItem(args, 0);
1063 if (!PyArg_Parse(format, "s", &fmt))
Guido van Rossum02975121992-08-17 08:55:12 +00001064 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001065 f = whichtable(&fmt);
1066 size = calcsize(fmt, f);
Guido van Rossum02975121992-08-17 08:55:12 +00001067 if (size < 0)
1068 return NULL;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001069 result = PyString_FromStringAndSize((char *)NULL, size);
Guido van Rossum02975121992-08-17 08:55:12 +00001070 if (result == NULL)
1071 return NULL;
1072
1073 s = fmt;
1074 i = 1;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001075 res = restart = PyString_AsString(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001076
1077 while ((c = *s++) != '\0') {
1078 if ('0' <= c && c <= '9') {
1079 num = c - '0';
1080 while ('0' <= (c = *s++) && c <= '9')
1081 num = num*10 + (c - '0');
1082 if (c == '\0')
1083 break;
1084 }
1085 else
1086 num = 1;
1087
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001088 e = getentry(c, f);
1089 if (e == NULL)
1090 goto fail;
Guido van Rossumb9d338c1997-01-03 15:40:33 +00001091 nres = restart + align((int)(res-restart), c, e);
1092 /* Fill padd bytes with zeros */
1093 while (res < nres)
1094 *res++ = '\0';
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001095 if (num == 0 && c != 's')
1096 continue;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001097 do {
1098 if (c == 'x') {
1099 /* doesn't consume arguments */
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001100 memset(res, '\0', num);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001101 res += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001102 break;
Guido van Rossum02975121992-08-17 08:55:12 +00001103 }
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001104 if (i >= n) {
1105 PyErr_SetString(StructError,
1106 "insufficient arguments to pack");
1107 goto fail;
1108 }
1109 v = PyTuple_GetItem(args, i++);
1110 if (v == NULL)
1111 goto fail;
1112 if (c == 's') {
1113 /* num is string size, not repeat count */
1114 int n;
1115 if (!PyString_Check(v)) {
1116 PyErr_SetString(StructError,
1117 "argument for 's' must be a string");
1118 goto fail;
1119 }
1120 n = PyString_Size(v);
1121 if (n > num)
1122 n = num;
1123 if (n > 0)
1124 memcpy(res, PyString_AsString(v), n);
1125 if (n < num)
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001126 memset(res+n, '\0', num-n);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001127 res += num;
1128 break;
1129 }
1130 else {
1131 if (e->pack(res, v, e) < 0)
1132 goto fail;
1133 res += e->size;
1134 }
1135 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001136 }
1137
1138 if (i < n) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001139 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001140 "too many arguments for pack format");
Guido van Rossum02975121992-08-17 08:55:12 +00001141 goto fail;
1142 }
1143
1144 return result;
1145
1146 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001147 Py_DECREF(result);
Guido van Rossum02975121992-08-17 08:55:12 +00001148 return NULL;
1149}
1150
1151
1152/* unpack(fmt, string) --> (v1, v2, ...) */
1153
Barry Warsaw30695fa1996-12-12 23:32:31 +00001154static PyObject *
Guido van Rossum02975121992-08-17 08:55:12 +00001155struct_unpack(self, args)
Barry Warsaw30695fa1996-12-12 23:32:31 +00001156 PyObject *self; /* Not used */
1157 PyObject *args;
Guido van Rossum02975121992-08-17 08:55:12 +00001158{
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001159 const formatdef *f, *e;
Guido van Rossum02975121992-08-17 08:55:12 +00001160 char *str, *start, *fmt, *s;
1161 char c;
Barry Warsawb9a781e1997-01-03 00:26:28 +00001162 int len, size, num;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001163 PyObject *res, *v;
Guido van Rossum02975121992-08-17 08:55:12 +00001164
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001165 if (!PyArg_ParseTuple(args, "ss#", &fmt, &start, &len))
Guido van Rossum02975121992-08-17 08:55:12 +00001166 return NULL;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001167 f = whichtable(&fmt);
1168 size = calcsize(fmt, f);
1169 if (size < 0)
1170 return NULL;
Guido van Rossum02975121992-08-17 08:55:12 +00001171 if (size != len) {
Barry Warsaw30695fa1996-12-12 23:32:31 +00001172 PyErr_SetString(StructError,
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001173 "unpack str size does not match format");
Guido van Rossum02975121992-08-17 08:55:12 +00001174 return NULL;
1175 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001176 res = PyList_New(0);
Guido van Rossum02975121992-08-17 08:55:12 +00001177 if (res == NULL)
1178 return NULL;
1179 str = start;
1180 s = fmt;
1181 while ((c = *s++) != '\0') {
1182 if ('0' <= c && c <= '9') {
1183 num = c - '0';
1184 while ('0' <= (c = *s++) && c <= '9')
1185 num = num*10 + (c - '0');
1186 if (c == '\0')
1187 break;
1188 }
1189 else
1190 num = 1;
1191
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001192 e = getentry(c, f);
1193 if (e == NULL)
1194 goto fail;
1195 str = start + align((int)(str-start), c, e);
Guido van Rossum3aa27fd1996-12-31 02:10:45 +00001196 if (num == 0 && c != 's')
1197 continue;
Guido van Rossum02975121992-08-17 08:55:12 +00001198
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001199 do {
1200 if (c == 'x') {
1201 str += num;
Guido van Rossum02975121992-08-17 08:55:12 +00001202 break;
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001203 }
1204 if (c == 's') {
1205 /* num is string size, not repeat count */
1206 v = PyString_FromStringAndSize(str, num);
1207 if (v == NULL)
1208 goto fail;
1209 str += num;
1210 num = 0;
1211 }
1212 else {
1213 v = e->unpack(str, e);
1214 if (v == NULL)
1215 goto fail;
1216 str += e->size;
Guido van Rossum02975121992-08-17 08:55:12 +00001217 }
Barry Warsaw30695fa1996-12-12 23:32:31 +00001218 if (v == NULL || PyList_Append(res, v) < 0)
Guido van Rossum02975121992-08-17 08:55:12 +00001219 goto fail;
Barry Warsaw30695fa1996-12-12 23:32:31 +00001220 Py_DECREF(v);
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001221 } while (--num > 0);
Guido van Rossum02975121992-08-17 08:55:12 +00001222 }
1223
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001224 v = PyList_AsTuple(res);
1225 Py_DECREF(res);
1226 return v;
Guido van Rossum02975121992-08-17 08:55:12 +00001227
1228 fail:
Barry Warsaw30695fa1996-12-12 23:32:31 +00001229 Py_DECREF(res);
Guido van Rossum02975121992-08-17 08:55:12 +00001230 return NULL;
1231}
1232
Guido van Rossum90ddb7b1992-08-19 16:44:15 +00001233
Guido van Rossum02975121992-08-17 08:55:12 +00001234/* List of functions */
1235
Barry Warsaw30695fa1996-12-12 23:32:31 +00001236static PyMethodDef struct_methods[] = {
Guido van Rossumf7e6b4b1996-12-31 01:41:25 +00001237 {"calcsize", struct_calcsize, METH_VARARGS},
1238 {"pack", struct_pack, METH_VARARGS},
1239 {"unpack", struct_unpack, METH_VARARGS},
Guido van Rossum02975121992-08-17 08:55:12 +00001240 {NULL, NULL} /* sentinel */
1241};
1242
1243
1244/* Module initialization */
1245
1246void
1247initstruct()
1248{
Barry Warsaw30695fa1996-12-12 23:32:31 +00001249 PyObject *m, *d;
Guido van Rossum02975121992-08-17 08:55:12 +00001250
1251 /* Create the module and add the functions */
Barry Warsaw30695fa1996-12-12 23:32:31 +00001252 m = Py_InitModule("struct", struct_methods);
Guido van Rossum02975121992-08-17 08:55:12 +00001253
1254 /* Add some symbolic constants to the module */
Barry Warsaw30695fa1996-12-12 23:32:31 +00001255 d = PyModule_GetDict(m);
1256 StructError = PyString_FromString("struct.error");
1257 PyDict_SetItemString(d, "error", StructError);
Guido van Rossum02975121992-08-17 08:55:12 +00001258
1259 /* Check for errors */
Barry Warsaw30695fa1996-12-12 23:32:31 +00001260 if (PyErr_Occurred())
1261 Py_FatalError("can't initialize module struct");
Guido van Rossum02975121992-08-17 08:55:12 +00001262}