annotate Modules/structmodule.c @ 18084:bbcdbd8f4469 legacy-trunk

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