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 /* warning messages */
21 #define FLOAT_COERCE_WARN "integer argument expected, got float"
22 #define NON_INTEGER_WARN "integer argument expected, got non-integer " \
23 "(implicit conversion using __int__ is deprecated)"
26 /* The translation function for each format character is table driven */
27 typedef struct _formatdef
{
31 PyObject
* (*unpack
)(const char *,
32 const struct _formatdef
*);
33 int (*pack
)(char *, PyObject
*,
34 const struct _formatdef
*);
37 typedef struct _formatcode
{
38 const struct _formatdef
*fmtdef
;
43 /* Struct object interface */
51 PyObject
*weakreflist
; /* List of weak references */
55 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
56 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
61 static PyObject
*StructError
;
64 /* Define various structs to figure out the alignments of types */
67 typedef struct { char c
; short x
; } st_short
;
68 typedef struct { char c
; int x
; } st_int
;
69 typedef struct { char c
; long x
; } st_long
;
70 typedef struct { char c
; float x
; } st_float
;
71 typedef struct { char c
; double x
; } st_double
;
72 typedef struct { char c
; void *x
; } st_void_p
;
74 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
75 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
76 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
77 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
78 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
79 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
81 /* We can't support q and Q in native mode unless the compiler does;
82 in std mode, they're 8 bytes on all platforms. */
84 typedef struct { char c
; PY_LONG_LONG x
; } s_long_long
;
85 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
89 #define BOOL_TYPE _Bool
90 typedef struct { char c
; _Bool x
; } s_bool
;
91 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
93 #define BOOL_TYPE char
97 #define STRINGIFY(x) #x
100 #pragma options align=reset
103 static char *integer_codes
= "bBhHiIlLqQ";
105 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
108 get_pylong(PyObject
*v
)
113 if (!PyInt_Check(v
) && !PyLong_Check(v
)) {
115 /* Not an integer; first try to use __index__ to
116 convert to an integer. If the __index__ method
117 doesn't exist, or raises a TypeError, try __int__.
118 Use of the latter is deprecated, and will fail in
121 m
= Py_TYPE(v
)->tp_as_number
;
122 if (PyIndex_Check(v
)) {
123 w
= PyNumber_Index(v
);
126 /* successfully converted to an integer */
129 else if (PyErr_ExceptionMatches(PyExc_TypeError
)) {
135 if (!converted
&& m
!= NULL
&& m
->nb_int
!= NULL
) {
136 /* Special case warning message for floats, for
137 backwards compatibility. */
138 if (PyFloat_Check(v
)) {
140 PyExc_DeprecationWarning
,
141 FLOAT_COERCE_WARN
, 1))
146 PyExc_DeprecationWarning
,
147 NON_INTEGER_WARN
, 1))
153 if (!PyInt_Check(v
) && !PyLong_Check(v
)) {
154 PyErr_SetString(PyExc_TypeError
,
155 "__int__ method returned "
162 PyErr_SetString(StructError
,
163 "cannot convert argument "
169 /* Ensure we own a reference to v. */
172 assert(PyInt_Check(v
) || PyLong_Check(v
));
173 if (PyInt_Check(v
)) {
174 r
= PyLong_FromLong(PyInt_AS_LONG(v
));
177 else if (PyLong_Check(v
)) {
178 assert(PyLong_Check(v
));
182 r
= NULL
; /* silence compiler warning about
183 possibly uninitialized variable */
184 assert(0); /* shouldn't ever get here */
190 /* Helper to convert a Python object to a C long. Sets an exception
191 (struct.error for an inconvertible type, OverflowError for
192 out-of-range values) and returns -1 on error. */
195 get_long(PyObject
*v
, long *p
)
202 assert(PyLong_Check(v
));
203 x
= PyLong_AsLong(v
);
205 if (x
== (long)-1 && PyErr_Occurred())
211 /* Same, but handling unsigned long */
214 get_ulong(PyObject
*v
, unsigned long *p
)
221 assert(PyLong_Check(v
));
222 x
= PyLong_AsUnsignedLong(v
);
224 if (x
== (unsigned long)-1 && PyErr_Occurred())
230 #ifdef HAVE_LONG_LONG
232 /* Same, but handling native long long. */
235 get_longlong(PyObject
*v
, PY_LONG_LONG
*p
)
242 assert(PyLong_Check(v
));
243 x
= PyLong_AsLongLong(v
);
245 if (x
== (PY_LONG_LONG
)-1 && PyErr_Occurred())
251 /* Same, but handling native unsigned long long. */
254 get_ulonglong(PyObject
*v
, unsigned PY_LONG_LONG
*p
)
256 unsigned PY_LONG_LONG x
;
261 assert(PyLong_Check(v
));
262 x
= PyLong_AsUnsignedLongLong(v
);
264 if (x
== (unsigned PY_LONG_LONG
)-1 && PyErr_Occurred())
272 /* Floating point helpers */
275 unpack_float(const char *p
, /* start of 4-byte string */
276 int le
) /* true for little-endian, false for big-endian */
280 x
= _PyFloat_Unpack4((unsigned char *)p
, le
);
281 if (x
== -1.0 && PyErr_Occurred())
283 return PyFloat_FromDouble(x
);
287 unpack_double(const char *p
, /* start of 8-byte string */
288 int le
) /* true for little-endian, false for big-endian */
292 x
= _PyFloat_Unpack8((unsigned char *)p
, le
);
293 if (x
== -1.0 && PyErr_Occurred())
295 return PyFloat_FromDouble(x
);
298 /* Helper to format the range error exceptions */
300 _range_error(const formatdef
*f
, int is_unsigned
)
302 /* ulargest is the largest unsigned value with f->size bytes.
303 * Note that the simpler:
304 * ((size_t)1 << (f->size * 8)) - 1
305 * doesn't work when f->size == sizeof(size_t) because C doesn't
306 * define what happens when a left shift count is >= the number of
307 * bits in the integer being shifted; e.g., on some boxes it doesn't
308 * shift at all when they're equal.
310 const size_t ulargest
= (size_t)-1 >> ((SIZEOF_SIZE_T
- f
->size
)*8);
311 assert(f
->size
>= 1 && f
->size
<= SIZEOF_SIZE_T
);
313 PyErr_Format(StructError
,
314 "'%c' format requires 0 <= number <= %zu",
318 const Py_ssize_t largest
= (Py_ssize_t
)(ulargest
>> 1);
319 PyErr_Format(StructError
,
320 "'%c' format requires %zd <= number <= %zd",
330 /* A large number of small routines follow, with names of the form
334 [bln] distiguishes among big-endian, little-endian and native.
335 [pu] distiguishes between pack (to struct) and unpack (from struct).
336 TYPE is one of char, byte, ubyte, etc.
339 /* Native mode routines. ****************************************************/
341 In all n[up]_<type> routines handling types larger than 1 byte, there is
342 *no* guarantee that the p pointer is properly aligned for each type,
343 therefore memcpy is called. An intermediate variable is used to
344 compensate for big-endian architectures.
345 Normally both the intermediate variable and the memcpy call will be
346 skipped by C optimisation in little-endian architectures (gcc >= 2.91
350 nu_char(const char *p
, const formatdef
*f
)
352 return PyString_FromStringAndSize(p
, 1);
356 nu_byte(const char *p
, const formatdef
*f
)
358 return PyInt_FromLong((long) *(signed char *)p
);
362 nu_ubyte(const char *p
, const formatdef
*f
)
364 return PyInt_FromLong((long) *(unsigned char *)p
);
368 nu_short(const char *p
, const formatdef
*f
)
371 memcpy((char *)&x
, p
, sizeof x
);
372 return PyInt_FromLong((long)x
);
376 nu_ushort(const char *p
, const formatdef
*f
)
379 memcpy((char *)&x
, p
, sizeof x
);
380 return PyInt_FromLong((long)x
);
384 nu_int(const char *p
, const formatdef
*f
)
387 memcpy((char *)&x
, p
, sizeof x
);
388 return PyInt_FromLong((long)x
);
392 nu_uint(const char *p
, const formatdef
*f
)
395 memcpy((char *)&x
, p
, sizeof x
);
396 #if (SIZEOF_LONG > SIZEOF_INT)
397 return PyInt_FromLong((long)x
);
399 if (x
<= ((unsigned int)LONG_MAX
))
400 return PyInt_FromLong((long)x
);
401 return PyLong_FromUnsignedLong((unsigned long)x
);
406 nu_long(const char *p
, const formatdef
*f
)
409 memcpy((char *)&x
, p
, sizeof x
);
410 return PyInt_FromLong(x
);
414 nu_ulong(const char *p
, const formatdef
*f
)
417 memcpy((char *)&x
, p
, sizeof x
);
419 return PyInt_FromLong((long)x
);
420 return PyLong_FromUnsignedLong(x
);
423 /* Native mode doesn't support q or Q unless the platform C supports
424 long long (or, on Windows, __int64). */
426 #ifdef HAVE_LONG_LONG
429 nu_longlong(const char *p
, const formatdef
*f
)
432 memcpy((char *)&x
, p
, sizeof x
);
433 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
434 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
435 return PyLong_FromLongLong(x
);
439 nu_ulonglong(const char *p
, const formatdef
*f
)
441 unsigned PY_LONG_LONG x
;
442 memcpy((char *)&x
, p
, sizeof x
);
444 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
445 return PyLong_FromUnsignedLongLong(x
);
451 nu_bool(const char *p
, const formatdef
*f
)
454 memcpy((char *)&x
, p
, sizeof x
);
455 return PyBool_FromLong(x
!= 0);
460 nu_float(const char *p
, const formatdef
*f
)
463 memcpy((char *)&x
, p
, sizeof x
);
464 return PyFloat_FromDouble((double)x
);
468 nu_double(const char *p
, const formatdef
*f
)
471 memcpy((char *)&x
, p
, sizeof x
);
472 return PyFloat_FromDouble(x
);
476 nu_void_p(const char *p
, const formatdef
*f
)
479 memcpy((char *)&x
, p
, sizeof x
);
480 return PyLong_FromVoidPtr(x
);
484 np_byte(char *p
, PyObject
*v
, const formatdef
*f
)
487 if (get_long(v
, &x
) < 0)
489 if (x
< -128 || x
> 127){
490 PyErr_SetString(StructError
,
491 "byte format requires -128 <= number <= 127");
499 np_ubyte(char *p
, PyObject
*v
, const formatdef
*f
)
502 if (get_long(v
, &x
) < 0)
504 if (x
< 0 || x
> 255){
505 PyErr_SetString(StructError
,
506 "ubyte format requires 0 <= number <= 255");
514 np_char(char *p
, PyObject
*v
, const formatdef
*f
)
516 if (!PyString_Check(v
) || PyString_Size(v
) != 1) {
517 PyErr_SetString(StructError
,
518 "char format require string of length 1");
521 *p
= *PyString_AsString(v
);
526 np_short(char *p
, PyObject
*v
, const formatdef
*f
)
530 if (get_long(v
, &x
) < 0)
532 if (x
< SHRT_MIN
|| x
> SHRT_MAX
){
533 PyErr_SetString(StructError
,
534 "short format requires " STRINGIFY(SHRT_MIN
)
535 " <= number <= " STRINGIFY(SHRT_MAX
));
539 memcpy(p
, (char *)&y
, sizeof y
);
544 np_ushort(char *p
, PyObject
*v
, const formatdef
*f
)
548 if (get_long(v
, &x
) < 0)
550 if (x
< 0 || x
> USHRT_MAX
){
551 PyErr_SetString(StructError
,
552 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX
));
555 y
= (unsigned short)x
;
556 memcpy(p
, (char *)&y
, sizeof y
);
561 np_int(char *p
, PyObject
*v
, const formatdef
*f
)
565 if (get_long(v
, &x
) < 0)
567 #if (SIZEOF_LONG > SIZEOF_INT)
568 if ((x
< ((long)INT_MIN
)) || (x
> ((long)INT_MAX
)))
569 return _range_error(f
, 0);
572 memcpy(p
, (char *)&y
, sizeof y
);
577 np_uint(char *p
, PyObject
*v
, const formatdef
*f
)
581 if (get_ulong(v
, &x
) < 0)
584 #if (SIZEOF_LONG > SIZEOF_INT)
585 if (x
> ((unsigned long)UINT_MAX
))
586 return _range_error(f
, 1);
588 memcpy(p
, (char *)&y
, sizeof y
);
593 np_long(char *p
, PyObject
*v
, const formatdef
*f
)
596 if (get_long(v
, &x
) < 0)
598 memcpy(p
, (char *)&x
, sizeof x
);
603 np_ulong(char *p
, PyObject
*v
, const formatdef
*f
)
606 if (get_ulong(v
, &x
) < 0)
608 memcpy(p
, (char *)&x
, sizeof x
);
612 #ifdef HAVE_LONG_LONG
615 np_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
618 if (get_longlong(v
, &x
) < 0)
620 memcpy(p
, (char *)&x
, sizeof x
);
625 np_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
627 unsigned PY_LONG_LONG x
;
628 if (get_ulonglong(v
, &x
) < 0)
630 memcpy(p
, (char *)&x
, sizeof x
);
637 np_bool(char *p
, PyObject
*v
, const formatdef
*f
)
641 y
= PyObject_IsTrue(v
);
645 memcpy(p
, (char *)&x
, sizeof x
);
650 np_float(char *p
, PyObject
*v
, const formatdef
*f
)
652 float x
= (float)PyFloat_AsDouble(v
);
653 if (x
== -1 && PyErr_Occurred()) {
654 PyErr_SetString(StructError
,
655 "required argument is not a float");
658 memcpy(p
, (char *)&x
, sizeof x
);
663 np_double(char *p
, PyObject
*v
, const formatdef
*f
)
665 double x
= PyFloat_AsDouble(v
);
666 if (x
== -1 && PyErr_Occurred()) {
667 PyErr_SetString(StructError
,
668 "required argument is not a float");
671 memcpy(p
, (char *)&x
, sizeof(double));
676 np_void_p(char *p
, PyObject
*v
, const formatdef
*f
)
683 assert(PyLong_Check(v
));
684 x
= PyLong_AsVoidPtr(v
);
686 if (x
== NULL
&& PyErr_Occurred())
688 memcpy(p
, (char *)&x
, sizeof x
);
692 static formatdef native_table
[] = {
693 {'x', sizeof(char), 0, NULL
},
694 {'b', sizeof(char), 0, nu_byte
, np_byte
},
695 {'B', sizeof(char), 0, nu_ubyte
, np_ubyte
},
696 {'c', sizeof(char), 0, nu_char
, np_char
},
697 {'s', sizeof(char), 0, NULL
},
698 {'p', sizeof(char), 0, NULL
},
699 {'h', sizeof(short), SHORT_ALIGN
, nu_short
, np_short
},
700 {'H', sizeof(short), SHORT_ALIGN
, nu_ushort
, np_ushort
},
701 {'i', sizeof(int), INT_ALIGN
, nu_int
, np_int
},
702 {'I', sizeof(int), INT_ALIGN
, nu_uint
, np_uint
},
703 {'l', sizeof(long), LONG_ALIGN
, nu_long
, np_long
},
704 {'L', sizeof(long), LONG_ALIGN
, nu_ulong
, np_ulong
},
705 #ifdef HAVE_LONG_LONG
706 {'q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_longlong
, np_longlong
},
707 {'Q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_ulonglong
,np_ulonglong
},
709 {'?', sizeof(BOOL_TYPE
), BOOL_ALIGN
, nu_bool
, np_bool
},
710 {'f', sizeof(float), FLOAT_ALIGN
, nu_float
, np_float
},
711 {'d', sizeof(double), DOUBLE_ALIGN
, nu_double
, np_double
},
712 {'P', sizeof(void *), VOID_P_ALIGN
, nu_void_p
, np_void_p
},
716 /* Big-endian routines. *****************************************************/
719 bu_int(const char *p
, const formatdef
*f
)
722 Py_ssize_t i
= f
->size
;
723 const unsigned char *bytes
= (const unsigned char *)p
;
725 x
= (x
<<8) | *bytes
++;
727 /* Extend the sign bit. */
728 if (SIZEOF_LONG
> f
->size
)
729 x
|= -(x
& (1L << ((8 * f
->size
) - 1)));
730 return PyInt_FromLong(x
);
734 bu_uint(const char *p
, const formatdef
*f
)
737 Py_ssize_t i
= f
->size
;
738 const unsigned char *bytes
= (const unsigned char *)p
;
740 x
= (x
<<8) | *bytes
++;
743 return PyInt_FromLong((long)x
);
744 return PyLong_FromUnsignedLong(x
);
748 bu_longlong(const char *p
, const formatdef
*f
)
750 #ifdef HAVE_LONG_LONG
752 Py_ssize_t i
= f
->size
;
753 const unsigned char *bytes
= (const unsigned char *)p
;
755 x
= (x
<<8) | *bytes
++;
757 /* Extend the sign bit. */
758 if (SIZEOF_LONG_LONG
> f
->size
)
759 x
|= -(x
& ((PY_LONG_LONG
)1 << ((8 * f
->size
) - 1)));
760 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
761 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
762 return PyLong_FromLongLong(x
);
764 return _PyLong_FromByteArray((const unsigned char *)p
,
766 0, /* little-endian */
772 bu_ulonglong(const char *p
, const formatdef
*f
)
774 #ifdef HAVE_LONG_LONG
775 unsigned PY_LONG_LONG x
= 0;
776 Py_ssize_t i
= f
->size
;
777 const unsigned char *bytes
= (const unsigned char *)p
;
779 x
= (x
<<8) | *bytes
++;
782 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
783 return PyLong_FromUnsignedLongLong(x
);
785 return _PyLong_FromByteArray((const unsigned char *)p
,
787 0, /* little-endian */
793 bu_float(const char *p
, const formatdef
*f
)
795 return unpack_float(p
, 0);
799 bu_double(const char *p
, const formatdef
*f
)
801 return unpack_double(p
, 0);
805 bu_bool(const char *p
, const formatdef
*f
)
808 memcpy((char *)&x
, p
, sizeof x
);
809 return PyBool_FromLong(x
!= 0);
813 bp_int(char *p
, PyObject
*v
, const formatdef
*f
)
817 if (get_long(v
, &x
) < 0)
820 if (i
!= SIZEOF_LONG
) {
821 if ((i
== 2) && (x
< -32768 || x
> 32767))
822 return _range_error(f
, 0);
823 #if (SIZEOF_LONG != 4)
824 else if ((i
== 4) && (x
< -2147483648L || x
> 2147483647L))
825 return _range_error(f
, 0);
836 bp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
840 if (get_ulong(v
, &x
) < 0)
843 if (i
!= SIZEOF_LONG
) {
844 unsigned long maxint
= 1;
845 maxint
<<= (unsigned long)(i
* 8);
847 return _range_error(f
, 1);
857 bp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
863 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
866 0, /* little_endian */
873 bp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
879 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
882 0, /* little_endian */
889 bp_float(char *p
, PyObject
*v
, const formatdef
*f
)
891 double x
= PyFloat_AsDouble(v
);
892 if (x
== -1 && PyErr_Occurred()) {
893 PyErr_SetString(StructError
,
894 "required argument is not a float");
897 return _PyFloat_Pack4(x
, (unsigned char *)p
, 0);
901 bp_double(char *p
, PyObject
*v
, const formatdef
*f
)
903 double x
= PyFloat_AsDouble(v
);
904 if (x
== -1 && PyErr_Occurred()) {
905 PyErr_SetString(StructError
,
906 "required argument is not a float");
909 return _PyFloat_Pack8(x
, (unsigned char *)p
, 0);
913 bp_bool(char *p
, PyObject
*v
, const formatdef
*f
)
916 y
= PyObject_IsTrue(v
);
923 static formatdef bigendian_table
[] = {
925 {'b', 1, 0, nu_byte
, np_byte
},
926 {'B', 1, 0, nu_ubyte
, np_ubyte
},
927 {'c', 1, 0, nu_char
, np_char
},
930 {'h', 2, 0, bu_int
, bp_int
},
931 {'H', 2, 0, bu_uint
, bp_uint
},
932 {'i', 4, 0, bu_int
, bp_int
},
933 {'I', 4, 0, bu_uint
, bp_uint
},
934 {'l', 4, 0, bu_int
, bp_int
},
935 {'L', 4, 0, bu_uint
, bp_uint
},
936 {'q', 8, 0, bu_longlong
, bp_longlong
},
937 {'Q', 8, 0, bu_ulonglong
, bp_ulonglong
},
938 {'?', 1, 0, bu_bool
, bp_bool
},
939 {'f', 4, 0, bu_float
, bp_float
},
940 {'d', 8, 0, bu_double
, bp_double
},
944 /* Little-endian routines. *****************************************************/
947 lu_int(const char *p
, const formatdef
*f
)
950 Py_ssize_t i
= f
->size
;
951 const unsigned char *bytes
= (const unsigned char *)p
;
953 x
= (x
<<8) | bytes
[--i
];
955 /* Extend the sign bit. */
956 if (SIZEOF_LONG
> f
->size
)
957 x
|= -(x
& (1L << ((8 * f
->size
) - 1)));
958 return PyInt_FromLong(x
);
962 lu_uint(const char *p
, const formatdef
*f
)
965 Py_ssize_t i
= f
->size
;
966 const unsigned char *bytes
= (const unsigned char *)p
;
968 x
= (x
<<8) | bytes
[--i
];
971 return PyInt_FromLong((long)x
);
972 return PyLong_FromUnsignedLong((long)x
);
976 lu_longlong(const char *p
, const formatdef
*f
)
978 #ifdef HAVE_LONG_LONG
980 Py_ssize_t i
= f
->size
;
981 const unsigned char *bytes
= (const unsigned char *)p
;
983 x
= (x
<<8) | bytes
[--i
];
985 /* Extend the sign bit. */
986 if (SIZEOF_LONG_LONG
> f
->size
)
987 x
|= -(x
& ((PY_LONG_LONG
)1 << ((8 * f
->size
) - 1)));
988 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
989 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
990 return PyLong_FromLongLong(x
);
992 return _PyLong_FromByteArray((const unsigned char *)p
,
994 1, /* little-endian */
1000 lu_ulonglong(const char *p
, const formatdef
*f
)
1002 #ifdef HAVE_LONG_LONG
1003 unsigned PY_LONG_LONG x
= 0;
1004 Py_ssize_t i
= f
->size
;
1005 const unsigned char *bytes
= (const unsigned char *)p
;
1007 x
= (x
<<8) | bytes
[--i
];
1010 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
1011 return PyLong_FromUnsignedLongLong(x
);
1013 return _PyLong_FromByteArray((const unsigned char *)p
,
1015 1, /* little-endian */
1021 lu_float(const char *p
, const formatdef
*f
)
1023 return unpack_float(p
, 1);
1027 lu_double(const char *p
, const formatdef
*f
)
1029 return unpack_double(p
, 1);
1033 lp_int(char *p
, PyObject
*v
, const formatdef
*f
)
1037 if (get_long(v
, &x
) < 0)
1040 if (i
!= SIZEOF_LONG
) {
1041 if ((i
== 2) && (x
< -32768 || x
> 32767))
1042 return _range_error(f
, 0);
1043 #if (SIZEOF_LONG != 4)
1044 else if ((i
== 4) && (x
< -2147483648L || x
> 2147483647L))
1045 return _range_error(f
, 0);
1056 lp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
1060 if (get_ulong(v
, &x
) < 0)
1063 if (i
!= SIZEOF_LONG
) {
1064 unsigned long maxint
= 1;
1065 maxint
<<= (unsigned long)(i
* 8);
1067 return _range_error(f
, 1);
1077 lp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
1083 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
1086 1, /* little_endian */
1093 lp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
1099 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
1102 1, /* little_endian */
1109 lp_float(char *p
, PyObject
*v
, const formatdef
*f
)
1111 double x
= PyFloat_AsDouble(v
);
1112 if (x
== -1 && PyErr_Occurred()) {
1113 PyErr_SetString(StructError
,
1114 "required argument is not a float");
1117 return _PyFloat_Pack4(x
, (unsigned char *)p
, 1);
1121 lp_double(char *p
, PyObject
*v
, const formatdef
*f
)
1123 double x
= PyFloat_AsDouble(v
);
1124 if (x
== -1 && PyErr_Occurred()) {
1125 PyErr_SetString(StructError
,
1126 "required argument is not a float");
1129 return _PyFloat_Pack8(x
, (unsigned char *)p
, 1);
1132 static formatdef lilendian_table
[] = {
1134 {'b', 1, 0, nu_byte
, np_byte
},
1135 {'B', 1, 0, nu_ubyte
, np_ubyte
},
1136 {'c', 1, 0, nu_char
, np_char
},
1139 {'h', 2, 0, lu_int
, lp_int
},
1140 {'H', 2, 0, lu_uint
, lp_uint
},
1141 {'i', 4, 0, lu_int
, lp_int
},
1142 {'I', 4, 0, lu_uint
, lp_uint
},
1143 {'l', 4, 0, lu_int
, lp_int
},
1144 {'L', 4, 0, lu_uint
, lp_uint
},
1145 {'q', 8, 0, lu_longlong
, lp_longlong
},
1146 {'Q', 8, 0, lu_ulonglong
, lp_ulonglong
},
1147 {'?', 1, 0, bu_bool
, bp_bool
}, /* Std rep not endian dep,
1148 but potentially different from native rep -- reuse bx_bool funcs. */
1149 {'f', 4, 0, lu_float
, lp_float
},
1150 {'d', 8, 0, lu_double
, lp_double
},
1155 static const formatdef
*
1156 whichtable(char **pfmt
)
1158 const char *fmt
= (*pfmt
)++; /* May be backed out of later */
1161 return lilendian_table
;
1163 case '!': /* Network byte order is big-endian */
1164 return bigendian_table
;
1165 case '=': { /* Host byte order -- different from native in alignment! */
1167 char *p
= (char *) &n
;
1169 return lilendian_table
;
1171 return bigendian_table
;
1174 --*pfmt
; /* Back out of pointer increment */
1177 return native_table
;
1182 /* Get the table entry for a format code */
1184 static const formatdef
*
1185 getentry(int c
, const formatdef
*f
)
1187 for (; f
->format
!= '\0'; f
++) {
1188 if (f
->format
== c
) {
1192 PyErr_SetString(StructError
, "bad char in struct format");
1197 /* Align a size according to a format code. Return -1 on overflow. */
1200 align(Py_ssize_t size
, char c
, const formatdef
*e
)
1204 if (e
->format
== c
) {
1205 if (e
->alignment
&& size
> 0) {
1206 extra
= (e
->alignment
- 1) - (size
- 1) % (e
->alignment
);
1207 if (extra
> PY_SSIZE_T_MAX
- size
)
1216 /* calculate the size of a format string */
1219 prepare_s(PyStructObject
*self
)
1228 Py_ssize_t size
, len
, num
, itemsize
;
1230 fmt
= PyString_AS_STRING(self
->s_format
);
1232 f
= whichtable((char **)&fmt
);
1237 while ((c
= *s
++) != '\0') {
1238 if (isspace(Py_CHARMASK(c
)))
1240 if ('0' <= c
&& c
<= '9') {
1242 while ('0' <= (c
= *s
++) && c
<= '9') {
1243 /* overflow-safe version of
1244 if (num*10 + (c - '0') > PY_SSIZE_T_MAX) { ... } */
1245 if (num
>= PY_SSIZE_T_MAX
/ 10 && (
1246 num
> PY_SSIZE_T_MAX
/ 10 ||
1247 (c
- '0') > PY_SSIZE_T_MAX
% 10))
1249 num
= num
*10 + (c
- '0');
1262 case 's': /* fall through */
1263 case 'p': len
++; break;
1265 default: len
+= num
; break;
1269 size
= align(size
, c
, e
);
1273 /* if (size + num * itemsize > PY_SSIZE_T_MAX) { ... } */
1274 if (num
> (PY_SSIZE_T_MAX
- size
) / itemsize
)
1276 size
+= num
* itemsize
;
1279 /* check for overflow */
1280 if ((len
+ 1) > (PY_SSIZE_T_MAX
/ sizeof(formatcode
))) {
1285 self
->s_size
= size
;
1287 codes
= PyMem_MALLOC((len
+ 1) * sizeof(formatcode
));
1288 if (codes
== NULL
) {
1292 /* Free any s_codes value left over from a previous initialization. */
1293 if (self
->s_codes
!= NULL
)
1294 PyMem_FREE(self
->s_codes
);
1295 self
->s_codes
= codes
;
1299 while ((c
= *s
++) != '\0') {
1300 if (isspace(Py_CHARMASK(c
)))
1302 if ('0' <= c
&& c
<= '9') {
1304 while ('0' <= (c
= *s
++) && c
<= '9')
1305 num
= num
*10 + (c
- '0');
1314 size
= align(size
, c
, e
);
1315 if (c
== 's' || c
== 'p') {
1316 codes
->offset
= size
;
1321 } else if (c
== 'x') {
1324 while (--num
>= 0) {
1325 codes
->offset
= size
;
1326 codes
->size
= e
->size
;
1333 codes
->fmtdef
= NULL
;
1334 codes
->offset
= size
;
1340 PyErr_SetString(StructError
,
1341 "total struct size too long");
1346 s_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1350 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
1352 self
= type
->tp_alloc(type
, 0);
1354 PyStructObject
*s
= (PyStructObject
*)self
;
1356 s
->s_format
= Py_None
;
1365 s_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1367 PyStructObject
*soself
= (PyStructObject
*)self
;
1368 PyObject
*o_format
= NULL
;
1370 static char *kwlist
[] = {"format", 0};
1372 assert(PyStruct_Check(self
));
1374 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "O:Struct", kwlist
,
1378 if (PyString_Check(o_format
)) {
1379 Py_INCREF(o_format
);
1380 Py_CLEAR(soself
->s_format
);
1381 soself
->s_format
= o_format
;
1383 else if (PyUnicode_Check(o_format
)) {
1384 PyObject
*str
= PyUnicode_AsEncodedString(o_format
, "ascii", NULL
);
1387 Py_CLEAR(soself
->s_format
);
1388 soself
->s_format
= str
;
1391 PyErr_Format(PyExc_TypeError
,
1392 "Struct() argument 1 must be string, not %s",
1393 Py_TYPE(o_format
)->tp_name
);
1397 ret
= prepare_s(soself
);
1402 s_dealloc(PyStructObject
*s
)
1404 if (s
->weakreflist
!= NULL
)
1405 PyObject_ClearWeakRefs((PyObject
*)s
);
1406 if (s
->s_codes
!= NULL
) {
1407 PyMem_FREE(s
->s_codes
);
1409 Py_XDECREF(s
->s_format
);
1410 Py_TYPE(s
)->tp_free((PyObject
*)s
);
1414 s_unpack_internal(PyStructObject
*soself
, char *startfrom
) {
1417 PyObject
*result
= PyTuple_New(soself
->s_len
);
1421 for (code
= soself
->s_codes
; code
->fmtdef
!= NULL
; code
++) {
1423 const formatdef
*e
= code
->fmtdef
;
1424 const char *res
= startfrom
+ code
->offset
;
1425 if (e
->format
== 's') {
1426 v
= PyString_FromStringAndSize(res
, code
->size
);
1427 } else if (e
->format
== 'p') {
1428 Py_ssize_t n
= *(unsigned char*)res
;
1429 if (n
>= code
->size
)
1431 v
= PyString_FromStringAndSize(res
+ 1, n
);
1433 v
= e
->unpack(res
, e
);
1437 PyTuple_SET_ITEM(result
, i
++, v
);
1447 PyDoc_STRVAR(s_unpack__doc__
,
1448 "S.unpack(str) -> (v1, v2, ...)\n\
1450 Return tuple containing values unpacked according to this Struct's format.\n\
1451 Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1455 s_unpack(PyObject
*self
, PyObject
*inputstr
)
1460 PyObject
*args
=NULL
, *result
;
1461 PyStructObject
*soself
= (PyStructObject
*)self
;
1462 assert(PyStruct_Check(self
));
1463 assert(soself
->s_codes
!= NULL
);
1464 if (inputstr
== NULL
)
1466 if (PyString_Check(inputstr
) &&
1467 PyString_GET_SIZE(inputstr
) == soself
->s_size
) {
1468 return s_unpack_internal(soself
, PyString_AS_STRING(inputstr
));
1470 args
= PyTuple_Pack(1, inputstr
);
1473 if (!PyArg_ParseTuple(args
, "s*:unpack", &buf
))
1477 if (soself
->s_size
!= len
) {
1478 PyBuffer_Release(&buf
);
1481 result
= s_unpack_internal(soself
, start
);
1483 PyBuffer_Release(&buf
);
1488 PyErr_Format(StructError
,
1489 "unpack requires a string argument of length %zd",
1494 PyDoc_STRVAR(s_unpack_from__doc__
,
1495 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1497 Return tuple containing values unpacked according to this Struct's format.\n\
1498 Unlike unpack, unpack_from can unpack values from any object supporting\n\
1499 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1500 See struct.__doc__ for more on format strings.");
1503 s_unpack_from(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1505 static char *kwlist
[] = {"buffer", "offset", 0};
1506 static char *fmt
= "z*|n:unpack_from";
1508 Py_ssize_t buffer_len
= 0, offset
= 0;
1509 char *buffer
= NULL
;
1510 PyStructObject
*soself
= (PyStructObject
*)self
;
1512 assert(PyStruct_Check(self
));
1513 assert(soself
->s_codes
!= NULL
);
1515 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, fmt
, kwlist
,
1519 buffer_len
= buf
.len
;
1520 if (buffer
== NULL
) {
1521 PyErr_Format(StructError
,
1522 "unpack_from requires a buffer argument");
1523 PyBuffer_Release(&buf
);
1528 offset
+= buffer_len
;
1530 if (offset
< 0 || (buffer_len
- offset
) < soself
->s_size
) {
1531 PyErr_Format(StructError
,
1532 "unpack_from requires a buffer of at least %zd bytes",
1534 PyBuffer_Release(&buf
);
1537 result
= s_unpack_internal(soself
, buffer
+ offset
);
1538 PyBuffer_Release(&buf
);
1544 * Guts of the pack function.
1546 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1547 * argument for where to start processing the arguments for packing, and a
1548 * character buffer for writing the packed string. The caller must insure
1549 * that the buffer may contain the required length for packing the arguments.
1550 * 0 is returned on success, 1 is returned if there is an error.
1554 s_pack_internal(PyStructObject
*soself
, PyObject
*args
, int offset
, char* buf
)
1557 /* XXX(nnorwitz): why does i need to be a local? can we use
1558 the offset parameter or do we need the wider width? */
1561 memset(buf
, '\0', soself
->s_size
);
1563 for (code
= soself
->s_codes
; code
->fmtdef
!= NULL
; code
++) {
1565 PyObject
*v
= PyTuple_GET_ITEM(args
, i
++);
1566 const formatdef
*e
= code
->fmtdef
;
1567 char *res
= buf
+ code
->offset
;
1568 if (e
->format
== 's') {
1569 if (!PyString_Check(v
)) {
1570 PyErr_SetString(StructError
,
1571 "argument for 's' must "
1575 n
= PyString_GET_SIZE(v
);
1579 memcpy(res
, PyString_AS_STRING(v
), n
);
1580 } else if (e
->format
== 'p') {
1581 if (!PyString_Check(v
)) {
1582 PyErr_SetString(StructError
,
1583 "argument for 'p' must "
1587 n
= PyString_GET_SIZE(v
);
1588 if (n
> (code
->size
- 1))
1591 memcpy(res
+ 1, PyString_AS_STRING(v
), n
);
1594 *res
= Py_SAFE_DOWNCAST(n
, Py_ssize_t
, unsigned char);
1595 } else if (e
->pack(res
, v
, e
) < 0) {
1596 if (strchr(integer_codes
, e
->format
) != NULL
&&
1597 PyErr_ExceptionMatches(PyExc_OverflowError
))
1598 PyErr_Format(StructError
,
1599 "integer out of range for "
1611 PyDoc_STRVAR(s_pack__doc__
,
1612 "S.pack(v1, v2, ...) -> string\n\
1614 Return a string containing values v1, v2, ... packed according to this\n\
1615 Struct's format. See struct.__doc__ for more on format strings.");
1618 s_pack(PyObject
*self
, PyObject
*args
)
1620 PyStructObject
*soself
;
1623 /* Validate arguments. */
1624 soself
= (PyStructObject
*)self
;
1625 assert(PyStruct_Check(self
));
1626 assert(soself
->s_codes
!= NULL
);
1627 if (PyTuple_GET_SIZE(args
) != soself
->s_len
)
1629 PyErr_Format(StructError
,
1630 "pack expected %zd items for packing (got %zd)", soself
->s_len
, PyTuple_GET_SIZE(args
));
1634 /* Allocate a new string */
1635 result
= PyString_FromStringAndSize((char *)NULL
, soself
->s_size
);
1640 if ( s_pack_internal(soself
, args
, 0, PyString_AS_STRING(result
)) != 0 ) {
1648 PyDoc_STRVAR(s_pack_into__doc__
,
1649 "S.pack_into(buffer, offset, v1, v2, ...)\n\
1651 Pack the values v1, v2, ... according to this Struct's format, write \n\
1652 the packed bytes into the writable buffer buf starting at offset. Note\n\
1653 that the offset is not an optional argument. See struct.__doc__ for \n\
1654 more on format strings.");
1657 s_pack_into(PyObject
*self
, PyObject
*args
)
1659 PyStructObject
*soself
;
1663 /* Validate arguments. +1 is for the first arg as buffer. */
1664 soself
= (PyStructObject
*)self
;
1665 assert(PyStruct_Check(self
));
1666 assert(soself
->s_codes
!= NULL
);
1667 if (PyTuple_GET_SIZE(args
) != (soself
->s_len
+ 2))
1669 if (PyTuple_GET_SIZE(args
) == 0) {
1670 PyErr_Format(StructError
,
1671 "pack_into expected buffer argument");
1673 else if (PyTuple_GET_SIZE(args
) == 1) {
1674 PyErr_Format(StructError
,
1675 "pack_into expected offset argument");
1678 PyErr_Format(StructError
,
1679 "pack_into expected %zd items for packing (got %zd)",
1680 soself
->s_len
, (PyTuple_GET_SIZE(args
) - 2));
1685 /* Extract a writable memory buffer from the first argument */
1686 if (!PyArg_Parse(PyTuple_GET_ITEM(args
, 0), "w*", &buf
))
1689 /* Extract the offset from the first argument */
1690 offset
= PyInt_AsSsize_t(PyTuple_GET_ITEM(args
, 1));
1691 if (offset
== -1 && PyErr_Occurred()) {
1692 PyBuffer_Release(&buf
);
1696 /* Support negative offsets. */
1700 /* Check boundaries */
1701 if (offset
< 0 || (buf
.len
- offset
) < soself
->s_size
) {
1702 PyErr_Format(StructError
,
1703 "pack_into requires a buffer of at least %zd bytes",
1705 PyBuffer_Release(&buf
);
1710 if (s_pack_internal(soself
, args
, 2, (char *)buf
.buf
+ offset
) != 0) {
1711 PyBuffer_Release(&buf
);
1714 PyBuffer_Release(&buf
);
1720 s_get_format(PyStructObject
*self
, void *unused
)
1722 Py_INCREF(self
->s_format
);
1723 return self
->s_format
;
1727 s_get_size(PyStructObject
*self
, void *unused
)
1729 return PyInt_FromSsize_t(self
->s_size
);
1732 PyDoc_STRVAR(s_sizeof__doc__
,
1733 "S.__sizeof__() -> size of S in memory, in bytes");
1736 s_sizeof(PyStructObject
*self
, void *unused
)
1740 size
= sizeof(PyStructObject
) + sizeof(formatcode
) * (self
->s_len
+ 1);
1741 return PyLong_FromSsize_t(size
);
1744 /* List of functions */
1746 static struct PyMethodDef s_methods
[] = {
1747 {"pack", s_pack
, METH_VARARGS
, s_pack__doc__
},
1748 {"pack_into", s_pack_into
, METH_VARARGS
, s_pack_into__doc__
},
1749 {"unpack", s_unpack
, METH_O
, s_unpack__doc__
},
1750 {"unpack_from", (PyCFunction
)s_unpack_from
, METH_VARARGS
|METH_KEYWORDS
,
1751 s_unpack_from__doc__
},
1752 {"__sizeof__", (PyCFunction
)s_sizeof
, METH_NOARGS
, s_sizeof__doc__
},
1753 {NULL
, NULL
} /* sentinel */
1756 PyDoc_STRVAR(s__doc__
, "Compiled struct object");
1758 #define OFF(x) offsetof(PyStructObject, x)
1760 static PyGetSetDef s_getsetlist
[] = {
1761 {"format", (getter
)s_get_format
, (setter
)NULL
, "struct format string", NULL
},
1762 {"size", (getter
)s_get_size
, (setter
)NULL
, "struct size in bytes", NULL
},
1763 {NULL
} /* sentinel */
1767 PyTypeObject PyStructType
= {
1768 PyVarObject_HEAD_INIT(NULL
, 0)
1770 sizeof(PyStructObject
),
1772 (destructor
)s_dealloc
, /* tp_dealloc */
1778 0, /* tp_as_number */
1779 0, /* tp_as_sequence */
1780 0, /* tp_as_mapping */
1784 PyObject_GenericGetAttr
, /* tp_getattro */
1785 PyObject_GenericSetAttr
, /* tp_setattro */
1786 0, /* tp_as_buffer */
1787 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_WEAKREFS
,/* tp_flags */
1788 s__doc__
, /* tp_doc */
1789 0, /* tp_traverse */
1791 0, /* tp_richcompare */
1792 offsetof(PyStructObject
, weakreflist
), /* tp_weaklistoffset */
1794 0, /* tp_iternext */
1795 s_methods
, /* tp_methods */
1796 NULL
, /* tp_members */
1797 s_getsetlist
, /* tp_getset */
1800 0, /* tp_descr_get */
1801 0, /* tp_descr_set */
1802 0, /* tp_dictoffset */
1803 s_init
, /* tp_init */
1804 PyType_GenericAlloc
,/* tp_alloc */
1806 PyObject_Del
, /* tp_free */
1810 /* ---- Standalone functions ---- */
1812 #define MAXCACHE 100
1813 static PyObject
*cache
= NULL
;
1816 cache_struct(PyObject
*fmt
)
1818 PyObject
* s_object
;
1820 if (cache
== NULL
) {
1821 cache
= PyDict_New();
1826 s_object
= PyDict_GetItem(cache
, fmt
);
1827 if (s_object
!= NULL
) {
1828 Py_INCREF(s_object
);
1832 s_object
= PyObject_CallFunctionObjArgs((PyObject
*)(&PyStructType
), fmt
, NULL
);
1833 if (s_object
!= NULL
) {
1834 if (PyDict_Size(cache
) >= MAXCACHE
)
1835 PyDict_Clear(cache
);
1836 /* Attempt to cache the result */
1837 if (PyDict_SetItem(cache
, fmt
, s_object
) == -1)
1843 PyDoc_STRVAR(clearcache_doc
,
1844 "Clear the internal cache.");
1847 clearcache(PyObject
*self
)
1853 PyDoc_STRVAR(calcsize_doc
,
1854 "Return size of C struct described by format string fmt.");
1857 calcsize(PyObject
*self
, PyObject
*fmt
)
1860 PyObject
*s_object
= cache_struct(fmt
);
1861 if (s_object
== NULL
)
1863 n
= ((PyStructObject
*)s_object
)->s_size
;
1864 Py_DECREF(s_object
);
1865 return PyInt_FromSsize_t(n
);
1868 PyDoc_STRVAR(pack_doc
,
1869 "Return string containing values v1, v2, ... packed according to fmt.");
1872 pack(PyObject
*self
, PyObject
*args
)
1874 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1875 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1878 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1881 fmt
= PyTuple_GET_ITEM(args
, 0);
1882 newargs
= PyTuple_GetSlice(args
, 1, n
);
1883 if (newargs
== NULL
)
1886 s_object
= cache_struct(fmt
);
1887 if (s_object
== NULL
) {
1891 result
= s_pack(s_object
, newargs
);
1893 Py_DECREF(s_object
);
1897 PyDoc_STRVAR(pack_into_doc
,
1898 "Pack the values v1, v2, ... according to fmt.\n\
1899 Write the packed bytes into the writable buffer buf starting at offset.");
1902 pack_into(PyObject
*self
, PyObject
*args
)
1904 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1905 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1908 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1911 fmt
= PyTuple_GET_ITEM(args
, 0);
1912 newargs
= PyTuple_GetSlice(args
, 1, n
);
1913 if (newargs
== NULL
)
1916 s_object
= cache_struct(fmt
);
1917 if (s_object
== NULL
) {
1921 result
= s_pack_into(s_object
, newargs
);
1923 Py_DECREF(s_object
);
1927 PyDoc_STRVAR(unpack_doc
,
1928 "Unpack the string containing packed C structure data, according to fmt.\n\
1929 Requires len(string) == calcsize(fmt).");
1932 unpack(PyObject
*self
, PyObject
*args
)
1934 PyObject
*s_object
, *fmt
, *inputstr
, *result
;
1936 if (!PyArg_UnpackTuple(args
, "unpack", 2, 2, &fmt
, &inputstr
))
1939 s_object
= cache_struct(fmt
);
1940 if (s_object
== NULL
)
1942 result
= s_unpack(s_object
, inputstr
);
1943 Py_DECREF(s_object
);
1947 PyDoc_STRVAR(unpack_from_doc
,
1948 "Unpack the buffer, containing packed C structure data, according to\n\
1949 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1952 unpack_from(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1954 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1955 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1958 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1961 fmt
= PyTuple_GET_ITEM(args
, 0);
1962 newargs
= PyTuple_GetSlice(args
, 1, n
);
1963 if (newargs
== NULL
)
1966 s_object
= cache_struct(fmt
);
1967 if (s_object
== NULL
) {
1971 result
= s_unpack_from(s_object
, newargs
, kwds
);
1973 Py_DECREF(s_object
);
1977 static struct PyMethodDef module_functions
[] = {
1978 {"_clearcache", (PyCFunction
)clearcache
, METH_NOARGS
, clearcache_doc
},
1979 {"calcsize", calcsize
, METH_O
, calcsize_doc
},
1980 {"pack", pack
, METH_VARARGS
, pack_doc
},
1981 {"pack_into", pack_into
, METH_VARARGS
, pack_into_doc
},
1982 {"unpack", unpack
, METH_VARARGS
, unpack_doc
},
1983 {"unpack_from", (PyCFunction
)unpack_from
,
1984 METH_VARARGS
|METH_KEYWORDS
, unpack_from_doc
},
1985 {NULL
, NULL
} /* sentinel */
1989 /* Module initialization */
1991 PyDoc_STRVAR(module_doc
,
1992 "Functions to convert between Python values and C structs represented\n\
1993 as Python strings. It uses format strings (explained below) as compact\n\
1994 descriptions of the lay-out of the C structs and the intended conversion\n\
1995 to/from Python values.\n\
1997 The optional first format char indicates byte order, size and alignment:\n\
1998 @: native order, size & alignment (default)\n\
1999 =: native order, std. size & alignment\n\
2000 <: little-endian, std. size & alignment\n\
2001 >: big-endian, std. size & alignment\n\
2004 The remaining chars indicate types of args and must match exactly;\n\
2005 these can be preceded by a decimal repeat count:\n\
2006 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
2007 ?: _Bool (requires C99; if not available, char is used instead)\n\
2008 h:short; H:unsigned short; i:int; I:unsigned int;\n\
2009 l:long; L:unsigned long; f:float; d:double.\n\
2010 Special cases (preceding decimal count indicates length):\n\
2011 s:string (array of char); p: pascal string (with count byte).\n\
2012 Special case (only available in native format):\n\
2013 P:an integer type that is wide enough to hold a pointer.\n\
2014 Special case (not in native mode unless 'long long' in platform C):\n\
2015 q:long long; Q:unsigned long long\n\
2016 Whitespace between formats is ignored.\n\
2018 The variable struct.error is an exception raised on errors.\n");
2025 ver
= PyString_FromString("0.2");
2029 m
= Py_InitModule3("_struct", module_functions
, module_doc
);
2033 Py_TYPE(&PyStructType
) = &PyType_Type
;
2034 if (PyType_Ready(&PyStructType
) < 0)
2037 /* This speed trick can't be used until overflow masking goes
2038 away, because native endian always raises exceptions
2039 instead of overflow masking. */
2041 /* Check endian and swap in faster functions */
2044 formatdef
*native
= native_table
;
2045 formatdef
*other
, *ptr
;
2046 if ((int)*(unsigned char*)&one
)
2047 other
= lilendian_table
;
2049 other
= bigendian_table
;
2050 /* Scan through the native table, find a matching
2051 entry in the endian table and swap in the
2052 native implementations whenever possible
2053 (64-bit platforms may not have "standard" sizes) */
2054 while (native
->format
!= '\0' && other
->format
!= '\0') {
2056 while (ptr
->format
!= '\0') {
2057 if (ptr
->format
== native
->format
) {
2058 /* Match faster when formats are
2059 listed in the same order */
2062 /* Only use the trick if the
2064 if (ptr
->size
!= native
->size
)
2066 /* Skip float and double, could be
2067 "unknown" float format */
2068 if (ptr
->format
== 'd' || ptr
->format
== 'f')
2070 ptr
->pack
= native
->pack
;
2071 ptr
->unpack
= native
->unpack
;
2080 /* Add some symbolic constants to the module */
2081 if (StructError
== NULL
) {
2082 StructError
= PyErr_NewException("struct.error", NULL
, NULL
);
2083 if (StructError
== NULL
)
2087 Py_INCREF(StructError
);
2088 PyModule_AddObject(m
, "error", StructError
);
2090 Py_INCREF((PyObject
*)&PyStructType
);
2091 PyModule_AddObject(m
, "Struct", (PyObject
*)&PyStructType
);
2093 PyModule_AddObject(m
, "__version__", ver
);
2095 PyModule_AddIntConstant(m
, "_PY_STRUCT_RANGE_CHECKING", 1);
2096 PyModule_AddIntConstant(m
, "_PY_STRUCT_FLOAT_COERCE", 1);