1 /* struct module -- pack values into and (out of) bytes objects */
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 /* The translation function for each format character is table driven */
16 typedef struct _formatdef
{
20 PyObject
* (*unpack
)(const char *,
21 const struct _formatdef
*);
22 int (*pack
)(char *, PyObject
*,
23 const struct _formatdef
*);
26 typedef struct _formatcode
{
27 const struct _formatdef
*fmtdef
;
32 /* Struct object interface */
40 PyObject
*weakreflist
; /* List of weak references */
44 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
45 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
50 static PyObject
*StructError
;
53 /* Define various structs to figure out the alignments of types */
56 typedef struct { char c
; short x
; } st_short
;
57 typedef struct { char c
; int x
; } st_int
;
58 typedef struct { char c
; long x
; } st_long
;
59 typedef struct { char c
; float x
; } st_float
;
60 typedef struct { char c
; double x
; } st_double
;
61 typedef struct { char c
; void *x
; } st_void_p
;
63 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
64 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
65 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
66 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
67 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
68 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
70 /* We can't support q and Q in native mode unless the compiler does;
71 in std mode, they're 8 bytes on all platforms. */
73 typedef struct { char c
; PY_LONG_LONG x
; } s_long_long
;
74 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
78 #define BOOL_TYPE _Bool
79 typedef struct { char c
; _Bool x
; } s_bool
;
80 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
82 #define BOOL_TYPE char
86 #define STRINGIFY(x) #x
89 #pragma options align=reset
92 /* Helper to get a PyLongObject. Caller should decref. */
95 get_pylong(PyObject
*v
)
98 if (!PyLong_Check(v
)) {
99 PyErr_SetString(StructError
,
100 "required argument is not an integer");
108 /* Helper routine to get a C long and raise the appropriate error if it isn't
112 get_long(PyObject
*v
, long *p
)
116 if (!PyLong_Check(v
)) {
117 PyErr_SetString(StructError
,
118 "required argument is not an integer");
121 x
= PyLong_AsLong(v
);
122 if (x
== -1 && PyErr_Occurred()) {
123 if (PyErr_ExceptionMatches(PyExc_OverflowError
))
124 PyErr_SetString(StructError
,
125 "argument out of range");
133 /* Same, but handling unsigned long */
135 #ifndef PY_STRUCT_OVERFLOW_MASKING
137 get_ulong(PyObject
*v
, unsigned long *p
)
141 if (!PyLong_Check(v
)) {
142 PyErr_SetString(StructError
,
143 "required argument is not an integer");
146 x
= PyLong_AsUnsignedLong(v
);
147 if (x
== (unsigned long)-1 && PyErr_Occurred()) {
148 if (PyErr_ExceptionMatches(PyExc_OverflowError
))
149 PyErr_SetString(StructError
,
150 "argument out of range");
156 #endif /* PY_STRUCT_OVERFLOW_MASKING */
158 #ifdef HAVE_LONG_LONG
160 /* Same, but handling native long long. */
163 get_longlong(PyObject
*v
, PY_LONG_LONG
*p
)
166 if (!PyLong_Check(v
)) {
167 PyErr_SetString(StructError
,
168 "required argument is not an integer");
171 x
= PyLong_AsLongLong(v
);
172 if (x
== -1 && PyErr_Occurred()) {
173 if (PyErr_ExceptionMatches(PyExc_OverflowError
))
174 PyErr_SetString(StructError
,
175 "argument out of range");
182 /* Same, but handling native unsigned long long. */
185 get_ulonglong(PyObject
*v
, unsigned PY_LONG_LONG
*p
)
187 unsigned PY_LONG_LONG x
;
188 if (!PyLong_Check(v
)) {
189 PyErr_SetString(StructError
,
190 "required argument is not an integer");
193 x
= PyLong_AsUnsignedLongLong(v
);
194 if (x
== -1 && PyErr_Occurred()) {
195 if (PyErr_ExceptionMatches(PyExc_OverflowError
))
196 PyErr_SetString(StructError
,
197 "argument out of range");
207 #define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
210 /* Floating point helpers */
213 unpack_float(const char *p
, /* start of 4-byte string */
214 int le
) /* true for little-endian, false for big-endian */
218 x
= _PyFloat_Unpack4((unsigned char *)p
, le
);
219 if (x
== -1.0 && PyErr_Occurred())
221 return PyFloat_FromDouble(x
);
225 unpack_double(const char *p
, /* start of 8-byte string */
226 int le
) /* true for little-endian, false for big-endian */
230 x
= _PyFloat_Unpack8((unsigned char *)p
, le
);
231 if (x
== -1.0 && PyErr_Occurred())
233 return PyFloat_FromDouble(x
);
236 /* Helper to format the range error exceptions */
238 _range_error(const formatdef
*f
, int is_unsigned
)
240 /* ulargest is the largest unsigned value with f->size bytes.
241 * Note that the simpler:
242 * ((size_t)1 << (f->size * 8)) - 1
243 * doesn't work when f->size == sizeof(size_t) because C doesn't
244 * define what happens when a left shift count is >= the number of
245 * bits in the integer being shifted; e.g., on some boxes it doesn't
246 * shift at all when they're equal.
248 const size_t ulargest
= (size_t)-1 >> ((SIZEOF_SIZE_T
- f
->size
)*8);
249 assert(f
->size
>= 1 && f
->size
<= SIZEOF_SIZE_T
);
251 PyErr_Format(StructError
,
252 "'%c' format requires 0 <= number <= %zu",
256 const Py_ssize_t largest
= (Py_ssize_t
)(ulargest
>> 1);
257 PyErr_Format(StructError
,
258 "'%c' format requires %zd <= number <= %zd",
269 /* A large number of small routines follow, with names of the form
273 [bln] distiguishes among big-endian, little-endian and native.
274 [pu] distiguishes between pack (to struct) and unpack (from struct).
275 TYPE is one of char, byte, ubyte, etc.
278 /* Native mode routines. ****************************************************/
280 In all n[up]_<type> routines handling types larger than 1 byte, there is
281 *no* guarantee that the p pointer is properly aligned for each type,
282 therefore memcpy is called. An intermediate variable is used to
283 compensate for big-endian architectures.
284 Normally both the intermediate variable and the memcpy call will be
285 skipped by C optimisation in little-endian architectures (gcc >= 2.91
289 nu_char(const char *p
, const formatdef
*f
)
291 return PyBytes_FromStringAndSize(p
, 1);
295 nu_byte(const char *p
, const formatdef
*f
)
297 return PyLong_FromLong((long) *(signed char *)p
);
301 nu_ubyte(const char *p
, const formatdef
*f
)
303 return PyLong_FromLong((long) *(unsigned char *)p
);
307 nu_short(const char *p
, const formatdef
*f
)
310 memcpy((char *)&x
, p
, sizeof x
);
311 return PyLong_FromLong((long)x
);
315 nu_ushort(const char *p
, const formatdef
*f
)
318 memcpy((char *)&x
, p
, sizeof x
);
319 return PyLong_FromLong((long)x
);
323 nu_int(const char *p
, const formatdef
*f
)
326 memcpy((char *)&x
, p
, sizeof x
);
327 return PyLong_FromLong((long)x
);
331 nu_uint(const char *p
, const formatdef
*f
)
334 memcpy((char *)&x
, p
, sizeof x
);
335 #if (SIZEOF_LONG > SIZEOF_INT)
336 return PyLong_FromLong((long)x
);
338 if (x
<= ((unsigned int)LONG_MAX
))
339 return PyLong_FromLong((long)x
);
340 return PyLong_FromUnsignedLong((unsigned long)x
);
345 nu_long(const char *p
, const formatdef
*f
)
348 memcpy((char *)&x
, p
, sizeof x
);
349 return PyLong_FromLong(x
);
353 nu_ulong(const char *p
, const formatdef
*f
)
356 memcpy((char *)&x
, p
, sizeof x
);
358 return PyLong_FromLong((long)x
);
359 return PyLong_FromUnsignedLong(x
);
362 /* Native mode doesn't support q or Q unless the platform C supports
363 long long (or, on Windows, __int64). */
365 #ifdef HAVE_LONG_LONG
368 nu_longlong(const char *p
, const formatdef
*f
)
371 memcpy((char *)&x
, p
, sizeof x
);
372 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
373 return PyLong_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
374 return PyLong_FromLongLong(x
);
378 nu_ulonglong(const char *p
, const formatdef
*f
)
380 unsigned PY_LONG_LONG x
;
381 memcpy((char *)&x
, p
, sizeof x
);
383 return PyLong_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
384 return PyLong_FromUnsignedLongLong(x
);
390 nu_bool(const char *p
, const formatdef
*f
)
393 memcpy((char *)&x
, p
, sizeof x
);
394 return PyBool_FromLong(x
!= 0);
399 nu_float(const char *p
, const formatdef
*f
)
402 memcpy((char *)&x
, p
, sizeof x
);
403 return PyFloat_FromDouble((double)x
);
407 nu_double(const char *p
, const formatdef
*f
)
410 memcpy((char *)&x
, p
, sizeof x
);
411 return PyFloat_FromDouble(x
);
415 nu_void_p(const char *p
, const formatdef
*f
)
418 memcpy((char *)&x
, p
, sizeof x
);
419 return PyLong_FromVoidPtr(x
);
423 np_byte(char *p
, PyObject
*v
, const formatdef
*f
)
426 if (get_long(v
, &x
) < 0)
428 if (x
< -128 || x
> 127){
429 PyErr_SetString(StructError
,
430 "byte format requires -128 <= number <= 127");
438 np_ubyte(char *p
, PyObject
*v
, const formatdef
*f
)
441 if (get_long(v
, &x
) < 0)
443 if (x
< 0 || x
> 255){
444 PyErr_SetString(StructError
,
445 "ubyte format requires 0 <= number <= 255");
453 np_char(char *p
, PyObject
*v
, const formatdef
*f
)
455 if (PyUnicode_Check(v
)) {
456 v
= _PyUnicode_AsDefaultEncodedString(v
, NULL
);
460 if (!PyBytes_Check(v
) || PyBytes_Size(v
) != 1) {
461 PyErr_SetString(StructError
,
462 "char format requires bytes or string of length 1");
465 *p
= *PyBytes_AsString(v
);
470 np_short(char *p
, PyObject
*v
, const formatdef
*f
)
474 if (get_long(v
, &x
) < 0)
476 if (x
< SHRT_MIN
|| x
> SHRT_MAX
){
477 PyErr_SetString(StructError
,
478 "short format requires " STRINGIFY(SHRT_MIN
)
479 " <= number <= " STRINGIFY(SHRT_MAX
));
483 memcpy(p
, (char *)&y
, sizeof y
);
488 np_ushort(char *p
, PyObject
*v
, const formatdef
*f
)
492 if (get_long(v
, &x
) < 0)
494 if (x
< 0 || x
> USHRT_MAX
){
495 PyErr_SetString(StructError
,
496 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX
));
499 y
= (unsigned short)x
;
500 memcpy(p
, (char *)&y
, sizeof y
);
505 np_int(char *p
, PyObject
*v
, const formatdef
*f
)
509 if (get_long(v
, &x
) < 0)
511 #if (SIZEOF_LONG > SIZEOF_INT)
512 if ((x
< ((long)INT_MIN
)) || (x
> ((long)INT_MAX
)))
513 RANGE_ERROR(x
, f
, 0, -1);
516 memcpy(p
, (char *)&y
, sizeof y
);
521 np_uint(char *p
, PyObject
*v
, const formatdef
*f
)
525 if (get_ulong(v
, &x
) < 0)
528 #if (SIZEOF_LONG > SIZEOF_INT)
529 if (x
> ((unsigned long)UINT_MAX
))
530 RANGE_ERROR(y
, f
, 1, -1);
532 memcpy(p
, (char *)&y
, sizeof y
);
537 np_long(char *p
, PyObject
*v
, const formatdef
*f
)
540 if (get_long(v
, &x
) < 0)
542 memcpy(p
, (char *)&x
, sizeof x
);
547 np_ulong(char *p
, PyObject
*v
, const formatdef
*f
)
550 if (get_ulong(v
, &x
) < 0)
552 memcpy(p
, (char *)&x
, sizeof x
);
556 #ifdef HAVE_LONG_LONG
559 np_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
562 if (get_longlong(v
, &x
) < 0)
564 memcpy(p
, (char *)&x
, sizeof x
);
569 np_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
571 unsigned PY_LONG_LONG x
;
572 if (get_ulonglong(v
, &x
) < 0)
574 memcpy(p
, (char *)&x
, sizeof x
);
581 np_bool(char *p
, PyObject
*v
, const formatdef
*f
)
584 y
= PyObject_IsTrue(v
);
585 memcpy(p
, (char *)&y
, sizeof y
);
590 np_float(char *p
, PyObject
*v
, const formatdef
*f
)
592 float x
= (float)PyFloat_AsDouble(v
);
593 if (x
== -1 && PyErr_Occurred()) {
594 PyErr_SetString(StructError
,
595 "required argument is not a float");
598 memcpy(p
, (char *)&x
, sizeof x
);
603 np_double(char *p
, PyObject
*v
, const formatdef
*f
)
605 double x
= PyFloat_AsDouble(v
);
606 if (x
== -1 && PyErr_Occurred()) {
607 PyErr_SetString(StructError
,
608 "required argument is not a float");
611 memcpy(p
, (char *)&x
, sizeof(double));
616 np_void_p(char *p
, PyObject
*v
, const formatdef
*f
)
623 assert(PyLong_Check(v
));
624 x
= PyLong_AsVoidPtr(v
);
626 if (x
== NULL
&& PyErr_Occurred())
628 memcpy(p
, (char *)&x
, sizeof x
);
632 static formatdef native_table
[] = {
633 {'x', sizeof(char), 0, NULL
},
634 {'b', sizeof(char), 0, nu_byte
, np_byte
},
635 {'B', sizeof(char), 0, nu_ubyte
, np_ubyte
},
636 {'c', sizeof(char), 0, nu_char
, np_char
},
637 {'s', sizeof(char), 0, NULL
},
638 {'p', sizeof(char), 0, NULL
},
639 {'h', sizeof(short), SHORT_ALIGN
, nu_short
, np_short
},
640 {'H', sizeof(short), SHORT_ALIGN
, nu_ushort
, np_ushort
},
641 {'i', sizeof(int), INT_ALIGN
, nu_int
, np_int
},
642 {'I', sizeof(int), INT_ALIGN
, nu_uint
, np_uint
},
643 {'l', sizeof(long), LONG_ALIGN
, nu_long
, np_long
},
644 {'L', sizeof(long), LONG_ALIGN
, nu_ulong
, np_ulong
},
645 #ifdef HAVE_LONG_LONG
646 {'q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_longlong
, np_longlong
},
647 {'Q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_ulonglong
,np_ulonglong
},
649 {'?', sizeof(BOOL_TYPE
), BOOL_ALIGN
, nu_bool
, np_bool
},
650 {'f', sizeof(float), FLOAT_ALIGN
, nu_float
, np_float
},
651 {'d', sizeof(double), DOUBLE_ALIGN
, nu_double
, np_double
},
652 {'P', sizeof(void *), VOID_P_ALIGN
, nu_void_p
, np_void_p
},
656 /* Big-endian routines. *****************************************************/
659 bu_int(const char *p
, const formatdef
*f
)
662 Py_ssize_t i
= f
->size
;
663 const unsigned char *bytes
= (const unsigned char *)p
;
665 x
= (x
<<8) | *bytes
++;
667 /* Extend the sign bit. */
668 if (SIZEOF_LONG
> f
->size
)
669 x
|= -(x
& (1L << ((8 * f
->size
) - 1)));
670 return PyLong_FromLong(x
);
674 bu_uint(const char *p
, const formatdef
*f
)
677 Py_ssize_t i
= f
->size
;
678 const unsigned char *bytes
= (const unsigned char *)p
;
680 x
= (x
<<8) | *bytes
++;
683 return PyLong_FromLong((long)x
);
684 return PyLong_FromUnsignedLong(x
);
688 bu_longlong(const char *p
, const formatdef
*f
)
690 #ifdef HAVE_LONG_LONG
692 Py_ssize_t i
= f
->size
;
693 const unsigned char *bytes
= (const unsigned char *)p
;
695 x
= (x
<<8) | *bytes
++;
697 /* Extend the sign bit. */
698 if (SIZEOF_LONG_LONG
> f
->size
)
699 x
|= -(x
& ((PY_LONG_LONG
)1 << ((8 * f
->size
) - 1)));
700 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
701 return PyLong_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
702 return PyLong_FromLongLong(x
);
704 return _PyLong_FromByteArray((const unsigned char *)p
,
706 0, /* little-endian */
712 bu_ulonglong(const char *p
, const formatdef
*f
)
714 #ifdef HAVE_LONG_LONG
715 unsigned PY_LONG_LONG x
= 0;
716 Py_ssize_t i
= f
->size
;
717 const unsigned char *bytes
= (const unsigned char *)p
;
719 x
= (x
<<8) | *bytes
++;
722 return PyLong_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
723 return PyLong_FromUnsignedLongLong(x
);
725 return _PyLong_FromByteArray((const unsigned char *)p
,
727 0, /* little-endian */
733 bu_float(const char *p
, const formatdef
*f
)
735 return unpack_float(p
, 0);
739 bu_double(const char *p
, const formatdef
*f
)
741 return unpack_double(p
, 0);
745 bu_bool(const char *p
, const formatdef
*f
)
748 memcpy((char *)&x
, p
, sizeof x
);
749 return PyBool_FromLong(x
!= 0);
753 bp_int(char *p
, PyObject
*v
, const formatdef
*f
)
757 if (get_long(v
, &x
) < 0)
760 if (i
!= SIZEOF_LONG
) {
761 if ((i
== 2) && (x
< -32768 || x
> 32767))
762 RANGE_ERROR(x
, f
, 0, 0xffffL
);
763 #if (SIZEOF_LONG != 4)
764 else if ((i
== 4) && (x
< -2147483648L || x
> 2147483647L))
765 RANGE_ERROR(x
, f
, 0, 0xffffffffL
);
776 bp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
780 if (get_ulong(v
, &x
) < 0)
783 if (i
!= SIZEOF_LONG
) {
784 unsigned long maxint
= 1;
785 maxint
<<= (unsigned long)(i
* 8);
787 RANGE_ERROR(x
, f
, 1, maxint
- 1);
797 bp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
803 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
806 0, /* little_endian */
813 bp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
819 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
822 0, /* little_endian */
829 bp_float(char *p
, PyObject
*v
, const formatdef
*f
)
831 double x
= PyFloat_AsDouble(v
);
832 if (x
== -1 && PyErr_Occurred()) {
833 PyErr_SetString(StructError
,
834 "required argument is not a float");
837 return _PyFloat_Pack4(x
, (unsigned char *)p
, 0);
841 bp_double(char *p
, PyObject
*v
, const formatdef
*f
)
843 double x
= PyFloat_AsDouble(v
);
844 if (x
== -1 && PyErr_Occurred()) {
845 PyErr_SetString(StructError
,
846 "required argument is not a float");
849 return _PyFloat_Pack8(x
, (unsigned char *)p
, 0);
853 bp_bool(char *p
, PyObject
*v
, const formatdef
*f
)
856 y
= PyObject_IsTrue(v
);
857 memcpy(p
, (char *)&y
, sizeof y
);
861 static formatdef bigendian_table
[] = {
863 {'b', 1, 0, nu_byte
, np_byte
},
864 {'B', 1, 0, nu_ubyte
, np_ubyte
},
865 {'c', 1, 0, nu_char
, np_char
},
868 {'h', 2, 0, bu_int
, bp_int
},
869 {'H', 2, 0, bu_uint
, bp_uint
},
870 {'i', 4, 0, bu_int
, bp_int
},
871 {'I', 4, 0, bu_uint
, bp_uint
},
872 {'l', 4, 0, bu_int
, bp_int
},
873 {'L', 4, 0, bu_uint
, bp_uint
},
874 {'q', 8, 0, bu_longlong
, bp_longlong
},
875 {'Q', 8, 0, bu_ulonglong
, bp_ulonglong
},
876 {'?', 1, 0, bu_bool
, bp_bool
},
877 {'f', 4, 0, bu_float
, bp_float
},
878 {'d', 8, 0, bu_double
, bp_double
},
882 /* Little-endian routines. *****************************************************/
885 lu_int(const char *p
, const formatdef
*f
)
888 Py_ssize_t i
= f
->size
;
889 const unsigned char *bytes
= (const unsigned char *)p
;
891 x
= (x
<<8) | bytes
[--i
];
893 /* Extend the sign bit. */
894 if (SIZEOF_LONG
> f
->size
)
895 x
|= -(x
& (1L << ((8 * f
->size
) - 1)));
896 return PyLong_FromLong(x
);
900 lu_uint(const char *p
, const formatdef
*f
)
903 Py_ssize_t i
= f
->size
;
904 const unsigned char *bytes
= (const unsigned char *)p
;
906 x
= (x
<<8) | bytes
[--i
];
909 return PyLong_FromLong((long)x
);
910 return PyLong_FromUnsignedLong((long)x
);
914 lu_longlong(const char *p
, const formatdef
*f
)
916 #ifdef HAVE_LONG_LONG
918 Py_ssize_t i
= f
->size
;
919 const unsigned char *bytes
= (const unsigned char *)p
;
921 x
= (x
<<8) | bytes
[--i
];
923 /* Extend the sign bit. */
924 if (SIZEOF_LONG_LONG
> f
->size
)
925 x
|= -(x
& ((PY_LONG_LONG
)1 << ((8 * f
->size
) - 1)));
926 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
927 return PyLong_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
928 return PyLong_FromLongLong(x
);
930 return _PyLong_FromByteArray((const unsigned char *)p
,
932 1, /* little-endian */
938 lu_ulonglong(const char *p
, const formatdef
*f
)
940 #ifdef HAVE_LONG_LONG
941 unsigned PY_LONG_LONG x
= 0;
942 Py_ssize_t i
= f
->size
;
943 const unsigned char *bytes
= (const unsigned char *)p
;
945 x
= (x
<<8) | bytes
[--i
];
948 return PyLong_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
949 return PyLong_FromUnsignedLongLong(x
);
951 return _PyLong_FromByteArray((const unsigned char *)p
,
953 1, /* little-endian */
959 lu_float(const char *p
, const formatdef
*f
)
961 return unpack_float(p
, 1);
965 lu_double(const char *p
, const formatdef
*f
)
967 return unpack_double(p
, 1);
971 lp_int(char *p
, PyObject
*v
, const formatdef
*f
)
975 if (get_long(v
, &x
) < 0)
978 if (i
!= SIZEOF_LONG
) {
979 if ((i
== 2) && (x
< -32768 || x
> 32767))
980 RANGE_ERROR(x
, f
, 0, 0xffffL
);
981 #if (SIZEOF_LONG != 4)
982 else if ((i
== 4) && (x
< -2147483648L || x
> 2147483647L))
983 RANGE_ERROR(x
, f
, 0, 0xffffffffL
);
994 lp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
998 if (get_ulong(v
, &x
) < 0)
1001 if (i
!= SIZEOF_LONG
) {
1002 unsigned long maxint
= 1;
1003 maxint
<<= (unsigned long)(i
* 8);
1005 RANGE_ERROR(x
, f
, 1, maxint
- 1);
1015 lp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
1021 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
1024 1, /* little_endian */
1031 lp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
1037 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
1040 1, /* little_endian */
1047 lp_float(char *p
, PyObject
*v
, const formatdef
*f
)
1049 double x
= PyFloat_AsDouble(v
);
1050 if (x
== -1 && PyErr_Occurred()) {
1051 PyErr_SetString(StructError
,
1052 "required argument is not a float");
1055 return _PyFloat_Pack4(x
, (unsigned char *)p
, 1);
1059 lp_double(char *p
, PyObject
*v
, const formatdef
*f
)
1061 double x
= PyFloat_AsDouble(v
);
1062 if (x
== -1 && PyErr_Occurred()) {
1063 PyErr_SetString(StructError
,
1064 "required argument is not a float");
1067 return _PyFloat_Pack8(x
, (unsigned char *)p
, 1);
1070 static formatdef lilendian_table
[] = {
1072 {'b', 1, 0, nu_byte
, np_byte
},
1073 {'B', 1, 0, nu_ubyte
, np_ubyte
},
1074 {'c', 1, 0, nu_char
, np_char
},
1077 {'h', 2, 0, lu_int
, lp_int
},
1078 {'H', 2, 0, lu_uint
, lp_uint
},
1079 {'i', 4, 0, lu_int
, lp_int
},
1080 {'I', 4, 0, lu_uint
, lp_uint
},
1081 {'l', 4, 0, lu_int
, lp_int
},
1082 {'L', 4, 0, lu_uint
, lp_uint
},
1083 {'q', 8, 0, lu_longlong
, lp_longlong
},
1084 {'Q', 8, 0, lu_ulonglong
, lp_ulonglong
},
1085 {'?', 1, 0, bu_bool
, bp_bool
}, /* Std rep not endian dep,
1086 but potentially different from native rep -- reuse bx_bool funcs. */
1087 {'f', 4, 0, lu_float
, lp_float
},
1088 {'d', 8, 0, lu_double
, lp_double
},
1093 static const formatdef
*
1094 whichtable(char **pfmt
)
1096 const char *fmt
= (*pfmt
)++; /* May be backed out of later */
1099 return lilendian_table
;
1101 case '!': /* Network byte order is big-endian */
1102 return bigendian_table
;
1103 case '=': { /* Host byte order -- different from native in aligment! */
1105 char *p
= (char *) &n
;
1107 return lilendian_table
;
1109 return bigendian_table
;
1112 --*pfmt
; /* Back out of pointer increment */
1115 return native_table
;
1120 /* Get the table entry for a format code */
1122 static const formatdef
*
1123 getentry(int c
, const formatdef
*f
)
1125 for (; f
->format
!= '\0'; f
++) {
1126 if (f
->format
== c
) {
1130 PyErr_SetString(StructError
, "bad char in struct format");
1135 /* Align a size according to a format code. Return -1 on overflow. */
1138 align(Py_ssize_t size
, char c
, const formatdef
*e
)
1142 if (e
->format
== c
) {
1143 if (e
->alignment
&& size
> 0) {
1144 extra
= (e
->alignment
- 1) - (size
- 1) % (e
->alignment
);
1145 if (extra
> PY_SSIZE_T_MAX
- size
)
1154 /* calculate the size of a format string */
1157 prepare_s(PyStructObject
*self
)
1166 Py_ssize_t size
, len
, num
, itemsize
;
1168 fmt
= PyBytes_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 /* overflow-safe version of
1182 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1183 if (num
>= PY_SSIZE_T_MAX
/ 10 && (
1184 num
> PY_SSIZE_T_MAX
/ 10 ||
1185 (c
- '0') > PY_SSIZE_T_MAX
% 10))
1187 num
= num
*10 + (c
- '0');
1200 case 's': /* fall through */
1201 case 'p': len
++; break;
1203 default: len
+= num
; break;
1207 size
= align(size
, c
, e
);
1211 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1212 if (num
> (PY_SSIZE_T_MAX
- size
) / itemsize
)
1214 size
+= num
* itemsize
;
1217 /* check for overflow */
1218 if ((len
+ 1) > (PY_SSIZE_T_MAX
/ sizeof(formatcode
))) {
1223 self
->s_size
= size
;
1225 codes
= PyMem_MALLOC((len
+ 1) * sizeof(formatcode
));
1226 if (codes
== NULL
) {
1230 self
->s_codes
= codes
;
1234 while ((c
= *s
++) != '\0') {
1235 if (isspace(Py_CHARMASK(c
)))
1237 if ('0' <= c
&& c
<= '9') {
1239 while ('0' <= (c
= *s
++) && c
<= '9')
1240 num
= num
*10 + (c
- '0');
1249 size
= align(size
, c
, e
);
1250 if (c
== 's' || c
== 'p') {
1251 codes
->offset
= size
;
1256 } else if (c
== 'x') {
1259 while (--num
>= 0) {
1260 codes
->offset
= size
;
1261 codes
->size
= e
->size
;
1268 codes
->fmtdef
= NULL
;
1269 codes
->offset
= size
;
1275 PyErr_SetString(StructError
,
1276 "total struct size too long");
1281 s_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1285 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
1287 self
= type
->tp_alloc(type
, 0);
1289 PyStructObject
*s
= (PyStructObject
*)self
;
1291 s
->s_format
= Py_None
;
1300 s_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1302 PyStructObject
*soself
= (PyStructObject
*)self
;
1303 PyObject
*o_format
= NULL
;
1305 static char *kwlist
[] = {"format", 0};
1307 assert(PyStruct_Check(self
));
1309 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O:Struct", kwlist
,
1313 if (PyUnicode_Check(o_format
)) {
1314 o_format
= PyUnicode_AsASCIIString(o_format
);
1315 if (o_format
== NULL
)
1318 /* XXX support buffer interface, too */
1320 Py_INCREF(o_format
);
1323 if (!PyBytes_Check(o_format
)) {
1324 Py_DECREF(o_format
);
1325 PyErr_Format(PyExc_TypeError
,
1326 "Struct() argument 1 must be bytes, not %.200s",
1327 Py_TYPE(o_format
)->tp_name
);
1331 Py_CLEAR(soself
->s_format
);
1332 soself
->s_format
= o_format
;
1334 ret
= prepare_s(soself
);
1339 s_dealloc(PyStructObject
*s
)
1341 if (s
->weakreflist
!= NULL
)
1342 PyObject_ClearWeakRefs((PyObject
*)s
);
1343 if (s
->s_codes
!= NULL
) {
1344 PyMem_FREE(s
->s_codes
);
1346 Py_XDECREF(s
->s_format
);
1347 Py_TYPE(s
)->tp_free((PyObject
*)s
);
1351 s_unpack_internal(PyStructObject
*soself
, char *startfrom
) {
1354 PyObject
*result
= PyTuple_New(soself
->s_len
);
1358 for (code
= soself
->s_codes
; code
->fmtdef
!= NULL
; code
++) {
1360 const formatdef
*e
= code
->fmtdef
;
1361 const char *res
= startfrom
+ code
->offset
;
1362 if (e
->format
== 's') {
1363 v
= PyBytes_FromStringAndSize(res
, code
->size
);
1364 } else if (e
->format
== 'p') {
1365 Py_ssize_t n
= *(unsigned char*)res
;
1366 if (n
>= code
->size
)
1368 v
= PyBytes_FromStringAndSize(res
+ 1, n
);
1370 v
= e
->unpack(res
, e
);
1374 PyTuple_SET_ITEM(result
, i
++, v
);
1384 PyDoc_STRVAR(s_unpack__doc__
,
1385 "S.unpack(buffer) -> (v1, v2, ...)\n\
1387 Return a tuple containing values unpacked according to the format\n\
1388 string S.format. Requires len(buffer) == S.size. See help(struct)\n\
1389 for more on format strings.");
1392 s_unpack(PyObject
*self
, PyObject
*input
)
1396 PyStructObject
*soself
= (PyStructObject
*)self
;
1398 assert(PyStruct_Check(self
));
1399 assert(soself
->s_codes
!= NULL
);
1400 if (PyObject_GetBuffer(input
, &vbuf
, PyBUF_SIMPLE
) < 0)
1402 if (vbuf
.len
!= soself
->s_size
) {
1403 PyErr_Format(StructError
,
1404 "unpack requires a bytes argument of length %zd",
1406 PyBuffer_Release(&vbuf
);
1409 result
= s_unpack_internal(soself
, vbuf
.buf
);
1410 PyBuffer_Release(&vbuf
);
1414 PyDoc_STRVAR(s_unpack_from__doc__
,
1415 "S.unpack_from(buffer, offset=0) -> (v1, v2, ...)\n\
1417 Return a tuple containing values unpacked according to the format\n\
1418 string S.format. Requires len(buffer[offset:]) >= S.size. See\n\
1419 help(struct) for more on format strings.");
1422 s_unpack_from(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1424 static char *kwlist
[] = {"buffer", "offset", 0};
1427 Py_ssize_t offset
= 0;
1430 PyStructObject
*soself
= (PyStructObject
*)self
;
1432 assert(PyStruct_Check(self
));
1433 assert(soself
->s_codes
!= NULL
);
1435 if (!PyArg_ParseTupleAndKeywords(args
, kwds
,
1436 "O|n:unpack_from", kwlist
,
1439 if (PyObject_GetBuffer(input
, &vbuf
, PyBUF_SIMPLE
) < 0)
1443 if (offset
< 0 || vbuf
.len
- offset
< soself
->s_size
) {
1444 PyErr_Format(StructError
,
1445 "unpack_from requires a buffer of at least %zd bytes",
1447 PyBuffer_Release(&vbuf
);
1450 result
= s_unpack_internal(soself
, (char*)vbuf
.buf
+ offset
);
1451 PyBuffer_Release(&vbuf
);
1457 * Guts of the pack function.
1459 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1460 * argument for where to start processing the arguments for packing, and a
1461 * character buffer for writing the packed string. The caller must insure
1462 * that the buffer may contain the required length for packing the arguments.
1463 * 0 is returned on success, 1 is returned if there is an error.
1467 s_pack_internal(PyStructObject
*soself
, PyObject
*args
, int offset
, char* buf
)
1470 /* XXX(nnorwitz): why does i need to be a local? can we use
1471 the offset parameter or do we need the wider width? */
1474 memset(buf
, '\0', soself
->s_size
);
1476 for (code
= soself
->s_codes
; code
->fmtdef
!= NULL
; code
++) {
1478 PyObject
*v
= PyTuple_GET_ITEM(args
, i
++);
1479 const formatdef
*e
= code
->fmtdef
;
1480 char *res
= buf
+ code
->offset
;
1481 if (e
->format
== 's') {
1484 if (PyUnicode_Check(v
)) {
1485 v
= _PyUnicode_AsDefaultEncodedString(v
, NULL
);
1489 isstring
= PyBytes_Check(v
);
1490 if (!isstring
&& !PyByteArray_Check(v
)) {
1491 PyErr_SetString(StructError
,
1492 "argument for 's' must be a bytes or string");
1496 n
= PyBytes_GET_SIZE(v
);
1497 p
= PyBytes_AS_STRING(v
);
1500 n
= PyByteArray_GET_SIZE(v
);
1501 p
= PyByteArray_AS_STRING(v
);
1507 } else if (e
->format
== 'p') {
1510 if (PyUnicode_Check(v
)) {
1511 v
= _PyUnicode_AsDefaultEncodedString(v
, NULL
);
1515 isstring
= PyBytes_Check(v
);
1516 if (!isstring
&& !PyByteArray_Check(v
)) {
1517 PyErr_SetString(StructError
,
1518 "argument for 'p' must be a bytes or string");
1522 n
= PyBytes_GET_SIZE(v
);
1523 p
= PyBytes_AS_STRING(v
);
1526 n
= PyByteArray_GET_SIZE(v
);
1527 p
= PyByteArray_AS_STRING(v
);
1529 if (n
> (code
->size
- 1))
1532 memcpy(res
+ 1, p
, n
);
1535 *res
= Py_SAFE_DOWNCAST(n
, Py_ssize_t
, unsigned char);
1537 if (e
->pack(res
, v
, e
) < 0) {
1538 if (PyLong_Check(v
) && PyErr_ExceptionMatches(PyExc_OverflowError
))
1539 PyErr_SetString(StructError
,
1540 "long too large to convert to int");
1551 PyDoc_STRVAR(s_pack__doc__
,
1552 "S.pack(v1, v2, ...) -> bytes\n\
1554 Return a bytes object containing values v1, v2, ... packed according\n\
1555 to the format string S.format. See help(struct) for more on format\n\
1559 s_pack(PyObject
*self
, PyObject
*args
)
1561 PyStructObject
*soself
;
1564 /* Validate arguments. */
1565 soself
= (PyStructObject
*)self
;
1566 assert(PyStruct_Check(self
));
1567 assert(soself
->s_codes
!= NULL
);
1568 if (PyTuple_GET_SIZE(args
) != soself
->s_len
)
1570 PyErr_Format(StructError
,
1571 "pack requires exactly %zd arguments", soself
->s_len
);
1575 /* Allocate a new string */
1576 result
= PyBytes_FromStringAndSize((char *)NULL
, soself
->s_size
);
1581 if ( s_pack_internal(soself
, args
, 0, PyBytes_AS_STRING(result
)) != 0 ) {
1589 PyDoc_STRVAR(s_pack_into__doc__
,
1590 "S.pack_into(buffer, offset, v1, v2, ...)\n\
1592 Pack the values v1, v2, ... according to the format string S.format\n\
1593 and write the packed bytes into the writable buffer buf starting at\n\
1594 offset. Note that the offset is a required argument. See\n\
1595 help(struct) for more on format strings.");
1598 s_pack_into(PyObject
*self
, PyObject
*args
)
1600 PyStructObject
*soself
;
1602 Py_ssize_t buffer_len
, offset
;
1604 /* Validate arguments. +1 is for the first arg as buffer. */
1605 soself
= (PyStructObject
*)self
;
1606 assert(PyStruct_Check(self
));
1607 assert(soself
->s_codes
!= NULL
);
1608 if (PyTuple_GET_SIZE(args
) != (soself
->s_len
+ 2))
1610 PyErr_Format(StructError
,
1611 "pack_into requires exactly %zd arguments",
1612 (soself
->s_len
+ 2));
1616 /* Extract a writable memory buffer from the first argument */
1617 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args
, 0),
1618 (void**)&buffer
, &buffer_len
) == -1 ) {
1621 assert( buffer_len
>= 0 );
1623 /* Extract the offset from the first argument */
1624 offset
= PyNumber_AsSsize_t(PyTuple_GET_ITEM(args
, 1), PyExc_IndexError
);
1625 if (offset
== -1 && PyErr_Occurred())
1628 /* Support negative offsets. */
1630 offset
+= buffer_len
;
1632 /* Check boundaries */
1633 if (offset
< 0 || (buffer_len
- offset
) < soself
->s_size
) {
1634 PyErr_Format(StructError
,
1635 "pack_into requires a buffer of at least %zd bytes",
1641 if ( s_pack_internal(soself
, args
, 2, buffer
+ offset
) != 0 ) {
1649 s_get_format(PyStructObject
*self
, void *unused
)
1651 Py_INCREF(self
->s_format
);
1652 return self
->s_format
;
1656 s_get_size(PyStructObject
*self
, void *unused
)
1658 return PyLong_FromSsize_t(self
->s_size
);
1661 /* List of functions */
1663 static struct PyMethodDef s_methods
[] = {
1664 {"pack", s_pack
, METH_VARARGS
, s_pack__doc__
},
1665 {"pack_into", s_pack_into
, METH_VARARGS
, s_pack_into__doc__
},
1666 {"unpack", s_unpack
, METH_O
, s_unpack__doc__
},
1667 {"unpack_from", (PyCFunction
)s_unpack_from
, METH_VARARGS
|METH_KEYWORDS
,
1668 s_unpack_from__doc__
},
1669 {NULL
, NULL
} /* sentinel */
1672 PyDoc_STRVAR(s__doc__
,
1673 "Struct(fmt) --> compiled struct object\n"
1675 "Return a new Struct object which writes and reads binary data according to\n"
1676 "the format string fmt. See help(struct) for more on format strings.");
1678 #define OFF(x) offsetof(PyStructObject, x)
1680 static PyGetSetDef s_getsetlist
[] = {
1681 {"format", (getter
)s_get_format
, (setter
)NULL
, "struct format string", NULL
},
1682 {"size", (getter
)s_get_size
, (setter
)NULL
, "struct size in bytes", NULL
},
1683 {NULL
} /* sentinel */
1687 PyTypeObject PyStructType
= {
1688 PyVarObject_HEAD_INIT(NULL
, 0)
1690 sizeof(PyStructObject
),
1692 (destructor
)s_dealloc
, /* tp_dealloc */
1696 0, /* tp_reserved */
1698 0, /* tp_as_number */
1699 0, /* tp_as_sequence */
1700 0, /* tp_as_mapping */
1704 PyObject_GenericGetAttr
, /* tp_getattro */
1705 PyObject_GenericSetAttr
, /* tp_setattro */
1706 0, /* tp_as_buffer */
1707 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
, /* tp_flags */
1708 s__doc__
, /* tp_doc */
1709 0, /* tp_traverse */
1711 0, /* tp_richcompare */
1712 offsetof(PyStructObject
, weakreflist
), /* tp_weaklistoffset */
1714 0, /* tp_iternext */
1715 s_methods
, /* tp_methods */
1716 NULL
, /* tp_members */
1717 s_getsetlist
, /* tp_getset */
1720 0, /* tp_descr_get */
1721 0, /* tp_descr_set */
1722 0, /* tp_dictoffset */
1723 s_init
, /* tp_init */
1724 PyType_GenericAlloc
,/* tp_alloc */
1726 PyObject_Del
, /* tp_free */
1730 /* ---- Standalone functions ---- */
1732 #define MAXCACHE 100
1733 static PyObject
*cache
= NULL
;
1736 cache_struct(PyObject
*fmt
)
1738 PyObject
* s_object
;
1740 if (cache
== NULL
) {
1741 cache
= PyDict_New();
1746 s_object
= PyDict_GetItem(cache
, fmt
);
1747 if (s_object
!= NULL
) {
1748 Py_INCREF(s_object
);
1752 s_object
= PyObject_CallFunctionObjArgs((PyObject
*)(&PyStructType
), fmt
, NULL
);
1753 if (s_object
!= NULL
) {
1754 if (PyDict_Size(cache
) >= MAXCACHE
)
1755 PyDict_Clear(cache
);
1756 /* Attempt to cache the result */
1757 if (PyDict_SetItem(cache
, fmt
, s_object
) == -1)
1763 PyDoc_STRVAR(clearcache_doc
,
1764 "Clear the internal cache.");
1767 clearcache(PyObject
*self
)
1773 PyDoc_STRVAR(calcsize_doc
,
1774 "calcsize(fmt) -> integer\n\
1776 Return size in bytes of the struct described by the format string fmt.");
1779 calcsize(PyObject
*self
, PyObject
*fmt
)
1782 PyObject
*s_object
= cache_struct(fmt
);
1783 if (s_object
== NULL
)
1785 n
= ((PyStructObject
*)s_object
)->s_size
;
1786 Py_DECREF(s_object
);
1787 return PyLong_FromSsize_t(n
);
1790 PyDoc_STRVAR(pack_doc
,
1791 "pack(fmt, v1, v2, ...) -> bytes\n\
1793 Return a bytes object containing the values v1, v2, ... packed according\n\
1794 to the format string fmt. See help(struct) for more on format strings.");
1797 pack(PyObject
*self
, PyObject
*args
)
1799 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1800 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1803 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1806 fmt
= PyTuple_GET_ITEM(args
, 0);
1807 newargs
= PyTuple_GetSlice(args
, 1, n
);
1808 if (newargs
== NULL
)
1811 s_object
= cache_struct(fmt
);
1812 if (s_object
== NULL
) {
1816 result
= s_pack(s_object
, newargs
);
1818 Py_DECREF(s_object
);
1822 PyDoc_STRVAR(pack_into_doc
,
1823 "pack_into(fmt, buffer, offset, v1, v2, ...)\n\
1825 Pack the values v1, v2, ... according to the format string fmt and write\n\
1826 the packed bytes into the writable buffer buf starting at offset. Note\n\
1827 that the offset is a required argument. See help(struct) for more\n\
1828 on format strings.");
1831 pack_into(PyObject
*self
, PyObject
*args
)
1833 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1834 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1837 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1840 fmt
= PyTuple_GET_ITEM(args
, 0);
1841 newargs
= PyTuple_GetSlice(args
, 1, n
);
1842 if (newargs
== NULL
)
1845 s_object
= cache_struct(fmt
);
1846 if (s_object
== NULL
) {
1850 result
= s_pack_into(s_object
, newargs
);
1852 Py_DECREF(s_object
);
1856 PyDoc_STRVAR(unpack_doc
,
1857 "unpack(fmt, buffer) -> (v1, v2, ...)\n\
1859 Return a tuple containing values unpacked according to the format string\n\
1860 fmt. Requires len(buffer) == calcsize(fmt). See help(struct) for more\n\
1861 on format strings.");
1864 unpack(PyObject
*self
, PyObject
*args
)
1866 PyObject
*s_object
, *fmt
, *inputstr
, *result
;
1868 if (!PyArg_UnpackTuple(args
, "unpack", 2, 2, &fmt
, &inputstr
))
1871 s_object
= cache_struct(fmt
);
1872 if (s_object
== NULL
)
1874 result
= s_unpack(s_object
, inputstr
);
1875 Py_DECREF(s_object
);
1879 PyDoc_STRVAR(unpack_from_doc
,
1880 "unpack_from(fmt, buffer, offset=0) -> (v1, v2, ...)\n\
1882 Return a tuple containing values unpacked according to the format string\n\
1883 fmt. Requires len(buffer[offset:]) >= calcsize(fmt). See help(struct)\n\
1884 for more on format strings.");
1887 unpack_from(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1889 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1890 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1893 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1896 fmt
= PyTuple_GET_ITEM(args
, 0);
1897 newargs
= PyTuple_GetSlice(args
, 1, n
);
1898 if (newargs
== NULL
)
1901 s_object
= cache_struct(fmt
);
1902 if (s_object
== NULL
) {
1906 result
= s_unpack_from(s_object
, newargs
, kwds
);
1908 Py_DECREF(s_object
);
1912 static struct PyMethodDef module_functions
[] = {
1913 {"_clearcache", (PyCFunction
)clearcache
, METH_NOARGS
, clearcache_doc
},
1914 {"calcsize", calcsize
, METH_O
, calcsize_doc
},
1915 {"pack", pack
, METH_VARARGS
, pack_doc
},
1916 {"pack_into", pack_into
, METH_VARARGS
, pack_into_doc
},
1917 {"unpack", unpack
, METH_VARARGS
, unpack_doc
},
1918 {"unpack_from", (PyCFunction
)unpack_from
,
1919 METH_VARARGS
|METH_KEYWORDS
, unpack_from_doc
},
1920 {NULL
, NULL
} /* sentinel */
1924 /* Module initialization */
1926 PyDoc_STRVAR(module_doc
,
1927 "Functions to convert between Python values and C structs.\n\
1928 Python bytes objects are used to hold the data representing the C struct\n\
1929 and also as format strings (explained below) to describe the layout of data\n\
1932 The optional first format char indicates byte order, size and alignment:\n\
1933 @: native order, size & alignment (default)\n\
1934 =: native order, std. size & alignment\n\
1935 <: little-endian, std. size & alignment\n\
1936 >: big-endian, std. size & alignment\n\
1939 The remaining chars indicate types of args and must match exactly;\n\
1940 these can be preceded by a decimal repeat count:\n\
1941 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
1942 ?: _Bool (requires C99; if not available, char is used instead)\n\
1943 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1944 l:long; L:unsigned long; f:float; d:double.\n\
1945 Special cases (preceding decimal count indicates length):\n\
1946 s:string (array of char); p: pascal string (with count byte).\n\
1947 Special case (only available in native format):\n\
1948 P:an integer type that is wide enough to hold a pointer.\n\
1949 Special case (not in native mode unless 'long long' in platform C):\n\
1950 q:long long; Q:unsigned long long\n\
1951 Whitespace between formats is ignored.\n\
1953 The variable struct.error is an exception raised on errors.\n");
1956 static struct PyModuleDef _structmodule
= {
1957 PyModuleDef_HEAD_INIT
,
1969 PyInit__struct(void)
1973 ver
= PyBytes_FromString("0.3");
1977 m
= PyModule_Create(&_structmodule
);
1981 Py_TYPE(&PyStructType
) = &PyType_Type
;
1982 if (PyType_Ready(&PyStructType
) < 0)
1985 /* Check endian and swap in faster functions */
1988 formatdef
*native
= native_table
;
1989 formatdef
*other
, *ptr
;
1990 if ((int)*(unsigned char*)&one
)
1991 other
= lilendian_table
;
1993 other
= bigendian_table
;
1994 /* Scan through the native table, find a matching
1995 entry in the endian table and swap in the
1996 native implementations whenever possible
1997 (64-bit platforms may not have "standard" sizes) */
1998 while (native
->format
!= '\0' && other
->format
!= '\0') {
2000 while (ptr
->format
!= '\0') {
2001 if (ptr
->format
== native
->format
) {
2002 /* Match faster when formats are
2003 listed in the same order */
2006 /* Only use the trick if the
2008 if (ptr
->size
!= native
->size
)
2010 /* Skip float and double, could be
2011 "unknown" float format */
2012 if (ptr
->format
== 'd' || ptr
->format
== 'f')
2014 ptr
->pack
= native
->pack
;
2015 ptr
->unpack
= native
->unpack
;
2024 /* Add some symbolic constants to the module */
2025 if (StructError
== NULL
) {
2026 StructError
= PyErr_NewException("struct.error", NULL
, NULL
);
2027 if (StructError
== NULL
)
2031 Py_INCREF(StructError
);
2032 PyModule_AddObject(m
, "error", StructError
);
2034 Py_INCREF((PyObject
*)&PyStructType
);
2035 PyModule_AddObject(m
, "Struct", (PyObject
*)&PyStructType
);
2037 PyModule_AddObject(m
, "__version__", ver
);