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_OVERFLOW_MASKING is defined, the struct module will wrap all input
21 numbers for explicit endians such that they fit in the given type, much
22 like explicit casting in C. A warning will be raised if the number did
23 not originally fit within the range of the requested type. If it is
24 not defined, then all range errors and overflow will be struct.error
27 #define PY_STRUCT_OVERFLOW_MASKING 1
29 #ifdef PY_STRUCT_OVERFLOW_MASKING
30 static PyObject
*pylong_ulong_mask
= NULL
;
31 static PyObject
*pyint_zero
= NULL
;
34 /* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float
35 arguments for integer formats with a warning for backwards
38 #define PY_STRUCT_FLOAT_COERCE 1
40 #ifdef PY_STRUCT_FLOAT_COERCE
41 #define FLOAT_COERCE "integer argument expected, got float"
45 /* The translation function for each format character is table driven */
46 typedef struct _formatdef
{
50 PyObject
* (*unpack
)(const char *,
51 const struct _formatdef
*);
52 int (*pack
)(char *, PyObject
*,
53 const struct _formatdef
*);
56 typedef struct _formatcode
{
57 const struct _formatdef
*fmtdef
;
62 /* Struct object interface */
70 PyObject
*weakreflist
; /* List of weak references */
74 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
75 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
80 static PyObject
*StructError
;
83 /* Define various structs to figure out the alignments of types */
86 typedef struct { char c
; short x
; } st_short
;
87 typedef struct { char c
; int x
; } st_int
;
88 typedef struct { char c
; long x
; } st_long
;
89 typedef struct { char c
; float x
; } st_float
;
90 typedef struct { char c
; double x
; } st_double
;
91 typedef struct { char c
; void *x
; } st_void_p
;
93 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
94 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
95 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
96 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
97 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
98 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
100 /* We can't support q and Q in native mode unless the compiler does;
101 in std mode, they're 8 bytes on all platforms. */
102 #ifdef HAVE_LONG_LONG
103 typedef struct { char c
; PY_LONG_LONG x
; } s_long_long
;
104 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
108 #define BOOL_TYPE _Bool
109 typedef struct { char c
; _Bool x
; } s_bool
;
110 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
112 #define BOOL_TYPE char
116 #define STRINGIFY(x) #x
119 #pragma options align=reset
122 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
125 get_pylong(PyObject
*v
)
131 return PyLong_FromLong(PyInt_AS_LONG(v
));
132 if (PyLong_Check(v
)) {
136 m
= Py_TYPE(v
)->tp_as_number
;
137 if (m
!= NULL
&& m
->nb_long
!= NULL
) {
145 PyErr_SetString(StructError
,
146 "cannot convert argument to long");
150 /* Helper routine to get a Python integer and raise the appropriate error
154 get_long(PyObject
*v
, long *p
)
156 long x
= PyInt_AsLong(v
);
157 if (x
== -1 && PyErr_Occurred()) {
158 #ifdef PY_STRUCT_FLOAT_COERCE
159 if (PyFloat_Check(v
)) {
163 if (PyErr_WarnEx(PyExc_DeprecationWarning
, FLOAT_COERCE
, 2) < 0)
168 res
= get_long(o
, p
);
173 if (PyErr_ExceptionMatches(PyExc_TypeError
))
174 PyErr_SetString(StructError
,
175 "required argument is not an integer");
183 /* Same, but handling unsigned long */
186 get_ulong(PyObject
*v
, unsigned long *p
)
188 if (PyLong_Check(v
)) {
189 unsigned long x
= PyLong_AsUnsignedLong(v
);
190 if (x
== (unsigned long)(-1) && PyErr_Occurred())
195 if (get_long(v
, (long *)p
) < 0)
197 if (((long)*p
) < 0) {
198 PyErr_SetString(StructError
,
199 "unsigned argument is < 0");
205 #ifdef HAVE_LONG_LONG
207 /* Same, but handling native long long. */
210 get_longlong(PyObject
*v
, PY_LONG_LONG
*p
)
217 assert(PyLong_Check(v
));
218 x
= PyLong_AsLongLong(v
);
220 if (x
== (PY_LONG_LONG
)-1 && PyErr_Occurred())
226 /* Same, but handling native unsigned long long. */
229 get_ulonglong(PyObject
*v
, unsigned PY_LONG_LONG
*p
)
231 unsigned PY_LONG_LONG x
;
236 assert(PyLong_Check(v
));
237 x
= PyLong_AsUnsignedLongLong(v
);
239 if (x
== (unsigned PY_LONG_LONG
)-1 && PyErr_Occurred())
247 #ifdef PY_STRUCT_OVERFLOW_MASKING
249 /* Helper routine to get a Python integer and raise the appropriate error
252 #define INT_OVERFLOW "struct integer overflow masking is deprecated"
255 get_wrapped_long(PyObject
*v
, long *p
)
257 if (get_long(v
, p
) < 0) {
258 if (PyLong_Check(v
) &&
259 PyErr_ExceptionMatches(PyExc_OverflowError
)) {
263 #ifdef PY_STRUCT_FLOAT_COERCE
264 if (PyFloat_Check(v
)) {
268 if (PyErr_WarnEx(PyExc_DeprecationWarning
, FLOAT_COERCE
, 2) < 0)
273 res
= get_wrapped_long(o
, p
);
278 if (PyErr_WarnEx(PyExc_DeprecationWarning
, INT_OVERFLOW
, 2) < 0)
280 wrapped
= PyNumber_And(v
, pylong_ulong_mask
);
283 x
= (long)PyLong_AsUnsignedLong(wrapped
);
285 if (x
== -1 && PyErr_Occurred())
296 get_wrapped_ulong(PyObject
*v
, unsigned long *p
)
298 long x
= (long)PyLong_AsUnsignedLong(v
);
299 if (x
== -1 && PyErr_Occurred()) {
302 #ifdef PY_STRUCT_FLOAT_COERCE
303 if (PyFloat_Check(v
)) {
307 if (PyErr_WarnEx(PyExc_DeprecationWarning
, FLOAT_COERCE
, 2) < 0)
312 res
= get_wrapped_ulong(o
, p
);
317 wrapped
= PyNumber_And(v
, pylong_ulong_mask
);
320 if (PyErr_WarnEx(PyExc_DeprecationWarning
, INT_OVERFLOW
, 2) < 0) {
324 x
= (long)PyLong_AsUnsignedLong(wrapped
);
326 if (x
== -1 && PyErr_Occurred())
329 *p
= (unsigned long)x
;
333 #define RANGE_ERROR(x, f, flag, mask) \
335 if (_range_error(f, flag) < 0) \
343 #define get_wrapped_long get_long
344 #define get_wrapped_ulong get_ulong
345 #define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag)
349 /* Floating point helpers */
352 unpack_float(const char *p
, /* start of 4-byte string */
353 int le
) /* true for little-endian, false for big-endian */
357 x
= _PyFloat_Unpack4((unsigned char *)p
, le
);
358 if (x
== -1.0 && PyErr_Occurred())
360 return PyFloat_FromDouble(x
);
364 unpack_double(const char *p
, /* start of 8-byte string */
365 int le
) /* true for little-endian, false for big-endian */
369 x
= _PyFloat_Unpack8((unsigned char *)p
, le
);
370 if (x
== -1.0 && PyErr_Occurred())
372 return PyFloat_FromDouble(x
);
375 /* Helper to format the range error exceptions */
377 _range_error(const formatdef
*f
, int is_unsigned
)
379 /* ulargest is the largest unsigned value with f->size bytes.
380 * Note that the simpler:
381 * ((size_t)1 << (f->size * 8)) - 1
382 * doesn't work when f->size == sizeof(size_t) because C doesn't
383 * define what happens when a left shift count is >= the number of
384 * bits in the integer being shifted; e.g., on some boxes it doesn't
385 * shift at all when they're equal.
387 const size_t ulargest
= (size_t)-1 >> ((SIZEOF_SIZE_T
- f
->size
)*8);
388 assert(f
->size
>= 1 && f
->size
<= SIZEOF_SIZE_T
);
390 PyErr_Format(StructError
,
391 "'%c' format requires 0 <= number <= %zu",
395 const Py_ssize_t largest
= (Py_ssize_t
)(ulargest
>> 1);
396 PyErr_Format(StructError
,
397 "'%c' format requires %zd <= number <= %zd",
402 #ifdef PY_STRUCT_OVERFLOW_MASKING
404 PyObject
*ptype
, *pvalue
, *ptraceback
;
407 PyErr_Fetch(&ptype
, &pvalue
, &ptraceback
);
408 assert(pvalue
!= NULL
);
409 msg
= PyObject_Str(pvalue
);
412 Py_XDECREF(ptraceback
);
415 rval
= PyErr_WarnEx(PyExc_DeprecationWarning
,
416 PyString_AS_STRING(msg
), 2);
427 /* A large number of small routines follow, with names of the form
431 [bln] distiguishes among big-endian, little-endian and native.
432 [pu] distiguishes between pack (to struct) and unpack (from struct).
433 TYPE is one of char, byte, ubyte, etc.
436 /* Native mode routines. ****************************************************/
438 In all n[up]_<type> routines handling types larger than 1 byte, there is
439 *no* guarantee that the p pointer is properly aligned for each type,
440 therefore memcpy is called. An intermediate variable is used to
441 compensate for big-endian architectures.
442 Normally both the intermediate variable and the memcpy call will be
443 skipped by C optimisation in little-endian architectures (gcc >= 2.91
447 nu_char(const char *p
, const formatdef
*f
)
449 return PyString_FromStringAndSize(p
, 1);
453 nu_byte(const char *p
, const formatdef
*f
)
455 return PyInt_FromLong((long) *(signed char *)p
);
459 nu_ubyte(const char *p
, const formatdef
*f
)
461 return PyInt_FromLong((long) *(unsigned char *)p
);
465 nu_short(const char *p
, const formatdef
*f
)
468 memcpy((char *)&x
, p
, sizeof x
);
469 return PyInt_FromLong((long)x
);
473 nu_ushort(const char *p
, const formatdef
*f
)
476 memcpy((char *)&x
, p
, sizeof x
);
477 return PyInt_FromLong((long)x
);
481 nu_int(const char *p
, const formatdef
*f
)
484 memcpy((char *)&x
, p
, sizeof x
);
485 return PyInt_FromLong((long)x
);
489 nu_uint(const char *p
, const formatdef
*f
)
492 memcpy((char *)&x
, p
, sizeof x
);
493 #if (SIZEOF_LONG > SIZEOF_INT)
494 return PyInt_FromLong((long)x
);
496 if (x
<= ((unsigned int)LONG_MAX
))
497 return PyInt_FromLong((long)x
);
498 return PyLong_FromUnsignedLong((unsigned long)x
);
503 nu_long(const char *p
, const formatdef
*f
)
506 memcpy((char *)&x
, p
, sizeof x
);
507 return PyInt_FromLong(x
);
511 nu_ulong(const char *p
, const formatdef
*f
)
514 memcpy((char *)&x
, p
, sizeof x
);
516 return PyInt_FromLong((long)x
);
517 return PyLong_FromUnsignedLong(x
);
520 /* Native mode doesn't support q or Q unless the platform C supports
521 long long (or, on Windows, __int64). */
523 #ifdef HAVE_LONG_LONG
526 nu_longlong(const char *p
, const formatdef
*f
)
529 memcpy((char *)&x
, p
, sizeof x
);
530 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
531 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
532 return PyLong_FromLongLong(x
);
536 nu_ulonglong(const char *p
, const formatdef
*f
)
538 unsigned PY_LONG_LONG x
;
539 memcpy((char *)&x
, p
, sizeof x
);
541 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
542 return PyLong_FromUnsignedLongLong(x
);
548 nu_bool(const char *p
, const formatdef
*f
)
551 memcpy((char *)&x
, p
, sizeof x
);
552 return PyBool_FromLong(x
!= 0);
557 nu_float(const char *p
, const formatdef
*f
)
560 memcpy((char *)&x
, p
, sizeof x
);
561 return PyFloat_FromDouble((double)x
);
565 nu_double(const char *p
, const formatdef
*f
)
568 memcpy((char *)&x
, p
, sizeof x
);
569 return PyFloat_FromDouble(x
);
573 nu_void_p(const char *p
, const formatdef
*f
)
576 memcpy((char *)&x
, p
, sizeof x
);
577 return PyLong_FromVoidPtr(x
);
581 np_byte(char *p
, PyObject
*v
, const formatdef
*f
)
584 if (get_long(v
, &x
) < 0)
586 if (x
< -128 || x
> 127){
587 PyErr_SetString(StructError
,
588 "byte format requires -128 <= number <= 127");
596 np_ubyte(char *p
, PyObject
*v
, const formatdef
*f
)
599 if (get_long(v
, &x
) < 0)
601 if (x
< 0 || x
> 255){
602 PyErr_SetString(StructError
,
603 "ubyte format requires 0 <= number <= 255");
611 np_char(char *p
, PyObject
*v
, const formatdef
*f
)
613 if (!PyString_Check(v
) || PyString_Size(v
) != 1) {
614 PyErr_SetString(StructError
,
615 "char format require string of length 1");
618 *p
= *PyString_AsString(v
);
623 np_short(char *p
, PyObject
*v
, const formatdef
*f
)
627 if (get_long(v
, &x
) < 0)
629 if (x
< SHRT_MIN
|| x
> SHRT_MAX
){
630 PyErr_SetString(StructError
,
631 "short format requires " STRINGIFY(SHRT_MIN
)
632 " <= number <= " STRINGIFY(SHRT_MAX
));
636 memcpy(p
, (char *)&y
, sizeof y
);
641 np_ushort(char *p
, PyObject
*v
, const formatdef
*f
)
645 if (get_long(v
, &x
) < 0)
647 if (x
< 0 || x
> USHRT_MAX
){
648 PyErr_SetString(StructError
,
649 "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX
));
652 y
= (unsigned short)x
;
653 memcpy(p
, (char *)&y
, sizeof y
);
658 np_int(char *p
, PyObject
*v
, const formatdef
*f
)
662 if (get_long(v
, &x
) < 0)
664 #if (SIZEOF_LONG > SIZEOF_INT)
665 if ((x
< ((long)INT_MIN
)) || (x
> ((long)INT_MAX
)))
666 return _range_error(f
, 0);
669 memcpy(p
, (char *)&y
, sizeof y
);
674 np_uint(char *p
, PyObject
*v
, const formatdef
*f
)
678 if (get_ulong(v
, &x
) < 0)
679 return _range_error(f
, 1);
681 #if (SIZEOF_LONG > SIZEOF_INT)
682 if (x
> ((unsigned long)UINT_MAX
))
683 return _range_error(f
, 1);
685 memcpy(p
, (char *)&y
, sizeof y
);
690 np_long(char *p
, PyObject
*v
, const formatdef
*f
)
693 if (get_long(v
, &x
) < 0)
695 memcpy(p
, (char *)&x
, sizeof x
);
700 np_ulong(char *p
, PyObject
*v
, const formatdef
*f
)
703 if (get_ulong(v
, &x
) < 0)
704 return _range_error(f
, 1);
705 memcpy(p
, (char *)&x
, sizeof x
);
709 #ifdef HAVE_LONG_LONG
712 np_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
715 if (get_longlong(v
, &x
) < 0)
717 memcpy(p
, (char *)&x
, sizeof x
);
722 np_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
724 unsigned PY_LONG_LONG x
;
725 if (get_ulonglong(v
, &x
) < 0)
727 memcpy(p
, (char *)&x
, sizeof x
);
734 np_bool(char *p
, PyObject
*v
, const formatdef
*f
)
737 y
= PyObject_IsTrue(v
);
738 memcpy(p
, (char *)&y
, sizeof y
);
743 np_float(char *p
, PyObject
*v
, const formatdef
*f
)
745 float x
= (float)PyFloat_AsDouble(v
);
746 if (x
== -1 && PyErr_Occurred()) {
747 PyErr_SetString(StructError
,
748 "required argument is not a float");
751 memcpy(p
, (char *)&x
, sizeof x
);
756 np_double(char *p
, PyObject
*v
, const formatdef
*f
)
758 double x
= PyFloat_AsDouble(v
);
759 if (x
== -1 && PyErr_Occurred()) {
760 PyErr_SetString(StructError
,
761 "required argument is not a float");
764 memcpy(p
, (char *)&x
, sizeof(double));
769 np_void_p(char *p
, PyObject
*v
, const formatdef
*f
)
776 assert(PyLong_Check(v
));
777 x
= PyLong_AsVoidPtr(v
);
779 if (x
== NULL
&& PyErr_Occurred())
781 memcpy(p
, (char *)&x
, sizeof x
);
785 static formatdef native_table
[] = {
786 {'x', sizeof(char), 0, NULL
},
787 {'b', sizeof(char), 0, nu_byte
, np_byte
},
788 {'B', sizeof(char), 0, nu_ubyte
, np_ubyte
},
789 {'c', sizeof(char), 0, nu_char
, np_char
},
790 {'s', sizeof(char), 0, NULL
},
791 {'p', sizeof(char), 0, NULL
},
792 {'h', sizeof(short), SHORT_ALIGN
, nu_short
, np_short
},
793 {'H', sizeof(short), SHORT_ALIGN
, nu_ushort
, np_ushort
},
794 {'i', sizeof(int), INT_ALIGN
, nu_int
, np_int
},
795 {'I', sizeof(int), INT_ALIGN
, nu_uint
, np_uint
},
796 {'l', sizeof(long), LONG_ALIGN
, nu_long
, np_long
},
797 {'L', sizeof(long), LONG_ALIGN
, nu_ulong
, np_ulong
},
798 #ifdef HAVE_LONG_LONG
799 {'q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_longlong
, np_longlong
},
800 {'Q', sizeof(PY_LONG_LONG
), LONG_LONG_ALIGN
, nu_ulonglong
,np_ulonglong
},
802 {'?', sizeof(BOOL_TYPE
), BOOL_ALIGN
, nu_bool
, np_bool
},
803 {'f', sizeof(float), FLOAT_ALIGN
, nu_float
, np_float
},
804 {'d', sizeof(double), DOUBLE_ALIGN
, nu_double
, np_double
},
805 {'P', sizeof(void *), VOID_P_ALIGN
, nu_void_p
, np_void_p
},
809 /* Big-endian routines. *****************************************************/
812 bu_int(const char *p
, const formatdef
*f
)
815 Py_ssize_t i
= f
->size
;
816 const unsigned char *bytes
= (const unsigned char *)p
;
818 x
= (x
<<8) | *bytes
++;
820 /* Extend the sign bit. */
821 if (SIZEOF_LONG
> f
->size
)
822 x
|= -(x
& (1L << ((8 * f
->size
) - 1)));
823 return PyInt_FromLong(x
);
827 bu_uint(const char *p
, const formatdef
*f
)
830 Py_ssize_t i
= f
->size
;
831 const unsigned char *bytes
= (const unsigned char *)p
;
833 x
= (x
<<8) | *bytes
++;
836 return PyInt_FromLong((long)x
);
837 return PyLong_FromUnsignedLong(x
);
841 bu_longlong(const char *p
, const formatdef
*f
)
843 #ifdef HAVE_LONG_LONG
845 Py_ssize_t i
= f
->size
;
846 const unsigned char *bytes
= (const unsigned char *)p
;
848 x
= (x
<<8) | *bytes
++;
850 /* Extend the sign bit. */
851 if (SIZEOF_LONG_LONG
> f
->size
)
852 x
|= -(x
& ((PY_LONG_LONG
)1 << ((8 * f
->size
) - 1)));
853 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
854 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
855 return PyLong_FromLongLong(x
);
857 return _PyLong_FromByteArray((const unsigned char *)p
,
859 0, /* little-endian */
865 bu_ulonglong(const char *p
, const formatdef
*f
)
867 #ifdef HAVE_LONG_LONG
868 unsigned PY_LONG_LONG x
= 0;
869 Py_ssize_t i
= f
->size
;
870 const unsigned char *bytes
= (const unsigned char *)p
;
872 x
= (x
<<8) | *bytes
++;
875 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
876 return PyLong_FromUnsignedLongLong(x
);
878 return _PyLong_FromByteArray((const unsigned char *)p
,
880 0, /* little-endian */
886 bu_float(const char *p
, const formatdef
*f
)
888 return unpack_float(p
, 0);
892 bu_double(const char *p
, const formatdef
*f
)
894 return unpack_double(p
, 0);
898 bu_bool(const char *p
, const formatdef
*f
)
901 memcpy((char *)&x
, p
, sizeof x
);
902 return PyBool_FromLong(x
!= 0);
906 bp_int(char *p
, PyObject
*v
, const formatdef
*f
)
910 if (get_wrapped_long(v
, &x
) < 0)
913 if (i
!= SIZEOF_LONG
) {
914 if ((i
== 2) && (x
< -32768 || x
> 32767))
915 RANGE_ERROR(x
, f
, 0, 0xffffL
);
916 #if (SIZEOF_LONG != 4)
917 else if ((i
== 4) && (x
< -2147483648L || x
> 2147483647L))
918 RANGE_ERROR(x
, f
, 0, 0xffffffffL
);
920 #ifdef PY_STRUCT_OVERFLOW_MASKING
921 else if ((i
== 1) && (x
< -128 || x
> 127))
922 RANGE_ERROR(x
, f
, 0, 0xffL
);
933 bp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
937 if (get_wrapped_ulong(v
, &x
) < 0)
940 if (i
!= SIZEOF_LONG
) {
941 unsigned long maxint
= 1;
942 maxint
<<= (unsigned long)(i
* 8);
944 RANGE_ERROR(x
, f
, 1, maxint
- 1);
954 bp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
960 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
963 0, /* little_endian */
970 bp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
976 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
979 0, /* little_endian */
986 bp_float(char *p
, PyObject
*v
, const formatdef
*f
)
988 double x
= PyFloat_AsDouble(v
);
989 if (x
== -1 && PyErr_Occurred()) {
990 PyErr_SetString(StructError
,
991 "required argument is not a float");
994 return _PyFloat_Pack4(x
, (unsigned char *)p
, 0);
998 bp_double(char *p
, PyObject
*v
, const formatdef
*f
)
1000 double x
= PyFloat_AsDouble(v
);
1001 if (x
== -1 && PyErr_Occurred()) {
1002 PyErr_SetString(StructError
,
1003 "required argument is not a float");
1006 return _PyFloat_Pack8(x
, (unsigned char *)p
, 0);
1010 bp_bool(char *p
, PyObject
*v
, const formatdef
*f
)
1013 y
= PyObject_IsTrue(v
);
1014 memcpy(p
, (char *)&y
, sizeof y
);
1018 static formatdef bigendian_table
[] = {
1020 #ifdef PY_STRUCT_OVERFLOW_MASKING
1021 /* Native packers do range checking without overflow masking. */
1022 {'b', 1, 0, nu_byte
, bp_int
},
1023 {'B', 1, 0, nu_ubyte
, bp_uint
},
1025 {'b', 1, 0, nu_byte
, np_byte
},
1026 {'B', 1, 0, nu_ubyte
, np_ubyte
},
1028 {'c', 1, 0, nu_char
, np_char
},
1031 {'h', 2, 0, bu_int
, bp_int
},
1032 {'H', 2, 0, bu_uint
, bp_uint
},
1033 {'i', 4, 0, bu_int
, bp_int
},
1034 {'I', 4, 0, bu_uint
, bp_uint
},
1035 {'l', 4, 0, bu_int
, bp_int
},
1036 {'L', 4, 0, bu_uint
, bp_uint
},
1037 {'q', 8, 0, bu_longlong
, bp_longlong
},
1038 {'Q', 8, 0, bu_ulonglong
, bp_ulonglong
},
1039 {'?', 1, 0, bu_bool
, bp_bool
},
1040 {'f', 4, 0, bu_float
, bp_float
},
1041 {'d', 8, 0, bu_double
, bp_double
},
1045 /* Little-endian routines. *****************************************************/
1048 lu_int(const char *p
, const formatdef
*f
)
1051 Py_ssize_t i
= f
->size
;
1052 const unsigned char *bytes
= (const unsigned char *)p
;
1054 x
= (x
<<8) | bytes
[--i
];
1056 /* Extend the sign bit. */
1057 if (SIZEOF_LONG
> f
->size
)
1058 x
|= -(x
& (1L << ((8 * f
->size
) - 1)));
1059 return PyInt_FromLong(x
);
1063 lu_uint(const char *p
, const formatdef
*f
)
1065 unsigned long x
= 0;
1066 Py_ssize_t i
= f
->size
;
1067 const unsigned char *bytes
= (const unsigned char *)p
;
1069 x
= (x
<<8) | bytes
[--i
];
1072 return PyInt_FromLong((long)x
);
1073 return PyLong_FromUnsignedLong((long)x
);
1077 lu_longlong(const char *p
, const formatdef
*f
)
1079 #ifdef HAVE_LONG_LONG
1081 Py_ssize_t i
= f
->size
;
1082 const unsigned char *bytes
= (const unsigned char *)p
;
1084 x
= (x
<<8) | bytes
[--i
];
1086 /* Extend the sign bit. */
1087 if (SIZEOF_LONG_LONG
> f
->size
)
1088 x
|= -(x
& ((PY_LONG_LONG
)1 << ((8 * f
->size
) - 1)));
1089 if (x
>= LONG_MIN
&& x
<= LONG_MAX
)
1090 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, PY_LONG_LONG
, long));
1091 return PyLong_FromLongLong(x
);
1093 return _PyLong_FromByteArray((const unsigned char *)p
,
1095 1, /* little-endian */
1101 lu_ulonglong(const char *p
, const formatdef
*f
)
1103 #ifdef HAVE_LONG_LONG
1104 unsigned PY_LONG_LONG x
= 0;
1105 Py_ssize_t i
= f
->size
;
1106 const unsigned char *bytes
= (const unsigned char *)p
;
1108 x
= (x
<<8) | bytes
[--i
];
1111 return PyInt_FromLong(Py_SAFE_DOWNCAST(x
, unsigned PY_LONG_LONG
, long));
1112 return PyLong_FromUnsignedLongLong(x
);
1114 return _PyLong_FromByteArray((const unsigned char *)p
,
1116 1, /* little-endian */
1122 lu_float(const char *p
, const formatdef
*f
)
1124 return unpack_float(p
, 1);
1128 lu_double(const char *p
, const formatdef
*f
)
1130 return unpack_double(p
, 1);
1134 lp_int(char *p
, PyObject
*v
, const formatdef
*f
)
1138 if (get_wrapped_long(v
, &x
) < 0)
1141 if (i
!= SIZEOF_LONG
) {
1142 if ((i
== 2) && (x
< -32768 || x
> 32767))
1143 RANGE_ERROR(x
, f
, 0, 0xffffL
);
1144 #if (SIZEOF_LONG != 4)
1145 else if ((i
== 4) && (x
< -2147483648L || x
> 2147483647L))
1146 RANGE_ERROR(x
, f
, 0, 0xffffffffL
);
1148 #ifdef PY_STRUCT_OVERFLOW_MASKING
1149 else if ((i
== 1) && (x
< -128 || x
> 127))
1150 RANGE_ERROR(x
, f
, 0, 0xffL
);
1161 lp_uint(char *p
, PyObject
*v
, const formatdef
*f
)
1165 if (get_wrapped_ulong(v
, &x
) < 0)
1168 if (i
!= SIZEOF_LONG
) {
1169 unsigned long maxint
= 1;
1170 maxint
<<= (unsigned long)(i
* 8);
1172 RANGE_ERROR(x
, f
, 1, maxint
- 1);
1182 lp_longlong(char *p
, PyObject
*v
, const formatdef
*f
)
1188 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
1191 1, /* little_endian */
1198 lp_ulonglong(char *p
, PyObject
*v
, const formatdef
*f
)
1204 res
= _PyLong_AsByteArray((PyLongObject
*)v
,
1207 1, /* little_endian */
1214 lp_float(char *p
, PyObject
*v
, const formatdef
*f
)
1216 double x
= PyFloat_AsDouble(v
);
1217 if (x
== -1 && PyErr_Occurred()) {
1218 PyErr_SetString(StructError
,
1219 "required argument is not a float");
1222 return _PyFloat_Pack4(x
, (unsigned char *)p
, 1);
1226 lp_double(char *p
, PyObject
*v
, const formatdef
*f
)
1228 double x
= PyFloat_AsDouble(v
);
1229 if (x
== -1 && PyErr_Occurred()) {
1230 PyErr_SetString(StructError
,
1231 "required argument is not a float");
1234 return _PyFloat_Pack8(x
, (unsigned char *)p
, 1);
1237 static formatdef lilendian_table
[] = {
1239 #ifdef PY_STRUCT_OVERFLOW_MASKING
1240 /* Native packers do range checking without overflow masking. */
1241 {'b', 1, 0, nu_byte
, lp_int
},
1242 {'B', 1, 0, nu_ubyte
, lp_uint
},
1244 {'b', 1, 0, nu_byte
, np_byte
},
1245 {'B', 1, 0, nu_ubyte
, np_ubyte
},
1247 {'c', 1, 0, nu_char
, np_char
},
1250 {'h', 2, 0, lu_int
, lp_int
},
1251 {'H', 2, 0, lu_uint
, lp_uint
},
1252 {'i', 4, 0, lu_int
, lp_int
},
1253 {'I', 4, 0, lu_uint
, lp_uint
},
1254 {'l', 4, 0, lu_int
, lp_int
},
1255 {'L', 4, 0, lu_uint
, lp_uint
},
1256 {'q', 8, 0, lu_longlong
, lp_longlong
},
1257 {'Q', 8, 0, lu_ulonglong
, lp_ulonglong
},
1258 {'?', 1, 0, bu_bool
, bp_bool
}, /* Std rep not endian dep,
1259 but potentially different from native rep -- reuse bx_bool funcs. */
1260 {'f', 4, 0, lu_float
, lp_float
},
1261 {'d', 8, 0, lu_double
, lp_double
},
1266 static const formatdef
*
1267 whichtable(char **pfmt
)
1269 const char *fmt
= (*pfmt
)++; /* May be backed out of later */
1272 return lilendian_table
;
1274 case '!': /* Network byte order is big-endian */
1275 return bigendian_table
;
1276 case '=': { /* Host byte order -- different from native in aligment! */
1278 char *p
= (char *) &n
;
1280 return lilendian_table
;
1282 return bigendian_table
;
1285 --*pfmt
; /* Back out of pointer increment */
1288 return native_table
;
1293 /* Get the table entry for a format code */
1295 static const formatdef
*
1296 getentry(int c
, const formatdef
*f
)
1298 for (; f
->format
!= '\0'; f
++) {
1299 if (f
->format
== c
) {
1303 PyErr_SetString(StructError
, "bad char in struct format");
1308 /* Align a size according to a format code */
1311 align(Py_ssize_t size
, char c
, const formatdef
*e
)
1313 if (e
->format
== c
) {
1315 size
= ((size
+ e
->alignment
- 1)
1324 /* calculate the size of a format string */
1327 prepare_s(PyStructObject
*self
)
1336 Py_ssize_t size
, len
, num
, itemsize
, x
;
1338 fmt
= PyString_AS_STRING(self
->s_format
);
1340 f
= whichtable((char **)&fmt
);
1345 while ((c
= *s
++) != '\0') {
1346 if (isspace(Py_CHARMASK(c
)))
1348 if ('0' <= c
&& c
<= '9') {
1350 while ('0' <= (c
= *s
++) && c
<= '9') {
1351 x
= num
*10 + (c
- '0');
1355 "overflow in item count");
1371 case 's': /* fall through */
1372 case 'p': len
++; break;
1374 default: len
+= num
; break;
1378 size
= align(size
, c
, e
);
1381 if (x
/itemsize
!= num
|| size
< 0) {
1382 PyErr_SetString(StructError
,
1383 "total struct size too long");
1388 self
->s_size
= size
;
1390 codes
= PyMem_MALLOC((len
+ 1) * sizeof(formatcode
));
1391 if (codes
== NULL
) {
1395 self
->s_codes
= codes
;
1399 while ((c
= *s
++) != '\0') {
1400 if (isspace(Py_CHARMASK(c
)))
1402 if ('0' <= c
&& c
<= '9') {
1404 while ('0' <= (c
= *s
++) && c
<= '9')
1405 num
= num
*10 + (c
- '0');
1414 size
= align(size
, c
, e
);
1415 if (c
== 's' || c
== 'p') {
1416 codes
->offset
= size
;
1421 } else if (c
== 'x') {
1424 while (--num
>= 0) {
1425 codes
->offset
= size
;
1426 codes
->size
= e
->size
;
1433 codes
->fmtdef
= NULL
;
1434 codes
->offset
= size
;
1441 s_new(PyTypeObject
*type
, PyObject
*args
, PyObject
*kwds
)
1445 assert(type
!= NULL
&& type
->tp_alloc
!= NULL
);
1447 self
= type
->tp_alloc(type
, 0);
1449 PyStructObject
*s
= (PyStructObject
*)self
;
1451 s
->s_format
= Py_None
;
1460 s_init(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1462 PyStructObject
*soself
= (PyStructObject
*)self
;
1463 PyObject
*o_format
= NULL
;
1465 static char *kwlist
[] = {"format", 0};
1467 assert(PyStruct_Check(self
));
1469 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, "S:Struct", kwlist
,
1473 Py_INCREF(o_format
);
1474 Py_CLEAR(soself
->s_format
);
1475 soself
->s_format
= o_format
;
1477 ret
= prepare_s(soself
);
1482 s_dealloc(PyStructObject
*s
)
1484 if (s
->weakreflist
!= NULL
)
1485 PyObject_ClearWeakRefs((PyObject
*)s
);
1486 if (s
->s_codes
!= NULL
) {
1487 PyMem_FREE(s
->s_codes
);
1489 Py_XDECREF(s
->s_format
);
1490 Py_TYPE(s
)->tp_free((PyObject
*)s
);
1494 s_unpack_internal(PyStructObject
*soself
, char *startfrom
) {
1497 PyObject
*result
= PyTuple_New(soself
->s_len
);
1501 for (code
= soself
->s_codes
; code
->fmtdef
!= NULL
; code
++) {
1503 const formatdef
*e
= code
->fmtdef
;
1504 const char *res
= startfrom
+ code
->offset
;
1505 if (e
->format
== 's') {
1506 v
= PyString_FromStringAndSize(res
, code
->size
);
1507 } else if (e
->format
== 'p') {
1508 Py_ssize_t n
= *(unsigned char*)res
;
1509 if (n
>= code
->size
)
1511 v
= PyString_FromStringAndSize(res
+ 1, n
);
1513 v
= e
->unpack(res
, e
);
1517 PyTuple_SET_ITEM(result
, i
++, v
);
1527 PyDoc_STRVAR(s_unpack__doc__
,
1528 "S.unpack(str) -> (v1, v2, ...)\n\
1530 Return tuple containing values unpacked according to this Struct's format.\n\
1531 Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1535 s_unpack(PyObject
*self
, PyObject
*inputstr
)
1539 PyObject
*args
=NULL
, *result
;
1540 PyStructObject
*soself
= (PyStructObject
*)self
;
1541 assert(PyStruct_Check(self
));
1542 assert(soself
->s_codes
!= NULL
);
1543 if (inputstr
== NULL
)
1545 if (PyString_Check(inputstr
) &&
1546 PyString_GET_SIZE(inputstr
) == soself
->s_size
) {
1547 return s_unpack_internal(soself
, PyString_AS_STRING(inputstr
));
1549 args
= PyTuple_Pack(1, inputstr
);
1552 if (!PyArg_ParseTuple(args
, "s#:unpack", &start
, &len
))
1554 if (soself
->s_size
!= len
)
1556 result
= s_unpack_internal(soself
, start
);
1562 PyErr_Format(StructError
,
1563 "unpack requires a string argument of length %zd",
1568 PyDoc_STRVAR(s_unpack_from__doc__
,
1569 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
1571 Return tuple containing values unpacked according to this Struct's format.\n\
1572 Unlike unpack, unpack_from can unpack values from any object supporting\n\
1573 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1574 See struct.__doc__ for more on format strings.");
1577 s_unpack_from(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1579 static char *kwlist
[] = {"buffer", "offset", 0};
1580 #if (PY_VERSION_HEX < 0x02050000)
1581 static char *fmt
= "z#|i:unpack_from";
1583 static char *fmt
= "z#|n:unpack_from";
1585 Py_ssize_t buffer_len
= 0, offset
= 0;
1586 char *buffer
= NULL
;
1587 PyStructObject
*soself
= (PyStructObject
*)self
;
1588 assert(PyStruct_Check(self
));
1589 assert(soself
->s_codes
!= NULL
);
1591 if (!PyArg_ParseTupleAndKeywords(args
, kwds
, fmt
, kwlist
,
1592 &buffer
, &buffer_len
, &offset
))
1595 if (buffer
== NULL
) {
1596 PyErr_Format(StructError
,
1597 "unpack_from requires a buffer argument");
1602 offset
+= buffer_len
;
1604 if (offset
< 0 || (buffer_len
- offset
) < soself
->s_size
) {
1605 PyErr_Format(StructError
,
1606 "unpack_from requires a buffer of at least %zd bytes",
1610 return s_unpack_internal(soself
, buffer
+ offset
);
1615 * Guts of the pack function.
1617 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1618 * argument for where to start processing the arguments for packing, and a
1619 * character buffer for writing the packed string. The caller must insure
1620 * that the buffer may contain the required length for packing the arguments.
1621 * 0 is returned on success, 1 is returned if there is an error.
1625 s_pack_internal(PyStructObject
*soself
, PyObject
*args
, int offset
, char* buf
)
1628 /* XXX(nnorwitz): why does i need to be a local? can we use
1629 the offset parameter or do we need the wider width? */
1632 memset(buf
, '\0', soself
->s_size
);
1634 for (code
= soself
->s_codes
; code
->fmtdef
!= NULL
; code
++) {
1636 PyObject
*v
= PyTuple_GET_ITEM(args
, i
++);
1637 const formatdef
*e
= code
->fmtdef
;
1638 char *res
= buf
+ code
->offset
;
1639 if (e
->format
== 's') {
1640 if (!PyString_Check(v
)) {
1641 PyErr_SetString(StructError
,
1642 "argument for 's' must be a string");
1645 n
= PyString_GET_SIZE(v
);
1649 memcpy(res
, PyString_AS_STRING(v
), n
);
1650 } else if (e
->format
== 'p') {
1651 if (!PyString_Check(v
)) {
1652 PyErr_SetString(StructError
,
1653 "argument for 'p' must be a string");
1656 n
= PyString_GET_SIZE(v
);
1657 if (n
> (code
->size
- 1))
1660 memcpy(res
+ 1, PyString_AS_STRING(v
), n
);
1663 *res
= Py_SAFE_DOWNCAST(n
, Py_ssize_t
, unsigned char);
1665 if (e
->pack(res
, v
, e
) < 0) {
1666 if (PyLong_Check(v
) && PyErr_ExceptionMatches(PyExc_OverflowError
))
1667 PyErr_SetString(StructError
,
1668 "long too large to convert to int");
1679 PyDoc_STRVAR(s_pack__doc__
,
1680 "S.pack(v1, v2, ...) -> string\n\
1682 Return a string containing values v1, v2, ... packed according to this\n\
1683 Struct's format. See struct.__doc__ for more on format strings.");
1686 s_pack(PyObject
*self
, PyObject
*args
)
1688 PyStructObject
*soself
;
1691 /* Validate arguments. */
1692 soself
= (PyStructObject
*)self
;
1693 assert(PyStruct_Check(self
));
1694 assert(soself
->s_codes
!= NULL
);
1695 if (PyTuple_GET_SIZE(args
) != soself
->s_len
)
1697 PyErr_Format(StructError
,
1698 "pack requires exactly %zd arguments", soself
->s_len
);
1702 /* Allocate a new string */
1703 result
= PyString_FromStringAndSize((char *)NULL
, soself
->s_size
);
1708 if ( s_pack_internal(soself
, args
, 0, PyString_AS_STRING(result
)) != 0 ) {
1716 PyDoc_STRVAR(s_pack_into__doc__
,
1717 "S.pack_into(buffer, offset, v1, v2, ...)\n\
1719 Pack the values v1, v2, ... according to this Struct's format, write \n\
1720 the packed bytes into the writable buffer buf starting at offset. Note\n\
1721 that the offset is not an optional argument. See struct.__doc__ for \n\
1722 more on format strings.");
1725 s_pack_into(PyObject
*self
, PyObject
*args
)
1727 PyStructObject
*soself
;
1729 Py_ssize_t buffer_len
, offset
;
1731 /* Validate arguments. +1 is for the first arg as buffer. */
1732 soself
= (PyStructObject
*)self
;
1733 assert(PyStruct_Check(self
));
1734 assert(soself
->s_codes
!= NULL
);
1735 if (PyTuple_GET_SIZE(args
) != (soself
->s_len
+ 2))
1737 PyErr_Format(StructError
,
1738 "pack_into requires exactly %zd arguments",
1739 (soself
->s_len
+ 2));
1743 /* Extract a writable memory buffer from the first argument */
1744 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args
, 0),
1745 (void**)&buffer
, &buffer_len
) == -1 ) {
1748 assert( buffer_len
>= 0 );
1750 /* Extract the offset from the first argument */
1751 offset
= PyInt_AsSsize_t(PyTuple_GET_ITEM(args
, 1));
1753 /* Support negative offsets. */
1755 offset
+= buffer_len
;
1757 /* Check boundaries */
1758 if (offset
< 0 || (buffer_len
- offset
) < soself
->s_size
) {
1759 PyErr_Format(StructError
,
1760 "pack_into requires a buffer of at least %zd bytes",
1766 if ( s_pack_internal(soself
, args
, 2, buffer
+ offset
) != 0 ) {
1774 s_get_format(PyStructObject
*self
, void *unused
)
1776 Py_INCREF(self
->s_format
);
1777 return self
->s_format
;
1781 s_get_size(PyStructObject
*self
, void *unused
)
1783 return PyInt_FromSsize_t(self
->s_size
);
1786 /* List of functions */
1788 static struct PyMethodDef s_methods
[] = {
1789 {"pack", s_pack
, METH_VARARGS
, s_pack__doc__
},
1790 {"pack_into", s_pack_into
, METH_VARARGS
, s_pack_into__doc__
},
1791 {"unpack", s_unpack
, METH_O
, s_unpack__doc__
},
1792 {"unpack_from", (PyCFunction
)s_unpack_from
, METH_VARARGS
|METH_KEYWORDS
,
1793 s_unpack_from__doc__
},
1794 {NULL
, NULL
} /* sentinel */
1797 PyDoc_STRVAR(s__doc__
, "Compiled struct object");
1799 #define OFF(x) offsetof(PyStructObject, x)
1801 static PyGetSetDef s_getsetlist
[] = {
1802 {"format", (getter
)s_get_format
, (setter
)NULL
, "struct format string", NULL
},
1803 {"size", (getter
)s_get_size
, (setter
)NULL
, "struct size in bytes", NULL
},
1804 {NULL
} /* sentinel */
1808 PyTypeObject PyStructType
= {
1809 PyVarObject_HEAD_INIT(NULL
, 0)
1811 sizeof(PyStructObject
),
1813 (destructor
)s_dealloc
, /* tp_dealloc */
1819 0, /* tp_as_number */
1820 0, /* tp_as_sequence */
1821 0, /* tp_as_mapping */
1825 PyObject_GenericGetAttr
, /* tp_getattro */
1826 PyObject_GenericSetAttr
, /* tp_setattro */
1827 0, /* tp_as_buffer */
1828 Py_TPFLAGS_DEFAULT
| Py_TPFLAGS_BASETYPE
| Py_TPFLAGS_HAVE_WEAKREFS
,/* tp_flags */
1829 s__doc__
, /* tp_doc */
1830 0, /* tp_traverse */
1832 0, /* tp_richcompare */
1833 offsetof(PyStructObject
, weakreflist
), /* tp_weaklistoffset */
1835 0, /* tp_iternext */
1836 s_methods
, /* tp_methods */
1837 NULL
, /* tp_members */
1838 s_getsetlist
, /* tp_getset */
1841 0, /* tp_descr_get */
1842 0, /* tp_descr_set */
1843 0, /* tp_dictoffset */
1844 s_init
, /* tp_init */
1845 PyType_GenericAlloc
,/* tp_alloc */
1847 PyObject_Del
, /* tp_free */
1851 /* ---- Standalone functions ---- */
1853 #define MAXCACHE 100
1854 static PyObject
*cache
= NULL
;
1857 cache_struct(PyObject
*fmt
)
1859 PyObject
* s_object
;
1861 if (cache
== NULL
) {
1862 cache
= PyDict_New();
1867 s_object
= PyDict_GetItem(cache
, fmt
);
1868 if (s_object
!= NULL
) {
1869 Py_INCREF(s_object
);
1873 s_object
= PyObject_CallFunctionObjArgs((PyObject
*)(&PyStructType
), fmt
, NULL
);
1874 if (s_object
!= NULL
) {
1875 if (PyDict_Size(cache
) >= MAXCACHE
)
1876 PyDict_Clear(cache
);
1877 /* Attempt to cache the result */
1878 if (PyDict_SetItem(cache
, fmt
, s_object
) == -1)
1884 PyDoc_STRVAR(clearcache_doc
,
1885 "Clear the internal cache.");
1888 clearcache(PyObject
*self
)
1894 PyDoc_STRVAR(calcsize_doc
,
1895 "Return size of C struct described by format string fmt.");
1898 calcsize(PyObject
*self
, PyObject
*fmt
)
1901 PyObject
*s_object
= cache_struct(fmt
);
1902 if (s_object
== NULL
)
1904 n
= ((PyStructObject
*)s_object
)->s_size
;
1905 Py_DECREF(s_object
);
1906 return PyInt_FromSsize_t(n
);
1909 PyDoc_STRVAR(pack_doc
,
1910 "Return string containing values v1, v2, ... packed according to fmt.");
1913 pack(PyObject
*self
, PyObject
*args
)
1915 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1916 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1919 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1922 fmt
= PyTuple_GET_ITEM(args
, 0);
1923 newargs
= PyTuple_GetSlice(args
, 1, n
);
1924 if (newargs
== NULL
)
1927 s_object
= cache_struct(fmt
);
1928 if (s_object
== NULL
) {
1932 result
= s_pack(s_object
, newargs
);
1934 Py_DECREF(s_object
);
1938 PyDoc_STRVAR(pack_into_doc
,
1939 "Pack the values v1, v2, ... according to fmt.\n\
1940 Write the packed bytes into the writable buffer buf starting at offset.");
1943 pack_into(PyObject
*self
, PyObject
*args
)
1945 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1946 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1949 PyErr_SetString(PyExc_TypeError
, "missing format argument");
1952 fmt
= PyTuple_GET_ITEM(args
, 0);
1953 newargs
= PyTuple_GetSlice(args
, 1, n
);
1954 if (newargs
== NULL
)
1957 s_object
= cache_struct(fmt
);
1958 if (s_object
== NULL
) {
1962 result
= s_pack_into(s_object
, newargs
);
1964 Py_DECREF(s_object
);
1968 PyDoc_STRVAR(unpack_doc
,
1969 "Unpack the string containing packed C structure data, according to fmt.\n\
1970 Requires len(string) == calcsize(fmt).");
1973 unpack(PyObject
*self
, PyObject
*args
)
1975 PyObject
*s_object
, *fmt
, *inputstr
, *result
;
1977 if (!PyArg_UnpackTuple(args
, "unpack", 2, 2, &fmt
, &inputstr
))
1980 s_object
= cache_struct(fmt
);
1981 if (s_object
== NULL
)
1983 result
= s_unpack(s_object
, inputstr
);
1984 Py_DECREF(s_object
);
1988 PyDoc_STRVAR(unpack_from_doc
,
1989 "Unpack the buffer, containing packed C structure data, according to\n\
1990 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1993 unpack_from(PyObject
*self
, PyObject
*args
, PyObject
*kwds
)
1995 PyObject
*s_object
, *fmt
, *newargs
, *result
;
1996 Py_ssize_t n
= PyTuple_GET_SIZE(args
);
1999 PyErr_SetString(PyExc_TypeError
, "missing format argument");
2002 fmt
= PyTuple_GET_ITEM(args
, 0);
2003 newargs
= PyTuple_GetSlice(args
, 1, n
);
2004 if (newargs
== NULL
)
2007 s_object
= cache_struct(fmt
);
2008 if (s_object
== NULL
) {
2012 result
= s_unpack_from(s_object
, newargs
, kwds
);
2014 Py_DECREF(s_object
);
2018 static struct PyMethodDef module_functions
[] = {
2019 {"_clearcache", (PyCFunction
)clearcache
, METH_NOARGS
, clearcache_doc
},
2020 {"calcsize", calcsize
, METH_O
, calcsize_doc
},
2021 {"pack", pack
, METH_VARARGS
, pack_doc
},
2022 {"pack_into", pack_into
, METH_VARARGS
, pack_into_doc
},
2023 {"unpack", unpack
, METH_VARARGS
, unpack_doc
},
2024 {"unpack_from", (PyCFunction
)unpack_from
,
2025 METH_VARARGS
|METH_KEYWORDS
, unpack_from_doc
},
2026 {NULL
, NULL
} /* sentinel */
2030 /* Module initialization */
2032 PyDoc_STRVAR(module_doc
,
2033 "Functions to convert between Python values and C structs.\n\
2034 Python strings are used to hold the data representing the C struct\n\
2035 and also as format strings to describe the layout of data in the C struct.\n\
2037 The optional first format char indicates byte order, size and alignment:\n\
2038 @: native order, size & alignment (default)\n\
2039 =: native order, std. size & alignment\n\
2040 <: little-endian, std. size & alignment\n\
2041 >: big-endian, std. size & alignment\n\
2044 The remaining chars indicate types of args and must match exactly;\n\
2045 these can be preceded by a decimal repeat count:\n\
2046 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
2047 h:short; H:unsigned short; i:int; I:unsigned int;\n\
2048 l:long; L:unsigned long; f:float; d:double.\n\
2049 Special cases (preceding decimal count indicates length):\n\
2050 s:string (array of char); p: pascal string (with count byte).\n\
2051 Special case (only available in native format):\n\
2052 P:an integer type that is wide enough to hold a pointer.\n\
2053 Special case (not in native mode unless 'long long' in platform C):\n\
2054 q:long long; Q:unsigned long long\n\
2055 Whitespace between formats is ignored.\n\
2057 The variable struct.error is an exception raised on errors.\n");
2064 ver
= PyString_FromString("0.2");
2068 m
= Py_InitModule3("_struct", module_functions
, module_doc
);
2072 Py_TYPE(&PyStructType
) = &PyType_Type
;
2073 if (PyType_Ready(&PyStructType
) < 0)
2076 #ifdef PY_STRUCT_OVERFLOW_MASKING
2077 if (pyint_zero
== NULL
) {
2078 pyint_zero
= PyInt_FromLong(0);
2079 if (pyint_zero
== NULL
)
2082 if (pylong_ulong_mask
== NULL
) {
2083 #if (SIZEOF_LONG == 4)
2084 pylong_ulong_mask
= PyLong_FromString("FFFFFFFF", NULL
, 16);
2086 pylong_ulong_mask
= PyLong_FromString("FFFFFFFFFFFFFFFF", NULL
, 16);
2088 if (pylong_ulong_mask
== NULL
)
2093 /* This speed trick can't be used until overflow masking goes away, because
2094 native endian always raises exceptions instead of overflow masking. */
2096 /* Check endian and swap in faster functions */
2099 formatdef
*native
= native_table
;
2100 formatdef
*other
, *ptr
;
2101 if ((int)*(unsigned char*)&one
)
2102 other
= lilendian_table
;
2104 other
= bigendian_table
;
2105 /* Scan through the native table, find a matching
2106 entry in the endian table and swap in the
2107 native implementations whenever possible
2108 (64-bit platforms may not have "standard" sizes) */
2109 while (native
->format
!= '\0' && other
->format
!= '\0') {
2111 while (ptr
->format
!= '\0') {
2112 if (ptr
->format
== native
->format
) {
2113 /* Match faster when formats are
2114 listed in the same order */
2117 /* Only use the trick if the
2119 if (ptr
->size
!= native
->size
)
2121 /* Skip float and double, could be
2122 "unknown" float format */
2123 if (ptr
->format
== 'd' || ptr
->format
== 'f')
2125 ptr
->pack
= native
->pack
;
2126 ptr
->unpack
= native
->unpack
;
2136 /* Add some symbolic constants to the module */
2137 if (StructError
== NULL
) {
2138 StructError
= PyErr_NewException("struct.error", NULL
, NULL
);
2139 if (StructError
== NULL
)
2143 Py_INCREF(StructError
);
2144 PyModule_AddObject(m
, "error", StructError
);
2146 Py_INCREF((PyObject
*)&PyStructType
);
2147 PyModule_AddObject(m
, "Struct", (PyObject
*)&PyStructType
);
2149 PyModule_AddObject(m
, "__version__", ver
);
2151 PyModule_AddIntConstant(m
, "_PY_STRUCT_RANGE_CHECKING", 1);
2152 #ifdef PY_STRUCT_OVERFLOW_MASKING
2153 PyModule_AddIntConstant(m
, "_PY_STRUCT_OVERFLOW_MASKING", 1);
2155 #ifdef PY_STRUCT_FLOAT_COERCE
2156 PyModule_AddIntConstant(m
, "_PY_STRUCT_FLOAT_COERCE", 1);