annotate Modules/structmodule.c @ 17581:c1937faabb23 2.1

Added support for optional explanation parameter to the \versionadded macro. Refactored do_cmd_versionadded() and do_cmd_versionchanged() to do most of the work in a helper function, with the do_cmd_*() wrappers just supplying a portion of the replacement text.
author Fred Drake <fdrake@acm.org>
date Wed, 18 Apr 2001 17:29:14 +0000
parents de9858530a55
children 104223b3226a 8e187740aa96
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\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
12 The optional first format char indicates byte ordering and alignment:\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
13 @: native w/native alignment(default)\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
14 =: native w/standard alignment\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
15 <: little-endian, std. alignment\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
16 >: big-endian, std. alignment\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
17 !: network, std (same as >)\n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
18 \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\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
25 s:string (array of char); p: pascal string (w. 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\
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
28 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
29 \n\
7629c4d985bc Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents: 6551
diff changeset
30 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
31
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
32 #include "Python.h"
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
33
6167
9e432468f756 Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents: 5318
diff changeset
34 #include <ctype.h>
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
35
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
36
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
37 /* Exception */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
38
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
39 static PyObject *StructError;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
40
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
41
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 /* Define various structs to figure out the alignments of types */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
43
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
44 #ifdef __MWERKS__
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
45 /*
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
46 ** XXXX We have a problem here. There are no unique alignment rules
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
47 ** on the PowerPC mac.
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
48 */
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
49 #ifdef __powerc
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
50 #pragma options align=mac68k
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
51 #endif
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
52 #endif /* __MWERKS__ */
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
53
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
54 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
55 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
56 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
57 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
58 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
59 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
60
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
61 #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
62 #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
63 #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
64 #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
65 #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
66 #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
67
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
68 #define STRINGIFY(x) #x
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
69
2278
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
70 #ifdef __powerc
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
71 #pragma options align=reset
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
72 #endif
65644766158a Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents: 2061
diff changeset
73
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
74 /* 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
75 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
76
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
77 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
78 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
79 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
80 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
81 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
82 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
83 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
84 "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
85 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
86 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
87 *p = x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
88 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
89 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
90
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
91
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
92 /* 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
93
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
94 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
95 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
96 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
97 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
98 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
99 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
100 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
101 *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
102 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
103 }
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
104 else {
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
105 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
106 }
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
107 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
108
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
109
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
110 /* Floating point helpers */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
111
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
112 /* 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
113 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
114 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
115
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
116 /* 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
117
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
118 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
119 pack_float(double x, /* The number to pack */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
120 char *p, /* Where to pack the high order byte */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
121 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
122 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
123 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
124 int e;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
125 double f;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
126 long fbits;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
127
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
128 if (x < 0) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
129 s = 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
130 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
131 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
132 else
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
133 s = 0;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
134
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
135 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
136
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
137 /* 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
138 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
139 f *= 2.0;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
140 e--;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
141 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
142 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
143 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
144 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
145 else {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
146 PyErr_SetString(PyExc_SystemError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
147 "frexp() result out of range");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
148 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
149 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
150
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
151 if (e >= 128) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
152 /* 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
153 PyErr_SetString(PyExc_OverflowError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
154 "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
155 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
156 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
157 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
158 /* Gradual underflow */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
159 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
160 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
161 }
6551
bdd4cb2a83b5 Fix due to Bill Noon for problem discovered by Ken Manheimer: packing
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
162 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
163 e += 127;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
164 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
165 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
166
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
167 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
168 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
169
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
170 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
171 *p = (s<<7) | (e>>1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
172 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
173
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
174 /* Second byte */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
175 *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
176 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
177
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
178 /* Third byte */
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
179 *p = (fbits>>8) & 0xFF;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
180 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
181
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
182 /* Fourth byte */
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
183 *p = fbits&0xFF;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
184
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
185 /* Done */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
186 return 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
187 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
188
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
189 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
190 pack_double(double x, /* The number to pack */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
191 char *p, /* Where to pack the high order byte */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
192 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
193 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
194 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
195 int e;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
196 double f;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
197 long fhi, flo;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
198
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
199 if (x < 0) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
200 s = 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
201 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
202 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
203 else
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
204 s = 0;
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
205
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
206 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
207
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
208 /* 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
209 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
210 f *= 2.0;
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
211 e--;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
212 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
213 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
214 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
215 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
216 else {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
217 PyErr_SetString(PyExc_SystemError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
218 "frexp() result out of range");
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
219 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
220 }
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
221
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
222 if (e >= 1024) {
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
223 /* 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
224 PyErr_SetString(PyExc_OverflowError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
225 "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
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 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
229 /* Gradual underflow */
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
230 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
231 e = 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
232 }
6551
bdd4cb2a83b5 Fix due to Bill Noon for problem discovered by Ken Manheimer: packing
Guido van Rossum <guido@python.org>
parents: 6367
diff changeset
233 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
234 e += 1023;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
235 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
236 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
237
4861
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
238 /* 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
239 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
240 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
241 f -= (double)fhi;
69ca5e3ccc6d Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents: 4859
diff changeset
242 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
243 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
244
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
245 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
246 *p = (s<<7) | (e>>4);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
247 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
248
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
249 /* Second byte */
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
250 *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
251 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
252
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
253 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
254 *p = (fhi>>16) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
255 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
256
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
257 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
258 *p = (fhi>>8) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
259 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
260
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
261 /* Fifth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
262 *p = fhi & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
263 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
264
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
265 /* Sixth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
266 *p = (flo>>16) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
267 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
268
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
269 /* Seventh byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
270 *p = (flo>>8) & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
271 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
272
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
273 /* Eighth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
274 *p = flo & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
275 p += incr;
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 /* Done */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
278 return 0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
279 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
280
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
281 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
282 unpack_float(const char *p, /* Where the high order byte is */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
283 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
284 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
285 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
286 int e;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
287 long f;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
288 double x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
289
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
290 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
291 s = (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
292 e = (*p & 0x7F) << 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
293 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
294
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
295 /* Second byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
296 e |= (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
297 f = (*p & 0x7F) << 16;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
298 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
299
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
300 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
301 f |= (*p & 0xFF) << 8;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
302 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
303
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
304 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
305 f |= *p & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
306
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
307 x = (double)f / 8388608.0;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
308
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
309 /* 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
310 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
311 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
312 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
313 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
314 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
315 }
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
316 x = ldexp(x, e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
317
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
318 if (s)
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
319 x = -x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
320
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
321 return PyFloat_FromDouble(x);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
322 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
323
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
324 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
325 unpack_double(const char *p, /* Where the high order byte is */
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
326 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
327 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
328 int s;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
329 int e;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
330 long fhi, flo;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
331 double x;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
332
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
333 /* First byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
334 s = (*p>>7) & 1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
335 e = (*p & 0x7F) << 4;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
336 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
337
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
338 /* Second byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
339 e |= (*p>>4) & 0xF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
340 fhi = (*p & 0xF) << 24;
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 /* Third byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
344 fhi |= (*p & 0xFF) << 16;
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 /* Fourth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
348 fhi |= (*p & 0xFF) << 8;
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 /* Fifth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
352 fhi |= *p & 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 /* Sixth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
356 flo = (*p & 0xFF) << 16;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
357 p += incr;
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 /* Seventh byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
360 flo |= (*p & 0xFF) << 8;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
361 p += incr;
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 /* Eighth byte */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
364 flo |= *p & 0xFF;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
365 p += incr;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
366
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
367 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
368 x /= 268435456.0; /* 2**28 */
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
369
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
370 /* 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
371 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
372 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
373 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
374 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
375 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
376 }
ab461c762559 Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents: 4858
diff changeset
377 x = ldexp(x, e);
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
378
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
379 if (s)
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
380 x = -x;
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 return PyFloat_FromDouble(x);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
383 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
384
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
385
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
386 /* 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
387
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
388 typedef struct _formatdef {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
389 char format;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
390 int size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
391 int alignment;
13447
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
392 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
393 const struct _formatdef *);
a89f7f268de4 Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents: 13256
diff changeset
394 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
395 const struct _formatdef *);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
396 } formatdef;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
397
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
398 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
399 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
400 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
401 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
402 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
403
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
404 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
405 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
406 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
407 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
408 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
409
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
410 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
411 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
412 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
413 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
414 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
415
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
416 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
417 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
418 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
419 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
420 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
421
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
422 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
423 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
424 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
425 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
426 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
427
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
428 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
429 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
430 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
431 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
432 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
433
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
434 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
435 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
436 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
437 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
438 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
439 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
440
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
441 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
442 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
443 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
444 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
445 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
446
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
447 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
448 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
449 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
450 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
451 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
452
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
453 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
454 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
455 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
456 float x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
457 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
458 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
459 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
460
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
461 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
462 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
463 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
464 double x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
465 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
466 return PyFloat_FromDouble(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
467 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
468
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
469 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
470 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
471 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
472 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
473 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
474
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
475 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
476 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
477 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
478 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
479 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
480 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
481 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
482 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
483 "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
484 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
485 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
486 *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
487 return 0;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
488 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
489
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
490 static int
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
491 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
492 {
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
493 long x;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
494 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
495 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
496 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
497 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
498 "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
499 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
500 }
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
501 *p = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
502 return 0;
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 int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
506 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
507 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
508 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
509 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
510 "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
511 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
512 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
513 *p = *PyString_AsString(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
514 return 0;
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 int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
518 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
519 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
520 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
521 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
522 return -1;
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
523 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
524 PyErr_SetString(StructError,
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
525 "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
526 "<=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
527 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
528 }
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
529 * (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
530 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
531 }
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 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
534 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
535 {
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
536 long x;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
537 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
538 return -1;
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
539 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
540 PyErr_SetString(StructError,
14587
e8d8758adfdb Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents: 14583
diff changeset
541 "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
542 return -1;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
543 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
544 * (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
545 return 0;
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
546 }
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
547
213dbc880159 Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents: 14375
diff changeset
548 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
549 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
550 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
551 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
552 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
553 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
554 * (int *)p = x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
555 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
556 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
557
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
558 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
559 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
560 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
561 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
562 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
563 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
564 * (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
565 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
566 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
567
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
568 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
569 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
570 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
571 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
572 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
573 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
574 * (long *)p = x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
575 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
576 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
577
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
578 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
579 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
580 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
581 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
582 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
583 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
584 * (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
585 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
586 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
587
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
588 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
589 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
590 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
591 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
592 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
593 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
594 "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
595 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
596 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
597 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
598 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
599 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
600
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
601 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
602 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
603 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
604 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
605 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
606 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
607 "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
608 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
609 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
610 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
611 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
612 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
613
9123
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
614 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
615 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
616 {
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
617 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
618 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
619 /* ### 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
620 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
621 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
622 "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
623 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
624 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
625 *(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
626 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
627 }
17d0c51e0887 Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents: 8580
diff changeset
628
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
629 static formatdef native_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
630 {'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
631 {'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
632 {'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
633 {'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
634 {'s', sizeof(char), 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
635 {'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
636 {'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
637 {'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
638 {'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
639 {'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
640 {'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
641 {'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
642 {'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
643 {'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
644 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p},
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
645 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
646 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
647
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
648 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
649 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
650 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
651 long x = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
652 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
653 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
654 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
655 } 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
656 /* 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
657 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
658 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
659 return PyInt_FromLong(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
660 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
661
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
662 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
663 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
664 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
665 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
666 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
667 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
668 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
669 } 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
670 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
671 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
672 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
673 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
674 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
675
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
676 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
677 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
678 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
679 return unpack_float(p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
680 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
681
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
682 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
683 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
684 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
685 return unpack_double(p, 1);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
686 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
687
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
688 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
689 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
690 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
691 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
692 int i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
693 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
694 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
695 i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
696 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
697 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
698 x >>= 8;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
699 } while (i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
700 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
701 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
702
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
703 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
704 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
705 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
706 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
707 int i;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
708 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
709 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
710 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
711 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
712 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
713 x >>= 8;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
714 } 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
715 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
716 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
717
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
718 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
719 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
720 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
721 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
722 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
723 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
724 "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
725 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
726 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
727 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
728 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
729
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
730 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
731 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
732 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
733 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
734 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
735 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
736 "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
737 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
738 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
739 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
740 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
741
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
742 static formatdef bigendian_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
743 {'x', 1, 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
744 {'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
745 {'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
746 {'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
747 {'s', 1, 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
748 {'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
749 {'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
750 {'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
751 {'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
752 {'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
753 {'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
754 {'L', 4, 0, bu_uint, bp_uint},
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
755 {'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
756 {'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
757 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
758 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
759
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
760 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
761 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
762 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
763 long x = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
764 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
765 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
766 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
767 } 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
768 /* 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
769 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
770 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
771 return PyInt_FromLong(x);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
772 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
773
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
774 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
775 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
776 {
4874
03bcdd22f6a4 Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents: 4867
diff changeset
777 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
778 int i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
779 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
780 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
781 } 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
782 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
783 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
784 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
785 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
786 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
787
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
788 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
789 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
790 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
791 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
792 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
793
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
794 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
795 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
796 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
797 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
798 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
799
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
800 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
801 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
802 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
803 long x;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
804 int i;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
805 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
806 return -1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
807 i = f->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
808 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
809 *p++ = (char)x;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
810 x >>= 8;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
811 } while (--i > 0);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
812 return 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
813 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
814
4846
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
815 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
816 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
817 {
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
818 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
819 int i;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
820 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
821 return -1;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
822 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
823 do {
5318
2c4b802a3b5d Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents: 4874
diff changeset
824 *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
825 x >>= 8;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
826 } 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
827 return 0;
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
828 }
972026d47274 Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents: 4842
diff changeset
829
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
830 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
831 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
832 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
833 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
834 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
835 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
836 "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
837 return -1;
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 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
840 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
841
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
842 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
843 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
844 {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
845 double x = PyFloat_AsDouble(v);
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
846 if (x == -1 && PyErr_Occurred()) {
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
847 PyErr_SetString(StructError,
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
848 "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
849 return -1;
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
850 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
851 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
852 }
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
853
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
854 static formatdef lilendian_table[] = {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
855 {'x', 1, 0, NULL},
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
856 {'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
857 {'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
858 {'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
859 {'s', 1, 0, NULL},
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
860 {'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
861 {'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
862 {'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
863 {'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
864 {'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
865 {'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
866 {'L', 4, 0, lu_uint, lp_uint},
4858
198989ee36d5 Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents: 4846
diff changeset
867 {'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
868 {'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
869 {0}
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
870 };
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
871
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
872
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
873 static const formatdef *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
874 whichtable(char **pfmt)
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
875 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
876 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
877 switch (*fmt) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
878 case '<':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
879 return lilendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
880 case '>':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
881 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
882 return bigendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
883 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
884 int n = 1;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
885 char *p = (char *) &n;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
886 if (*p == 1)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
887 return lilendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
888 else
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
889 return bigendian_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
890 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
891 default:
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
892 --*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
893 /* Fall through */
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
894 case '@':
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
895 return native_table;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
896 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
897 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
898
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
899
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
900 /* 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
901
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
902 static const formatdef *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
903 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
904 {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
905 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
906 if (f->format == c) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
907 return f;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
908 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
909 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
910 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
911 return NULL;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
912 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
913
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
914
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
915 /* 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
916
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
917 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
918 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
919 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
920 if (e->format == c) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
921 if (e->alignment) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
922 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
923 / e->alignment)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
924 * e->alignment;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
925 }
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
926 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
927 return size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
928 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
929
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
930
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
931 /* 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
932
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
933 static int
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
934 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
935 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
936 const formatdef *e;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
937 const char *s;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
938 char c;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
939 int size, num, itemsize, x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
940
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
941 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
942 size = 0;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
943 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
944 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
945 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
946 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
947 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
948 while ('0' <= (c = *s++) && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
949 x = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
950 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
951 PyErr_SetString(
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
952 StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
953 "overflow in item count");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
954 return -1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
955 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
956 num = x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
957 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
958 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
959 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
960 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
961 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
962 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
963
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
964 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
965 if (e == NULL)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
966 return -1;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
967 itemsize = e->size;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
968 size = align(size, c, e);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
969 x = num * itemsize;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
970 size += x;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
971 if (x/itemsize != num || size < 0) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
972 PyErr_SetString(StructError,
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
973 "total struct size too long");
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
974 return -1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
975 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
976 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
977
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
978 return size;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
979 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
980
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
981
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
982 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
983 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
984 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
985 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
986
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
987 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
988 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
989 {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
990 char *fmt;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
991 const formatdef *f;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
992 int size;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
993
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
994 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
995 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
996 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
997 size = calcsize(fmt, f);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
998 if (size < 0)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
999 return NULL;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1000 return PyInt_FromLong((long)size);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1001 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1002
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1003
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
1004 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
1005 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
1006 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
1007 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
1008
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1009 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1010 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
1011 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1012 const formatdef *f, *e;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1013 PyObject *format, *result, *v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1014 char *fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1015 int size, num;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1016 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
1017 char *s, *res, *restart, *nres;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1018 char c;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1019
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1020 if (args == NULL || !PyTuple_Check(args) ||
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1021 (n = PyTuple_Size(args)) < 1)
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1022 {
12814
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 11882
diff changeset
1023 PyErr_SetString(PyExc_TypeError,
b1b260a2de98 Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents: 11882
diff changeset
1024 "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
1025 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1026 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1027 format = PyTuple_GetItem(args, 0);
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1028 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
1029 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1030 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1031 size = calcsize(fmt, f);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1032 if (size < 0)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1033 return NULL;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1034 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
1035 if (result == NULL)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1036 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1037
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1038 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1039 i = 1;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1040 res = restart = PyString_AsString(result);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1041
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1042 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1043 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
1044 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1045 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1046 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1047 while ('0' <= (c = *s++) && c <= '9')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1048 num = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1049 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1050 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1051 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1052 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1053 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1054
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1055 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1056 if (e == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1057 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
1058 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
1059 /* 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
1060 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
1061 *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
1062 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
1063 continue;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1064 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1065 if (c == 'x') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1066 /* 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
1067 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
1068 res += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1069 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1070 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1071 if (i >= n) {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1072 PyErr_SetString(StructError,
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1073 "insufficient arguments to pack");
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1074 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1075 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1076 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
1077 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1078 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1079 if (c == 's') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1080 /* 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
1081 int n;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1082 if (!PyString_Check(v)) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1083 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1084 "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
1085 goto fail;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1086 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1087 n = PyString_Size(v);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1088 if (n > num)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1089 n = num;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1090 if (n > 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1091 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
1092 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
1093 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
1094 res += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1095 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1096 }
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1097 else if (c == 'p') {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1098 /* num is string size + 1,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1099 to fit in the count byte */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1100 int n;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1101 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
1102 if (!PyString_Check(v)) {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1103 PyErr_SetString(StructError,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1104 "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
1105 goto fail;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1106 }
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1107 n = PyString_Size(v);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1108 if (n > num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1109 n = num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1110 if (n > 0)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1111 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
1112 if (n < num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1113 /* 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
1114 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
1115 *res++ = n; /* store the length byte */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1116 res += num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1117 break;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1118 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1119 else {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1120 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
1121 goto fail;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1122 res += e->size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1123 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1124 } while (--num > 0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1125 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1126
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1127 if (i < n) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1128 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1129 "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
1130 goto fail;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1131 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1132
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1133 return result;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1134
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1135 fail:
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1136 Py_DECREF(result);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1137 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1138 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1139
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1140
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
1141 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
1142 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
1143 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
1144 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
1145 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
1146
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1147 static PyObject *
13502
7348cceee8fa ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents: 13447
diff changeset
1148 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
1149 {
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1150 const formatdef *f, *e;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1151 char *str, *start, *fmt, *s;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1152 char c;
4865
f5073e24147c Scratch the ears of gcc -Wall.
Barry Warsaw <barry@python.org>
parents: 4861
diff changeset
1153 int len, size, num;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1154 PyObject *res, *v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1155
11882
dc65c5f6ad7c Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents: 9588
diff changeset
1156 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
1157 return NULL;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1158 f = whichtable(&fmt);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1159 size = calcsize(fmt, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1160 if (size < 0)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1161 return NULL;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1162 if (size != len) {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1163 PyErr_SetString(StructError,
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1164 "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
1165 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1166 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1167 res = PyList_New(0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1168 if (res == NULL)
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1169 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1170 str = start;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1171 s = fmt;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1172 while ((c = *s++) != '\0') {
7982
a909a2869ac5 Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents: 6927
diff changeset
1173 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
1174 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1175 if ('0' <= c && c <= '9') {
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1176 num = c - '0';
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1177 while ('0' <= (c = *s++) && c <= '9')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1178 num = num*10 + (c - '0');
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1179 if (c == '\0')
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1180 break;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1181 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1182 else
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1183 num = 1;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1184
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1185 e = getentry(c, f);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1186 if (e == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1187 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1188 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
1189 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
1190 continue;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1191
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1192 do {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1193 if (c == 'x') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1194 str += num;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1195 break;
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1196 }
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1197 if (c == 's') {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1198 /* 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
1199 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
1200 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1201 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1202 str += num;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1203 num = 0;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1204 }
6241
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1205 else if (c == 'p') {
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1206 /* num is string buffer size,
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1207 not repeat count */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1208 int n = *(unsigned char*)str;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1209 /* first byte (unsigned) is string size */
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1210 if (n >= num)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1211 n = num-1;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1212 v = PyString_FromStringAndSize(str+1, n);
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1213 if (v == NULL)
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1214 goto fail;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1215 str += num;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1216 num = 0;
ab64344e87db Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents: 6167
diff changeset
1217 }
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1218 else {
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1219 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
1220 if (v == NULL)
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1221 goto fail;
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1222 str += e->size;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1223 }
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1224 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
1225 goto fail;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1226 Py_DECREF(v);
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1227 } while (--num > 0);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1228 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1229
4841
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1230 v = PyList_AsTuple(res);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1231 Py_DECREF(res);
a7b9c7c81337 Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents: 4705
diff changeset
1232 return v;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1233
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1234 fail:
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1235 Py_DECREF(res);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1236 return NULL;
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1237 }
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1238
976
31b018bda7e2 unpack() now returns a tuple, not a list
Guido van Rossum <guido@python.org>
parents: 960
diff changeset
1239
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1240 /* List of functions */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1241
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1242 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
1243 {"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
1244 {"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
1245 {"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
1246 {NULL, NULL} /* sentinel */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1247 };
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1248
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1249
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1250 /* Module initialization */
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1251
9588
38023a425e48 Add DL_EXPORT() to all modules that could possibly be used
Guido van Rossum <guido@python.org>
parents: 9123
diff changeset
1252 DL_EXPORT(void)
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13502
diff changeset
1253 initstruct(void)
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1254 {
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1255 PyObject *m, *d;
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1256
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1257 /* 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
1258 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
1259 (PyObject*)NULL, PYTHON_API_VERSION);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1260
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1261 /* Add some symbolic constants to the module */
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1262 d = PyModule_GetDict(m);
6367
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1263 StructError = PyErr_NewException("struct.error", NULL, NULL);
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1264 if (StructError == NULL)
2c29b8982643 Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents: 6241
diff changeset
1265 return;
4705
cc525182bbb1 Renamed.
Barry Warsaw <barry@python.org>
parents: 4503
diff changeset
1266 PyDict_SetItemString(d, "error", StructError);
960
7d3a053c6765 struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff changeset
1267 }