annotate Modules/structmodule.c @ 68237:364638d6434d 2.3

Close 2.3 branch.
author Georg Brandl <georg@python.org>
date Sat, 05 Mar 2011 14:55:46 +0100
parents 871da703e4c0
children 4c8160088b34
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1 /* struct module -- pack values into and (out of) strings */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
2
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
3 /* New version supporting byte order, alignment and size options,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
4 character strings, and unsigned numbers */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
5
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
6 #include "Python.h"
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
7 #include <ctype.h>
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
8
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
9 PyDoc_STRVAR(struct__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
10 "Functions to convert between Python values and C structs.\n\
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
11 Python strings are used to hold the data representing the C struct\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
12 and also as format strings to describe the layout of data in the C struct.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
13 \n\
18077
622867e4d832 Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents: 18069
diff changeset
14 The optional first format char indicates byte order, size and alignment:\n\
622867e4d832 Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents: 18069
diff changeset
15 @: native order, size & alignment (default)\n\
622867e4d832 Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents: 18069
diff changeset
16 =: native order, std. size & alignment\n\
622867e4d832 Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents: 18069
diff changeset
17 <: little-endian, std. size & alignment\n\
622867e4d832 Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents: 18069
diff changeset
18 >: big-endian, std. size & alignment\n\
622867e4d832 Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents: 18069
diff changeset
19 !: same as >\n\
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
20 \n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
21 The remaining chars indicate types of args and must match exactly;\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
22 these can be preceded by a decimal repeat count:\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
23 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
24 h:short; H:unsigned short; i:int; I:unsigned int;\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
25 l:long; L:unsigned long; f:float; d:double.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
26 Special cases (preceding decimal count indicates length):\n\
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
27 s:string (array of char); p: pascal string (with count byte).\n\
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
28 Special case (only available in native format):\n\
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
29 P:an integer type that is wide enough to hold a pointer.\n\
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
30 Special case (not in native mode unless 'long long' in platform C):\n\
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
31 q:long long; Q:unsigned long long\n\
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
32 Whitespace between formats is ignored.\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
33 \n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
34 The variable struct.error is an exception raised on errors.");
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
35
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
36
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
37 /* Exception */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
38
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
39 static PyObject *StructError;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 /* Define various structs to figure out the alignments of types */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
44 #ifdef __MWERKS__
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
45 /*
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
46 ** XXXX We have a problem here. There are no unique alignment rules
18079
bc05b0b5e057 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 18078
diff changeset
47 ** on the PowerPC mac.
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
48 */
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
49 #ifdef __powerc
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
50 #pragma options align=mac68k
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
51 #endif
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
52 #endif /* __MWERKS__ */
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
53
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
54 typedef struct { char c; short x; } st_short;
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
55 typedef struct { char c; int x; } st_int;
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
56 typedef struct { char c; long x; } st_long;
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
57 typedef struct { char c; float x; } st_float;
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
58 typedef struct { char c; double x; } st_double;
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
59 typedef struct { char c; void *x; } st_void_p;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
60
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
61 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short))
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
62 #define INT_ALIGN (sizeof(st_int) - sizeof(int))
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
63 #define LONG_ALIGN (sizeof(st_long) - sizeof(long))
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
64 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float))
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
65 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double))
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
66 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *))
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
67
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
68 /* We can't support q and Q in native mode unless the compiler does;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
69 in std mode, they're 8 bytes on all platforms. */
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
70 #ifdef HAVE_LONG_LONG
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
71 typedef struct { char c; PY_LONG_LONG x; } s_long_long;
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
72 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG))
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
73 #endif
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
74
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
75 #define STRINGIFY(x) #x
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
76
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
77 #ifdef __powerc
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
78 #pragma options align=reset
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
79 #endif
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
80
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
81 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
82
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
83 static PyObject *
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
84 get_pylong(PyObject *v)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
85 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
86 PyNumberMethods *m;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
87
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
88 assert(v != NULL);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
89 if (PyInt_Check(v))
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
90 return PyLong_FromLong(PyInt_AS_LONG(v));
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
91 if (PyLong_Check(v)) {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
92 Py_INCREF(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
93 return v;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
94 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
95 m = v->ob_type->tp_as_number;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
96 if (m != NULL && m->nb_long != NULL) {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
97 v = m->nb_long(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
98 if (v == NULL)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
99 return NULL;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
100 if (PyLong_Check(v))
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
101 return v;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
102 Py_DECREF(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
103 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
104 PyErr_SetString(StructError,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
105 "cannot convert argument to long");
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
106 return NULL;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
107 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
108
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
109 /* Helper routine to get a Python integer and raise the appropriate error
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
110 if it isn't one */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
111
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
112 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
113 get_long(PyObject *v, long *p)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
114 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
115 long x = PyInt_AsLong(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
116 if (x == -1 && PyErr_Occurred()) {
8434
ae746eab2a96 get_long(): Use PyErr_ExceptionMatches(...) instead of PyErr_Occurred(...).
Fred Drake <fdrake@acm.org>
parents: 7982
diff changeset
117 if (PyErr_ExceptionMatches(PyExc_TypeError))
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
118 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
119 "required argument is not an integer");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
120 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
121 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
122 *p = x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
123 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
124 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
125
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
126
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
127 /* Same, but handling unsigned long */
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
128
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
129 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
130 get_ulong(PyObject *v, unsigned long *p)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
131 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
132 if (PyLong_Check(v)) {
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
133 unsigned long x = PyLong_AsUnsignedLong(v);
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
134 if (x == (unsigned long)(-1) && PyErr_Occurred())
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
135 return -1;
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
136 *p = x;
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
137 return 0;
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
138 }
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
139 else {
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
140 return get_long(v, (long *)p);
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
141 }
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
142 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
143
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
144 #ifdef HAVE_LONG_LONG
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
145
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
146 /* Same, but handling native long long. */
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
147
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
148 static int
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
149 get_longlong(PyObject *v, PY_LONG_LONG *p)
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
150 {
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
151 PY_LONG_LONG x;
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
152
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
153 v = get_pylong(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
154 if (v == NULL)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
155 return -1;
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
156 assert(PyLong_Check(v));
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
157 x = PyLong_AsLongLong(v);
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
158 Py_DECREF(v);
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
159 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred())
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
160 return -1;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
161 *p = x;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
162 return 0;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
163 }
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
164
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
165 /* Same, but handling native unsigned long long. */
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
166
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
167 static int
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
168 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p)
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
169 {
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
170 unsigned PY_LONG_LONG x;
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
171
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
172 v = get_pylong(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
173 if (v == NULL)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
174 return -1;
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
175 assert(PyLong_Check(v));
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
176 x = PyLong_AsUnsignedLongLong(v);
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
177 Py_DECREF(v);
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
178 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred())
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
179 return -1;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
180 *p = x;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
181 return 0;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
182 }
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
183
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
184 #endif
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
185
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
186 /* Floating point helpers */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
187
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
188 static PyObject *
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
189 unpack_float(const char *p, /* start of 4-byte string */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
190 int le) /* true for little-endian, false for big-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
191 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
192 double x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
193
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
194 x = _PyFloat_Unpack4((unsigned char *)p, le);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
195 if (x == -1.0 && PyErr_Occurred())
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
196 return NULL;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
197 return PyFloat_FromDouble(x);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
198 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
199
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
200 static PyObject *
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
201 unpack_double(const char *p, /* start of 8-byte string */
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
202 int le) /* true for little-endian, false for big-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
203 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
204 double x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
205
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
206 x = _PyFloat_Unpack8((unsigned char *)p, le);
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
207 if (x == -1.0 && PyErr_Occurred())
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
208 return NULL;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
209 return PyFloat_FromDouble(x);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
210 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
211
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
212
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
213 /* The translation function for each format character is table driven */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
214
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
215 typedef struct _formatdef {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
216 char format;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
217 int size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
218 int alignment;
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
219 PyObject* (*unpack)(const char *,
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
220 const struct _formatdef *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
221 int (*pack)(char *, PyObject *,
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
222 const struct _formatdef *);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
223 } formatdef;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
224
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
225 /* A large number of small routines follow, with names of the form
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
226
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
227 [bln][up]_TYPE
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
228
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
229 [bln] distiguishes among big-endian, little-endian and native.
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
230 [pu] distiguishes between pack (to struct) and unpack (from struct).
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
231 TYPE is one of char, byte, ubyte, etc.
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
232 */
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
233
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
234 /* Native mode routines. ****************************************************/
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
235 /* NOTE:
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
236 In all n[up]_<type> routines handling types larger than 1 byte, there is
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
237 *no* guarantee that the p pointer is properly aligned for each type,
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
238 therefore memcpy is called. An intermediate variable is used to
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
239 compensate for big-endian architectures.
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
240 Normally both the intermediate variable and the memcpy call will be
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
241 skipped by C optimisation in little-endian architectures (gcc >= 2.91
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
242 does this). */
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
243
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
244 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
245 nu_char(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
246 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
247 return PyString_FromStringAndSize(p, 1);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
248 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
249
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
250 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
251 nu_byte(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
252 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
253 return PyInt_FromLong((long) *(signed char *)p);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
254 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
255
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
256 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
257 nu_ubyte(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
258 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
259 return PyInt_FromLong((long) *(unsigned char *)p);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
260 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
261
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
262 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
263 nu_short(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
264 {
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
265 short x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
266 memcpy((char *)&x, p, sizeof x);
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
267 return PyInt_FromLong((long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
268 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
269
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
270 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
271 nu_ushort(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
272 {
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
273 unsigned short x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
274 memcpy((char *)&x, p, sizeof x);
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
275 return PyInt_FromLong((long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
276 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
277
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
278 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
279 nu_int(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
280 {
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
281 int x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
282 memcpy((char *)&x, p, sizeof x);
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
283 return PyInt_FromLong((long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
284 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
285
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
286 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
287 nu_uint(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
288 {
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
289 unsigned int x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
290 memcpy((char *)&x, p, sizeof x);
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
291 return PyLong_FromUnsignedLong((unsigned long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
292 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
293
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
294 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
295 nu_long(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
296 {
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
297 long x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
298 memcpy((char *)&x, p, sizeof x);
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
299 return PyInt_FromLong(x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
300 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
301
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
302 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
303 nu_ulong(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
304 {
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
305 unsigned long x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
306 memcpy((char *)&x, p, sizeof x);
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
307 return PyLong_FromUnsignedLong(x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
308 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
309
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
310 /* Native mode doesn't support q or Q unless the platform C supports
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
311 long long (or, on Windows, __int64). */
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
312
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
313 #ifdef HAVE_LONG_LONG
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
314
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
315 static PyObject *
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
316 nu_longlong(const char *p, const formatdef *f)
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
317 {
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
318 PY_LONG_LONG x;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
319 memcpy((char *)&x, p, sizeof x);
18705
8df77415a61d SF bug #442520: test_struct fails on SPARC.
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
320 return PyLong_FromLongLong(x);
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
321 }
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
322
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
323 static PyObject *
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
324 nu_ulonglong(const char *p, const formatdef *f)
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
325 {
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
326 unsigned PY_LONG_LONG x;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
327 memcpy((char *)&x, p, sizeof x);
18705
8df77415a61d SF bug #442520: test_struct fails on SPARC.
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
328 return PyLong_FromUnsignedLongLong(x);
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
329 }
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
330
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
331 #endif
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
332
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
333 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
334 nu_float(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
335 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
336 float x;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
337 memcpy((char *)&x, p, sizeof x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
338 return PyFloat_FromDouble((double)x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
339 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
340
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
341 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
342 nu_double(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
343 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
344 double x;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
345 memcpy((char *)&x, p, sizeof x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
346 return PyFloat_FromDouble(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
347 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
348
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
349 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
350 nu_void_p(const char *p, const formatdef *f)
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
351 {
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
352 void *x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
353 memcpy((char *)&x, p, sizeof x);
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
354 return PyLong_FromVoidPtr(x);
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
355 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
356
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
357 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
358 np_byte(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
359 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
360 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
361 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
362 return -1;
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
363 if (x < -128 || x > 127){
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
364 PyErr_SetString(StructError,
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
365 "byte format requires -128<=number<=127");
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
366 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
367 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
368 *p = (char)x;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
369 return 0;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
370 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
371
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
372 static int
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
373 np_ubyte(char *p, PyObject *v, const formatdef *f)
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
374 {
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
375 long x;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
376 if (get_long(v, &x) < 0)
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
377 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
378 if (x < 0 || x > 255){
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
379 PyErr_SetString(StructError,
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
380 "ubyte format requires 0<=number<=255");
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
381 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
382 }
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
383 *p = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
384 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
385 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
386
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
387 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
388 np_char(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
389 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
390 if (!PyString_Check(v) || PyString_Size(v) != 1) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
391 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
392 "char format require string of length 1");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
393 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
394 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
395 *p = *PyString_AsString(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
396 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
397 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
398
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
399 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
400 np_short(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
401 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
402 long x;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
403 short y;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
404 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
405 return -1;
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
406 if (x < SHRT_MIN || x > SHRT_MAX){
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
407 PyErr_SetString(StructError,
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
408 "short format requires " STRINGIFY(SHRT_MIN)
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
409 "<=number<=" STRINGIFY(SHRT_MAX));
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
410 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
411 }
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
412 y = (short)x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
413 memcpy(p, (char *)&y, sizeof y);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
414 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
415 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
416
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
417 static int
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
418 np_ushort(char *p, PyObject *v, const formatdef *f)
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
419 {
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
420 long x;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
421 unsigned short y;
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
422 if (get_long(v, &x) < 0)
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
423 return -1;
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
424 if (x < 0 || x > USHRT_MAX){
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
425 PyErr_SetString(StructError,
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
426 "short format requires 0<=number<=" STRINGIFY(USHRT_MAX));
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
427 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
428 }
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
429 y = (unsigned short)x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
430 memcpy(p, (char *)&y, sizeof y);
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
431 return 0;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
432 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
433
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
434 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
435 np_int(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
436 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
437 long x;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
438 int y;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
439 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
440 return -1;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
441 y = (int)x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
442 memcpy(p, (char *)&y, sizeof y);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
443 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
444 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
445
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
446 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
447 np_uint(char *p, PyObject *v, const formatdef *f)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
448 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
449 unsigned long x;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
450 unsigned int y;
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
451 if (get_ulong(v, &x) < 0)
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
452 return -1;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
453 y = (unsigned int)x;
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
454 memcpy(p, (char *)&y, sizeof y);
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
455 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
456 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
457
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
458 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
459 np_long(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
460 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
461 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
462 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
463 return -1;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
464 memcpy(p, (char *)&x, sizeof x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
465 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
466 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
467
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
468 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
469 np_ulong(char *p, PyObject *v, const formatdef *f)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
470 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
471 unsigned long x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
472 if (get_ulong(v, &x) < 0)
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
473 return -1;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
474 memcpy(p, (char *)&x, sizeof x);
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
475 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
476 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
477
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
478 #ifdef HAVE_LONG_LONG
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
479
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
480 static int
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
481 np_longlong(char *p, PyObject *v, const formatdef *f)
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
482 {
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
483 PY_LONG_LONG x;
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
484 if (get_longlong(v, &x) < 0)
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
485 return -1;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
486 memcpy(p, (char *)&x, sizeof x);
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
487 return 0;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
488 }
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
489
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
490 static int
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
491 np_ulonglong(char *p, PyObject *v, const formatdef *f)
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
492 {
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
493 unsigned PY_LONG_LONG x;
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
494 if (get_ulonglong(v, &x) < 0)
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
495 return -1;
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
496 memcpy(p, (char *)&x, sizeof x);
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
497 return 0;
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
498 }
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
499 #endif
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
500
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
501 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
502 np_float(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
503 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
504 float x = (float)PyFloat_AsDouble(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
505 if (x == -1 && PyErr_Occurred()) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
506 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
507 "required argument is not a float");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
508 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
509 }
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
510 memcpy(p, (char *)&x, sizeof x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
511 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
512 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
513
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
514 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
515 np_double(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
516 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
517 double x = PyFloat_AsDouble(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
518 if (x == -1 && PyErr_Occurred()) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
519 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
520 "required argument is not a float");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
521 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
522 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
523 memcpy(p, (char *)&x, sizeof(double));
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
524 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
525 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
526
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
527 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
528 np_void_p(char *p, PyObject *v, const formatdef *f)
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
529 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
530 void *x = PyLong_AsVoidPtr(v);
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
531 if (x == NULL && PyErr_Occurred()) {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
532 /* ### hrm. PyLong_AsVoidPtr raises SystemError */
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
533 if (PyErr_ExceptionMatches(PyExc_TypeError))
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
534 PyErr_SetString(StructError,
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
535 "required argument is not an integer");
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
536 return -1;
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
537 }
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
538 memcpy(p, (char *)&x, sizeof x);
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
539 return 0;
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
540 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
541
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
542 static formatdef native_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
543 {'x', sizeof(char), 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
544 {'b', sizeof(char), 0, nu_byte, np_byte},
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
545 {'B', sizeof(char), 0, nu_ubyte, np_ubyte},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
546 {'c', sizeof(char), 0, nu_char, np_char},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
547 {'s', sizeof(char), 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
548 {'p', sizeof(char), 0, NULL},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
549 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short},
14583
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
550 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
551 {'i', sizeof(int), INT_ALIGN, nu_int, np_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
552 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
553 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
554 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
555 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
556 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double},
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
557 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
558 #ifdef HAVE_LONG_LONG
28437
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
559 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
871da703e4c0 Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents: 28335
diff changeset
560 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
18069
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
561 #endif
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
562 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
563 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
564
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
565 /* Big-endian routines. *****************************************************/
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
566
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
567 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
568 bu_int(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
569 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
570 long x = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
571 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
572 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
573 x = (x<<8) | (*p++ & 0xFF);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
574 } while (--i > 0);
17343
de9858530a55 Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
575 /* Extend the sign bit. */
de9858530a55 Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
576 if (SIZEOF_LONG > f->size)
de9858530a55 Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
577 x |= -(x & (1L << (8*f->size - 1)));
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
578 return PyInt_FromLong(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
579 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
580
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
581 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
582 bu_uint(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
583 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
584 unsigned long x = 0;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
585 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
586 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
587 x = (x<<8) | (*p++ & 0xFF);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
588 } while (--i > 0);
8580
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
589 if (f->size >= 4)
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
590 return PyLong_FromUnsignedLong(x);
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
591 else
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
592 return PyInt_FromLong((long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
593 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
594
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
595 static PyObject *
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
596 bu_longlong(const char *p, const formatdef *f)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
597 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
598 return _PyLong_FromByteArray((const unsigned char *)p,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
599 8,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
600 0, /* little-endian */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
601 1 /* signed */);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
602 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
603
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
604 static PyObject *
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
605 bu_ulonglong(const char *p, const formatdef *f)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
606 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
607 return _PyLong_FromByteArray((const unsigned char *)p,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
608 8,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
609 0, /* little-endian */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
610 0 /* signed */);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
611 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
612
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
613 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
614 bu_float(const char *p, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
615 {
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
616 return unpack_float(p, 0);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
617 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
618
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
619 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
620 bu_double(const char *p, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
621 {
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
622 return unpack_double(p, 0);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
623 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
624
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
625 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
626 bp_int(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
627 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
628 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
629 int i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
630 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
631 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
632 i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
633 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
634 p[--i] = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
635 x >>= 8;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
636 } while (i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
637 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
638 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
639
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
640 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
641 bp_uint(char *p, PyObject *v, const formatdef *f)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
642 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
643 unsigned long x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
644 int i;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
645 if (get_ulong(v, &x) < 0)
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
646 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
647 i = f->size;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
648 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
649 p[--i] = (char)x;
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
650 x >>= 8;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
651 } while (i > 0);
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
652 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
653 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
654
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
655 static int
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
656 bp_longlong(char *p, PyObject *v, const formatdef *f)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
657 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
658 int res;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
659 v = get_pylong(v);
18094
2367b7a001a7 The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
660 if (v == NULL)
2367b7a001a7 The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
661 return -1;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
662 res = _PyLong_AsByteArray((PyLongObject *)v,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
663 (unsigned char *)p,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
664 8,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
665 0, /* little_endian */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
666 1 /* signed */);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
667 Py_DECREF(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
668 return res;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
669 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
670
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
671 static int
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
672 bp_ulonglong(char *p, PyObject *v, const formatdef *f)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
673 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
674 int res;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
675 v = get_pylong(v);
18094
2367b7a001a7 The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
676 if (v == NULL)
2367b7a001a7 The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
677 return -1;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
678 res = _PyLong_AsByteArray((PyLongObject *)v,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
679 (unsigned char *)p,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
680 8,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
681 0, /* little_endian */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
682 0 /* signed */);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
683 Py_DECREF(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
684 return res;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
685 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
686
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
687 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
688 bp_float(char *p, PyObject *v, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
689 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
690 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
691 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
692 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
693 "required argument is not a float");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
694 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
695 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
696 return _PyFloat_Pack4(x, (unsigned char *)p, 0);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
697 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
698
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
699 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
700 bp_double(char *p, PyObject *v, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
701 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
702 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
703 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
704 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
705 "required argument is not a float");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
706 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
707 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
708 return _PyFloat_Pack8(x, (unsigned char *)p, 0);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
709 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
710
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
711 static formatdef bigendian_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
712 {'x', 1, 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
713 {'b', 1, 0, bu_int, bp_int},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
714 {'B', 1, 0, bu_uint, bp_int},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
715 {'c', 1, 0, nu_char, np_char},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
716 {'s', 1, 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
717 {'p', 1, 0, NULL},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
718 {'h', 2, 0, bu_int, bp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
719 {'H', 2, 0, bu_uint, bp_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
720 {'i', 4, 0, bu_int, bp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
721 {'I', 4, 0, bu_uint, bp_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
722 {'l', 4, 0, bu_int, bp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
723 {'L', 4, 0, bu_uint, bp_uint},
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
724 {'q', 8, 0, bu_longlong, bp_longlong},
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
725 {'Q', 8, 0, bu_ulonglong, bp_ulonglong},
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
726 {'f', 4, 0, bu_float, bp_float},
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
727 {'d', 8, 0, bu_double, bp_double},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
728 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
729 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
730
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
731 /* Little-endian routines. *****************************************************/
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
732
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
733 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
734 lu_int(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
735 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
736 long x = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
737 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
738 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
739 x = (x<<8) | (p[--i] & 0xFF);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
740 } while (i > 0);
17343
de9858530a55 Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
741 /* Extend the sign bit. */
de9858530a55 Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
742 if (SIZEOF_LONG > f->size)
de9858530a55 Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents: 14869
diff changeset
743 x |= -(x & (1L << (8*f->size - 1)));
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
744 return PyInt_FromLong(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
745 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
746
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
747 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
748 lu_uint(const char *p, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
749 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
750 unsigned long x = 0;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
751 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
752 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
753 x = (x<<8) | (p[--i] & 0xFF);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
754 } while (i > 0);
8580
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
755 if (f->size >= 4)
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
756 return PyLong_FromUnsignedLong(x);
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
757 else
1ca6895024d9 Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents: 8434
diff changeset
758 return PyInt_FromLong((long)x);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
759 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
760
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
761 static PyObject *
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
762 lu_longlong(const char *p, const formatdef *f)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
763 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
764 return _PyLong_FromByteArray((const unsigned char *)p,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
765 8,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
766 1, /* little-endian */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
767 1 /* signed */);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
768 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
769
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
770 static PyObject *
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
771 lu_ulonglong(const char *p, const formatdef *f)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
772 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
773 return _PyLong_FromByteArray((const unsigned char *)p,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
774 8,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
775 1, /* little-endian */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
776 0 /* signed */);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
777 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
778
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
779 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
780 lu_float(const char *p, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
781 {
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
782 return unpack_float(p, 1);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
783 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
784
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
785 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
786 lu_double(const char *p, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
787 {
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
788 return unpack_double(p, 1);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
789 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
790
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
791 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
792 lp_int(char *p, PyObject *v, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
793 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
794 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
795 int i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
796 if (get_long(v, &x) < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
797 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
798 i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
799 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
800 *p++ = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
801 x >>= 8;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
802 } while (--i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
803 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
804 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
805
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
806 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
807 lp_uint(char *p, PyObject *v, const formatdef *f)
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
808 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
809 unsigned long x;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
810 int i;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
811 if (get_ulong(v, &x) < 0)
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
812 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
813 i = f->size;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
814 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
815 *p++ = (char)x;
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
816 x >>= 8;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
817 } while (--i > 0);
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
818 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
819 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
820
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
821 static int
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
822 lp_longlong(char *p, PyObject *v, const formatdef *f)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
823 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
824 int res;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
825 v = get_pylong(v);
18094
2367b7a001a7 The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
826 if (v == NULL)
2367b7a001a7 The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
827 return -1;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
828 res = _PyLong_AsByteArray((PyLongObject*)v,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
829 (unsigned char *)p,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
830 8,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
831 1, /* little_endian */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
832 1 /* signed */);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
833 Py_DECREF(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
834 return res;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
835 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
836
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
837 static int
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
838 lp_ulonglong(char *p, PyObject *v, const formatdef *f)
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
839 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
840 int res;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
841 v = get_pylong(v);
18094
2367b7a001a7 The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
842 if (v == NULL)
2367b7a001a7 The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents: 18084
diff changeset
843 return -1;
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
844 res = _PyLong_AsByteArray((PyLongObject*)v,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
845 (unsigned char *)p,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
846 8,
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
847 1, /* little_endian */
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
848 0 /* signed */);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
849 Py_DECREF(v);
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
850 return res;
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
851 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
852
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
853 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
854 lp_float(char *p, PyObject *v, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
855 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
856 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
857 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
858 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
859 "required argument is not a float");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
860 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
861 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
862 return _PyFloat_Pack4(x, (unsigned char *)p, 1);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
863 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
864
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
865 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
866 lp_double(char *p, PyObject *v, const formatdef *f)
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
867 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
868 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
869 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
870 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
871 "required argument is not a float");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
872 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
873 }
28335
70003b0cb837 New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents: 28334
diff changeset
874 return _PyFloat_Pack8(x, (unsigned char *)p, 1);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
875 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
876
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
877 static formatdef lilendian_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
878 {'x', 1, 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
879 {'b', 1, 0, lu_int, lp_int},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
880 {'B', 1, 0, lu_uint, lp_int},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
881 {'c', 1, 0, nu_char, np_char},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
882 {'s', 1, 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
883 {'p', 1, 0, NULL},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
884 {'h', 2, 0, lu_int, lp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
885 {'H', 2, 0, lu_uint, lp_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
886 {'i', 4, 0, lu_int, lp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
887 {'I', 4, 0, lu_uint, lp_uint},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
888 {'l', 4, 0, lu_int, lp_int},
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
889 {'L', 4, 0, lu_uint, lp_uint},
18084
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
890 {'q', 8, 0, lu_longlong, lp_longlong},
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
891 {'Q', 8, 0, lu_ulonglong, lp_ulonglong},
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
892 {'f', 4, 0, lu_float, lp_float},
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
893 {'d', 8, 0, lu_double, lp_double},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
894 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
895 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
896
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
897
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
898 static const formatdef *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
899 whichtable(char **pfmt)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
900 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
901 const char *fmt = (*pfmt)++; /* May be backed out of later */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
902 switch (*fmt) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
903 case '<':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
904 return lilendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
905 case '>':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
906 case '!': /* Network byte order is big-endian */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
907 return bigendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
908 case '=': { /* Host byte order -- different from native in aligment! */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
909 int n = 1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
910 char *p = (char *) &n;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
911 if (*p == 1)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
912 return lilendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
913 else
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
914 return bigendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
915 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
916 default:
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
917 --*pfmt; /* Back out of pointer increment */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
918 /* Fall through */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
919 case '@':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
920 return native_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
921 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
922 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
923
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
924
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
925 /* Get the table entry for a format code */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
926
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
927 static const formatdef *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
928 getentry(int c, const formatdef *f)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
929 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
930 for (; f->format != '\0'; f++) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
931 if (f->format == c) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
932 return f;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
933 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
934 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
935 PyErr_SetString(StructError, "bad char in struct format");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
936 return NULL;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
937 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
938
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
939
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
940 /* Align a size according to a format code */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
941
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
942 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
943 align(int size, int c, const formatdef *e)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
944 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
945 if (e->format == c) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
946 if (e->alignment) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
947 size = ((size + e->alignment - 1)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
948 / e->alignment)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
949 * e->alignment;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
950 }
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
951 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
952 return size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
953 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
954
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
955
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
956 /* calculate the size of a format string */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
957
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
959 calcsize(const char *fmt, const formatdef *f)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
960 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
961 const formatdef *e;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
962 const char *s;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
963 char c;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
964 int size, num, itemsize, x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
965
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
967 size = 0;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
968 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
969 if (isspace((int)c))
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
970 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
972 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
973 while ('0' <= (c = *s++) && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 x = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 if (x/10 != num) {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
976 PyErr_SetString(
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
977 StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
978 "overflow in item count");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 return -1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
980 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
981 num = x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
982 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
983 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
984 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
985 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
986 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
987 num = 1;
18079
bc05b0b5e057 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 18078
diff changeset
988
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
989 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
990 if (e == NULL)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
991 return -1;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
992 itemsize = e->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
993 size = align(size, c, e);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
994 x = num * itemsize;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
995 size += x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
996 if (x/itemsize != num || size < 0) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
997 PyErr_SetString(StructError,
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
998 "total struct size too long");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 return -1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1000 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003 return size;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1004 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1005
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1006
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1007 PyDoc_STRVAR(calcsize__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1008 "calcsize(fmt) -> int\n\
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1009 Return size of C struct described by format string fmt.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1010 See struct.__doc__ for more on format strings.");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1011
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1012 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1013 struct_calcsize(PyObject *self, PyObject *args)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 char *fmt;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1016 const formatdef *f;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1017 int size;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1019 if (!PyArg_ParseTuple(args, "s:calcsize", &fmt))
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1020 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1021 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1022 size = calcsize(fmt, f);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1023 if (size < 0)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1024 return NULL;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1025 return PyInt_FromLong((long)size);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1027
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1028
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1029 PyDoc_STRVAR(pack__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1030 "pack(fmt, v1, v2, ...) -> string\n\
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1031 Return string containing values v1, v2, ... packed according to fmt.\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1032 See struct.__doc__ for more on format strings.");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1034 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1035 struct_pack(PyObject *self, PyObject *args)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1037 const formatdef *f, *e;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1038 PyObject *format, *result, *v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 char *fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1040 int size, num;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041 int i, n;
4867
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1042 char *s, *res, *restart, *nres;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1043 char c;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1044
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1045 if (args == NULL || !PyTuple_Check(args) ||
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1046 (n = PyTuple_Size(args)) < 1)
25104
4b580a462508 (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents: 24471
diff changeset
1047 {
18079
bc05b0b5e057 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 18078
diff changeset
1048 PyErr_SetString(PyExc_TypeError,
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 11882
diff changeset
1049 "struct.pack requires at least one argument");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1051 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1052 format = PyTuple_GetItem(args, 0);
22771
260a1720f629 Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21945
diff changeset
1053 fmt = PyString_AsString(format);
260a1720f629 Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents: 21945
diff changeset
1054 if (!fmt)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1055 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1056 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1057 size = calcsize(fmt, f);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1058 if (size < 0)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1059 return NULL;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1060 result = PyString_FromStringAndSize((char *)NULL, size);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1061 if (result == NULL)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1062 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1063
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1064 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1065 i = 1;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1066 res = restart = PyString_AsString(result);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1067
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1068 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1069 if (isspace((int)c))
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1070 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1071 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1072 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1073 while ('0' <= (c = *s++) && c <= '9')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1074 num = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1075 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1076 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1077 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1078 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1079 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1080
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1081 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1082 if (e == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1083 goto fail;
4867
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1084 nres = restart + align((int)(res-restart), c, e);
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1085 /* Fill padd bytes with zeros */
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1086 while (res < nres)
e3f2ab0b0aa1 Fill pad bytes with zeros (fixing a bug dating from the very first version!).
Guido van Rossum <guido@python.org>
parents: 4865
diff changeset
1087 *res++ = '\0';
4842
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1088 if (num == 0 && c != 's')
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1089 continue;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1090 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1091 if (c == 'x') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1092 /* doesn't consume arguments */
4842
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1093 memset(res, '\0', num);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1094 res += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1096 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1097 if (i >= n) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1098 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1099 "insufficient arguments to pack");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1100 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1101 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1102 v = PyTuple_GetItem(args, i++);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1103 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1104 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1105 if (c == 's') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1106 /* num is string size, not repeat count */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1107 int n;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1108 if (!PyString_Check(v)) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1109 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1110 "argument for 's' must be a string");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1111 goto fail;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1112 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1113 n = PyString_Size(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1114 if (n > num)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1115 n = num;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1116 if (n > 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1117 memcpy(res, PyString_AsString(v), n);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1118 if (n < num)
4842
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1119 memset(res+n, '\0', num-n);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1120 res += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1121 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1122 }
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1123 else if (c == 'p') {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1124 /* num is string size + 1,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1125 to fit in the count byte */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1126 int n;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1127 num--; /* now num is max string size */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1128 if (!PyString_Check(v)) {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1129 PyErr_SetString(StructError,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1130 "argument for 'p' must be a string");
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1131 goto fail;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1132 }
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1133 n = PyString_Size(v);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1134 if (n > num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1135 n = num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1136 if (n > 0)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1137 memcpy(res+1, PyString_AsString(v), n);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1138 if (n < num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1139 /* no real need, just to be nice */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1140 memset(res+1+n, '\0', num-n);
19924
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18705
diff changeset
1141 if (n > 255)
01f66f5db326 The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents: 18705
diff changeset
1142 n = 255;
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1143 *res++ = n; /* store the length byte */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1144 res += num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1145 break;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1146 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1147 else {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1148 if (e->pack(res, v, e) < 0)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1149 goto fail;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1150 res += e->size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1152 } while (--num > 0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1153 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1154
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1155 if (i < n) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1156 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1157 "too many arguments for pack format");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1158 goto fail;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1159 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1160
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1161 return result;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1162
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1163 fail:
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1164 Py_DECREF(result);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1165 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1167
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1169 PyDoc_STRVAR(unpack__doc__,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1170 "unpack(fmt, string) -> (v1, v2, ...)\n\
6927
52125ce376a4 Oops, left in a non-standard multi-line doc string that GCC finds okay
Guido van Rossum <guido@python.org>
parents: 6922
diff changeset
1171 Unpack the string, containing packed C structure data, according\n\
52125ce376a4 Oops, left in a non-standard multi-line doc string that GCC finds okay
Guido van Rossum <guido@python.org>
parents: 6922
diff changeset
1172 to fmt. Requires len(string)==calcsize(fmt).\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22771
diff changeset
1173 See struct.__doc__ for more on format strings.");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1174
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1175 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1176 struct_unpack(PyObject *self, PyObject *args)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1178 const formatdef *f, *e;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 char *str, *start, *fmt, *s;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 char c;
4865
f5073e24147c Scratch the ears of gcc -Wall.
Barry Warsaw <barry@python.org>
parents: 4861
diff changeset
1181 int len, size, num;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1182 PyObject *res, *v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1183
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1184 if (!PyArg_ParseTuple(args, "ss#:unpack", &fmt, &start, &len))
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1185 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1186 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1187 size = calcsize(fmt, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1188 if (size < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1189 return NULL;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1190 if (size != len) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1191 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1192 "unpack str size does not match format");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1193 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1194 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1195 res = PyList_New(0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1196 if (res == NULL)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1197 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1198 str = start;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1199 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1200 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1201 if (isspace((int)c))
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
1202 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1203 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205 while ('0' <= (c = *s++) && c <= '9')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 num = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1207 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1208 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1209 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1210 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1211 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1212
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1213 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1214 if (e == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1215 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1216 str = start + align((int)(str-start), c, e);
4842
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1217 if (num == 0 && c != 's')
84679f7bbe6d Fix the first bugs... treatment of 0 count was wrong, and memchr()
Guido van Rossum <guido@python.org>
parents: 4841
diff changeset
1218 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1219
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1220 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1221 if (c == 'x') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1222 str += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1224 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1225 if (c == 's') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1226 /* num is string size, not repeat count */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1227 v = PyString_FromStringAndSize(str, num);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1228 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1229 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1230 str += num;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1231 num = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1232 }
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1233 else if (c == 'p') {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1234 /* num is string buffer size,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1235 not repeat count */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1236 int n = *(unsigned char*)str;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1237 /* first byte (unsigned) is string size */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1238 if (n >= num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1239 n = num-1;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1240 v = PyString_FromStringAndSize(str+1, n);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1241 if (v == NULL)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1242 goto fail;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1243 str += num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1244 num = 0;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1245 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1246 else {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1247 v = e->unpack(str, e);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1248 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1249 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1250 str += e->size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1251 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1252 if (v == NULL || PyList_Append(res, v) < 0)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1253 goto fail;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1254 Py_DECREF(v);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1255 } while (--num > 0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1257
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1258 v = PyList_AsTuple(res);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1259 Py_DECREF(res);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1260 return v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1262 fail:
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1263 Py_DECREF(res);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1264 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1265 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1266
976
31b018bda7e2 unpack() now returns a tuple, not a list
Guido van Rossum <guido@python.org>
parents: 960
diff changeset
1267
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1268 /* List of functions */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1269
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1270 static PyMethodDef struct_methods[] = {
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1271 {"calcsize", struct_calcsize, METH_VARARGS, calcsize__doc__},
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1272 {"pack", struct_pack, METH_VARARGS, pack__doc__},
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1273 {"unpack", struct_unpack, METH_VARARGS, unpack__doc__},
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 {NULL, NULL} /* sentinel */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1275 };
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1276
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1277
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1278 /* Module initialization */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1279
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
1280 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13502
diff changeset
1281 initstruct(void)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1282 {
21944
f39213405e05 Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents: 20654
diff changeset
1283 PyObject *m;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1284
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1285 /* Create the module and add the functions */
6922
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1286 m = Py_InitModule4("struct", struct_methods, struct__doc__,
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
1287 (PyObject*)NULL, PYTHON_API_VERSION);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1288
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1289 /* Add some symbolic constants to the module */
21944
f39213405e05 Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents: 20654
diff changeset
1290 if (StructError == NULL) {
f39213405e05 Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents: 20654
diff changeset
1291 StructError = PyErr_NewException("struct.error", NULL, NULL);
f39213405e05 Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents: 20654
diff changeset
1292 if (StructError == NULL)
f39213405e05 Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents: 20654
diff changeset
1293 return;
f39213405e05 Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents: 20654
diff changeset
1294 }
f39213405e05 Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents: 20654
diff changeset
1295 Py_INCREF(StructError);
21945
cbf1dbca1f63 Fix typo.
Fred Drake <fdrake@acm.org>
parents: 21944
diff changeset
1296 PyModule_AddObject(m, "error", StructError);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1297 }