blob: fe54a47267bfeca7d54a3a02eb18480ad5da0d60 [file] [log] [blame]
Bob Ippolito232f3c92006-05-23 19:12:41 +00001/* struct module -- pack values into and (out of) strings */
2
3/* New version supporting byte order, alignment and size options,
4 character strings, and unsigned numbers */
5
Bob Ippolitoaa70a172006-05-26 20:25:23 +00006#define PY_SSIZE_T_CLEAN
7
Bob Ippolito232f3c92006-05-23 19:12:41 +00008#include "Python.h"
9#include "structseq.h"
10#include "structmember.h"
11#include <ctype.h>
12
Bob Ippolitod3611eb2006-05-23 19:31:23 +000013static PyTypeObject PyStructType;
Bob Ippolito232f3c92006-05-23 19:12:41 +000014
15/* compatibility macros */
16#if (PY_VERSION_HEX < 0x02050000)
17typedef int Py_ssize_t;
18#endif
19
Mark Dickinson154b7ad2010-03-07 16:24:45 +000020/* 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)"
Bob Ippolitoe6c9f982006-08-04 23:59:21 +000024
25
Bob Ippolito232f3c92006-05-23 19:12:41 +000026/* The translation function for each format character is table driven */
Bob Ippolito232f3c92006-05-23 19:12:41 +000027typedef struct _formatdef {
28 char format;
Bob Ippolitoaa70a172006-05-26 20:25:23 +000029 Py_ssize_t size;
30 Py_ssize_t alignment;
Bob Ippolito232f3c92006-05-23 19:12:41 +000031 PyObject* (*unpack)(const char *,
32 const struct _formatdef *);
33 int (*pack)(char *, PyObject *,
34 const struct _formatdef *);
35} formatdef;
36
37typedef struct _formatcode {
38 const struct _formatdef *fmtdef;
Bob Ippolitoaa70a172006-05-26 20:25:23 +000039 Py_ssize_t offset;
40 Py_ssize_t size;
Bob Ippolito232f3c92006-05-23 19:12:41 +000041} formatcode;
42
43/* Struct object interface */
44
45typedef struct {
46 PyObject_HEAD
Bob Ippolitoaa70a172006-05-26 20:25:23 +000047 Py_ssize_t s_size;
48 Py_ssize_t s_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +000049 formatcode *s_codes;
50 PyObject *s_format;
51 PyObject *weakreflist; /* List of weak references */
52} PyStructObject;
53
Bob Ippolitoeb621272006-05-24 15:32:06 +000054
Bob Ippolito07c023b2006-05-23 19:32:25 +000055#define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType)
Christian Heimese93237d2007-12-19 02:37:44 +000056#define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType)
Bob Ippolito232f3c92006-05-23 19:12:41 +000057
58
59/* Exception */
60
61static PyObject *StructError;
62
63
64/* Define various structs to figure out the alignments of types */
65
66
67typedef struct { char c; short x; } st_short;
68typedef struct { char c; int x; } st_int;
69typedef struct { char c; long x; } st_long;
70typedef struct { char c; float x; } st_float;
71typedef struct { char c; double x; } st_double;
72typedef struct { char c; void *x; } st_void_p;
73
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 *))
80
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. */
83#ifdef HAVE_LONG_LONG
84typedef struct { char c; PY_LONG_LONG x; } s_long_long;
85#define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
86#endif
87
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +000088#ifdef HAVE_C99_BOOL
89#define BOOL_TYPE _Bool
90typedef struct { char c; _Bool x; } s_bool;
91#define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE))
92#else
93#define BOOL_TYPE char
94#define BOOL_ALIGN 0
95#endif
96
Bob Ippolito232f3c92006-05-23 19:12:41 +000097#define STRINGIFY(x) #x
98
99#ifdef __powerc
100#pragma options align=reset
101#endif
102
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000103static char *integer_codes = "bBhHiIlLqQ";
104
Bob Ippolito232f3c92006-05-23 19:12:41 +0000105/* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
106
107static PyObject *
108get_pylong(PyObject *v)
109{
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000110 PyObject *r, *w;
111 int converted = 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000112 assert(v != NULL);
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000113 if (!PyInt_Check(v) && !PyLong_Check(v)) {
114 PyNumberMethods *m;
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000115 /* 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
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000119 Python 3.x. */
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000120
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000121 m = Py_TYPE(v)->tp_as_number;
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000122 if (PyIndex_Check(v)) {
123 w = PyNumber_Index(v);
124 if (w != NULL) {
125 v = w;
126 if (!PyInt_Check(v) && !PyLong_Check(v)) {
127 PyErr_SetString(PyExc_TypeError,
128 "__index__ method "
129 "returned non-integer");
130 return NULL;
131 }
132 /* successfully converted to an integer */
133 converted = 1;
134 }
135 else if (PyErr_ExceptionMatches(PyExc_TypeError)) {
136 PyErr_Clear();
137 }
138 else
139 return NULL;
140 }
141 if (!converted && m != NULL && m->nb_int != NULL) {
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000142 /* Special case warning message for floats, for
143 backwards compatibility. */
144 if (PyFloat_Check(v)) {
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000145 if (PyErr_WarnEx(
146 PyExc_DeprecationWarning,
147 FLOAT_COERCE_WARN, 1))
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000148 return NULL;
149 }
150 else {
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000151 if (PyErr_WarnEx(
152 PyExc_DeprecationWarning,
153 NON_INTEGER_WARN, 1))
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000154 return NULL;
155 }
156 v = m->nb_int(v);
157 if (v == NULL)
158 return NULL;
159 if (!PyInt_Check(v) && !PyLong_Check(v)) {
160 PyErr_SetString(PyExc_TypeError,
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000161 "__int__ method returned "
162 "non-integer");
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000163 return NULL;
164 }
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000165 converted = 1;
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000166 }
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000167 if (!converted) {
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000168 PyErr_SetString(StructError,
Mark Dickinson4846a8e2010-04-03 14:05:10 +0000169 "cannot convert argument "
170 "to integer");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000171 return NULL;
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000172 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000173 }
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000174 else
175 /* Ensure we own a reference to v. */
176 Py_INCREF(v);
177
178 if (PyInt_Check(v)) {
179 r = PyLong_FromLong(PyInt_AS_LONG(v));
180 Py_DECREF(v);
181 }
182 else if (PyLong_Check(v)) {
183 assert(PyLong_Check(v));
184 r = v;
185 }
Mark Dickinson2db56872010-03-07 17:10:19 +0000186 else {
187 r = NULL; /* silence compiler warning about
188 possibly uninitialized variable */
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000189 assert(0); /* shouldn't ever get here */
Mark Dickinson2db56872010-03-07 17:10:19 +0000190 }
Mark Dickinson154b7ad2010-03-07 16:24:45 +0000191
192 return r;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000193}
194
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000195/* Helper to convert a Python object to a C long. Sets an exception
196 (struct.error for an inconvertible type, OverflowError for
197 out-of-range values) and returns -1 on error. */
Bob Ippolito232f3c92006-05-23 19:12:41 +0000198
199static int
200get_long(PyObject *v, long *p)
201{
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000202 long x;
203
204 v = get_pylong(v);
205 if (v == NULL)
206 return -1;
207 assert(PyLong_Check(v));
208 x = PyLong_AsLong(v);
209 Py_DECREF(v);
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000210 if (x == (long)-1 && PyErr_Occurred())
Bob Ippolito232f3c92006-05-23 19:12:41 +0000211 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000212 *p = x;
213 return 0;
214}
215
Bob Ippolito232f3c92006-05-23 19:12:41 +0000216/* Same, but handling unsigned long */
217
218static int
219get_ulong(PyObject *v, unsigned long *p)
220{
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000221 unsigned long x;
222
223 v = get_pylong(v);
224 if (v == NULL)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000225 return -1;
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000226 assert(PyLong_Check(v));
227 x = PyLong_AsUnsignedLong(v);
228 Py_DECREF(v);
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000229 if (x == (unsigned long)-1 && PyErr_Occurred())
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000230 return -1;
Mark Dickinson463dc4b2009-07-05 10:01:24 +0000231 *p = x;
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000232 return 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000233}
234
235#ifdef HAVE_LONG_LONG
236
237/* Same, but handling native long long. */
238
239static int
240get_longlong(PyObject *v, PY_LONG_LONG *p)
241{
242 PY_LONG_LONG x;
243
244 v = get_pylong(v);
245 if (v == NULL)
246 return -1;
247 assert(PyLong_Check(v));
248 x = PyLong_AsLongLong(v);
249 Py_DECREF(v);
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000250 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
Bob Ippolito232f3c92006-05-23 19:12:41 +0000251 return -1;
252 *p = x;
253 return 0;
254}
255
256/* Same, but handling native unsigned long long. */
257
258static int
259get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
260{
261 unsigned PY_LONG_LONG x;
262
263 v = get_pylong(v);
264 if (v == NULL)
265 return -1;
266 assert(PyLong_Check(v));
267 x = PyLong_AsUnsignedLongLong(v);
268 Py_DECREF(v);
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000269 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
Bob Ippolito232f3c92006-05-23 19:12:41 +0000270 return -1;
271 *p = x;
272 return 0;
273}
274
275#endif
276
277/* Floating point helpers */
278
279static PyObject *
280unpack_float(const char *p, /* start of 4-byte string */
281 int le) /* true for little-endian, false for big-endian */
282{
283 double x;
284
285 x = _PyFloat_Unpack4((unsigned char *)p, le);
286 if (x == -1.0 && PyErr_Occurred())
287 return NULL;
288 return PyFloat_FromDouble(x);
289}
290
291static PyObject *
292unpack_double(const char *p, /* start of 8-byte string */
293 int le) /* true for little-endian, false for big-endian */
294{
295 double x;
296
297 x = _PyFloat_Unpack8((unsigned char *)p, le);
298 if (x == -1.0 && PyErr_Occurred())
299 return NULL;
300 return PyFloat_FromDouble(x);
301}
302
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000303/* Helper to format the range error exceptions */
304static int
Armin Rigo162997e2006-05-29 17:59:47 +0000305_range_error(const formatdef *f, int is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000306{
Tim Petersd6a6f022006-05-31 15:33:22 +0000307 /* ulargest is the largest unsigned value with f->size bytes.
308 * Note that the simpler:
309 * ((size_t)1 << (f->size * 8)) - 1
Tim Peters72270c22006-05-31 15:34:37 +0000310 * doesn't work when f->size == sizeof(size_t) because C doesn't
311 * define what happens when a left shift count is >= the number of
312 * bits in the integer being shifted; e.g., on some boxes it doesn't
313 * shift at all when they're equal.
Tim Petersd6a6f022006-05-31 15:33:22 +0000314 */
315 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8);
316 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T);
317 if (is_unsigned)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000318 PyErr_Format(StructError,
Neal Norwitz971ea112006-05-31 07:43:27 +0000319 "'%c' format requires 0 <= number <= %zu",
Bob Ippolito28b26862006-05-29 15:47:29 +0000320 f->format,
Tim Petersd6a6f022006-05-31 15:33:22 +0000321 ulargest);
322 else {
323 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1);
324 PyErr_Format(StructError,
325 "'%c' format requires %zd <= number <= %zd",
326 f->format,
327 ~ largest,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000328 largest);
329 }
330 return -1;
331}
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000332
333
Bob Ippolito232f3c92006-05-23 19:12:41 +0000334
335/* A large number of small routines follow, with names of the form
336
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000337 [bln][up]_TYPE
Bob Ippolito232f3c92006-05-23 19:12:41 +0000338
339 [bln] distiguishes among big-endian, little-endian and native.
340 [pu] distiguishes between pack (to struct) and unpack (from struct).
341 TYPE is one of char, byte, ubyte, etc.
342*/
343
344/* Native mode routines. ****************************************************/
345/* NOTE:
346 In all n[up]_<type> routines handling types larger than 1 byte, there is
347 *no* guarantee that the p pointer is properly aligned for each type,
348 therefore memcpy is called. An intermediate variable is used to
349 compensate for big-endian architectures.
350 Normally both the intermediate variable and the memcpy call will be
351 skipped by C optimisation in little-endian architectures (gcc >= 2.91
352 does this). */
353
354static PyObject *
355nu_char(const char *p, const formatdef *f)
356{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000357 return PyString_FromStringAndSize(p, 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000358}
359
360static PyObject *
361nu_byte(const char *p, const formatdef *f)
362{
363 return PyInt_FromLong((long) *(signed char *)p);
364}
365
366static PyObject *
367nu_ubyte(const char *p, const formatdef *f)
368{
369 return PyInt_FromLong((long) *(unsigned char *)p);
370}
371
372static PyObject *
373nu_short(const char *p, const formatdef *f)
374{
375 short x;
376 memcpy((char *)&x, p, sizeof x);
377 return PyInt_FromLong((long)x);
378}
379
380static PyObject *
381nu_ushort(const char *p, const formatdef *f)
382{
383 unsigned short x;
384 memcpy((char *)&x, p, sizeof x);
385 return PyInt_FromLong((long)x);
386}
387
388static PyObject *
389nu_int(const char *p, const formatdef *f)
390{
391 int x;
392 memcpy((char *)&x, p, sizeof x);
393 return PyInt_FromLong((long)x);
394}
395
396static PyObject *
397nu_uint(const char *p, const formatdef *f)
398{
399 unsigned int x;
400 memcpy((char *)&x, p, sizeof x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000401#if (SIZEOF_LONG > SIZEOF_INT)
402 return PyInt_FromLong((long)x);
403#else
404 if (x <= ((unsigned int)LONG_MAX))
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000405 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000406 return PyLong_FromUnsignedLong((unsigned long)x);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000407#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000408}
409
410static PyObject *
411nu_long(const char *p, const formatdef *f)
412{
413 long x;
414 memcpy((char *)&x, p, sizeof x);
415 return PyInt_FromLong(x);
416}
417
418static PyObject *
419nu_ulong(const char *p, const formatdef *f)
420{
421 unsigned long x;
422 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000423 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000424 return PyInt_FromLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000425 return PyLong_FromUnsignedLong(x);
426}
427
428/* Native mode doesn't support q or Q unless the platform C supports
429 long long (or, on Windows, __int64). */
430
431#ifdef HAVE_LONG_LONG
432
433static PyObject *
434nu_longlong(const char *p, const formatdef *f)
435{
436 PY_LONG_LONG x;
437 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000438 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000439 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000440 return PyLong_FromLongLong(x);
441}
442
443static PyObject *
444nu_ulonglong(const char *p, const formatdef *f)
445{
446 unsigned PY_LONG_LONG x;
447 memcpy((char *)&x, p, sizeof x);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000448 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000449 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000450 return PyLong_FromUnsignedLongLong(x);
451}
452
453#endif
454
455static PyObject *
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000456nu_bool(const char *p, const formatdef *f)
457{
458 BOOL_TYPE x;
459 memcpy((char *)&x, p, sizeof x);
460 return PyBool_FromLong(x != 0);
461}
462
463
464static PyObject *
Bob Ippolito232f3c92006-05-23 19:12:41 +0000465nu_float(const char *p, const formatdef *f)
466{
467 float x;
468 memcpy((char *)&x, p, sizeof x);
469 return PyFloat_FromDouble((double)x);
470}
471
472static PyObject *
473nu_double(const char *p, const formatdef *f)
474{
475 double x;
476 memcpy((char *)&x, p, sizeof x);
477 return PyFloat_FromDouble(x);
478}
479
480static PyObject *
481nu_void_p(const char *p, const formatdef *f)
482{
483 void *x;
484 memcpy((char *)&x, p, sizeof x);
485 return PyLong_FromVoidPtr(x);
486}
487
488static int
489np_byte(char *p, PyObject *v, const formatdef *f)
490{
491 long x;
492 if (get_long(v, &x) < 0)
493 return -1;
494 if (x < -128 || x > 127){
495 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000496 "byte format requires -128 <= number <= 127");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000497 return -1;
498 }
499 *p = (char)x;
500 return 0;
501}
502
503static int
504np_ubyte(char *p, PyObject *v, const formatdef *f)
505{
506 long x;
507 if (get_long(v, &x) < 0)
508 return -1;
509 if (x < 0 || x > 255){
510 PyErr_SetString(StructError,
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000511 "ubyte format requires 0 <= number <= 255");
Bob Ippolito232f3c92006-05-23 19:12:41 +0000512 return -1;
513 }
514 *p = (char)x;
515 return 0;
516}
517
518static int
519np_char(char *p, PyObject *v, const formatdef *f)
520{
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000521 if (!PyString_Check(v) || PyString_Size(v) != 1) {
Bob Ippolito232f3c92006-05-23 19:12:41 +0000522 PyErr_SetString(StructError,
523 "char format require string of length 1");
524 return -1;
525 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +0000526 *p = *PyString_AsString(v);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000527 return 0;
528}
529
530static int
531np_short(char *p, PyObject *v, const formatdef *f)
532{
533 long x;
534 short y;
535 if (get_long(v, &x) < 0)
536 return -1;
537 if (x < SHRT_MIN || x > SHRT_MAX){
538 PyErr_SetString(StructError,
539 "short format requires " STRINGIFY(SHRT_MIN)
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000540 " <= number <= " STRINGIFY(SHRT_MAX));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000541 return -1;
542 }
543 y = (short)x;
544 memcpy(p, (char *)&y, sizeof y);
545 return 0;
546}
547
548static int
549np_ushort(char *p, PyObject *v, const formatdef *f)
550{
551 long x;
552 unsigned short y;
553 if (get_long(v, &x) < 0)
554 return -1;
555 if (x < 0 || x > USHRT_MAX){
556 PyErr_SetString(StructError,
Mark Dickinson24766ba2009-07-07 10:18:22 +0000557 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000558 return -1;
559 }
560 y = (unsigned short)x;
561 memcpy(p, (char *)&y, sizeof y);
562 return 0;
563}
564
565static int
566np_int(char *p, PyObject *v, const formatdef *f)
567{
568 long x;
569 int y;
570 if (get_long(v, &x) < 0)
571 return -1;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000572#if (SIZEOF_LONG > SIZEOF_INT)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000573 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX)))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000574 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000575#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000576 y = (int)x;
577 memcpy(p, (char *)&y, sizeof y);
578 return 0;
579}
580
581static int
582np_uint(char *p, PyObject *v, const formatdef *f)
583{
584 unsigned long x;
585 unsigned int y;
Mark Dickinson716a9cc2009-09-27 16:39:28 +0000586 if (get_ulong(v, &x) < 0)
Georg Brandl6269fec2009-01-01 12:15:31 +0000587 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000588 y = (unsigned int)x;
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000589#if (SIZEOF_LONG > SIZEOF_INT)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000590 if (x > ((unsigned long)UINT_MAX))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000591 return _range_error(f, 1);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000592#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000593 memcpy(p, (char *)&y, sizeof y);
594 return 0;
595}
596
597static int
598np_long(char *p, PyObject *v, const formatdef *f)
599{
600 long x;
601 if (get_long(v, &x) < 0)
602 return -1;
603 memcpy(p, (char *)&x, sizeof x);
604 return 0;
605}
606
607static int
608np_ulong(char *p, PyObject *v, const formatdef *f)
609{
610 unsigned long x;
Mark Dickinson716a9cc2009-09-27 16:39:28 +0000611 if (get_ulong(v, &x) < 0)
Georg Brandl6269fec2009-01-01 12:15:31 +0000612 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000613 memcpy(p, (char *)&x, sizeof x);
614 return 0;
615}
616
617#ifdef HAVE_LONG_LONG
618
619static int
620np_longlong(char *p, PyObject *v, const formatdef *f)
621{
622 PY_LONG_LONG x;
623 if (get_longlong(v, &x) < 0)
624 return -1;
625 memcpy(p, (char *)&x, sizeof x);
626 return 0;
627}
628
629static int
630np_ulonglong(char *p, PyObject *v, const formatdef *f)
631{
632 unsigned PY_LONG_LONG x;
633 if (get_ulonglong(v, &x) < 0)
634 return -1;
635 memcpy(p, (char *)&x, sizeof x);
636 return 0;
637}
638#endif
639
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000640
641static int
642np_bool(char *p, PyObject *v, const formatdef *f)
643{
644 BOOL_TYPE y;
645 y = PyObject_IsTrue(v);
646 memcpy(p, (char *)&y, sizeof y);
647 return 0;
648}
649
Bob Ippolito232f3c92006-05-23 19:12:41 +0000650static int
651np_float(char *p, PyObject *v, const formatdef *f)
652{
653 float x = (float)PyFloat_AsDouble(v);
654 if (x == -1 && PyErr_Occurred()) {
655 PyErr_SetString(StructError,
656 "required argument is not a float");
657 return -1;
658 }
659 memcpy(p, (char *)&x, sizeof x);
660 return 0;
661}
662
663static int
664np_double(char *p, PyObject *v, const formatdef *f)
665{
666 double x = PyFloat_AsDouble(v);
667 if (x == -1 && PyErr_Occurred()) {
668 PyErr_SetString(StructError,
669 "required argument is not a float");
670 return -1;
671 }
672 memcpy(p, (char *)&x, sizeof(double));
673 return 0;
674}
675
676static int
677np_void_p(char *p, PyObject *v, const formatdef *f)
678{
679 void *x;
680
681 v = get_pylong(v);
682 if (v == NULL)
683 return -1;
684 assert(PyLong_Check(v));
685 x = PyLong_AsVoidPtr(v);
686 Py_DECREF(v);
687 if (x == NULL && PyErr_Occurred())
688 return -1;
689 memcpy(p, (char *)&x, sizeof x);
690 return 0;
691}
692
693static formatdef native_table[] = {
694 {'x', sizeof(char), 0, NULL},
695 {'b', sizeof(char), 0, nu_byte, np_byte},
696 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
697 {'c', sizeof(char), 0, nu_char, np_char},
698 {'s', sizeof(char), 0, NULL},
699 {'p', sizeof(char), 0, NULL},
700 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
701 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
702 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
703 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
704 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
705 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000706#ifdef HAVE_LONG_LONG
707 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
708 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
709#endif
Thomas Hellerf3c05592008-03-05 15:34:29 +0000710 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool},
Bob Ippolitoa99865b2006-05-25 19:56:56 +0000711 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
712 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
713 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000714 {0}
715};
716
717/* Big-endian routines. *****************************************************/
718
719static PyObject *
720bu_int(const char *p, const formatdef *f)
721{
722 long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000723 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000724 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000725 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000726 x = (x<<8) | *bytes++;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000727 } while (--i > 0);
728 /* Extend the sign bit. */
729 if (SIZEOF_LONG > f->size)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000730 x |= -(x & (1L << ((8 * f->size) - 1)));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000731 return PyInt_FromLong(x);
732}
733
734static PyObject *
735bu_uint(const char *p, const formatdef *f)
736{
737 unsigned long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000738 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000739 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000740 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000741 x = (x<<8) | *bytes++;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000742 } while (--i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000743 if (x <= LONG_MAX)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000744 return PyInt_FromLong((long)x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000745 return PyLong_FromUnsignedLong(x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000746}
747
748static PyObject *
749bu_longlong(const char *p, const formatdef *f)
750{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000751#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000752 PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000753 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000754 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000755 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000756 x = (x<<8) | *bytes++;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000757 } while (--i > 0);
758 /* Extend the sign bit. */
759 if (SIZEOF_LONG_LONG > f->size)
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +0000760 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Bob Ippolito04ab9942006-05-25 19:33:38 +0000761 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000762 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000763 return PyLong_FromLongLong(x);
764#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000765 return _PyLong_FromByteArray((const unsigned char *)p,
766 8,
767 0, /* little-endian */
768 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000769#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000770}
771
772static PyObject *
773bu_ulonglong(const char *p, const formatdef *f)
774{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000775#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000776 unsigned PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000777 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000778 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000779 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000780 x = (x<<8) | *bytes++;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000781 } while (--i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000782 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000783 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000784 return PyLong_FromUnsignedLongLong(x);
785#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000786 return _PyLong_FromByteArray((const unsigned char *)p,
787 8,
788 0, /* little-endian */
789 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000790#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000791}
792
793static PyObject *
794bu_float(const char *p, const formatdef *f)
795{
796 return unpack_float(p, 0);
797}
798
799static PyObject *
800bu_double(const char *p, const formatdef *f)
801{
802 return unpack_double(p, 0);
803}
804
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000805static PyObject *
806bu_bool(const char *p, const formatdef *f)
807{
808 char x;
809 memcpy((char *)&x, p, sizeof x);
810 return PyBool_FromLong(x != 0);
811}
812
Bob Ippolito232f3c92006-05-23 19:12:41 +0000813static int
814bp_int(char *p, PyObject *v, const formatdef *f)
815{
816 long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000817 Py_ssize_t i;
Mark Dickinson716a9cc2009-09-27 16:39:28 +0000818 if (get_long(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000819 return -1;
820 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000821 if (i != SIZEOF_LONG) {
822 if ((i == 2) && (x < -32768 || x > 32767))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000823 return _range_error(f, 0);
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000824#if (SIZEOF_LONG != 4)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000825 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000826 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000827#endif
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000828 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000829 do {
830 p[--i] = (char)x;
831 x >>= 8;
832 } while (i > 0);
833 return 0;
834}
835
836static int
837bp_uint(char *p, PyObject *v, const formatdef *f)
838{
839 unsigned long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000840 Py_ssize_t i;
Mark Dickinson716a9cc2009-09-27 16:39:28 +0000841 if (get_ulong(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000842 return -1;
843 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000844 if (i != SIZEOF_LONG) {
845 unsigned long maxint = 1;
846 maxint <<= (unsigned long)(i * 8);
847 if (x >= maxint)
Mark Dickinson5fd3af22009-07-07 15:08:28 +0000848 return _range_error(f, 1);
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000849 }
Bob Ippolito232f3c92006-05-23 19:12:41 +0000850 do {
851 p[--i] = (char)x;
852 x >>= 8;
853 } while (i > 0);
854 return 0;
855}
856
857static int
858bp_longlong(char *p, PyObject *v, const formatdef *f)
859{
860 int res;
861 v = get_pylong(v);
862 if (v == NULL)
863 return -1;
864 res = _PyLong_AsByteArray((PyLongObject *)v,
865 (unsigned char *)p,
866 8,
867 0, /* little_endian */
868 1 /* signed */);
869 Py_DECREF(v);
870 return res;
871}
872
873static int
874bp_ulonglong(char *p, PyObject *v, const formatdef *f)
875{
876 int res;
877 v = get_pylong(v);
878 if (v == NULL)
879 return -1;
880 res = _PyLong_AsByteArray((PyLongObject *)v,
881 (unsigned char *)p,
882 8,
883 0, /* little_endian */
884 0 /* signed */);
885 Py_DECREF(v);
886 return res;
887}
888
889static int
890bp_float(char *p, PyObject *v, const formatdef *f)
891{
892 double x = PyFloat_AsDouble(v);
893 if (x == -1 && PyErr_Occurred()) {
894 PyErr_SetString(StructError,
895 "required argument is not a float");
896 return -1;
897 }
898 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
899}
900
901static int
902bp_double(char *p, PyObject *v, const formatdef *f)
903{
904 double x = PyFloat_AsDouble(v);
905 if (x == -1 && PyErr_Occurred()) {
906 PyErr_SetString(StructError,
907 "required argument is not a float");
908 return -1;
909 }
910 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
911}
912
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +0000913static int
914bp_bool(char *p, PyObject *v, const formatdef *f)
915{
916 char y;
917 y = PyObject_IsTrue(v);
918 memcpy(p, (char *)&y, sizeof y);
919 return 0;
920}
921
Bob Ippolito232f3c92006-05-23 19:12:41 +0000922static formatdef bigendian_table[] = {
923 {'x', 1, 0, NULL},
Bob Ippolitoe27337b2006-05-26 13:15:44 +0000924 {'b', 1, 0, nu_byte, np_byte},
925 {'B', 1, 0, nu_ubyte, np_ubyte},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000926 {'c', 1, 0, nu_char, np_char},
927 {'s', 1, 0, NULL},
928 {'p', 1, 0, NULL},
929 {'h', 2, 0, bu_int, bp_int},
930 {'H', 2, 0, bu_uint, bp_uint},
931 {'i', 4, 0, bu_int, bp_int},
932 {'I', 4, 0, bu_uint, bp_uint},
933 {'l', 4, 0, bu_int, bp_int},
934 {'L', 4, 0, bu_uint, bp_uint},
935 {'q', 8, 0, bu_longlong, bp_longlong},
936 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
Thomas Hellerf3c05592008-03-05 15:34:29 +0000937 {'?', 1, 0, bu_bool, bp_bool},
Bob Ippolito232f3c92006-05-23 19:12:41 +0000938 {'f', 4, 0, bu_float, bp_float},
939 {'d', 8, 0, bu_double, bp_double},
940 {0}
941};
942
943/* Little-endian routines. *****************************************************/
944
945static PyObject *
946lu_int(const char *p, const formatdef *f)
947{
948 long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000949 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000950 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000951 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000952 x = (x<<8) | bytes[--i];
Bob Ippolito232f3c92006-05-23 19:12:41 +0000953 } while (i > 0);
954 /* Extend the sign bit. */
955 if (SIZEOF_LONG > f->size)
Bob Ippolitocd51ca52006-05-27 15:53:49 +0000956 x |= -(x & (1L << ((8 * f->size) - 1)));
Bob Ippolito232f3c92006-05-23 19:12:41 +0000957 return PyInt_FromLong(x);
958}
959
960static PyObject *
961lu_uint(const char *p, const formatdef *f)
962{
963 unsigned long x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000964 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000965 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito232f3c92006-05-23 19:12:41 +0000966 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000967 x = (x<<8) | bytes[--i];
Bob Ippolito232f3c92006-05-23 19:12:41 +0000968 } while (i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +0000969 if (x <= LONG_MAX)
Bob Ippolito232f3c92006-05-23 19:12:41 +0000970 return PyInt_FromLong((long)x);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000971 return PyLong_FromUnsignedLong((long)x);
Bob Ippolito232f3c92006-05-23 19:12:41 +0000972}
973
974static PyObject *
975lu_longlong(const char *p, const formatdef *f)
976{
Bob Ippolito90bd0a52006-05-27 11:47:12 +0000977#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000978 PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +0000979 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +0000980 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000981 do {
Bob Ippolito28b26862006-05-29 15:47:29 +0000982 x = (x<<8) | bytes[--i];
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000983 } while (i > 0);
984 /* Extend the sign bit. */
985 if (SIZEOF_LONG_LONG > f->size)
Kristján Valur Jónsson67387fb2007-04-25 00:17:39 +0000986 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1)));
Bob Ippolito04ab9942006-05-25 19:33:38 +0000987 if (x >= LONG_MIN && x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000988 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000989 return PyLong_FromLongLong(x);
990#else
Bob Ippolito232f3c92006-05-23 19:12:41 +0000991 return _PyLong_FromByteArray((const unsigned char *)p,
992 8,
993 1, /* little-endian */
994 1 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +0000995#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +0000996}
997
998static PyObject *
999lu_ulonglong(const char *p, const formatdef *f)
1000{
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001001#ifdef HAVE_LONG_LONG
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001002 unsigned PY_LONG_LONG x = 0;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001003 Py_ssize_t i = f->size;
Bob Ippolito28b26862006-05-29 15:47:29 +00001004 const unsigned char *bytes = (const unsigned char *)p;
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001005 do {
Bob Ippolito28b26862006-05-29 15:47:29 +00001006 x = (x<<8) | bytes[--i];
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001007 } while (i > 0);
Bob Ippolito04ab9942006-05-25 19:33:38 +00001008 if (x <= LONG_MAX)
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001009 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long));
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001010 return PyLong_FromUnsignedLongLong(x);
1011#else
Bob Ippolito232f3c92006-05-23 19:12:41 +00001012 return _PyLong_FromByteArray((const unsigned char *)p,
1013 8,
1014 1, /* little-endian */
1015 0 /* signed */);
Bob Ippolito94f68ee2006-05-25 18:44:50 +00001016#endif
Bob Ippolito232f3c92006-05-23 19:12:41 +00001017}
1018
1019static PyObject *
1020lu_float(const char *p, const formatdef *f)
1021{
1022 return unpack_float(p, 1);
1023}
1024
1025static PyObject *
1026lu_double(const char *p, const formatdef *f)
1027{
1028 return unpack_double(p, 1);
1029}
1030
1031static int
1032lp_int(char *p, PyObject *v, const formatdef *f)
1033{
1034 long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001035 Py_ssize_t i;
Mark Dickinson716a9cc2009-09-27 16:39:28 +00001036 if (get_long(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001037 return -1;
1038 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001039 if (i != SIZEOF_LONG) {
1040 if ((i == 2) && (x < -32768 || x > 32767))
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001041 return _range_error(f, 0);
Bob Ippolito90bd0a52006-05-27 11:47:12 +00001042#if (SIZEOF_LONG != 4)
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001043 else if ((i == 4) && (x < -2147483648L || x > 2147483647L))
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001044 return _range_error(f, 0);
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001045#endif
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001046 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001047 do {
1048 *p++ = (char)x;
1049 x >>= 8;
1050 } while (--i > 0);
1051 return 0;
1052}
1053
1054static int
1055lp_uint(char *p, PyObject *v, const formatdef *f)
1056{
1057 unsigned long x;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001058 Py_ssize_t i;
Mark Dickinson716a9cc2009-09-27 16:39:28 +00001059 if (get_ulong(v, &x) < 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001060 return -1;
1061 i = f->size;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001062 if (i != SIZEOF_LONG) {
1063 unsigned long maxint = 1;
1064 maxint <<= (unsigned long)(i * 8);
1065 if (x >= maxint)
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001066 return _range_error(f, 1);
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001067 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001068 do {
1069 *p++ = (char)x;
1070 x >>= 8;
1071 } while (--i > 0);
1072 return 0;
1073}
1074
1075static int
1076lp_longlong(char *p, PyObject *v, const formatdef *f)
1077{
1078 int res;
1079 v = get_pylong(v);
1080 if (v == NULL)
1081 return -1;
1082 res = _PyLong_AsByteArray((PyLongObject*)v,
1083 (unsigned char *)p,
1084 8,
1085 1, /* little_endian */
1086 1 /* signed */);
1087 Py_DECREF(v);
1088 return res;
1089}
1090
1091static int
1092lp_ulonglong(char *p, PyObject *v, const formatdef *f)
1093{
1094 int res;
1095 v = get_pylong(v);
1096 if (v == NULL)
1097 return -1;
1098 res = _PyLong_AsByteArray((PyLongObject*)v,
1099 (unsigned char *)p,
1100 8,
1101 1, /* little_endian */
1102 0 /* signed */);
1103 Py_DECREF(v);
1104 return res;
1105}
1106
1107static int
1108lp_float(char *p, PyObject *v, const formatdef *f)
1109{
1110 double x = PyFloat_AsDouble(v);
1111 if (x == -1 && PyErr_Occurred()) {
1112 PyErr_SetString(StructError,
1113 "required argument is not a float");
1114 return -1;
1115 }
1116 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
1117}
1118
1119static int
1120lp_double(char *p, PyObject *v, const formatdef *f)
1121{
1122 double x = PyFloat_AsDouble(v);
1123 if (x == -1 && PyErr_Occurred()) {
1124 PyErr_SetString(StructError,
1125 "required argument is not a float");
1126 return -1;
1127 }
1128 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
1129}
1130
1131static formatdef lilendian_table[] = {
1132 {'x', 1, 0, NULL},
Bob Ippolitoe27337b2006-05-26 13:15:44 +00001133 {'b', 1, 0, nu_byte, np_byte},
1134 {'B', 1, 0, nu_ubyte, np_ubyte},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001135 {'c', 1, 0, nu_char, np_char},
1136 {'s', 1, 0, NULL},
1137 {'p', 1, 0, NULL},
1138 {'h', 2, 0, lu_int, lp_int},
1139 {'H', 2, 0, lu_uint, lp_uint},
1140 {'i', 4, 0, lu_int, lp_int},
1141 {'I', 4, 0, lu_uint, lp_uint},
1142 {'l', 4, 0, lu_int, lp_int},
1143 {'L', 4, 0, lu_uint, lp_uint},
1144 {'q', 8, 0, lu_longlong, lp_longlong},
1145 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
Thomas Hellerf3c05592008-03-05 15:34:29 +00001146 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep,
Martin v. Löwisaef4c6b2007-01-21 09:33:07 +00001147 but potentially different from native rep -- reuse bx_bool funcs. */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001148 {'f', 4, 0, lu_float, lp_float},
1149 {'d', 8, 0, lu_double, lp_double},
1150 {0}
1151};
1152
1153
1154static const formatdef *
1155whichtable(char **pfmt)
1156{
1157 const char *fmt = (*pfmt)++; /* May be backed out of later */
1158 switch (*fmt) {
1159 case '<':
1160 return lilendian_table;
1161 case '>':
1162 case '!': /* Network byte order is big-endian */
1163 return bigendian_table;
1164 case '=': { /* Host byte order -- different from native in aligment! */
1165 int n = 1;
1166 char *p = (char *) &n;
1167 if (*p == 1)
1168 return lilendian_table;
1169 else
1170 return bigendian_table;
1171 }
1172 default:
1173 --*pfmt; /* Back out of pointer increment */
1174 /* Fall through */
1175 case '@':
1176 return native_table;
1177 }
1178}
1179
1180
1181/* Get the table entry for a format code */
1182
1183static const formatdef *
1184getentry(int c, const formatdef *f)
1185{
1186 for (; f->format != '\0'; f++) {
1187 if (f->format == c) {
1188 return f;
1189 }
1190 }
1191 PyErr_SetString(StructError, "bad char in struct format");
1192 return NULL;
1193}
1194
1195
1196/* Align a size according to a format code */
1197
1198static int
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001199align(Py_ssize_t size, char c, const formatdef *e)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001200{
1201 if (e->format == c) {
1202 if (e->alignment) {
1203 size = ((size + e->alignment - 1)
1204 / e->alignment)
1205 * e->alignment;
1206 }
1207 }
1208 return size;
1209}
1210
1211
1212/* calculate the size of a format string */
1213
1214static int
1215prepare_s(PyStructObject *self)
1216{
1217 const formatdef *f;
1218 const formatdef *e;
1219 formatcode *codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001220
Bob Ippolito232f3c92006-05-23 19:12:41 +00001221 const char *s;
1222 const char *fmt;
1223 char c;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001224 Py_ssize_t size, len, num, itemsize, x;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001225
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001226 fmt = PyString_AS_STRING(self->s_format);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001227
1228 f = whichtable((char **)&fmt);
Tim Petersc2b550e2006-05-31 14:28:07 +00001229
Bob Ippolito232f3c92006-05-23 19:12:41 +00001230 s = fmt;
1231 size = 0;
1232 len = 0;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001233 while ((c = *s++) != '\0') {
1234 if (isspace(Py_CHARMASK(c)))
1235 continue;
1236 if ('0' <= c && c <= '9') {
1237 num = c - '0';
1238 while ('0' <= (c = *s++) && c <= '9') {
1239 x = num*10 + (c - '0');
1240 if (x/10 != num) {
1241 PyErr_SetString(
1242 StructError,
1243 "overflow in item count");
1244 return -1;
1245 }
1246 num = x;
1247 }
1248 if (c == '\0')
1249 break;
1250 }
1251 else
1252 num = 1;
1253
1254 e = getentry(c, f);
1255 if (e == NULL)
1256 return -1;
Tim Petersc2b550e2006-05-31 14:28:07 +00001257
Bob Ippolito232f3c92006-05-23 19:12:41 +00001258 switch (c) {
1259 case 's': /* fall through */
1260 case 'p': len++; break;
1261 case 'x': break;
1262 default: len += num; break;
1263 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001264
1265 itemsize = e->size;
1266 size = align(size, c, e);
1267 x = num * itemsize;
1268 size += x;
1269 if (x/itemsize != num || size < 0) {
1270 PyErr_SetString(StructError,
1271 "total struct size too long");
1272 return -1;
1273 }
1274 }
1275
Gregory P. Smith9d534572008-06-11 07:41:16 +00001276 /* check for overflow */
1277 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) {
1278 PyErr_NoMemory();
1279 return -1;
1280 }
1281
Bob Ippolito232f3c92006-05-23 19:12:41 +00001282 self->s_size = size;
1283 self->s_len = len;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001284 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001285 if (codes == NULL) {
1286 PyErr_NoMemory();
1287 return -1;
1288 }
1289 self->s_codes = codes;
Tim Petersc2b550e2006-05-31 14:28:07 +00001290
Bob Ippolito232f3c92006-05-23 19:12:41 +00001291 s = fmt;
1292 size = 0;
1293 while ((c = *s++) != '\0') {
1294 if (isspace(Py_CHARMASK(c)))
1295 continue;
1296 if ('0' <= c && c <= '9') {
1297 num = c - '0';
1298 while ('0' <= (c = *s++) && c <= '9')
1299 num = num*10 + (c - '0');
1300 if (c == '\0')
1301 break;
1302 }
1303 else
1304 num = 1;
1305
1306 e = getentry(c, f);
Tim Petersc2b550e2006-05-31 14:28:07 +00001307
Bob Ippolito232f3c92006-05-23 19:12:41 +00001308 size = align(size, c, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001309 if (c == 's' || c == 'p') {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001310 codes->offset = size;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001311 codes->size = num;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001312 codes->fmtdef = e;
1313 codes++;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001314 size += num;
1315 } else if (c == 'x') {
1316 size += num;
1317 } else {
1318 while (--num >= 0) {
1319 codes->offset = size;
1320 codes->size = e->size;
1321 codes->fmtdef = e;
1322 codes++;
1323 size += e->size;
1324 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001325 }
Bob Ippolito232f3c92006-05-23 19:12:41 +00001326 }
1327 codes->fmtdef = NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001328 codes->offset = size;
1329 codes->size = 0;
Tim Petersc2b550e2006-05-31 14:28:07 +00001330
Bob Ippolito232f3c92006-05-23 19:12:41 +00001331 return 0;
1332}
1333
1334static PyObject *
1335s_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1336{
1337 PyObject *self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001338
1339 assert(type != NULL && type->tp_alloc != NULL);
1340
1341 self = type->tp_alloc(type, 0);
1342 if (self != NULL) {
1343 PyStructObject *s = (PyStructObject*)self;
1344 Py_INCREF(Py_None);
1345 s->s_format = Py_None;
1346 s->s_codes = NULL;
1347 s->s_size = -1;
1348 s->s_len = -1;
1349 }
1350 return self;
1351}
1352
1353static int
1354s_init(PyObject *self, PyObject *args, PyObject *kwds)
1355{
1356 PyStructObject *soself = (PyStructObject *)self;
1357 PyObject *o_format = NULL;
1358 int ret = 0;
1359 static char *kwlist[] = {"format", 0};
1360
1361 assert(PyStruct_Check(self));
1362
1363 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist,
1364 &o_format))
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001365 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001366
1367 Py_INCREF(o_format);
Amaury Forgeot d'Arc588ff932008-02-16 14:34:57 +00001368 Py_CLEAR(soself->s_format);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001369 soself->s_format = o_format;
Tim Petersc2b550e2006-05-31 14:28:07 +00001370
Bob Ippolito232f3c92006-05-23 19:12:41 +00001371 ret = prepare_s(soself);
1372 return ret;
1373}
1374
1375static void
1376s_dealloc(PyStructObject *s)
1377{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001378 if (s->weakreflist != NULL)
1379 PyObject_ClearWeakRefs((PyObject *)s);
1380 if (s->s_codes != NULL) {
1381 PyMem_FREE(s->s_codes);
1382 }
1383 Py_XDECREF(s->s_format);
Christian Heimese93237d2007-12-19 02:37:44 +00001384 Py_TYPE(s)->tp_free((PyObject *)s);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001385}
1386
Bob Ippolitoeb621272006-05-24 15:32:06 +00001387static PyObject *
1388s_unpack_internal(PyStructObject *soself, char *startfrom) {
1389 formatcode *code;
1390 Py_ssize_t i = 0;
1391 PyObject *result = PyTuple_New(soself->s_len);
1392 if (result == NULL)
1393 return NULL;
1394
1395 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1396 PyObject *v;
1397 const formatdef *e = code->fmtdef;
1398 const char *res = startfrom + code->offset;
1399 if (e->format == 's') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001400 v = PyString_FromStringAndSize(res, code->size);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001401 } else if (e->format == 'p') {
1402 Py_ssize_t n = *(unsigned char*)res;
1403 if (n >= code->size)
1404 n = code->size - 1;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001405 v = PyString_FromStringAndSize(res + 1, n);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001406 } else {
1407 v = e->unpack(res, e);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001408 }
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001409 if (v == NULL)
1410 goto fail;
1411 PyTuple_SET_ITEM(result, i++, v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001412 }
1413
1414 return result;
1415fail:
1416 Py_DECREF(result);
1417 return NULL;
Bob Ippolitocd51ca52006-05-27 15:53:49 +00001418}
Bob Ippolitoeb621272006-05-24 15:32:06 +00001419
1420
Bob Ippolito232f3c92006-05-23 19:12:41 +00001421PyDoc_STRVAR(s_unpack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001422"S.unpack(str) -> (v1, v2, ...)\n\
Bob Ippolito232f3c92006-05-23 19:12:41 +00001423\n\
1424Return tuple containing values unpacked according to this Struct's format.\n\
1425Requires len(str) == self.size. See struct.__doc__ for more on format\n\
1426strings.");
1427
1428static PyObject *
1429s_unpack(PyObject *self, PyObject *inputstr)
1430{
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001431 char *start;
1432 Py_ssize_t len;
1433 PyObject *args=NULL, *result;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001434 PyStructObject *soself = (PyStructObject *)self;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001435 assert(PyStruct_Check(self));
Tim Petersc2b550e2006-05-31 14:28:07 +00001436 assert(soself->s_codes != NULL);
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001437 if (inputstr == NULL)
1438 goto fail;
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001439 if (PyString_Check(inputstr) &&
1440 PyString_GET_SIZE(inputstr) == soself->s_size) {
1441 return s_unpack_internal(soself, PyString_AS_STRING(inputstr));
Bob Ippolito232f3c92006-05-23 19:12:41 +00001442 }
Raymond Hettinger7a3d41f2007-04-05 18:00:03 +00001443 args = PyTuple_Pack(1, inputstr);
1444 if (args == NULL)
1445 return NULL;
1446 if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len))
1447 goto fail;
1448 if (soself->s_size != len)
1449 goto fail;
1450 result = s_unpack_internal(soself, start);
1451 Py_DECREF(args);
1452 return result;
1453
1454fail:
1455 Py_XDECREF(args);
1456 PyErr_Format(StructError,
1457 "unpack requires a string argument of length %zd",
1458 soself->s_size);
1459 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001460}
1461
1462PyDoc_STRVAR(s_unpack_from__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001463"S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\
Bob Ippolitoeb621272006-05-24 15:32:06 +00001464\n\
1465Return tuple containing values unpacked according to this Struct's format.\n\
1466Unlike unpack, unpack_from can unpack values from any object supporting\n\
1467the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\
1468See struct.__doc__ for more on format strings.");
1469
1470static PyObject *
1471s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1472{
1473 static char *kwlist[] = {"buffer", "offset", 0};
1474#if (PY_VERSION_HEX < 0x02050000)
1475 static char *fmt = "z#|i:unpack_from";
1476#else
1477 static char *fmt = "z#|n:unpack_from";
1478#endif
1479 Py_ssize_t buffer_len = 0, offset = 0;
1480 char *buffer = NULL;
1481 PyStructObject *soself = (PyStructObject *)self;
1482 assert(PyStruct_Check(self));
1483 assert(soself->s_codes != NULL);
1484
1485 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist,
1486 &buffer, &buffer_len, &offset))
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001487 return NULL;
Bob Ippolitoeb621272006-05-24 15:32:06 +00001488
1489 if (buffer == NULL) {
1490 PyErr_Format(StructError,
1491 "unpack_from requires a buffer argument");
Bob Ippolito232f3c92006-05-23 19:12:41 +00001492 return NULL;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001493 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001494
Bob Ippolitoeb621272006-05-24 15:32:06 +00001495 if (offset < 0)
1496 offset += buffer_len;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001497
Bob Ippolitoeb621272006-05-24 15:32:06 +00001498 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1499 PyErr_Format(StructError,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001500 "unpack_from requires a buffer of at least %zd bytes",
Bob Ippolitoeb621272006-05-24 15:32:06 +00001501 soself->s_size);
1502 return NULL;
1503 }
1504 return s_unpack_internal(soself, buffer + offset);
1505}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001506
Bob Ippolito232f3c92006-05-23 19:12:41 +00001507
Martin Blais2856e5f2006-05-26 12:03:27 +00001508/*
1509 * Guts of the pack function.
1510 *
1511 * Takes a struct object, a tuple of arguments, and offset in that tuple of
1512 * argument for where to start processing the arguments for packing, and a
1513 * character buffer for writing the packed string. The caller must insure
1514 * that the buffer may contain the required length for packing the arguments.
1515 * 0 is returned on success, 1 is returned if there is an error.
1516 *
1517 */
1518static int
1519s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001520{
Bob Ippolito232f3c92006-05-23 19:12:41 +00001521 formatcode *code;
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001522 /* XXX(nnorwitz): why does i need to be a local? can we use
1523 the offset parameter or do we need the wider width? */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001524 Py_ssize_t i;
Martin Blais2856e5f2006-05-26 12:03:27 +00001525
1526 memset(buf, '\0', soself->s_size);
1527 i = offset;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001528 for (code = soself->s_codes; code->fmtdef != NULL; code++) {
1529 Py_ssize_t n;
Neal Norwitz3c5431e2006-06-11 05:45:25 +00001530 PyObject *v = PyTuple_GET_ITEM(args, i++);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001531 const formatdef *e = code->fmtdef;
Martin Blais2856e5f2006-05-26 12:03:27 +00001532 char *res = buf + code->offset;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001533 if (e->format == 's') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001534 if (!PyString_Check(v)) {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001535 PyErr_SetString(StructError,
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001536 "argument for 's' must "
1537 "be a string");
Martin Blais2856e5f2006-05-26 12:03:27 +00001538 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001539 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001540 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001541 if (n > code->size)
1542 n = code->size;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001543 if (n > 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001544 memcpy(res, PyString_AS_STRING(v), n);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001545 } else if (e->format == 'p') {
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001546 if (!PyString_Check(v)) {
Bob Ippolito232f3c92006-05-23 19:12:41 +00001547 PyErr_SetString(StructError,
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001548 "argument for 'p' must "
1549 "be a string");
Martin Blais2856e5f2006-05-26 12:03:27 +00001550 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001551 }
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001552 n = PyString_GET_SIZE(v);
Bob Ippolitoeb621272006-05-24 15:32:06 +00001553 if (n > (code->size - 1))
1554 n = code->size - 1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001555 if (n > 0)
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001556 memcpy(res + 1, PyString_AS_STRING(v), n);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001557 if (n > 255)
1558 n = 255;
1559 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char);
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001560 } else if (e->pack(res, v, e) < 0) {
1561 if (strchr(integer_codes, e->format) != NULL &&
1562 PyErr_ExceptionMatches(PyExc_OverflowError))
1563 PyErr_Format(StructError,
1564 "integer out of range for "
1565 "'%c' format code",
1566 e->format);
1567 return -1;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001568 }
1569 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001570
Martin Blais2856e5f2006-05-26 12:03:27 +00001571 /* Success */
1572 return 0;
1573}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001574
Martin Blais2856e5f2006-05-26 12:03:27 +00001575
1576PyDoc_STRVAR(s_pack__doc__,
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001577"S.pack(v1, v2, ...) -> string\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001578\n\
1579Return a string containing values v1, v2, ... packed according to this\n\
1580Struct's format. See struct.__doc__ for more on format strings.");
1581
1582static PyObject *
1583s_pack(PyObject *self, PyObject *args)
1584{
1585 PyStructObject *soself;
1586 PyObject *result;
1587
1588 /* Validate arguments. */
1589 soself = (PyStructObject *)self;
1590 assert(PyStruct_Check(self));
1591 assert(soself->s_codes != NULL);
Martin Blaisaf2ae722006-06-04 13:49:49 +00001592 if (PyTuple_GET_SIZE(args) != soself->s_len)
Martin Blais2856e5f2006-05-26 12:03:27 +00001593 {
1594 PyErr_Format(StructError,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001595 "pack requires exactly %zd arguments", soself->s_len);
Martin Blais2856e5f2006-05-26 12:03:27 +00001596 return NULL;
1597 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001598
Martin Blais2856e5f2006-05-26 12:03:27 +00001599 /* Allocate a new string */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001600 result = PyString_FromStringAndSize((char *)NULL, soself->s_size);
Martin Blais2856e5f2006-05-26 12:03:27 +00001601 if (result == NULL)
1602 return NULL;
Tim Petersc2b550e2006-05-31 14:28:07 +00001603
Martin Blais2856e5f2006-05-26 12:03:27 +00001604 /* Call the guts */
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001605 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) {
Martin Blais2856e5f2006-05-26 12:03:27 +00001606 Py_DECREF(result);
1607 return NULL;
1608 }
1609
1610 return result;
1611}
1612
Martin Blaisaf2ae722006-06-04 13:49:49 +00001613PyDoc_STRVAR(s_pack_into__doc__,
1614"S.pack_into(buffer, offset, v1, v2, ...)\n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001615\n\
Martin Blaisaf2ae722006-06-04 13:49:49 +00001616Pack the values v1, v2, ... according to this Struct's format, write \n\
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001617the packed bytes into the writable buffer buf starting at offset. Note\n\
1618that the offset is not an optional argument. See struct.__doc__ for \n\
Martin Blais2856e5f2006-05-26 12:03:27 +00001619more on format strings.");
1620
1621static PyObject *
Martin Blaisaf2ae722006-06-04 13:49:49 +00001622s_pack_into(PyObject *self, PyObject *args)
Martin Blais2856e5f2006-05-26 12:03:27 +00001623{
1624 PyStructObject *soself;
1625 char *buffer;
1626 Py_ssize_t buffer_len, offset;
1627
1628 /* Validate arguments. +1 is for the first arg as buffer. */
1629 soself = (PyStructObject *)self;
1630 assert(PyStruct_Check(self));
1631 assert(soself->s_codes != NULL);
Martin Blaisaf2ae722006-06-04 13:49:49 +00001632 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2))
Martin Blais2856e5f2006-05-26 12:03:27 +00001633 {
1634 PyErr_Format(StructError,
Martin Blaisaf2ae722006-06-04 13:49:49 +00001635 "pack_into requires exactly %zd arguments",
Martin Blais2856e5f2006-05-26 12:03:27 +00001636 (soself->s_len + 2));
1637 return NULL;
1638 }
1639
1640 /* Extract a writable memory buffer from the first argument */
Tim Petersc2b550e2006-05-31 14:28:07 +00001641 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0),
1642 (void**)&buffer, &buffer_len) == -1 ) {
Martin Blais2856e5f2006-05-26 12:03:27 +00001643 return NULL;
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001644 }
1645 assert( buffer_len >= 0 );
Martin Blais2856e5f2006-05-26 12:03:27 +00001646
1647 /* Extract the offset from the first argument */
Martin Blaisaf2ae722006-06-04 13:49:49 +00001648 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1));
Benjamin Peterson02252482008-09-30 02:11:07 +00001649 if (offset == -1 && PyErr_Occurred())
1650 return NULL;
Martin Blais2856e5f2006-05-26 12:03:27 +00001651
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001652 /* Support negative offsets. */
Martin Blais2856e5f2006-05-26 12:03:27 +00001653 if (offset < 0)
1654 offset += buffer_len;
1655
1656 /* Check boundaries */
1657 if (offset < 0 || (buffer_len - offset) < soself->s_size) {
1658 PyErr_Format(StructError,
Martin Blaisaf2ae722006-06-04 13:49:49 +00001659 "pack_into requires a buffer of at least %zd bytes",
Martin Blais2856e5f2006-05-26 12:03:27 +00001660 soself->s_size);
1661 return NULL;
1662 }
Tim Petersc2b550e2006-05-31 14:28:07 +00001663
Martin Blais2856e5f2006-05-26 12:03:27 +00001664 /* Call the guts */
1665 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) {
1666 return NULL;
1667 }
1668
Georg Brandlc26025c2006-05-28 21:42:54 +00001669 Py_RETURN_NONE;
Bob Ippolito232f3c92006-05-23 19:12:41 +00001670}
1671
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001672static PyObject *
1673s_get_format(PyStructObject *self, void *unused)
1674{
1675 Py_INCREF(self->s_format);
1676 return self->s_format;
1677}
1678
1679static PyObject *
1680s_get_size(PyStructObject *self, void *unused)
1681{
1682 return PyInt_FromSsize_t(self->s_size);
1683}
Bob Ippolito232f3c92006-05-23 19:12:41 +00001684
1685/* List of functions */
1686
1687static struct PyMethodDef s_methods[] = {
Martin Blaisaf2ae722006-06-04 13:49:49 +00001688 {"pack", s_pack, METH_VARARGS, s_pack__doc__},
1689 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__},
1690 {"unpack", s_unpack, METH_O, s_unpack__doc__},
Neal Norwitza84dcd72007-05-22 07:16:44 +00001691 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS,
Tim Peters5ec2e852006-06-04 15:49:07 +00001692 s_unpack_from__doc__},
Bob Ippolito232f3c92006-05-23 19:12:41 +00001693 {NULL, NULL} /* sentinel */
1694};
1695
1696PyDoc_STRVAR(s__doc__, "Compiled struct object");
1697
1698#define OFF(x) offsetof(PyStructObject, x)
1699
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001700static PyGetSetDef s_getsetlist[] = {
Bob Ippolito1fcdc232006-05-27 12:11:36 +00001701 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL},
1702 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL},
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001703 {NULL} /* sentinel */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001704};
1705
Bob Ippolito232f3c92006-05-23 19:12:41 +00001706static
1707PyTypeObject PyStructType = {
Martin v. Löwis68192102007-07-21 06:55:02 +00001708 PyVarObject_HEAD_INIT(NULL, 0)
Bob Ippolito232f3c92006-05-23 19:12:41 +00001709 "Struct",
1710 sizeof(PyStructObject),
1711 0,
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001712 (destructor)s_dealloc, /* tp_dealloc */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001713 0, /* tp_print */
1714 0, /* tp_getattr */
1715 0, /* tp_setattr */
1716 0, /* tp_compare */
1717 0, /* tp_repr */
1718 0, /* tp_as_number */
1719 0, /* tp_as_sequence */
1720 0, /* tp_as_mapping */
1721 0, /* tp_hash */
1722 0, /* tp_call */
1723 0, /* tp_str */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001724 PyObject_GenericGetAttr, /* tp_getattro */
1725 PyObject_GenericSetAttr, /* tp_setattro */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001726 0, /* tp_as_buffer */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001727 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */
1728 s__doc__, /* tp_doc */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001729 0, /* tp_traverse */
1730 0, /* tp_clear */
1731 0, /* tp_richcompare */
1732 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */
1733 0, /* tp_iter */
1734 0, /* tp_iternext */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001735 s_methods, /* tp_methods */
1736 NULL, /* tp_members */
1737 s_getsetlist, /* tp_getset */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001738 0, /* tp_base */
1739 0, /* tp_dict */
1740 0, /* tp_descr_get */
1741 0, /* tp_descr_set */
1742 0, /* tp_dictoffset */
Bob Ippolitoaa70a172006-05-26 20:25:23 +00001743 s_init, /* tp_init */
1744 PyType_GenericAlloc,/* tp_alloc */
1745 s_new, /* tp_new */
1746 PyObject_Del, /* tp_free */
Bob Ippolito232f3c92006-05-23 19:12:41 +00001747};
1748
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001749
1750/* ---- Standalone functions ---- */
1751
1752#define MAXCACHE 100
Christian Heimes76d19f62008-01-04 02:54:42 +00001753static PyObject *cache = NULL;
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001754
1755static PyObject *
1756cache_struct(PyObject *fmt)
1757{
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001758 PyObject * s_object;
1759
1760 if (cache == NULL) {
1761 cache = PyDict_New();
1762 if (cache == NULL)
1763 return NULL;
1764 }
1765
1766 s_object = PyDict_GetItem(cache, fmt);
1767 if (s_object != NULL) {
1768 Py_INCREF(s_object);
1769 return s_object;
1770 }
1771
1772 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL);
1773 if (s_object != NULL) {
1774 if (PyDict_Size(cache) >= MAXCACHE)
1775 PyDict_Clear(cache);
1776 /* Attempt to cache the result */
1777 if (PyDict_SetItem(cache, fmt, s_object) == -1)
1778 PyErr_Clear();
1779 }
1780 return s_object;
1781}
1782
Christian Heimes76d19f62008-01-04 02:54:42 +00001783PyDoc_STRVAR(clearcache_doc,
1784"Clear the internal cache.");
1785
1786static PyObject *
1787clearcache(PyObject *self)
1788{
Raymond Hettinger18e08e52008-01-18 00:10:42 +00001789 Py_CLEAR(cache);
Christian Heimes76d19f62008-01-04 02:54:42 +00001790 Py_RETURN_NONE;
1791}
1792
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001793PyDoc_STRVAR(calcsize_doc,
1794"Return size of C struct described by format string fmt.");
1795
1796static PyObject *
1797calcsize(PyObject *self, PyObject *fmt)
1798{
1799 Py_ssize_t n;
1800 PyObject *s_object = cache_struct(fmt);
1801 if (s_object == NULL)
1802 return NULL;
1803 n = ((PyStructObject *)s_object)->s_size;
1804 Py_DECREF(s_object);
1805 return PyInt_FromSsize_t(n);
1806}
1807
1808PyDoc_STRVAR(pack_doc,
1809"Return string containing values v1, v2, ... packed according to fmt.");
1810
1811static PyObject *
1812pack(PyObject *self, PyObject *args)
1813{
1814 PyObject *s_object, *fmt, *newargs, *result;
1815 Py_ssize_t n = PyTuple_GET_SIZE(args);
1816
1817 if (n == 0) {
1818 PyErr_SetString(PyExc_TypeError, "missing format argument");
1819 return NULL;
1820 }
1821 fmt = PyTuple_GET_ITEM(args, 0);
1822 newargs = PyTuple_GetSlice(args, 1, n);
1823 if (newargs == NULL)
1824 return NULL;
1825
1826 s_object = cache_struct(fmt);
1827 if (s_object == NULL) {
1828 Py_DECREF(newargs);
1829 return NULL;
1830 }
1831 result = s_pack(s_object, newargs);
1832 Py_DECREF(newargs);
1833 Py_DECREF(s_object);
1834 return result;
1835}
1836
1837PyDoc_STRVAR(pack_into_doc,
1838"Pack the values v1, v2, ... according to fmt.\n\
1839Write the packed bytes into the writable buffer buf starting at offset.");
1840
1841static PyObject *
1842pack_into(PyObject *self, PyObject *args)
1843{
1844 PyObject *s_object, *fmt, *newargs, *result;
1845 Py_ssize_t n = PyTuple_GET_SIZE(args);
1846
1847 if (n == 0) {
1848 PyErr_SetString(PyExc_TypeError, "missing format argument");
1849 return NULL;
1850 }
1851 fmt = PyTuple_GET_ITEM(args, 0);
1852 newargs = PyTuple_GetSlice(args, 1, n);
1853 if (newargs == NULL)
1854 return NULL;
1855
1856 s_object = cache_struct(fmt);
1857 if (s_object == NULL) {
1858 Py_DECREF(newargs);
1859 return NULL;
1860 }
1861 result = s_pack_into(s_object, newargs);
1862 Py_DECREF(newargs);
1863 Py_DECREF(s_object);
1864 return result;
1865}
1866
1867PyDoc_STRVAR(unpack_doc,
1868"Unpack the string containing packed C structure data, according to fmt.\n\
1869Requires len(string) == calcsize(fmt).");
1870
1871static PyObject *
1872unpack(PyObject *self, PyObject *args)
1873{
1874 PyObject *s_object, *fmt, *inputstr, *result;
1875
1876 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr))
1877 return NULL;
1878
1879 s_object = cache_struct(fmt);
1880 if (s_object == NULL)
1881 return NULL;
1882 result = s_unpack(s_object, inputstr);
1883 Py_DECREF(s_object);
1884 return result;
1885}
1886
1887PyDoc_STRVAR(unpack_from_doc,
1888"Unpack the buffer, containing packed C structure data, according to\n\
1889fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt).");
1890
1891static PyObject *
1892unpack_from(PyObject *self, PyObject *args, PyObject *kwds)
1893{
1894 PyObject *s_object, *fmt, *newargs, *result;
1895 Py_ssize_t n = PyTuple_GET_SIZE(args);
1896
1897 if (n == 0) {
1898 PyErr_SetString(PyExc_TypeError, "missing format argument");
1899 return NULL;
1900 }
1901 fmt = PyTuple_GET_ITEM(args, 0);
1902 newargs = PyTuple_GetSlice(args, 1, n);
1903 if (newargs == NULL)
1904 return NULL;
1905
1906 s_object = cache_struct(fmt);
1907 if (s_object == NULL) {
1908 Py_DECREF(newargs);
1909 return NULL;
1910 }
1911 result = s_unpack_from(s_object, newargs, kwds);
1912 Py_DECREF(newargs);
1913 Py_DECREF(s_object);
1914 return result;
1915}
1916
1917static struct PyMethodDef module_functions[] = {
Christian Heimes76d19f62008-01-04 02:54:42 +00001918 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc},
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001919 {"calcsize", calcsize, METH_O, calcsize_doc},
1920 {"pack", pack, METH_VARARGS, pack_doc},
1921 {"pack_into", pack_into, METH_VARARGS, pack_into_doc},
1922 {"unpack", unpack, METH_VARARGS, unpack_doc},
1923 {"unpack_from", (PyCFunction)unpack_from,
1924 METH_VARARGS|METH_KEYWORDS, unpack_from_doc},
1925 {NULL, NULL} /* sentinel */
1926};
1927
1928
Bob Ippolito232f3c92006-05-23 19:12:41 +00001929/* Module initialization */
1930
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001931PyDoc_STRVAR(module_doc,
Mark Dickinson3d830822009-10-08 15:54:10 +00001932"Functions to convert between Python values and C structs represented\n\
1933as Python strings. It uses format strings (explained below) as compact\n\
1934descriptions of the lay-out of the C structs and the intended conversion\n\
1935to/from Python values.\n\
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001936\n\
1937The optional first format char indicates byte order, size and alignment:\n\
Mark Dickinson3d830822009-10-08 15:54:10 +00001938 @: native order, size & alignment (default)\n\
1939 =: native order, std. size & alignment\n\
1940 <: little-endian, std. size & alignment\n\
1941 >: big-endian, std. size & alignment\n\
1942 !: same as >\n\
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001943\n\
1944The remaining chars indicate types of args and must match exactly;\n\
1945these can be preceded by a decimal repeat count:\n\
1946 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
Mark Dickinson3d830822009-10-08 15:54:10 +00001947 ?: _Bool (requires C99; if not available, char is used instead)\n\
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001948 h:short; H:unsigned short; i:int; I:unsigned int;\n\
1949 l:long; L:unsigned long; f:float; d:double.\n\
1950Special cases (preceding decimal count indicates length):\n\
1951 s:string (array of char); p: pascal string (with count byte).\n\
1952Special case (only available in native format):\n\
1953 P:an integer type that is wide enough to hold a pointer.\n\
1954Special case (not in native mode unless 'long long' in platform C):\n\
1955 q:long long; Q:unsigned long long\n\
1956Whitespace between formats is ignored.\n\
1957\n\
1958The variable struct.error is an exception raised on errors.\n");
1959
Bob Ippolito232f3c92006-05-23 19:12:41 +00001960PyMODINIT_FUNC
1961init_struct(void)
1962{
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001963 PyObject *ver, *m;
1964
Gregory P. Smithdd96db62008-06-09 04:58:54 +00001965 ver = PyString_FromString("0.2");
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00001966 if (ver == NULL)
1967 return;
1968
1969 m = Py_InitModule3("_struct", module_functions, module_doc);
Bob Ippolito232f3c92006-05-23 19:12:41 +00001970 if (m == NULL)
1971 return;
1972
Christian Heimese93237d2007-12-19 02:37:44 +00001973 Py_TYPE(&PyStructType) = &PyType_Type;
Bob Ippolito3fc2bb92006-05-25 19:03:19 +00001974 if (PyType_Ready(&PyStructType) < 0)
1975 return;
1976
Mark Dickinson5fd3af22009-07-07 15:08:28 +00001977 /* This speed trick can't be used until overflow masking goes
1978 away, because native endian always raises exceptions
1979 instead of overflow masking. */
Tim Petersc2b550e2006-05-31 14:28:07 +00001980
Bob Ippolitoa99865b2006-05-25 19:56:56 +00001981 /* Check endian and swap in faster functions */
1982 {
1983 int one = 1;
1984 formatdef *native = native_table;
1985 formatdef *other, *ptr;
1986 if ((int)*(unsigned char*)&one)
1987 other = lilendian_table;
1988 else
1989 other = bigendian_table;
Bob Ippolito964e02a2006-05-25 21:09:45 +00001990 /* Scan through the native table, find a matching
1991 entry in the endian table and swap in the
1992 native implementations whenever possible
1993 (64-bit platforms may not have "standard" sizes) */
Bob Ippolitoa99865b2006-05-25 19:56:56 +00001994 while (native->format != '\0' && other->format != '\0') {
1995 ptr = other;
1996 while (ptr->format != '\0') {
1997 if (ptr->format == native->format) {
Bob Ippolito964e02a2006-05-25 21:09:45 +00001998 /* Match faster when formats are
1999 listed in the same order */
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002000 if (ptr == other)
2001 other++;
Tim Petersc2b550e2006-05-31 14:28:07 +00002002 /* Only use the trick if the
Bob Ippolito964e02a2006-05-25 21:09:45 +00002003 size matches */
2004 if (ptr->size != native->size)
2005 break;
2006 /* Skip float and double, could be
2007 "unknown" float format */
2008 if (ptr->format == 'd' || ptr->format == 'f')
2009 break;
2010 ptr->pack = native->pack;
2011 ptr->unpack = native->unpack;
Bob Ippolitoa99865b2006-05-25 19:56:56 +00002012 break;
2013 }
2014 ptr++;
2015 }
2016 native++;
2017 }
2018 }
Tim Petersc2b550e2006-05-31 14:28:07 +00002019
Bob Ippolito232f3c92006-05-23 19:12:41 +00002020 /* Add some symbolic constants to the module */
2021 if (StructError == NULL) {
2022 StructError = PyErr_NewException("struct.error", NULL, NULL);
2023 if (StructError == NULL)
2024 return;
2025 }
Bob Ippolito04ab9942006-05-25 19:33:38 +00002026
Bob Ippolito232f3c92006-05-23 19:12:41 +00002027 Py_INCREF(StructError);
2028 PyModule_AddObject(m, "error", StructError);
Bob Ippolito04ab9942006-05-25 19:33:38 +00002029
Bob Ippolito232f3c92006-05-23 19:12:41 +00002030 Py_INCREF((PyObject*)&PyStructType);
2031 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType);
Tim Petersc2b550e2006-05-31 14:28:07 +00002032
Raymond Hettinger2f6621c2008-01-04 00:01:15 +00002033 PyModule_AddObject(m, "__version__", ver);
2034
Bob Ippolito2fd39772006-05-29 22:55:48 +00002035 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1);
Bob Ippolitoe6c9f982006-08-04 23:59:21 +00002036 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1);
Bob Ippolito232f3c92006-05-23 19:12:41 +00002037}