annotate Modules/structmodule.c @ 25366:0b68d810a8be 2.2

Backport 2.57 from trunk: (Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule: 64bit, big endian (issue 2 only). This adds a bunch of memcpy calls via a temporary variable to avoid alignment errors. That's needed for some platforms.
author Guido van Rossum <guido@python.org>
date Mon, 23 Sep 2002 20:54:04 +0000
parents edd9d0b3c47e
children eec3fd164a7d
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
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
6 static char 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
7 Functions to convert between Python values and C structs.\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
8 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
9 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
10 \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
11 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
12 @: 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
13 =: 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
14 <: 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
15 >: 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
16 !: 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
17 \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
18 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
19 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
20 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
21 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
22 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
23 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
24 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
25 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
26 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
27 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
28 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
29 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
30 \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
31 The variable struct.error is an exception raised on errors.";
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
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
33 #include "Python.h"
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
34
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
35 #include <ctype.h>
4841
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
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
38 /* Exception */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
39
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
40 static PyObject *StructError;
960
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
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
43 /* 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
44
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
45 #ifdef __MWERKS__
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
46 /*
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
47 ** 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
48 ** 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
49 */
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
50 #ifdef __powerc
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
51 #pragma options align=mac68k
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
52 #endif
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
53 #endif /* __MWERKS__ */
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
54
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
55 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
56 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
57 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
58 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
59 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
60 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
61
20654
edd9d0b3c47e SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents: 19924
diff changeset
62 #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
63 #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
64 #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
65 #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
66 #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
67 #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
68
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
69 /* 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
70 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
71 #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
72 typedef struct { char c; LONG_LONG x; } s_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
73 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(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
74 #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
75
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
76 #define STRINGIFY(x) #x
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
77
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
78 #ifdef __powerc
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
79 #pragma options align=reset
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
80 #endif
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
81
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
82 /* 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
83
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 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
85 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
86 {
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 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
88
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 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
90 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
91 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
92 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
93 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
94 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
95 }
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 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
97 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
98 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
99 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
100 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
101 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
102 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
103 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
104 }
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 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
106 "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
107 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
108 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
109
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
110 /* 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
111 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
112
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
113 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
114 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
115 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
116 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
117 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
118 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
119 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
120 "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
121 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
122 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
123 *p = x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
124 return 0;
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
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
127
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
128 /* 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
129
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
130 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
131 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
132 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
133 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
134 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
135 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
136 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
137 *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
138 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
139 }
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
140 else {
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
141 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
142 }
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
143 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
144
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
145 #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
146
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 /* 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
148
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
149 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
150 get_longlong(PyObject *v, LONG_LONG *p)
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
151 {
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 LONG_LONG 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
153
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
154 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
155 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
156 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
157 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
158 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
159 Py_DECREF(v);
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 if (x == (LONG_LONG)-1 && PyErr_Occurred())
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 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
162 *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
163 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
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
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 /* 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
167
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
168 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
169 get_ulonglong(PyObject *v, unsigned LONG_LONG *p)
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
170 {
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 unsigned LONG_LONG 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
172
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
173 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
174 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
175 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
176 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
177 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
178 Py_DECREF(v);
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 if (x == (unsigned LONG_LONG)-1 && PyErr_Occurred())
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 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
181 *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
182 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
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
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
185 #endif
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
186
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
187 /* Floating point helpers */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
188
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
189 /* These use ANSI/IEEE Standard 754-1985 (Standard for Binary Floating
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
190 Point Arithmetic). See the following URL:
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
191 http://www.psc.edu/general/software/packages/ieee/ieee.html */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
192
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
193 /* XXX Inf/NaN are not handled quite right (but underflow is!) */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
194
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
195 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
196 pack_float(double x, /* The number to pack */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
197 char *p, /* Where to pack the high order byte */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
198 int incr) /* 1 for big-endian; -1 for little-endian */
4858
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 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
201 int e;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
202 double f;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
203 long fbits;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
204
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
205 if (x < 0) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
206 s = 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
207 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
208 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
209 else
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
210 s = 0;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
211
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
212 f = frexp(x, &e);
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
213
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
214 /* Normalize f to be in the range [1.0, 2.0) */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
215 if (0.5 <= f && f < 1.0) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
216 f *= 2.0;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
217 e--;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
218 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
219 else if (f == 0.0) {
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
220 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
221 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
222 else {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
223 PyErr_SetString(PyExc_SystemError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
224 "frexp() result out of range");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
225 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
226 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
227
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
228 if (e >= 128) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
229 /* XXX 128 itself is reserved for Inf/NaN */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
230 PyErr_SetString(PyExc_OverflowError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
231 "float too large to pack with f format");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
232 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
233 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
234 else if (e < -126) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
235 /* Gradual underflow */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
236 f = ldexp(f, 126 + e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
237 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
238 }
6551
bdd4cb2a83b5 Fix due to Bill Noon for problem discovered by Ken Manheimer: packing
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
239 else if (!(e == 0 && f == 0.0)) {
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
240 e += 127;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
241 f -= 1.0; /* Get rid of leading 1 */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
242 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
243
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
244 f *= 8388608.0; /* 2**23 */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
245 fbits = (long) floor(f + 0.5); /* Round */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
246
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
247 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
248 *p = (s<<7) | (e>>1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
249 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
250
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
251 /* Second byte */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
252 *p = (char) (((e&1)<<7) | (fbits>>16));
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
253 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
254
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
255 /* Third byte */
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
256 *p = (fbits>>8) & 0xFF;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
257 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
258
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
259 /* Fourth byte */
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
260 *p = fbits&0xFF;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
261
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
262 /* Done */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
263 return 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
264 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
265
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
266 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
267 pack_double(double x, /* The number to pack */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
268 char *p, /* Where to pack the high order byte */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
269 int incr) /* 1 for big-endian; -1 for little-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
270 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
271 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
272 int e;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
273 double f;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
274 long fhi, flo;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
275
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
276 if (x < 0) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
277 s = 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
278 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
279 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
280 else
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
281 s = 0;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
282
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
283 f = frexp(x, &e);
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
284
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
285 /* Normalize f to be in the range [1.0, 2.0) */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
286 if (0.5 <= f && f < 1.0) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
287 f *= 2.0;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
288 e--;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
289 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
290 else if (f == 0.0) {
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
291 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
292 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
293 else {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
294 PyErr_SetString(PyExc_SystemError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
295 "frexp() result out of range");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
296 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
297 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
298
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
299 if (e >= 1024) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
300 /* XXX 1024 itself is reserved for Inf/NaN */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
301 PyErr_SetString(PyExc_OverflowError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
302 "float too large to pack with d format");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
303 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
304 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
305 else if (e < -1022) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
306 /* Gradual underflow */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
307 f = ldexp(f, 1022 + e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
308 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
309 }
6551
bdd4cb2a83b5 Fix due to Bill Noon for problem discovered by Ken Manheimer: packing
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
310 else if (!(e == 0 && f == 0.0)) {
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
311 e += 1023;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
312 f -= 1.0; /* Get rid of leading 1 */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
313 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
314
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
315 /* fhi receives the high 28 bits; flo the low 24 bits (== 52 bits) */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
316 f *= 268435456.0; /* 2**28 */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
317 fhi = (long) floor(f); /* Truncate */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
318 f -= (double)fhi;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
319 f *= 16777216.0; /* 2**24 */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
320 flo = (long) floor(f + 0.5); /* Round */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
321
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
322 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
323 *p = (s<<7) | (e>>4);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
324 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
325
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
326 /* Second byte */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
327 *p = (char) (((e&0xF)<<4) | (fhi>>24));
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
328 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
329
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
330 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
331 *p = (fhi>>16) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
332 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
333
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
334 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
335 *p = (fhi>>8) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
336 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
337
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
338 /* Fifth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
339 *p = fhi & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
340 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
341
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
342 /* Sixth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
343 *p = (flo>>16) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
344 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
345
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
346 /* Seventh byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
347 *p = (flo>>8) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
348 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
349
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
350 /* Eighth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
351 *p = flo & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
352 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
353
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
354 /* Done */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
355 return 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
356 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
357
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
358 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
359 unpack_float(const char *p, /* Where the high order byte is */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
360 int incr) /* 1 for big-endian; -1 for little-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
361 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
362 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
363 int e;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
364 long f;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
365 double x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
366
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
367 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
368 s = (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
369 e = (*p & 0x7F) << 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
370 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
371
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
372 /* Second byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
373 e |= (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
374 f = (*p & 0x7F) << 16;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
375 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
376
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
377 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
378 f |= (*p & 0xFF) << 8;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
379 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
380
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
381 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
382 f |= *p & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
383
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
384 x = (double)f / 8388608.0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
385
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
386 /* XXX This sadly ignores Inf/NaN issues */
4859
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
387 if (e == 0)
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
388 e = -126;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
389 else {
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
390 x += 1.0;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
391 e -= 127;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
392 }
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
393 x = ldexp(x, e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
394
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
395 if (s)
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
396 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
397
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
398 return PyFloat_FromDouble(x);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
399 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
400
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
401 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
402 unpack_double(const char *p, /* Where the high order byte is */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
403 int incr) /* 1 for big-endian; -1 for little-endian */
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
404 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
405 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
406 int e;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
407 long fhi, flo;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
408 double x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
409
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
410 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
411 s = (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
412 e = (*p & 0x7F) << 4;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
413 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
414
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
415 /* Second byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
416 e |= (*p>>4) & 0xF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
417 fhi = (*p & 0xF) << 24;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
418 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
419
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
420 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
421 fhi |= (*p & 0xFF) << 16;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
422 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
423
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
424 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
425 fhi |= (*p & 0xFF) << 8;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
426 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
427
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
428 /* Fifth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
429 fhi |= *p & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
430 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
431
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
432 /* Sixth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
433 flo = (*p & 0xFF) << 16;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
434 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
435
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
436 /* Seventh byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
437 flo |= (*p & 0xFF) << 8;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
438 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
439
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
440 /* Eighth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
441 flo |= *p & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
442 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
443
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
444 x = (double)fhi + (double)flo / 16777216.0; /* 2**24 */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
445 x /= 268435456.0; /* 2**28 */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
446
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
447 /* XXX This sadly ignores Inf/NaN */
4859
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
448 if (e == 0)
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
449 e = -1022;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
450 else {
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
451 x += 1.0;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
452 e -= 1023;
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
453 }
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
454 x = ldexp(x, e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
455
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
456 if (s)
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
457 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
458
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
459 return PyFloat_FromDouble(x);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
460 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
461
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
462
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
463 /* 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
464
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
465 typedef struct _formatdef {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
466 char format;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
467 int size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
468 int alignment;
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
469 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
470 const struct _formatdef *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
471 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
472 const struct _formatdef *);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
473 } formatdef;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
474
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
475 /* 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
476
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
477 [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
478
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 [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
480 [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
481 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
482 */
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
483
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
484 /* Native mode routines. ****************************************************/
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
485 /* NOTE:
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
486 In all n[up]_<type> routines handling types larger than 1 byte, there is
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
487 *no* guarantee that the p pointer is properly aligned for each type,
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
488 therefore memcpy is called. An intermediate variable is used to
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
489 compensate for big-endian architectures.
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
490 Normally both the intermediate variable and the memcpy call will be
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
491 skipped by C optimisation in little-endian architectures (gcc >= 2.91
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
492 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
493
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
494 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
495 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
496 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
497 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
498 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
499
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
500 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
501 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
502 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
503 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
504 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
505
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
506 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
507 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
508 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
509 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
510 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
511
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
512 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
513 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
514 {
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
515 short x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
516 memcpy((char *)&x, p, sizeof x);
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
517 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
518 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
519
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
520 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
521 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
522 {
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
523 unsigned short x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
524 memcpy((char *)&x, p, sizeof x);
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
525 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
526 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
527
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
528 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
529 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
530 {
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
531 int x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
532 memcpy((char *)&x, p, sizeof x);
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
533 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
534 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
535
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
536 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
537 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
538 {
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
539 unsigned int x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
540 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
541 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
542 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
543
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
544 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
545 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
546 {
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
547 long x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
548 memcpy((char *)&x, p, sizeof x);
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
549 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
550 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
551
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
552 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
553 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
554 {
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
555 unsigned long x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
556 memcpy((char *)&x, p, sizeof x);
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
557 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
558 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
559
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
560 /* 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
561 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
562
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
563 #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
564
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
565 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
566 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
567 {
18705
8df77415a61d SF bug #442520: test_struct fails on SPARC.
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
568 LONG_LONG x;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
569 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
570 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
571 }
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
572
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
573 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
574 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
575 {
18705
8df77415a61d SF bug #442520: test_struct fails on SPARC.
Tim Peters <tim.peters@gmail.com>
parents: 18094
diff changeset
576 unsigned LONG_LONG x;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
577 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
578 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
579 }
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
580
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
581 #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
582
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
583 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
584 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
585 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
586 float x;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
587 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
588 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
589 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
590
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
591 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
592 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
593 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
594 double x;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
595 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
596 return PyFloat_FromDouble(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
597 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
598
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
599 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
600 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
601 {
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
602 void *x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
603 memcpy((char *)&x, p, sizeof x);
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
604 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
605 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
606
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
607 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
608 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
609 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
610 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
611 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
612 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
613 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
614 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
615 "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
616 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
617 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
618 *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
619 return 0;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
620 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
621
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
622 static int
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
623 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
624 {
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
625 long x;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
626 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
627 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
628 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
629 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
630 "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
631 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
632 }
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
633 *p = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
634 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
635 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
636
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
637 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
638 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
639 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
640 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
641 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
642 "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
643 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
644 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
645 *p = *PyString_AsString(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
646 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
647 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
648
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
649 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
650 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
651 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
652 long x;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
653 short y;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
654 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
655 return -1;
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
656 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
657 PyErr_SetString(StructError,
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
658 "short format requires " STRINGIFY(SHRT_MIN)
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
659 "<=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
660 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
661 }
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
662 y = (short)x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
663 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
664 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
665 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
666
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
667 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
668 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
669 {
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
670 long x;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
671 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
672 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
673 return -1;
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
674 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
675 PyErr_SetString(StructError,
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
676 "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
677 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
678 }
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
679 y = (unsigned short)x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
680 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
681 return 0;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
682 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
683
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
684 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
685 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
686 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
687 long x;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
688 int y;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
689 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
690 return -1;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
691 y = (int)x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
692 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
693 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
694 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
695
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
696 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
697 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
698 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
699 unsigned long x;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
700 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
701 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
702 return -1;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
703 y = (unsigned int)x;
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
704 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
705 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
706 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
707
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
708 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
709 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
710 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
711 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
712 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
713 return -1;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
714 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
715 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
716 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
717
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
718 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
719 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
720 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
721 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
722 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
723 return -1;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
724 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
725 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
726 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
727
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
728 #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
729
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
730 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
731 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
732 {
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
733 LONG_LONG 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
734 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
735 return -1;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
736 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
737 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
738 }
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
739
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
740 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
741 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
742 {
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
743 unsigned LONG_LONG 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
744 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
745 return -1;
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
746 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
747 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
748 }
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
749 #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
750
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
751 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
752 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
753 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
754 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
755 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
756 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
757 "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
758 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
759 }
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
760 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
761 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
762 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
763
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
764 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
765 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
766 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
767 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
768 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
769 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
770 "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
771 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
772 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
773 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
774 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
775 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
776
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
777 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
778 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
779 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
780 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
781 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
782 /* ### 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
783 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
784 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
785 "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
786 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
787 }
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
788 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
789 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
790 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
791
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
792 static formatdef native_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
793 {'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
794 {'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
795 {'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
796 {'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
797 {'s', sizeof(char), 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
798 {'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
799 {'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
800 {'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
801 {'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
802 {'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
803 {'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
804 {'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
805 {'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
806 {'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
807 {'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
808 #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
809 {'q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong},
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
810 {'Q', sizeof(LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong},
8e187740aa96 Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents: 17343
diff changeset
811 #endif
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
812 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
813 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
814
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
815 /* 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
816
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
817 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
818 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
819 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
820 long x = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
821 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
822 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
823 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
824 } 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
825 /* 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
826 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
827 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
828 return PyInt_FromLong(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
829 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
830
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
831 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
832 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
833 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
834 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
835 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
836 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
837 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
838 } 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
839 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
840 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
841 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
842 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
843 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
844
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
845 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
846 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
847 {
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 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
849 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
850 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
851 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
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
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
854 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
855 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
856 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
857 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
858 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
859 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
860 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
861 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
862
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
863 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
864 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
865 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
866 return unpack_float(p, 1);
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
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
869 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
870 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
871 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
872 return unpack_double(p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
873 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
874
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
875 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
876 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
877 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
878 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
879 int i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
880 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
881 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
882 i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
883 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
884 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
885 x >>= 8;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
886 } while (i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
887 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
888 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
889
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
890 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
891 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
892 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
893 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
894 int i;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
895 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
896 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
897 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
898 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
899 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
900 x >>= 8;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
901 } 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
902 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
903 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
904
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
905 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
906 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
907 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
908 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
909 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
910 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
911 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
912 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
913 (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
914 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
915 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
916 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
917 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
918 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
919 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
920
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
921 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
922 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
923 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
924 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
925 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
926 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
927 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
928 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
929 (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
930 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
931 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
932 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
933 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
934 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
935 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
936
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
937 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
938 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
939 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
940 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
941 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
942 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
943 "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
944 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
945 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
946 return pack_float(x, p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
947 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
948
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
949 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
950 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
951 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
952 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
953 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
954 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
955 "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
956 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
957 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
958 return pack_double(x, p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
959 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
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 static formatdef bigendian_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
962 {'x', 1, 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
963 {'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
964 {'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
965 {'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
966 {'s', 1, 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
967 {'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
968 {'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
969 {'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
970 {'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
971 {'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
972 {'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
973 {'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
974 {'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
975 {'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
976 {'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
977 {'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
978 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
979 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
980
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
981 /* 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
982
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
983 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
984 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
985 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
986 long x = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
987 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
988 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
989 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
990 } 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
991 /* 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
992 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
993 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
994 return PyInt_FromLong(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
995 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
996
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
997 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
998 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
999 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
1000 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
1001 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1002 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1003 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
1004 } 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
1005 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
1006 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
1007 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
1008 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
1009 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1010
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1011 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
1012 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
1013 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1014 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
1015 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
1016 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
1017 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
1018 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1019
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1020 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
1021 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
1022 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1023 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
1024 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
1025 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
1026 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
1027 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1028
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1029 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1030 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
1031 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1032 return unpack_float(p+3, -1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1033 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1034
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1035 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1036 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
1037 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1038 return unpack_double(p+7, -1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1039 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1040
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1041 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1042 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
1043 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1044 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1045 int i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1046 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
1047 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1048 i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1049 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
1050 *p++ = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1051 x >>= 8;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1052 } while (--i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1053 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1054 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1055
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
1056 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1057 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
1058 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
1059 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
1060 int i;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
1061 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
1062 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
1063 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
1064 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
1065 *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
1066 x >>= 8;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
1067 } 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
1068 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
1069 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
1070
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1071 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
1072 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
1073 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1074 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
1075 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
1076 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
1077 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
1078 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
1079 (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
1080 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
1081 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
1082 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
1083 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
1084 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
1085 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1086
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1087 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
1088 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
1089 {
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1090 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
1091 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
1092 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
1093 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
1094 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
1095 (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
1096 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
1097 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
1098 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
1099 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
1100 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
1101 }
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1102
bbcdbd8f4469 Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents: 18079
diff changeset
1103 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1104 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
1105 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1106 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1107 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1108 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1109 "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
1110 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1111 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1112 return pack_float(x, p+3, -1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1113 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1114
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1115 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1116 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
1117 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1118 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1119 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1120 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1121 "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
1122 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1123 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1124 return pack_double(x, p+7, -1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1125 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
1126
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1127 static formatdef lilendian_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1128 {'x', 1, 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1129 {'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
1130 {'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
1131 {'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
1132 {'s', 1, 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1133 {'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
1134 {'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
1135 {'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
1136 {'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
1137 {'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
1138 {'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
1139 {'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
1140 {'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
1141 {'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
1142 {'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
1143 {'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
1144 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1145 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1146
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1147
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1148 static const formatdef *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1149 whichtable(char **pfmt)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1150 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1151 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
1152 switch (*fmt) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1153 case '<':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1154 return lilendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1155 case '>':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1156 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
1157 return bigendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1158 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
1159 int n = 1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1160 char *p = (char *) &n;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1161 if (*p == 1)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1162 return lilendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1163 else
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1164 return bigendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1165 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1166 default:
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1167 --*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
1168 /* Fall through */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1169 case '@':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1170 return native_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1171 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1172 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1173
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1174
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1175 /* 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
1176
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1177 static const formatdef *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1178 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
1179 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1180 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
1181 if (f->format == c) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1182 return f;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1183 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1184 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1185 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
1186 return NULL;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1187 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1188
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1189
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1190 /* 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
1191
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1192 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1193 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
1194 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1195 if (e->format == c) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1196 if (e->alignment) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1197 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
1198 / e->alignment)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1199 * e->alignment;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1200 }
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1201 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1202 return size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1203 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1204
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1205
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1206 /* 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
1207
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1208 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1209 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
1210 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1211 const formatdef *e;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1212 const char *s;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1213 char c;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1214 int size, num, itemsize, x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1215
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1216 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1217 size = 0;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1218 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1219 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
1220 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1221 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1222 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 while ('0' <= (c = *s++) && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1224 x = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1225 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
1226 PyErr_SetString(
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1227 StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1228 "overflow in item count");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229 return -1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1230 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1231 num = x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1232 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1234 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1235 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1236 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1237 num = 1;
18079
bc05b0b5e057 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 18078
diff changeset
1238
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1239 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1240 if (e == NULL)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1241 return -1;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1242 itemsize = e->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1243 size = align(size, c, e);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1244 x = num * itemsize;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1245 size += x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1246 if (x/itemsize != num || size < 0) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1247 PyErr_SetString(StructError,
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
1248 "total struct size too long");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1249 return -1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1250 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1251 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1252
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1253 return size;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1254 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1255
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256
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
1257 static char 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
1258 calcsize(fmt) -> 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
1259 Return size of C struct described by format string fmt.\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
1260 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
1261
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1262 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1263 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
1264 {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1265 char *fmt;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1266 const formatdef *f;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267 int size;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1268
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1269 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
1270 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1271 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1272 size = calcsize(fmt, f);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1273 if (size < 0)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1274 return NULL;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1275 return PyInt_FromLong((long)size);
960
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
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
1279 static char 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
1280 pack(fmt, v1, v2, ...) -> string\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
1281 Return string containing values v1, v2, ... packed according to fmt.\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
1282 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
1283
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1284 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1285 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
1286 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1287 const formatdef *f, *e;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1288 PyObject *format, *result, *v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1289 char *fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1290 int size, num;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1291 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
1292 char *s, *res, *restart, *nres;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1293 char c;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1294
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1295 if (args == NULL || !PyTuple_Check(args) ||
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1296 (n = PyTuple_Size(args)) < 1)
25366
0b68d810a8be Backport 2.57 from trunk:
Guido van Rossum <guido@python.org>
parents: 20654
diff changeset
1297 {
18079
bc05b0b5e057 Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents: 18078
diff changeset
1298 PyErr_SetString(PyExc_TypeError,
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 11882
diff changeset
1299 "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
1300 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1301 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1302 format = PyTuple_GetItem(args, 0);
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1303 if (!PyArg_Parse(format, "s", &fmt))
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1304 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1305 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1306 size = calcsize(fmt, f);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1307 if (size < 0)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1308 return NULL;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1309 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
1310 if (result == NULL)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1311 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1312
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1313 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1314 i = 1;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1315 res = restart = PyString_AsString(result);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1316
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1317 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1318 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
1319 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1320 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1321 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1322 while ('0' <= (c = *s++) && c <= '9')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1323 num = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1324 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1325 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1326 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1327 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1328 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1329
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1330 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1331 if (e == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1332 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
1333 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
1334 /* 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
1335 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
1336 *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
1337 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
1338 continue;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1339 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1340 if (c == 'x') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1341 /* 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
1342 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
1343 res += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1344 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1345 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1346 if (i >= n) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1347 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1348 "insufficient arguments to pack");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1349 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1350 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1351 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
1352 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1353 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1354 if (c == 's') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1355 /* 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
1356 int n;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1357 if (!PyString_Check(v)) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1358 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1359 "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
1360 goto fail;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1361 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1362 n = PyString_Size(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1363 if (n > num)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1364 n = num;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1365 if (n > 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1366 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
1367 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
1368 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
1369 res += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1370 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1371 }
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1372 else if (c == 'p') {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1373 /* num is string size + 1,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1374 to fit in the count byte */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1375 int n;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1376 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
1377 if (!PyString_Check(v)) {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1378 PyErr_SetString(StructError,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1379 "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
1380 goto fail;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1381 }
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1382 n = PyString_Size(v);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1383 if (n > num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1384 n = num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1385 if (n > 0)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1386 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
1387 if (n < num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1388 /* 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
1389 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
1390 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
1391 n = 255;
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1392 *res++ = n; /* store the length byte */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1393 res += num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1394 break;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1395 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1396 else {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1397 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
1398 goto fail;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1399 res += e->size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1400 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1401 } while (--num > 0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1402 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1403
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1404 if (i < n) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1405 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1406 "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
1407 goto fail;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1408 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1409
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1410 return result;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1411
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1412 fail:
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1413 Py_DECREF(result);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1414 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1415 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1416
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1417
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
1418 static char unpack__doc__[] = "\
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
1419 unpack(fmt, string) -> (v1, v2, ...)\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
1420 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
1421 to fmt. Requires len(string)==calcsize(fmt).\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
1422 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
1423
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1424 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1425 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
1426 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1427 const formatdef *f, *e;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1428 char *str, *start, *fmt, *s;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1429 char c;
4865
f5073e24147c Scratch the ears of gcc -Wall.
Barry Warsaw <barry@python.org>
parents: 4861
diff changeset
1430 int len, size, num;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1431 PyObject *res, *v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1432
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1433 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
1434 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1435 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1436 size = calcsize(fmt, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1437 if (size < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1438 return NULL;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1439 if (size != len) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1440 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1441 "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
1442 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1443 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1444 res = PyList_New(0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1445 if (res == NULL)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1446 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1447 str = start;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1448 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1449 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1450 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
1451 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1452 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1453 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1454 while ('0' <= (c = *s++) && c <= '9')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1455 num = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1456 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1457 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1458 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1459 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1460 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1461
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1462 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1463 if (e == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1464 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1465 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
1466 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
1467 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1468
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1469 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1470 if (c == 'x') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1471 str += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1472 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1473 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1474 if (c == 's') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1475 /* 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
1476 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
1477 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1478 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1479 str += num;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1480 num = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1481 }
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1482 else if (c == 'p') {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1483 /* num is string buffer size,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1484 not repeat count */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1485 int n = *(unsigned char*)str;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1486 /* first byte (unsigned) is string size */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1487 if (n >= num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1488 n = num-1;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1489 v = PyString_FromStringAndSize(str+1, n);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1490 if (v == NULL)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1491 goto fail;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1492 str += num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1493 num = 0;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1494 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1495 else {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1496 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
1497 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1498 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1499 str += e->size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1500 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1501 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
1502 goto fail;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1503 Py_DECREF(v);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1504 } while (--num > 0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1505 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1506
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1507 v = PyList_AsTuple(res);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1508 Py_DECREF(res);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1509 return v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1510
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1511 fail:
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1512 Py_DECREF(res);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1513 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1514 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1515
976
31b018bda7e2 unpack() now returns a tuple, not a list
Guido van Rossum <guido@python.org>
parents: 960
diff changeset
1516
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1517 /* List of functions */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1518
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1519 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
1520 {"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
1521 {"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
1522 {"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
1523 {NULL, NULL} /* sentinel */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1524 };
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1525
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1526
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1527 /* Module initialization */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1528
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9123
diff changeset
1529 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13502
diff changeset
1530 initstruct(void)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1531 {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1532 PyObject *m, *d;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1533
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1534 /* 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
1535 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
1536 (PyObject*)NULL, PYTHON_API_VERSION);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1537
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1538 /* Add some symbolic constants to the module */
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1539 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1540 StructError = PyErr_NewException("struct.error", NULL, NULL);
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1541 if (StructError == NULL)
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1542 return;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1543 PyDict_SetItemString(d, "error", StructError);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1544 }