1 /* struct module -- pack values into and (out of) strings */
3 /* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
6 #define PY_SSIZE_T_CLEAN
10 #include "structmember.h"
13 static PyTypeObject PyStructType
;
15 /* compatibility macros */
16 #if (PY_VERSION_HEX < 0x02050000)
17 typedef int Py_ssize_t
;
20 /* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float
21 arguments for integer formats with a warning for backwards
24 #define PY_STRUCT_FLOAT_COERCE 1
26 #ifdef PY_STRUCT_FLOAT_COERCE
27 #define FLOAT_COERCE "integer argument expected, got float"
31 /* The translation function for each format character is table driven */
32 typedef struct _formatdef
{
36 PyObject
* (*unpack
)(const char *,
37 const struct _formatdef
*);
38 int (*pack
)(char *, PyObject
*,
39 const struct _formatdef
*);
42 typedef struct _formatcode
{
43 const struct _formatdef
*fmtdef
;
48 /* Struct object interface */
56 PyObject
*weakreflist
; /* List of weak references */
60 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
61 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
66 static PyObject
*StructError
;
69 /* Define various structs to figure out the alignments of types */
72 typedef struct { char c
; short x
; } st_short
;
73 typedef struct { char c
; int x
; } st_int
;
74 typedef struct { char c
; long x
; } st_long
;
75 typedef struct { char c
; float x
; } st_float
;
76 typedef struct { char c
; double x
; } st_double
;
77 typedef struct { char c
; void *x
; } st_void_p
;
79 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
80 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
81 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
82 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
83 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
84 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
86 /* We can't support q and Q in native mode unless the compiler does;
87 in std mode, they're 8 bytes on all platforms. */
89 typedef struct { char c
; PY_LONG_LONG x
; } s_long_long
;
90 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
94 #define BOOL_TYPE _Bool
95 typedef struct { char c
; _Bool x
; } s_bool
;
96 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
98 #define BOOL_TYPE char
102 #define STRINGIFY(x) #x
105 #pragma options align=reset
108 static char *integer_codes
= "bBhHiIlLqQ";
110 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
113 get_pylong(PyObject
*v
)
117 return PyLong_FromLong(PyInt_AS_LONG(v
));
118 if (PyLong_Check(v
)) {
122 #ifdef PY_STRUCT_FLOAT_COERCE
123 if (PyFloat_Check(v
)) {
124 if (PyErr_WarnEx(PyExc_DeprecationWarning
, FLOAT_COERCE
, 2)<0)
126 return PyNumber_Long(v
);
129 PyErr_SetString(StructError
,
130 "cannot convert argument to long");
134 /* Helper to convert a Python object to a C long. Sets an exception
135 (struct.error for an inconvertible type, OverflowError for
136 out-of-range values) and returns -1 on error. */
139 get_long(PyObject
*v
, long *p
)
146 assert(PyLong_Check(v
));
147 x
= PyLong_AsLong(v
);
149 if (x
== (long)-1 && PyErr_Occurred())
155 /* Same, but handling unsigned long */
158 get_ulong(PyObject
*v
, unsigned long *p
)
165 assert(PyLong_Check(v
));
166 x
= PyLong_AsUnsignedLong(v
);
168 if (x
== (unsigned long)-1 && PyErr_Occurred())
174 #ifdef HAVE_LONG_LONG
176 /* Same, but handling native long long. */
179 get_longlong(PyObject
*v
, PY_LONG_LONG
*p
)
186 assert(PyLong_Check(v
));
187 x
= PyLong_AsLongLong(v
);
189 if (x
== (PY_LONG_LONG
)-1 && PyErr_Occurred())
195 /* Same, but handling native unsigned long long. */
198 get_ulonglong(PyObject
*v
, unsigned PY_LONG_LONG
*p
)
200 unsigned PY_LONG_LONG x
;
205 assert(PyLong_Check(v
));
206 x
= PyLong_AsUnsignedLongLong(v
);
208 if (x
== (unsigned PY_LONG_LONG
)-1 && PyErr_Occurred())
216 #define get_wrapped_long get_long
217 #define get_wrapped_ulong get_ulong
219 /* Floating point helpers */
222 unpack_float(const char *p
, /* start of 4-byte string */
223 int le
) /* true for little-endian, false for big-endian */
227 x
= _PyFloat_Unpack4((unsigned char *)p
, le
);
228 if (x
== -1.0 && PyErr_Occurred())
230 return PyFloat_FromDouble(x
);
234 unpack_double(const char *p
, /* start of 8-byte string */
235 int le
) /* true for little-endian, false for big-endian */
239 x
= _PyFloat_Unpack8((unsigned char *)p
, le
);
240 if (x
== -1.0 && PyErr_Occurred())
242 return PyFloat_FromDouble(x
);
245 /* Helper to format the range error exceptions */
247 _range_error(const formatdef
*f
, int is_unsigned
)
249 /* ulargest is the largest unsigned value with f->size bytes.
250 * Note that the simpler:
251 * ((size_t)1 << (f->size * 8)) - 1
252 * doesn't work when f->size == sizeof(size_t) because C doesn't
253 * define what happens when a left shift count is >= the number of
254 * bits in the integer being shifted; e.g., on some boxes it doesn't
255 * shift at all when they're equal.
257 const size_t ulargest
= (size_t)-1 >> ((SIZEOF_SIZE_T
- f
->size
)*8);
258 assert(f
->size
>= 1 && f
->size
<= SIZEOF_SIZE_T
);
260 PyErr_Format(StructError
,
261 "'%c' format requires 0 <= number <= %zu",
265 const Py_ssize_t largest
= (Py_ssize_t
)(ulargest
>> 1);
266 PyErr_Format(StructError
,
267 "'%c' format requires %zd <= number <= %zd",
277 /* A large number of small routines follow, with names of the form
281 [bln] distiguishes among big-endian, little-endian and native.
282 [pu] distiguishes between pack (to struct) and unpack (from struct).
283 TYPE is one of char, byte, ubyte, etc.
286 /* Native mode routines. ****************************************************/
288 In all n[up]_<type> routines handling types larger than 1 byte, there is
289 *no* guarantee that the p pointer is properly aligned for each type,
290 therefore memcpy is called. An intermediate variable is used to
291 compensate for big-endian architectures.
292 Normally both the intermediate variable and the memcpy call will be
293 skipped by C optimisation in little-endian architectures (gcc >= 2.91
297 nu_char(const char *p
, const formatdef
*f
)
299 return PyString_FromStringAndSize(p
, 1);
303 nu_byte(const char *p
, const formatdef
*f
)
305 return PyInt_FromLong((long) *(signed char *)p
);
309 nu_ubyte(const char *p
, const formatdef
*f
)
311 return PyInt_FromLong((long) *(unsigned char *)p
);
315 nu_short(const char *p
, const formatdef
*f
)
318 memcpy((char *)&x
, p
, sizeof x
);
319 return PyInt_FromLong((long)x
);
323 nu_ushort(const char *p
, const formatdef
*f
)
326 memcpy((char *)&x
, p
, sizeof x
);
327 return PyInt_FromLong((long)x
);
331 nu_int(const char *p
, const formatdef
*f
)
334 memcpy((char *)&x
, p
, sizeof x
);
335 return PyInt_FromLong((long)x
);
339 nu_uint(const char *p
, const formatdef
*f
)
342 memcpy((char *)&x
, p
, sizeof x
);
343 #if (SIZEOF_LONG > SIZEOF_INT)
344 return PyInt_FromLong((long)x
);
346 if (x
<= ((unsigned int)LONG_MAX
))
347 return PyInt_FromLong((long)x
);
348 return PyLong_FromUnsignedLong((unsigned long)x
);
353 nu_long(const char *p
, const formatdef
*f
)
356 memcpy((char *)&x
, p
, sizeof x
);
357 return PyInt_FromLong(x
);
361 nu_ulong(const char *p
, const formatdef
*f
)
364 memcpy((char *)&x
, p
, sizeof x
);
366 return PyInt_FromLong((long)x
);
367 return PyLong_FromUnsignedLong(x
);
370 /* Native mode doesn't support q or Q unless the platform C supports
371 long long (or, on Windows, __int64). */
373 #ifdef HAVE_LONG_LONG
376 nu_longlong(const char *p
, const formatdef
*f
)
379 memcpy((char *)&x
, p
, sizeof x
);
380 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
381 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
382 return PyLong_FromLongLong(x
);
386 nu_ulonglong(const char *p
, const formatdef
*f
)
388 unsigned PY_LONG_LONG x
;
389 memcpy((char *)&x
, p
, sizeof x
);
391 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
392 return PyLong_FromUnsignedLongLong(x
);
398 nu_bool(const char *p
, const formatdef
*f
)
401 memcpy((char *)&x
, p
, sizeof x
);
402 return PyBool_FromLong(x
!= 0);
407 nu_float(const char *p
, const formatdef
*f
)
410 memcpy((char *)&x
, p
, sizeof x
);
411 return PyFloat_FromDouble((double)x
);
415 nu_double(const char *p
, const formatdef
*f
)
418 memcpy((char *)&x
, p
, sizeof x
);
419 return PyFloat_FromDouble(x
);
423 nu_void_p(const char *p
, const formatdef
*f
)
426 memcpy((char *)&x
, p
, sizeof x
);
427 return PyLong_FromVoidPtr(x
);
431 np_byte(char *p
, PyObject
*v
, const formatdef
*f
)
434 if (get_long(v
, &x
) < 0)
436 if (x
< -128 || x
> 127){
437 PyErr_SetString(StructError
,
438 "byte format requires -128 <= number <= 127");
446 np_ubyte(char *p
, PyObject
*v
, const formatdef
*f
)
449 if (get_long(v
, &x
) < 0)
451 if (x
< 0 || x
> 255){
452 PyErr_SetString(StructError
,
453 "ubyte format requires 0 <= number <= 255");
461 np_char(char *p
, PyObject
*v
, const formatdef
*f
)
463 if (!PyString_Check(v
) || PyString_Size(v
) != 1) {
464 PyErr_SetString(StructError
,
465 "char format require string of length 1");
468 *p
= *PyString_AsString(v
);
473 np_short(char *p
, PyObject
*v
, const formatdef
*f
)
477 if (get_long(v
, &x
) < 0)
479 if (x
< SHRT_MIN
|| x
> SHRT_MAX
){
480 PyErr_SetString(StructError
,
481 "short format requires " STRINGIFY(SHRT_MIN
)
482 " <= number <= " STRINGIFY(SHRT_MAX
));
486 memcpy(p
, (char *)&y
, sizeof y
);
491 np_ushort(char *p
, PyObject
*v
, const formatdef
*f
)
495 if (get_long(v
, &x
) < 0)
497 if (x
< 0 || x
> USHRT_MAX
){
498 PyErr_SetString(StructError
,
499 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX
));
502 y
= (unsigned short)x
;
503 memcpy(p
, (char *)&y
, sizeof y
);
508 np_int(char *p
, PyObject
*v
, const formatdef
*f
)
512 if (get_long(v
, &x
) < 0)
514 #if (SIZEOF_LONG > SIZEOF_INT)
515 if ((x
< ((long)INT_MIN
)) || (x
> ((long)INT_MAX
)))
516 return _range_error(f
, 0);
519 memcpy(p
, (char *)&y
, sizeof y
);
524 np_uint(char *p
, PyObject
*v
, const formatdef
*f
)
528 if (get_wrapped_ulong(v
, &x
) < 0)
531 #if (SIZEOF_LONG > SIZEOF_INT)
532 if (x
> ((unsigned long)UINT_MAX
))
533 return _range_error(f
, 1);
535 memcpy(p
, (char *)&y
, sizeof y
);
540 np_long(char *p
, PyObject
*v
, const formatdef
*f
)
543 if (get_long(v
, &x
) < 0)
545 memcpy(p
, (char *)&x
, sizeof x
);
550 np_ulong(char *p
, PyObject
*v
, const formatdef
*f
)
553 if (get_wrapped_ulong(v
, &x
) < 0)
555 memcpy(p
, (char *)&x
, sizeof x
);
559 #ifdef HAVE_LONG_LONG
562 np_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
565 if (get_longlong(v
, &x
) < 0)
567 memcpy(p
, (char *)&x
, sizeof x
);
572 np_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
574 unsigned PY_LONG_LONG x
;
575 if (get_ulonglong(v
, &x
) < 0)
577 memcpy(p
, (char *)&x
, sizeof x
);
584 np_bool(char *p
, PyObject
*v
, const formatdef
*f
)
587 y
= PyObject_IsTrue(v
);
588 memcpy(p
, (char *)&y
, sizeof y
);
593 np_float(char *p
, PyObject
*v
, const formatdef
*f
)
595 float x
= (float)PyFloat_AsDouble(v
);
596 if (x
== -1 && PyErr_Occurred()) {
597 PyErr_SetString(StructError
,
598 "required argument is not a float");
601 memcpy(p
, (char *)&x
, sizeof x
);
606 np_double(char *p
, PyObject
*v
, const formatdef
*f
)
608 double x
= PyFloat_AsDouble(v
);
609 if (x
== -1 && PyErr_Occurred()) {
610 PyErr_SetString(StructError
,
611 "required argument is not a float");
614 memcpy(p
, (char *)&x
, sizeof(double));
619 np_void_p(char *p
, PyObject
*v
, const formatdef
*f
)
626 assert(PyLong_Check(v
));
627 x
= PyLong_AsVoidPtr(v
);
629 if (x
== NULL
&& PyErr_Occurred())
631 memcpy(p
, (char *)&x
, sizeof x
);
635 static formatdef native_table
[] = {
636 {'x', sizeof(char), 0, NULL
},
637 {'b', sizeof(char), 0, nu_byte
, np_byte
},
638 {'B', sizeof(char), 0, nu_ubyte
, np_ubyte
},
639 {'c', sizeof(char), 0, nu_char
, np_char
},
640 {'s', sizeof(char), 0, NULL
},
641 {'p', sizeof(char), 0, NULL
},
642 {'h', sizeof(short), SHORT_ALIGN
, nu_short
, np_short
},
643 {'H', sizeof(short), SHORT_ALIGN
, nu_ushort
, np_ushort
},
644 {'i', sizeof(int), INT_ALIGN
, nu_int
, np_int
},
645 {'I', sizeof(int), INT_ALIGN
, nu_uint
, np_uint
},
646 {'l', sizeof(long), LONG_ALIGN
, nu_long
, np_long
},
647 {'L', sizeof(long), LONG_ALIGN
, nu_ulong
, np_ulong
},
648 #ifdef HAVE_LONG_LONG
649 {'q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_longlong
, np_longlong
},
650 {'Q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_ulonglong
,np_ulonglong
},
652 {'?', sizeof(BOOL_TYPE
), BOOL_ALIGN
, nu_bool
, np_bool
},
653 {'f', sizeof(float), FLOAT_ALIGN
, nu_float
, np_float
},
654 {'d', sizeof(double), DOUBLE_ALIGN
, nu_double
, np_double
},
655 {'P', sizeof(void *), VOID_P_ALIGN
, nu_void_p
, np_void_p
},
659 /* Big-endian routines. *****************************************************/
662 bu_int(const char *p
, const formatdef
*f
)
665 Py_ssize_t i
= f
->size
;
666 const unsigned char *bytes
= (const unsigned char *)p
;
668 x
= (x
<<8) | *bytes
++;
670 /* Extend the sign bit. */
671 if (SIZEOF_LONG
> f
->size
)
672 x
|= -(x
& (1L << ((8 * f
->size
) - 1)));
673 return PyInt_FromLong(x
);
677 bu_uint(const char *p
, const formatdef
*f
)
680 Py_ssize_t i
= f
->size
;
681 const unsigned char *bytes
= (const unsigned char *)p
;
683 x
= (x
<<8) | *bytes
++;
686 return PyInt_FromLong((long)x
);
687 return PyLong_FromUnsignedLong(x
);
691 bu_longlong(const char *p
, const formatdef
*f
)
693 #ifdef HAVE_LONG_LONG
695 Py_ssize_t i
= f
->size
;
696 const unsigned char *bytes
= (const unsigned char *)p
;
698 x
= (x
<<8) | *bytes
++;
700 /* Extend the sign bit. */
701 if (SIZEOF_LONG_LONG
> f
->size
)
702 x
|= -(x
& ((PY_LONG_LONG
)1 << ((8 * f
->size
) - 1)));
703 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
704 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
705 return PyLong_FromLongLong(x
);
707 return _PyLong_FromByteArray((const unsigned char *)p
,
709 0, /* little-endian */
715 bu_ulonglong(const char *p
, const formatdef
*f
)
717 #ifdef HAVE_LONG_LONG
718 unsigned PY_LONG_LONG x
= 0;
719 Py_ssize_t i
= f
->size
;
720 const unsigned char *bytes
= (const unsigned char *)p
;
722 x
= (x
<<8) | *bytes
++;
725 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
726 return PyLong_FromUnsignedLongLong(x
);
728 return _PyLong_FromByteArray((const unsigned char *)p
,
730 0, /* little-endian */
736 bu_float(const char *p
, const formatdef
*f
)
738 return unpack_float(p
, 0);
742 bu_double(const char *p
, const formatdef
*f
)
744 return unpack_double(p
, 0);
748 bu_bool(const char *p
, const formatdef
*f
)
751 memcpy((char *)&x
, p
, sizeof x
);
752 return PyBool_FromLong(x
!= 0);
756 bp_int(char *p
, PyObject
*v
, const formatdef
*f
)
760 if (get_wrapped_long(v
, &x
) < 0)
763 if (i
!= SIZEOF_LONG
) {
764 if ((i
== 2) && (x
< -32768 || x
> 32767))
765 return _range_error(f
, 0);
766 #if (SIZEOF_LONG != 4)
767 else if ((i
== 4) && (x
< -2147483648L || x
> 2147483647L))
768 return _range_error(f
, 0);
779 bp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
783 if (get_wrapped_ulong(v
, &x
) < 0)
786 if (i
!= SIZEOF_LONG
) {
787 unsigned long maxint
= 1;
788 maxint
<<= (unsigned long)(i
* 8);
790 return _range_error(f
, 1);
800 bp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
806 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
809 0, /* little_endian */
816 bp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
822 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
825 0, /* little_endian */
832 bp_float(char *p
, PyObject
*v
, const formatdef
*f
)
834 double x
= PyFloat_AsDouble(v
);
835 if (x
== -1 && PyErr_Occurred()) {
836 PyErr_SetString(StructError
,
837 "required argument is not a float");
840 return _PyFloat_Pack4(x
, (unsigned char *)p
, 0);
844 bp_double(char *p
, PyObject
*v
, const formatdef
*f
)
846 double x
= PyFloat_AsDouble(v
);
847 if (x
== -1 && PyErr_Occurred()) {
848 PyErr_SetString(StructError
,
849 "required argument is not a float");
852 return _PyFloat_Pack8(x
, (unsigned char *)p
, 0);
856 bp_bool(char *p
, PyObject
*v
, const formatdef
*f
)
859 y
= PyObject_IsTrue(v
);
860 memcpy(p
, (char *)&y
, sizeof y
);
864 static formatdef bigendian_table
[] = {
866 {'b', 1, 0, nu_byte
, np_byte
},
867 {'B', 1, 0, nu_ubyte
, np_ubyte
},
868 {'c', 1, 0, nu_char
, np_char
},
871 {'h', 2, 0, bu_int
, bp_int
},
872 {'H', 2, 0, bu_uint
, bp_uint
},
873 {'i', 4, 0, bu_int
, bp_int
},
874 {'I', 4, 0, bu_uint
, bp_uint
},
875 {'l', 4, 0, bu_int
, bp_int
},
876 {'L', 4, 0, bu_uint
, bp_uint
},
877 {'q', 8, 0, bu_longlong
, bp_longlong
},
878 {'Q', 8, 0, bu_ulonglong
, bp_ulonglong
},
879 {'?', 1, 0, bu_bool
, bp_bool
},
880 {'f', 4, 0, bu_float
, bp_float
},
881 {'d', 8, 0, bu_double
, bp_double
},
885 /* Little-endian routines. *****************************************************/
888 lu_int(const char *p
, const formatdef
*f
)
891 Py_ssize_t i
= f
->size
;
892 const unsigned char *bytes
= (const unsigned char *)p
;
894 x
= (x
<<8) | bytes
[--i
];
896 /* Extend the sign bit. */
897 if (SIZEOF_LONG
> f
->size
)
898 x
|= -(x
& (1L << ((8 * f
->size
) - 1)));
899 return PyInt_FromLong(x
);
903 lu_uint(const char *p
, const formatdef
*f
)
906 Py_ssize_t i
= f
->size
;
907 const unsigned char *bytes
= (const unsigned char *)p
;
909 x
= (x
<<8) | bytes
[--i
];
912 return PyInt_FromLong((long)x
);
913 return PyLong_FromUnsignedLong((long)x
);
917 lu_longlong(const char *p
, const formatdef
*f
)
919 #ifdef HAVE_LONG_LONG
921 Py_ssize_t i
= f
->size
;
922 const unsigned char *bytes
= (const unsigned char *)p
;
924 x
= (x
<<8) | bytes
[--i
];
926 /* Extend the sign bit. */
927 if (SIZEOF_LONG_LONG
> f
->size
)
928 x
|= -(x
& ((PY_LONG_LONG
)1 << ((8 * f
->size
) - 1)));
929 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
930 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
931 return PyLong_FromLongLong(x
);
933 return _PyLong_FromByteArray((const unsigned char *)p
,
935 1, /* little-endian */
941 lu_ulonglong(const char *p
, const formatdef
*f
)
943 #ifdef HAVE_LONG_LONG
944 unsigned PY_LONG_LONG x
= 0;
945 Py_ssize_t i
= f
->size
;
946 const unsigned char *bytes
= (const unsigned char *)p
;
948 x
= (x
<<8) | bytes
[--i
];
951 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
952 return PyLong_FromUnsignedLongLong(x
);
954 return _PyLong_FromByteArray((const unsigned char *)p
,
956 1, /* little-endian */
962 lu_float(const char *p
, const formatdef
*f
)
964 return unpack_float(p
, 1);
968 lu_double(const char *p
, const formatdef
*f
)
970 return unpack_double(p
, 1);
974 lp_int(char *p
, PyObject
*v
, const formatdef
*f
)
978 if (get_wrapped_long(v
, &x
) < 0)
981 if (i
!= SIZEOF_LONG
) {
982 if ((i
== 2) && (x
< -32768 || x
> 32767))
983 return _range_error(f
, 0);
984 #if (SIZEOF_LONG != 4)
985 else if ((i
== 4) && (x
< -2147483648L || x
> 2147483647L))
986 return _range_error(f
, 0);
997 lp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
1001 if (get_wrapped_ulong(v
, &x
) < 0)
1004 if (i
!= SIZEOF_LONG
) {
1005 unsigned long maxint
= 1;
1006 maxint
<<= (unsigned long)(i
* 8);
1008 return _range_error(f
, 1);
1018 lp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
1024 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
1027 1, /* little_endian */
1034 lp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
1040 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
1043 1, /* little_endian */
1050 lp_float(char *p
, PyObject
*v
, const formatdef
*f
)
1052 double x
= PyFloat_AsDouble(v
);
1053 if (x
== -1 && PyErr_Occurred()) {
1054 PyErr_SetString(StructError
,
1055 "required argument is not a float");
1058 return _PyFloat_Pack4(x
, (unsigned char *)p
, 1);
1062 lp_double(char *p
, PyObject
*v
, const formatdef
*f
)
1064 double x
= PyFloat_AsDouble(v
);
1065 if (x
== -1 && PyErr_Occurred()) {
1066 PyErr_SetString(StructError
,
1067 "required argument is not a float");
1070 return _PyFloat_Pack8(x
, (unsigned char *)p
, 1);
1073 static formatdef lilendian_table
[] = {
1075 {'b', 1, 0, nu_byte
, np_byte
},
1076 {'B', 1, 0, nu_ubyte
, np_ubyte
},
1077 {'c', 1, 0, nu_char
, np_char
},
1080 {'h', 2, 0, lu_int
, lp_int
},
1081 {'H', 2, 0, lu_uint
, lp_uint
},
1082 {'i', 4, 0, lu_int
, lp_int
},
1083 {'I', 4, 0, lu_uint
, lp_uint
},
1084 {'l', 4, 0, lu_int
, lp_int
},
1085 {'L', 4, 0, lu_uint
, lp_uint
},
1086 {'q', 8, 0, lu_longlong
, lp_longlong
},
1087 {'Q', 8, 0, lu_ulonglong
, lp_ulonglong
},
1088 {'?', 1, 0, bu_bool
, bp_bool
}, /* Std rep not endian dep,
1089 but potentially different from native rep -- reuse bx_bool funcs. */
1090 {'f', 4, 0, lu_float
, lp_float
},
1091 {'d', 8, 0, lu_double
, lp_double
},
1096 static const formatdef
*
1097 whichtable(char **pfmt
)
1099 const char *fmt
= (*pfmt
)++; /* May be backed out of later */
1102 return lilendian_table
;
1104 case '!': /* Network byte order is big-endian */
1105 return bigendian_table
;
1106 case '=': { /* Host byte order -- different from native in aligment! */
1108 char *p
= (char *) &n
;
1110 return lilendian_table
;
1112 return bigendian_table
;
1115 --*pfmt
; /* Back out of pointer increment */
1118 return native_table
;
1123 /* Get the table entry for a format code */
1125 static const formatdef
*
1126 getentry(int c
, const formatdef
*f
)
1128 for (; f
->format
!= '\0'; f
++) {
1129 if (f
->format
== c
) {
1133 PyErr_SetString(StructError
, "bad char in struct format");
1138 /* Align a size according to a format code */
1141 align(Py_ssize_t size
, char c
, const formatdef
*e
)
1143 if (e
->format
== c
) {
1145 size
= ((size
+ e
->alignment
- 1)
1154 /* calculate the size of a format string */
1157 prepare_s(PyStructObject
*self
)
1166 Py_ssize_t size
, len
, num
, itemsize
, x
;
1168 fmt
= PyString_AS_STRING(self
->s_format
);
1170 f
= whichtable((char **)&fmt
);
1175 while ((c
= *s
++) != '\0') {
1176 if (isspace(Py_CHARMASK(c
)))
1178 if ('0' <= c
&& c
<= '9') {
1180 while ('0' <= (c
= *s
++) && c
<= '9') {
1181 x
= num
*10 + (c
- '0');
1185 "overflow in item count");
1201 case 's': /* fall through */
1202 case 'p': len
++; break;
1204 default: len
+= num
; break;
1208 size
= align(size
, c
, e
);
1211 if (x
/itemsize
!= num
|| size
< 0) {
1212 PyErr_SetString(StructError
,
1213 "total struct size too long");
1218 /* check for overflow */
1219 if ((len
+ 1) > (PY_SSIZE_T_MAX
/ sizeof(formatcode
))) {
1224 self
->s_size
= size
;
1226 codes
= PyMem_MALLOC((len
+ 1) * sizeof(formatcode
));
1227 if (codes
== NULL
) {
1231 self
->s_codes
= codes
;
1235 while ((c
= *s
++) != '\0') {
1236 if (isspace(Py_CHARMASK(c
)))
1238 if ('0' <= c
&& c
<= '9') {
1240 while ('0' <= (c
= *s
++) && c
<= '9')
1241 num
= num
*10 + (c
- '0');
1250 size
= align(size
, c
, e
);
1251 if (c
== 's' || c
== 'p') {
1252 codes
->offset
= size
;
1257 } else if (c
== 'x') {
1260 while (--num
>= 0) {
1261 codes
->offset
= size
;
1262 codes
->size
= e
->size
;
1269 codes
->fmtdef
= NULL
;
1270 codes
->offset
= size
;
1277 s_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1281 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
1283 self
= type
->tp_alloc(type
, 0);
1285 PyStructObject
*s
= (PyStructObject
*)self
;
1287 s
->s_format
= Py_None
;
1296 s_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1298 PyStructObject
*soself
= (PyStructObject
*)self
;
1299 PyObject
*o_format
= NULL
;
1301 static char *kwlist
[] = {"format", 0};
1303 assert(PyStruct_Check(self
));
1305 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "S:Struct", kwlist
,
1309 Py_INCREF(o_format
);
1310 Py_CLEAR(soself
->s_format
);
1311 soself
->s_format
= o_format
;
1313 ret
= prepare_s(soself
);
1318 s_dealloc(PyStructObject
*s
)
1320 if (s
->weakreflist
!= NULL
)
1321 PyObject_ClearWeakRefs((PyObject
*)s
);
1322 if (s
->s_codes
!= NULL
) {
1323 PyMem_FREE(s
->s_codes
);
1325 Py_XDECREF(s
->s_format
);
1326 Py_TYPE(s
)->tp_free((PyObject
*)s
);
1330 s_unpack_internal(PyStructObject
*soself
, char *startfrom
) {
1333 PyObject
*result
= PyTuple_New(soself
->s_len
);
1337 for (code
= soself
->s_codes
; code
->fmtdef
!= NULL
; code
++) {
1339 const formatdef
*e
= code
->fmtdef
;
1340 const char *res
= startfrom
+ code
->offset
;
1341 if (e
->format
== 's') {
1342 v
= PyString_FromStringAndSize(res
, code
->size
);
1343 } else if (e
->format
== 'p') {
1344 Py_ssize_t n
= *(unsigned char*)res
;
1345 if (n
>= code
->size
)
1347 v
= PyString_FromStringAndSize(res
+ 1, n
);
1349 v
= e
->unpack(res
, e
);
1353 PyTuple_SET_ITEM(result
, i
++, v
);
1363 PyDoc_STRVAR(s_unpack__doc__
,
1364 "S.unpack(str) -> (v1, v2, ...)\n\
1366 Return tuple containing values unpacked according to this Struct's format.\n\
1367 Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1371 s_unpack(PyObject
*self
, PyObject
*inputstr
)
1375 PyObject
*args
=NULL
, *result
;
1376 PyStructObject
*soself
= (PyStructObject
*)self
;
1377 assert(PyStruct_Check(self
));
1378 assert(soself
->s_codes
!= NULL
);
1379 if (inputstr
== NULL
)
1381 if (PyString_Check(inputstr
) &&
1382 PyString_GET_SIZE(inputstr
) == soself
->s_size
) {
1383 return s_unpack_internal(soself
, PyString_AS_STRING(inputstr
));
1385 args
= PyTuple_Pack(1, inputstr
);
1388 if (!PyArg_ParseTuple(args
, "s#:unpack", &start
, &len
))
1390 if (soself
->s_size
!= len
)
1392 result
= s_unpack_internal(soself
, start
);
1398 PyErr_Format(StructError
,
1399 "unpack requires a string argument of length %zd",
1404 PyDoc_STRVAR(s_unpack_from__doc__
,
1405 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1407 Return tuple containing values unpacked according to this Struct's format.\n\
1408 Unlike unpack, unpack_from can unpack values from any object supporting\n\
1409 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1410 See struct.__doc__ for more on format strings.");
1413 s_unpack_from(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1415 static char *kwlist
[] = {"buffer", "offset", 0};
1416 #if (PY_VERSION_HEX < 0x02050000)
1417 static char *fmt
= "z#|i:unpack_from";
1419 static char *fmt
= "z#|n:unpack_from";
1421 Py_ssize_t buffer_len
= 0, offset
= 0;
1422 char *buffer
= NULL
;
1423 PyStructObject
*soself
= (PyStructObject
*)self
;
1424 assert(PyStruct_Check(self
));
1425 assert(soself
->s_codes
!= NULL
);
1427 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, fmt
, kwlist
,
1428 &buffer
, &buffer_len
, &offset
))
1431 if (buffer
== NULL
) {
1432 PyErr_Format(StructError
,
1433 "unpack_from requires a buffer argument");
1438 offset
+= buffer_len
;
1440 if (offset
< 0 || (buffer_len
- offset
) < soself
->s_size
) {
1441 PyErr_Format(StructError
,
1442 "unpack_from requires a buffer of at least %zd bytes",
1446 return s_unpack_internal(soself
, buffer
+ offset
);
1451 * Guts of the pack function.
1453 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1454 * argument for where to start processing the arguments for packing, and a
1455 * character buffer for writing the packed string. The caller must insure
1456 * that the buffer may contain the required length for packing the arguments.
1457 * 0 is returned on success, 1 is returned if there is an error.
1461 s_pack_internal(PyStructObject
*soself
, PyObject
*args
, int offset
, char* buf
)
1464 /* XXX(nnorwitz): why does i need to be a local? can we use
1465 the offset parameter or do we need the wider width? */
1468 memset(buf
, '\0', soself
->s_size
);
1470 for (code
= soself
->s_codes
; code
->fmtdef
!= NULL
; code
++) {
1472 PyObject
*v
= PyTuple_GET_ITEM(args
, i
++);
1473 const formatdef
*e
= code
->fmtdef
;
1474 char *res
= buf
+ code
->offset
;
1475 if (e
->format
== 's') {
1476 if (!PyString_Check(v
)) {
1477 PyErr_SetString(StructError
,
1478 "argument for 's' must "
1482 n
= PyString_GET_SIZE(v
);
1486 memcpy(res
, PyString_AS_STRING(v
), n
);
1487 } else if (e
->format
== 'p') {
1488 if (!PyString_Check(v
)) {
1489 PyErr_SetString(StructError
,
1490 "argument for 'p' must "
1494 n
= PyString_GET_SIZE(v
);
1495 if (n
> (code
->size
- 1))
1498 memcpy(res
+ 1, PyString_AS_STRING(v
), n
);
1501 *res
= Py_SAFE_DOWNCAST(n
, Py_ssize_t
, unsigned char);
1502 } else if (e
->pack(res
, v
, e
) < 0) {
1503 if (strchr(integer_codes
, e
->format
) != NULL
&&
1504 PyErr_ExceptionMatches(PyExc_OverflowError
))
1505 PyErr_Format(StructError
,
1506 "integer out of range for "
1518 PyDoc_STRVAR(s_pack__doc__
,
1519 "S.pack(v1, v2, ...) -> string\n\
1521 Return a string containing values v1, v2, ... packed according to this\n\
1522 Struct's format. See struct.__doc__ for more on format strings.");
1525 s_pack(PyObject
*self
, PyObject
*args
)
1527 PyStructObject
*soself
;
1530 /* Validate arguments. */
1531 soself
= (PyStructObject
*)self
;
1532 assert(PyStruct_Check(self
));
1533 assert(soself
->s_codes
!= NULL
);
1534 if (PyTuple_GET_SIZE(args
) != soself
->s_len
)
1536 PyErr_Format(StructError
,
1537 "pack requires exactly %zd arguments", soself
->s_len
);
1541 /* Allocate a new string */
1542 result
= PyString_FromStringAndSize((char *)NULL
, soself
->s_size
);
1547 if ( s_pack_internal(soself
, args
, 0, PyString_AS_STRING(result
)) != 0 ) {
1555 PyDoc_STRVAR(s_pack_into__doc__
,
1556 "S.pack_into(buffer, offset, v1, v2, ...)\n\
1558 Pack the values v1, v2, ... according to this Struct's format, write \n\
1559 the packed bytes into the writable buffer buf starting at offset. Note\n\
1560 that the offset is not an optional argument. See struct.__doc__ for \n\
1561 more on format strings.");
1564 s_pack_into(PyObject
*self
, PyObject
*args
)
1566 PyStructObject
*soself
;
1568 Py_ssize_t buffer_len
, offset
;
1570 /* Validate arguments. +1 is for the first arg as buffer. */
1571 soself
= (PyStructObject
*)self
;
1572 assert(PyStruct_Check(self
));
1573 assert(soself
->s_codes
!= NULL
);
1574 if (PyTuple_GET_SIZE(args
) != (soself
->s_len
+ 2))
1576 PyErr_Format(StructError
,
1577 "pack_into requires exactly %zd arguments",
1578 (soself
->s_len
+ 2));
1582 /* Extract a writable memory buffer from the first argument */
1583 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args
, 0),
1584 (void**)&buffer
, &buffer_len
) == -1 ) {
1587 assert( buffer_len
>= 0 );
1589 /* Extract the offset from the first argument */
1590 offset
= PyInt_AsSsize_t(PyTuple_GET_ITEM(args
, 1));
1591 if (offset
== -1 && PyErr_Occurred())
1594 /* Support negative offsets. */
1596 offset
+= buffer_len
;
1598 /* Check boundaries */
1599 if (offset
< 0 || (buffer_len
- offset
) < soself
->s_size
) {
1600 PyErr_Format(StructError
,
1601 "pack_into requires a buffer of at least %zd bytes",
1607 if ( s_pack_internal(soself
, args
, 2, buffer
+ offset
) != 0 ) {
1615 s_get_format(PyStructObject
*self
, void *unused
)
1617 Py_INCREF(self
->s_format
);
1618 return self
->s_format
;
1622 s_get_size(PyStructObject
*self
, void *unused
)
1624 return PyInt_FromSsize_t(self
->s_size
);
1627 /* List of functions */
1629 static struct PyMethodDef s_methods
[] = {
1630 {"pack", s_pack
, METH_VARARGS
, s_pack__doc__
},
1631 {"pack_into", s_pack_into
, METH_VARARGS
, s_pack_into__doc__
},
1632 {"unpack", s_unpack
, METH_O
, s_unpack__doc__
},
1633 {"unpack_from", (PyCFunction
)s_unpack_from
, METH_VARARGS
|METH_KEYWORDS
,
1634 s_unpack_from__doc__
},
1635 {NULL
, NULL
} /* sentinel */
1638 PyDoc_STRVAR(s__doc__
, "Compiled struct object");
1640 #define OFF(x) offsetof(PyStructObject, x)
1642 static PyGetSetDef s_getsetlist
[] = {
1643 {"format", (getter
)s_get_format
, (setter
)NULL
, "struct format string", NULL
},
1644 {"size", (getter
)s_get_size
, (setter
)NULL
, "struct size in bytes", NULL
},
1645 {NULL
} /* sentinel */
1649 PyTypeObject PyStructType
= {
1650 PyVarObject_HEAD_INIT(NULL
, 0)
1652 sizeof(PyStructObject
),
1654 (destructor
)s_dealloc
, /* tp_dealloc */
1660 0, /* tp_as_number */
1661 0, /* tp_as_sequence */
1662 0, /* tp_as_mapping */
1666 PyObject_GenericGetAttr
, /* tp_getattro */
1667 PyObject_GenericSetAttr
, /* tp_setattro */
1668 0, /* tp_as_buffer */
1669 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_WEAKREFS
,/* tp_flags */
1670 s__doc__
, /* tp_doc */
1671 0, /* tp_traverse */
1673 0, /* tp_richcompare */
1674 offsetof(PyStructObject
, weakreflist
), /* tp_weaklistoffset */
1676 0, /* tp_iternext */
1677 s_methods
, /* tp_methods */
1678 NULL
, /* tp_members */
1679 s_getsetlist
, /* tp_getset */
1682 0, /* tp_descr_get */
1683 0, /* tp_descr_set */
1684 0, /* tp_dictoffset */
1685 s_init
, /* tp_init */
1686 PyType_GenericAlloc
,/* tp_alloc */
1688 PyObject_Del
, /* tp_free */
1692 /* ---- Standalone functions ---- */
1694 #define MAXCACHE 100
1695 static PyObject
*cache
= NULL
;
1698 cache_struct(PyObject
*fmt
)
1700 PyObject
* s_object
;
1702 if (cache
== NULL
) {
1703 cache
= PyDict_New();
1708 s_object
= PyDict_GetItem(cache
, fmt
);
1709 if (s_object
!= NULL
) {
1710 Py_INCREF(s_object
);
1714 s_object
= PyObject_CallFunctionObjArgs((PyObject
*)(&PyStructType
), fmt
, NULL
);
1715 if (s_object
!= NULL
) {
1716 if (PyDict_Size(cache
) >= MAXCACHE
)
1717 PyDict_Clear(cache
);
1718 /* Attempt to cache the result */
1719 if (PyDict_SetItem(cache
, fmt
, s_object
) == -1)
1725 PyDoc_STRVAR(clearcache_doc
,
1726 "Clear the internal cache.");
1729 clearcache(PyObject
*self
)
1735 PyDoc_STRVAR(calcsize_doc
,
1736 "Return size of C struct described by format string fmt.");
1739 calcsize(PyObject
*self
, PyObject
*fmt
)
1742 PyObject
*s_object
= cache_struct(fmt
);
1743 if (s_object
== NULL
)
1745 n
= ((PyStructObject
*)s_object
)->s_size
;
1746 Py_DECREF(s_object
);
1747 return PyInt_FromSsize_t(n
);
1750 PyDoc_STRVAR(pack_doc
,
1751 "Return string containing values v1, v2, ... packed according to fmt.");
1754 pack(PyObject
*self
, PyObject
*args
)
1756 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1757 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1760 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1763 fmt
= PyTuple_GET_ITEM(args
, 0);
1764 newargs
= PyTuple_GetSlice(args
, 1, n
);
1765 if (newargs
== NULL
)
1768 s_object
= cache_struct(fmt
);
1769 if (s_object
== NULL
) {
1773 result
= s_pack(s_object
, newargs
);
1775 Py_DECREF(s_object
);
1779 PyDoc_STRVAR(pack_into_doc
,
1780 "Pack the values v1, v2, ... according to fmt.\n\
1781 Write the packed bytes into the writable buffer buf starting at offset.");
1784 pack_into(PyObject
*self
, PyObject
*args
)
1786 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1787 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1790 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1793 fmt
= PyTuple_GET_ITEM(args
, 0);
1794 newargs
= PyTuple_GetSlice(args
, 1, n
);
1795 if (newargs
== NULL
)
1798 s_object
= cache_struct(fmt
);
1799 if (s_object
== NULL
) {
1803 result
= s_pack_into(s_object
, newargs
);
1805 Py_DECREF(s_object
);
1809 PyDoc_STRVAR(unpack_doc
,
1810 "Unpack the string containing packed C structure data, according to fmt.\n\
1811 Requires len(string) == calcsize(fmt).");
1814 unpack(PyObject
*self
, PyObject
*args
)
1816 PyObject
*s_object
, *fmt
, *inputstr
, *result
;
1818 if (!PyArg_UnpackTuple(args
, "unpack", 2, 2, &fmt
, &inputstr
))
1821 s_object
= cache_struct(fmt
);
1822 if (s_object
== NULL
)
1824 result
= s_unpack(s_object
, inputstr
);
1825 Py_DECREF(s_object
);
1829 PyDoc_STRVAR(unpack_from_doc
,
1830 "Unpack the buffer, containing packed C structure data, according to\n\
1831 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1834 unpack_from(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1836 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1837 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1840 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1843 fmt
= PyTuple_GET_ITEM(args
, 0);
1844 newargs
= PyTuple_GetSlice(args
, 1, n
);
1845 if (newargs
== NULL
)
1848 s_object
= cache_struct(fmt
);
1849 if (s_object
== NULL
) {
1853 result
= s_unpack_from(s_object
, newargs
, kwds
);
1855 Py_DECREF(s_object
);
1859 static struct PyMethodDef module_functions
[] = {
1860 {"_clearcache", (PyCFunction
)clearcache
, METH_NOARGS
, clearcache_doc
},
1861 {"calcsize", calcsize
, METH_O
, calcsize_doc
},
1862 {"pack", pack
, METH_VARARGS
, pack_doc
},
1863 {"pack_into", pack_into
, METH_VARARGS
, pack_into_doc
},
1864 {"unpack", unpack
, METH_VARARGS
, unpack_doc
},
1865 {"unpack_from", (PyCFunction
)unpack_from
,
1866 METH_VARARGS
|METH_KEYWORDS
, unpack_from_doc
},
1867 {NULL
, NULL
} /* sentinel */
1871 /* Module initialization */
1873 PyDoc_STRVAR(module_doc
,
1874 "Functions to convert between Python values and C structs.\n\
1875 Python strings are used to hold the data representing the C struct\n\
1876 and also as format strings to describe the layout of data in the C struct.\n\
1878 The optional first format char indicates byte order, size and alignment:\n\
1879 @: native order, size & alignment (default)\n\
1880 =: native order, std. size & alignment\n\
1881 <: little-endian, std. size & alignment\n\
1882 >: big-endian, std. size & alignment\n\
1885 The remaining chars indicate types of args and must match exactly;\n\
1886 these can be preceded by a decimal repeat count:\n\
1887 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
1888 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1889 l:long; L:unsigned long; f:float; d:double.\n\
1890 Special cases (preceding decimal count indicates length):\n\
1891 s:string (array of char); p: pascal string (with count byte).\n\
1892 Special case (only available in native format):\n\
1893 P:an integer type that is wide enough to hold a pointer.\n\
1894 Special case (not in native mode unless 'long long' in platform C):\n\
1895 q:long long; Q:unsigned long long\n\
1896 Whitespace between formats is ignored.\n\
1898 The variable struct.error is an exception raised on errors.\n");
1905 ver
= PyString_FromString("0.2");
1909 m
= Py_InitModule3("_struct", module_functions
, module_doc
);
1913 Py_TYPE(&PyStructType
) = &PyType_Type
;
1914 if (PyType_Ready(&PyStructType
) < 0)
1917 /* This speed trick can't be used until overflow masking goes
1918 away, because native endian always raises exceptions
1919 instead of overflow masking. */
1921 /* Check endian and swap in faster functions */
1924 formatdef
*native
= native_table
;
1925 formatdef
*other
, *ptr
;
1926 if ((int)*(unsigned char*)&one
)
1927 other
= lilendian_table
;
1929 other
= bigendian_table
;
1930 /* Scan through the native table, find a matching
1931 entry in the endian table and swap in the
1932 native implementations whenever possible
1933 (64-bit platforms may not have "standard" sizes) */
1934 while (native
->format
!= '\0' && other
->format
!= '\0') {
1936 while (ptr
->format
!= '\0') {
1937 if (ptr
->format
== native
->format
) {
1938 /* Match faster when formats are
1939 listed in the same order */
1942 /* Only use the trick if the
1944 if (ptr
->size
!= native
->size
)
1946 /* Skip float and double, could be
1947 "unknown" float format */
1948 if (ptr
->format
== 'd' || ptr
->format
== 'f')
1950 ptr
->pack
= native
->pack
;
1951 ptr
->unpack
= native
->unpack
;
1960 /* Add some symbolic constants to the module */
1961 if (StructError
== NULL
) {
1962 StructError
= PyErr_NewException("struct.error", NULL
, NULL
);
1963 if (StructError
== NULL
)
1967 Py_INCREF(StructError
);
1968 PyModule_AddObject(m
, "error", StructError
);
1970 Py_INCREF((PyObject
*)&PyStructType
);
1971 PyModule_AddObject(m
, "Struct", (PyObject
*)&PyStructType
);
1973 PyModule_AddObject(m
, "__version__", ver
);
1975 PyModule_AddIntConstant(m
, "_PY_STRUCT_RANGE_CHECKING", 1);
1976 #ifdef PY_STRUCT_FLOAT_COERCE
1977 PyModule_AddIntConstant(m
, "_PY_STRUCT_FLOAT_COERCE", 1);