Mercurial > cpython-lru_cache-weakref
annotate Modules/structmodule.c @ 7278:59d2761ba1cf legacy-trunk
Remove obsolete pagenumbering / pagestyle cruft.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Fri, 13 Feb 1998 07:08:56 +0000 |
| parents | 52125ce376a4 |
| children | a909a2869ac5 |
| rev | line source |
|---|---|
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /*********************************************************** |
|
2061
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
2 Copyright 1991-1995 by Stichting Mathematisch Centrum, Amsterdam, |
|
172ba04a5191
Added 1995 to copyright message.
Guido van Rossum <guido@python.org>
parents:
1746
diff
changeset
|
3 The Netherlands. |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
4 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
5 All Rights Reserved |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
6 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
7 Permission to use, copy, modify, and distribute this software and its |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
8 documentation for any purpose and without fee is hereby granted, |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
9 provided that the above copyright notice appear in all copies and that |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
10 both that copyright notice and this permission notice appear in |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
11 supporting documentation, and that the names of Stichting Mathematisch |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
12 Centrum or CWI or Corporation for National Research Initiatives or |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
13 CNRI not be used in advertising or publicity pertaining to |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
14 distribution of the software without specific, written prior |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
15 permission. |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
16 |
|
4503
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
17 While CWI is the initial source for this software, a modified version |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
18 is made available by the Corporation for National Research Initiatives |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
19 (CNRI) at the Internet address ftp://ftp.python.org. |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
20 |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
21 STICHTING MATHEMATISCH CENTRUM AND CNRI DISCLAIM ALL WARRANTIES WITH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
22 REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
23 MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
24 CENTRUM OR CNRI BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
25 DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
26 PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
27 TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR |
|
65d2d8e67da3
New permission notice, includes CNRI.
Guido van Rossum <guido@python.org>
parents:
2278
diff
changeset
|
28 PERFORMANCE OF THIS SOFTWARE. |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
29 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
30 ******************************************************************/ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
31 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
32 /* 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
|
33 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
34 /* 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
|
35 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
|
36 |
|
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
|
37 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
|
38 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
|
39 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
|
40 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
|
41 \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
|
42 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
|
43 @: 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
|
44 =: 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
|
45 <: 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
|
46 >: 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
|
47 !: 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
|
48 \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
|
49 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
|
50 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
|
51 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
|
52 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
|
53 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
|
54 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
|
55 s:string (array of char); p: pascal string (w. count 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
|
56 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
|
57 \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
|
58 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
|
59 |
| 4705 | 60 #include "Python.h" |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
61 #include "mymath.h" |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
62 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
63 #include <limits.h> |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
64 #include <ctype.h> |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
65 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
66 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
67 /* Exception */ |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
68 |
| 4705 | 69 static PyObject *StructError; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
70 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
71 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
72 /* 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
|
73 |
|
2278
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
74 #ifdef __MWERKS__ |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
75 /* |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
76 ** 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
|
77 ** on the PowerPC mac. |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
78 */ |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
79 #ifdef __powerc |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
80 #pragma options align=mac68k |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
81 #endif |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
82 #endif /* __MWERKS__ */ |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
83 |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
84 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
|
85 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
|
86 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
|
87 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
|
88 typedef struct { char c; double x; } s_double; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
89 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
90 #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
|
91 #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
|
92 #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
|
93 #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
|
94 #define DOUBLE_ALIGN (sizeof(s_double) - sizeof(double)) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
95 |
|
2278
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
96 #ifdef __powerc |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
97 #pragma options align=reset |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
98 #endif |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
99 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
100 /* 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
|
101 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
|
102 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
103 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
104 get_long(v, p) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
105 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
106 long *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
107 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
108 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
|
109 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
|
110 if (PyErr_Occurred() == PyExc_TypeError) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
111 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
112 "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
|
113 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
114 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
115 *p = x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
116 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
117 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
118 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
119 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
120 /* 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
|
121 |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
122 static int |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
123 get_ulong(v, p) |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
124 PyObject *v; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
125 unsigned long *p; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
126 { |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
127 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
|
128 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
|
129 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
|
130 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
|
131 *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
|
132 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
|
133 } |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
134 else { |
|
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
135 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
|
136 } |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
137 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
138 |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
139 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
140 /* Floating point helpers */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
141 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
142 /* 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
|
143 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
|
144 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
|
145 |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
146 /* 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
|
147 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
148 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
149 pack_float(x, p, incr) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
150 double x; /* The number to pack */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
151 char *p; /* Where to pack the high order byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
152 int incr; /* 1 for big-endian; -1 for little-endian */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
153 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
154 int s; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
155 int e; |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
156 double f; |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
157 long fbits; |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
158 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
159 if (x < 0) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
160 s = 1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
161 x = -x; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
162 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
163 else |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
164 s = 0; |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
165 |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
166 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
|
167 |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
168 /* 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
|
169 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
|
170 f *= 2.0; |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
171 e--; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
172 } |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
173 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
|
174 e = 0; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
175 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
176 else { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
177 PyErr_SetString(PyExc_SystemError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
178 "frexp() result out of range"); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
179 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
180 } |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
181 |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
182 if (e >= 128) { |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
183 /* 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
|
184 PyErr_SetString(PyExc_OverflowError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
185 "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
|
186 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
187 } |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
188 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
|
189 /* Gradual underflow */ |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
190 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
|
191 e = 0; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
192 } |
|
6551
bdd4cb2a83b5
Fix due to Bill Noon for problem discovered by Ken Manheimer: packing
Guido van Rossum <guido@python.org>
parents:
6367
diff
changeset
|
193 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
|
194 e += 127; |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
195 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
|
196 } |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
197 |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
198 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
|
199 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
|
200 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
201 /* First byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
202 *p = (s<<7) | (e>>1); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
203 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
204 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
205 /* Second byte */ |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
206 *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
|
207 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
208 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
209 /* Third byte */ |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
210 *p = (fbits>>8) & 0xFF; |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
211 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
212 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
213 /* Fourth byte */ |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
214 *p = fbits&0xFF; |
|
4858
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 /* Done */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
217 return 0; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
218 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
219 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
220 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
221 pack_double(x, p, incr) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
222 double x; /* The number to pack */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
223 char *p; /* Where to pack the high order byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
224 int incr; /* 1 for big-endian; -1 for little-endian */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
225 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
226 int s; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
227 int e; |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
228 double f; |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
229 long fhi, flo; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
230 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
231 if (x < 0) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
232 s = 1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
233 x = -x; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
234 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
235 else |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
236 s = 0; |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
237 |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
238 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
|
239 |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
240 /* 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
|
241 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
|
242 f *= 2.0; |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
243 e--; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
244 } |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
245 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
|
246 e = 0; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
247 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
248 else { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
249 PyErr_SetString(PyExc_SystemError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
250 "frexp() result out of range"); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
251 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
252 } |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
253 |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
254 if (e >= 1024) { |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
255 /* 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
|
256 PyErr_SetString(PyExc_OverflowError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
257 "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
|
258 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
259 } |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
260 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
|
261 /* Gradual underflow */ |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
262 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
|
263 e = 0; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
264 } |
|
6551
bdd4cb2a83b5
Fix due to Bill Noon for problem discovered by Ken Manheimer: packing
Guido van Rossum <guido@python.org>
parents:
6367
diff
changeset
|
265 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
|
266 e += 1023; |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
267 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
|
268 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
269 |
|
4861
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
270 /* 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
|
271 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
|
272 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
|
273 f -= (double)fhi; |
|
69ca5e3ccc6d
Ok, ok, I've fixed gradual underflow on packing too.
Guido van Rossum <guido@python.org>
parents:
4859
diff
changeset
|
274 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
|
275 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
|
276 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
277 /* First byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
278 *p = (s<<7) | (e>>4); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
279 p += incr; |
|
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 /* Second byte */ |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
282 *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
|
283 p += incr; |
|
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 /* Third byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
286 *p = (fhi>>16) & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
287 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
288 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
289 /* Fourth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
290 *p = (fhi>>8) & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
291 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
292 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
293 /* Fifth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
294 *p = fhi & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
295 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
296 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
297 /* Sixth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
298 *p = (flo>>16) & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
299 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
300 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
301 /* Seventh byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
302 *p = (flo>>8) & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
303 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
304 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
305 /* Eighth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
306 *p = flo & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
307 p += incr; |
|
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 /* Done */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
310 return 0; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
311 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
312 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
313 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
314 unpack_float(p, incr) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
315 char *p; /* Where the high order byte is */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
316 int incr; /* 1 for big-endian; -1 for little-endian */ |
|
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 int s; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
319 int e; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
320 long f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
321 double 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 /* First byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
324 s = (*p>>7) & 1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
325 e = (*p & 0x7F) << 1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
326 p += incr; |
|
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 /* Second byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
329 e |= (*p>>7) & 1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
330 f = (*p & 0x7F) << 16; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
331 p += incr; |
|
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 /* Third byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
334 f |= (*p & 0xFF) << 8; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
335 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
336 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
337 /* Fourth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
338 f |= *p & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
339 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
340 x = (double)f / 8388608.0; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
341 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
342 /* 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
|
343 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
|
344 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
|
345 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
|
346 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
|
347 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
|
348 } |
|
ab461c762559
Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents:
4858
diff
changeset
|
349 x = ldexp(x, e); |
|
4858
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 if (s) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
352 x = -x; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
353 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
354 return PyFloat_FromDouble(x); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
355 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
356 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
357 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
358 unpack_double(p, incr) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
359 char *p; /* Where the high order byte is */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
360 int incr; /* 1 for big-endian; -1 for little-endian */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
361 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
362 int s; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
363 int e; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
364 long fhi, flo; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
365 double x; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
366 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
367 /* First byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
368 s = (*p>>7) & 1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
369 e = (*p & 0x7F) << 4; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
370 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
371 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
372 /* Second byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
373 e |= (*p>>4) & 0xF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
374 fhi = (*p & 0xF) << 24; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
375 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
376 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
377 /* Third byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
378 fhi |= (*p & 0xFF) << 16; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
379 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
380 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
381 /* Fourth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
382 fhi |= (*p & 0xFF) << 8; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
383 p += incr; |
|
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 /* Fifth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
386 fhi |= *p & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
387 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
388 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
389 /* Sixth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
390 flo = (*p & 0xFF) << 16; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
391 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
392 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
393 /* Seventh byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
394 flo |= (*p & 0xFF) << 8; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
395 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
396 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
397 /* Eighth byte */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
398 flo |= *p & 0xFF; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
399 p += incr; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
400 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
401 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
|
402 x /= 268435456.0; /* 2**28 */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
403 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
404 /* 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
|
405 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
|
406 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
|
407 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
|
408 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
|
409 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
|
410 } |
|
ab461c762559
Oops -- unpack float/double didn't do the right thing if e==0.
Guido van Rossum <guido@python.org>
parents:
4858
diff
changeset
|
411 x = ldexp(x, e); |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
412 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
413 if (s) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
414 x = -x; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
415 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
416 return PyFloat_FromDouble(x); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
417 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
418 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
419 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
420 /* 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
|
421 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
422 typedef struct _formatdef { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
423 char format; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
424 int size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
425 int alignment; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
426 PyObject* (*unpack) Py_PROTO((const char *, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
427 const struct _formatdef *)); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
428 int (*pack) Py_PROTO((char *, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
429 PyObject *, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
430 const struct _formatdef *)); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
431 } formatdef; |
|
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 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
434 nu_char(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
435 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
436 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
437 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
438 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
|
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 * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
442 nu_byte(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
443 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
444 const formatdef *f; |
|
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 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
|
447 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
448 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
449 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
450 nu_ubyte(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
451 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
452 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
453 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
454 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
|
455 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
456 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
457 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
458 nu_short(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
459 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
460 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
461 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
462 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
|
463 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
464 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
465 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
466 nu_ushort(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
467 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
468 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
469 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
470 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
|
471 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
472 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
473 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
474 nu_int(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
475 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
476 const formatdef *f; |
|
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 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
|
479 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
480 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
481 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
482 nu_uint(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
483 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
484 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
485 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
486 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
|
487 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
|
488 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
489 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
490 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
491 nu_long(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
492 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
493 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
494 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
495 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
|
496 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
497 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
498 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
499 nu_ulong(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
500 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
501 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
502 { |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
503 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
|
504 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
505 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
506 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
507 nu_float(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
508 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
509 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
510 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
511 float x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
512 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
|
513 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
|
514 } |
|
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 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
517 nu_double(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
518 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
519 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
520 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
521 double x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
522 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
|
523 return PyFloat_FromDouble(x); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
524 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
525 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
526 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
527 np_byte(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
528 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
529 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
530 const formatdef *f; |
|
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 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
533 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
|
534 return -1; |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
535 *p = (char)x; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
536 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
537 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
538 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
539 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
540 np_char(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
541 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
542 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
543 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
544 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
545 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
|
546 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
547 "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
|
548 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
549 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
550 *p = *PyString_AsString(v); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
551 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
552 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
553 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
554 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
555 np_short(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
556 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
557 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
558 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
559 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
560 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
561 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
|
562 return -1; |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
563 * (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
|
564 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
565 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
566 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
567 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
568 np_int(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
569 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
570 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
571 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
572 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
573 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
574 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
|
575 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
576 * (int *)p = x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
577 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
578 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
579 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
580 static int |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
581 np_uint(p, v, f) |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
582 char *p; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
583 PyObject *v; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
584 const formatdef *f; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
585 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
586 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
|
587 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
|
588 return -1; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
589 * (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
|
590 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
591 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
592 |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
593 static int |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
594 np_long(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
595 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
596 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
597 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
598 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
599 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
600 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
|
601 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
602 * (long *)p = x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
603 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
604 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
605 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
606 static int |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
607 np_ulong(p, v, f) |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
608 char *p; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
609 PyObject *v; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
610 const formatdef *f; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
611 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
612 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
|
613 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
|
614 return -1; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
615 * (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
|
616 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
617 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
618 |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
619 static int |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
620 np_float(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
621 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
622 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
623 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
624 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
625 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
|
626 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
|
627 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
628 "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
|
629 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
630 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
631 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
|
632 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
633 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
634 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
635 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
636 np_double(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
637 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
638 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
639 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
640 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
641 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
|
642 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
|
643 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
644 "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
|
645 return -1; |
|
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 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
|
648 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
649 } |
|
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 static formatdef native_table[] = { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
652 {'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
|
653 {'b', sizeof(char), 0, nu_byte, np_byte}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
654 {'B', sizeof(char), 0, nu_ubyte, np_byte}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
655 {'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
|
656 {'s', sizeof(char), 0, NULL}, |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
657 {'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
|
658 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
659 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_short}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
660 {'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
|
661 {'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
|
662 {'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
|
663 {'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
|
664 {'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
|
665 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
666 {0} |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
667 }; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
668 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
669 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
670 bu_int(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
671 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
672 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
673 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
674 long x = 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
675 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
676 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
677 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
|
678 } while (--i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
679 i = 8*(sizeof(long) - f->size); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
680 if (i) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
681 x <<= i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
682 x >>= i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
683 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
684 return PyInt_FromLong(x); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
685 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
686 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
687 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
688 bu_uint(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
689 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
690 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
691 { |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
692 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
|
693 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
694 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
695 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
|
696 } while (--i > 0); |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
697 return PyLong_FromUnsignedLong(x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
698 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
699 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
700 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
701 bu_float(p, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
702 const char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
703 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
704 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
705 return unpack_float(p, 1); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
706 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
707 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
708 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
709 bu_double(p, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
710 const char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
711 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
712 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
713 return unpack_double(p, 1); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
714 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
715 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
716 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
717 bp_int(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
718 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
719 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
720 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
721 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
722 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
723 int i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
724 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
|
725 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
726 i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
727 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
728 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
|
729 x >>= 8; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
730 } while (i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
731 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
732 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
733 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
734 static int |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
735 bp_uint(p, v, f) |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
736 char *p; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
737 PyObject *v; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
738 const formatdef *f; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
739 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
740 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
|
741 int i; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
742 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
|
743 return -1; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
744 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
|
745 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
746 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
|
747 x >>= 8; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
748 } 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
|
749 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
750 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
751 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
752 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
753 bp_float(p, v, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
754 char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
755 PyObject *v; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
756 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
757 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
758 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
759 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
760 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
761 "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
|
762 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
763 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
764 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
|
765 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
766 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
767 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
768 bp_double(p, v, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
769 char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
770 PyObject *v; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
771 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
772 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
773 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
774 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
775 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
776 "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
|
777 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
778 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
779 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
|
780 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
781 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
782 static formatdef bigendian_table[] = { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
783 {'x', 1, 0, NULL}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
784 {'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
|
785 {'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
|
786 {'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
|
787 {'s', 1, 0, NULL}, |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
788 {'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
|
789 {'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
|
790 {'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
|
791 {'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
|
792 {'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
|
793 {'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
|
794 {'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
|
795 {'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
|
796 {'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
|
797 {0} |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
798 }; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
799 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
800 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
801 lu_int(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
802 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
803 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
804 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
805 long x = 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
806 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
807 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
808 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
|
809 } while (i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
810 i = 8*(sizeof(long) - f->size); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
811 if (i) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
812 x <<= i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
813 x >>= i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
814 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
815 return PyInt_FromLong(x); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
816 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
817 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
818 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
819 lu_uint(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
820 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
821 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
822 { |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
823 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
|
824 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
825 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
826 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
|
827 } while (i > 0); |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
828 return PyLong_FromUnsignedLong(x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
829 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
830 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
831 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
832 lu_float(p, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
833 const char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
834 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
835 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
836 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
|
837 } |
|
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 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
840 lu_double(p, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
841 const char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
842 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
843 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
844 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
|
845 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
846 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
847 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
848 lp_int(p, v, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
849 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
850 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
851 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
852 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
853 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
854 int i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
855 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
|
856 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
857 i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
858 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
859 *p++ = (char)x; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
860 x >>= 8; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
861 } while (--i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
862 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
863 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
864 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
865 static int |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
866 lp_uint(p, v, f) |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
867 char *p; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
868 PyObject *v; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
869 const formatdef *f; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
870 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
871 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
|
872 int i; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
873 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
|
874 return -1; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
875 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
|
876 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
877 *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
|
878 x >>= 8; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
879 } 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
|
880 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
881 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
882 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
883 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
884 lp_float(p, v, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
885 char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
886 PyObject *v; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
887 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
888 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
889 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
890 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
891 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
892 "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
|
893 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
894 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
895 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
|
896 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
897 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
898 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
899 lp_double(p, v, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
900 char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
901 PyObject *v; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
902 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
903 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
904 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
905 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
906 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
907 "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
|
908 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
909 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
910 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
|
911 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
912 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
913 static formatdef lilendian_table[] = { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
914 {'x', 1, 0, NULL}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
915 {'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
|
916 {'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
|
917 {'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
|
918 {'s', 1, 0, NULL}, |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
919 {'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
|
920 {'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
|
921 {'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
|
922 {'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
|
923 {'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
|
924 {'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
|
925 {'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
|
926 {'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
|
927 {'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
|
928 {0} |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
929 }; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
930 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
931 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
932 static const formatdef * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
933 whichtable(pfmt) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
934 const char **pfmt; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
935 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
936 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
|
937 switch (*fmt) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
938 case '<': |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
939 return lilendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
940 case '>': |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
941 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
|
942 return bigendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
943 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
|
944 int n = 1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
945 char *p = (char *) &n; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
946 if (*p == 1) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
947 return lilendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
948 else |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
949 return bigendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
950 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
951 default: |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
952 --*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
|
953 /* Fall through */ |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
954 case '@': |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
955 return native_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
956 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
957 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
958 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
959 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
960 /* 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
|
961 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
962 static const formatdef * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
963 getentry(c, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
964 int c; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
965 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
966 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
967 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
|
968 if (f->format == c) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
969 return f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
970 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
971 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
972 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
|
973 return NULL; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
974 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
975 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
976 |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
977 /* 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
|
978 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
979 static int |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
980 align(size, c, e) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
981 int size; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
982 int c; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
983 const formatdef *e; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
984 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
985 if (e->format == c) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
986 if (e->alignment) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
987 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
|
988 / e->alignment) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
989 * e->alignment; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
990 } |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
991 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
992 return size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
993 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
994 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
995 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
996 /* 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
|
997 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
998 static int |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
999 calcsize(fmt, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1000 const char *fmt; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1001 const formatdef *f; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1002 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1003 const formatdef *e; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1004 const char *s; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1005 char c; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1006 int size, num, itemsize, x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1007 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1008 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1009 size = 0; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1010 while ((c = *s++) != '\0') { |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1011 if (isspace(c)) |
|
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1012 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1013 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1014 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1015 while ('0' <= (c = *s++) && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1016 x = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1017 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
|
1018 PyErr_SetString( |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1019 StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1020 "overflow in item count"); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1021 return -1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1022 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1023 num = x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1024 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1025 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1026 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1027 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1028 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1029 num = 1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1030 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1031 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1032 if (e == NULL) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1033 return -1; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1034 itemsize = e->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1035 size = align(size, c, e); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1036 x = num * itemsize; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1037 size += x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1038 if (x/itemsize != num || size < 0) { |
| 4705 | 1039 PyErr_SetString(StructError, |
| 1040 "total struct size too long"); | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1041 return -1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1042 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1043 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1044 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1045 return size; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1046 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1047 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1048 |
|
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
|
1049 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
|
1050 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
|
1051 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
|
1052 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
|
1053 |
| 4705 | 1054 static PyObject * |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1055 struct_calcsize(self, args) |
| 4705 | 1056 PyObject *self; /* Not used */ |
| 1057 PyObject *args; | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1058 { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1059 char *fmt; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1060 const formatdef *f; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1061 int size; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1062 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1063 if (!PyArg_ParseTuple(args, "s", &fmt)) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1064 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1065 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1066 size = calcsize(fmt, f); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1067 if (size < 0) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1068 return NULL; |
| 4705 | 1069 return PyInt_FromLong((long)size); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1070 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1071 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1072 |
|
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
|
1073 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
|
1074 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
|
1075 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
|
1076 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
|
1077 |
| 4705 | 1078 static PyObject * |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1079 struct_pack(self, args) |
| 4705 | 1080 PyObject *self; /* Not used */ |
| 1081 PyObject *args; | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1082 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1083 const formatdef *f, *e; |
| 4705 | 1084 PyObject *format, *result, *v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1085 char *fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1086 int size, num; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1087 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
|
1088 char *s, *res, *restart, *nres; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1089 char c; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1090 |
| 4705 | 1091 if (args == NULL || !PyTuple_Check(args) || |
| 1092 (n = PyTuple_Size(args)) < 1) | |
| 1093 { | |
| 1094 PyErr_BadArgument(); | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1095 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1096 } |
| 4705 | 1097 format = PyTuple_GetItem(args, 0); |
| 1098 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
|
1099 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1100 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1101 size = calcsize(fmt, f); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1102 if (size < 0) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1103 return NULL; |
| 4705 | 1104 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
|
1105 if (result == NULL) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1106 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1107 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1108 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1109 i = 1; |
| 4705 | 1110 res = restart = PyString_AsString(result); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1111 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1112 while ((c = *s++) != '\0') { |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1113 if (isspace(c)) |
|
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1114 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1115 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1116 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1117 while ('0' <= (c = *s++) && c <= '9') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1118 num = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1119 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1120 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1121 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1122 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1123 num = 1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1124 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1125 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1126 if (e == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1127 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
|
1128 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
|
1129 /* 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
|
1130 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
|
1131 *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
|
1132 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
|
1133 continue; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1134 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1135 if (c == 'x') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1136 /* 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
|
1137 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
|
1138 res += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1139 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1140 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1141 if (i >= n) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1142 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1143 "insufficient arguments to pack"); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1144 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1145 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1146 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
|
1147 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1148 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1149 if (c == 's') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1150 /* 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
|
1151 int n; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1152 if (!PyString_Check(v)) { |
| 4705 | 1153 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1154 "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
|
1155 goto fail; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1156 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1157 n = PyString_Size(v); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1158 if (n > num) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1159 n = num; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1160 if (n > 0) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1161 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
|
1162 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
|
1163 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
|
1164 res += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1165 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1166 } |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1167 else if (c == 'p') { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1168 /* num is string size + 1, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1169 to fit in the count byte */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1170 int n; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1171 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
|
1172 if (!PyString_Check(v)) { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1173 PyErr_SetString(StructError, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1174 "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
|
1175 goto fail; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1176 } |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1177 n = PyString_Size(v); |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1178 if (n > num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1179 n = num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1180 if (n > 0) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1181 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
|
1182 if (n < num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1183 /* 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
|
1184 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
|
1185 *res++ = n; /* store the length byte */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1186 res += num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1187 break; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1188 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1189 else { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1190 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
|
1191 goto fail; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1192 res += e->size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1193 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1194 } while (--num > 0); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1195 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1196 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1197 if (i < n) { |
| 4705 | 1198 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1199 "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
|
1200 goto fail; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1201 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1202 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1203 return result; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1204 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1205 fail: |
| 4705 | 1206 Py_DECREF(result); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1207 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1208 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1209 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1210 |
|
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
|
1211 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
|
1212 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
|
1213 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
|
1214 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
|
1215 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
|
1216 |
| 4705 | 1217 static PyObject * |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1218 struct_unpack(self, args) |
| 4705 | 1219 PyObject *self; /* Not used */ |
| 1220 PyObject *args; | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1221 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1222 const formatdef *f, *e; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1223 char *str, *start, *fmt, *s; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1224 char c; |
|
4865
f5073e24147c
Scratch the ears of gcc -Wall.
Barry Warsaw <barry@python.org>
parents:
4861
diff
changeset
|
1225 int len, size, num; |
| 4705 | 1226 PyObject *res, *v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1227 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1228 if (!PyArg_ParseTuple(args, "ss#", &fmt, &start, &len)) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1229 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1230 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1231 size = calcsize(fmt, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1232 if (size < 0) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1233 return NULL; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1234 if (size != len) { |
| 4705 | 1235 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1236 "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
|
1237 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1238 } |
| 4705 | 1239 res = PyList_New(0); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1240 if (res == NULL) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1241 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1242 str = start; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1243 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1244 while ((c = *s++) != '\0') { |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1245 if (isspace(c)) |
|
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1246 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1247 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1248 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1249 while ('0' <= (c = *s++) && c <= '9') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1250 num = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1251 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1252 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1253 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1254 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1255 num = 1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1256 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1257 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1258 if (e == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1259 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1260 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
|
1261 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
|
1262 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1263 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1264 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1265 if (c == 'x') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1266 str += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1267 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1268 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1269 if (c == 's') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1270 /* 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
|
1271 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
|
1272 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1273 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1274 str += num; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1275 num = 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1276 } |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1277 else if (c == 'p') { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1278 /* num is string buffer size, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1279 not repeat count */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1280 int n = *(unsigned char*)str; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1281 /* first byte (unsigned) is string size */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1282 if (n >= num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1283 n = num-1; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1284 v = PyString_FromStringAndSize(str+1, n); |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1285 if (v == NULL) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1286 goto fail; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1287 str += num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1288 num = 0; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1289 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1290 else { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1291 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
|
1292 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1293 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1294 str += e->size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1295 } |
| 4705 | 1296 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
|
1297 goto fail; |
| 4705 | 1298 Py_DECREF(v); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1299 } while (--num > 0); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1300 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1301 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1302 v = PyList_AsTuple(res); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1303 Py_DECREF(res); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1304 return v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1305 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1306 fail: |
| 4705 | 1307 Py_DECREF(res); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1308 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1309 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1310 |
|
976
31b018bda7e2
unpack() now returns a tuple, not a list
Guido van Rossum <guido@python.org>
parents:
960
diff
changeset
|
1311 |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1312 /* List of functions */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1313 |
| 4705 | 1314 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
|
1315 {"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
|
1316 {"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
|
1317 {"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
|
1318 {NULL, NULL} /* sentinel */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1319 }; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1320 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1321 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1322 /* Module initialization */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1323 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1324 void |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1325 initstruct() |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1326 { |
| 4705 | 1327 PyObject *m, *d; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1328 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1329 /* 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
|
1330 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
|
1331 (PyObject*)NULL, PYTHON_API_VERSION); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1332 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1333 /* Add some symbolic constants to the module */ |
| 4705 | 1334 d = PyModule_GetDict(m); |
|
6367
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
6241
diff
changeset
|
1335 StructError = PyErr_NewException("struct.error", NULL, NULL); |
|
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
6241
diff
changeset
|
1336 if (StructError == NULL) |
|
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
6241
diff
changeset
|
1337 return; |
| 4705 | 1338 PyDict_SetItemString(d, "error", StructError); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1339 } |
