Mercurial > cpython-lru_cache-weakref
annotate Modules/structmodule.c @ 8864:5cdecaaa9beb legacy-trunk
New organization of internet modules into protocols/support and data
chapters.
| author | Fred Drake <fdrake@acm.org> |
|---|---|
| date | Fri, 07 Aug 1998 15:58:31 +0000 |
| parents | 1ca6895024d9 |
| children | 17d0c51e0887 |
| 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()) { |
|
8434
ae746eab2a96
get_long(): Use PyErr_ExceptionMatches(...) instead of PyErr_Occurred(...).
Fred Drake <fdrake@acm.org>
parents:
7982
diff
changeset
|
110 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
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); |
|
8580
1ca6895024d9
Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents:
8434
diff
changeset
|
697 if (f->size >= 4) |
|
1ca6895024d9
Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents:
8434
diff
changeset
|
698 return PyLong_FromUnsignedLong(x); |
|
1ca6895024d9
Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents:
8434
diff
changeset
|
699 else |
|
1ca6895024d9
Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents:
8434
diff
changeset
|
700 return PyInt_FromLong((long)x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
701 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
702 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
703 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
704 bu_float(p, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
705 const char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
706 const formatdef *f; |
|
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 return unpack_float(p, 1); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
709 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
710 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
711 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
712 bu_double(p, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
713 const char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
714 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
715 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
716 return unpack_double(p, 1); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
717 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
718 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
719 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
720 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
|
721 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
722 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
723 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
724 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
725 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
726 int i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
727 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
|
728 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
729 i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
730 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
731 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
|
732 x >>= 8; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
733 } while (i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
734 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
735 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
736 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
737 static int |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
738 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
|
739 char *p; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
740 PyObject *v; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
741 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
|
742 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
743 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
|
744 int i; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
745 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
|
746 return -1; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
747 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
|
748 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
749 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
|
750 x >>= 8; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
751 } 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
|
752 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
753 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
754 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
755 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
756 bp_float(p, v, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
757 char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
758 PyObject *v; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
759 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
760 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
761 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
762 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
763 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
764 "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
|
765 return -1; |
|
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 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
|
768 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
769 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
770 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
771 bp_double(p, v, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
772 char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
773 PyObject *v; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
774 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
775 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
776 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
777 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
778 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
779 "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
|
780 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
781 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
782 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
|
783 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
784 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
785 static formatdef bigendian_table[] = { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
786 {'x', 1, 0, NULL}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
787 {'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
|
788 {'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
|
789 {'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
|
790 {'s', 1, 0, NULL}, |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
791 {'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
|
792 {'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
|
793 {'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
|
794 {'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
|
795 {'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
|
796 {'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
|
797 {'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
|
798 {'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
|
799 {'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
|
800 {0} |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
801 }; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
802 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
803 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
804 lu_int(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
805 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
806 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
807 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
808 long x = 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
809 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
810 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
811 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
|
812 } while (i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
813 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
|
814 if (i) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
815 x <<= i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
816 x >>= i; |
|
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 return PyInt_FromLong(x); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
819 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
820 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
821 static PyObject * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
822 lu_uint(p, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
823 const char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
824 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
825 { |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
826 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
|
827 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
828 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
829 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
|
830 } while (i > 0); |
|
8580
1ca6895024d9
Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents:
8434
diff
changeset
|
831 if (f->size >= 4) |
|
1ca6895024d9
Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents:
8434
diff
changeset
|
832 return PyLong_FromUnsignedLong(x); |
|
1ca6895024d9
Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents:
8434
diff
changeset
|
833 else |
|
1ca6895024d9
Unsigned 1 and 2 byte sized formats shouldn't result in long integer values!
Guido van Rossum <guido@python.org>
parents:
8434
diff
changeset
|
834 return PyInt_FromLong((long)x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
835 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
836 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
837 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
838 lu_float(p, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
839 const char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
840 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
841 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
842 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
|
843 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
844 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
845 static PyObject * |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
846 lu_double(p, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
847 const char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
848 const formatdef *f; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
849 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
850 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
|
851 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
852 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
853 static int |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
854 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
|
855 char *p; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
856 PyObject *v; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
857 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
858 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
859 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
860 int i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
861 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
|
862 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
863 i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
864 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
865 *p++ = (char)x; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
866 x >>= 8; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
867 } while (--i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
868 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
869 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
870 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
871 static int |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
872 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
|
873 char *p; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
874 PyObject *v; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
875 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
|
876 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
877 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
|
878 int i; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
879 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
|
880 return -1; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
881 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
|
882 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
883 *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
|
884 x >>= 8; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
885 } 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
|
886 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
887 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
888 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
889 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
890 lp_float(p, v, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
891 char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
892 PyObject *v; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
893 const formatdef *f; |
|
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 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
896 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
897 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
898 "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
|
899 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
900 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
901 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
|
902 } |
|
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 static int |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
905 lp_double(p, v, f) |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
906 char *p; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
907 PyObject *v; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
908 const formatdef *f; |
|
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 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
911 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
912 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
913 "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
|
914 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
915 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
916 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
|
917 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
918 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
919 static formatdef lilendian_table[] = { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
920 {'x', 1, 0, NULL}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
921 {'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
|
922 {'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
|
923 {'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
|
924 {'s', 1, 0, NULL}, |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
925 {'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
|
926 {'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
|
927 {'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
|
928 {'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
|
929 {'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
|
930 {'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
|
931 {'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
|
932 {'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
|
933 {'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
|
934 {0} |
|
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 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
937 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
938 static const formatdef * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
939 whichtable(pfmt) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
940 const char **pfmt; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
941 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
942 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
|
943 switch (*fmt) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
944 case '<': |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
945 return lilendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
946 case '>': |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
947 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
|
948 return bigendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
949 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
|
950 int n = 1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
951 char *p = (char *) &n; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
952 if (*p == 1) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
953 return lilendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
954 else |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
955 return bigendian_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 default: |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
958 --*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
|
959 /* Fall through */ |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
960 case '@': |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
961 return native_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
962 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
963 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
964 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
965 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
966 /* 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
|
967 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
968 static const formatdef * |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
969 getentry(c, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
970 int c; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
971 const formatdef *f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
972 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
973 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
|
974 if (f->format == c) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
975 return f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
976 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
977 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
978 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
|
979 return NULL; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
980 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
981 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
982 |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
983 /* 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
|
984 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
985 static int |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
986 align(size, c, e) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
987 int size; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
988 int c; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
989 const formatdef *e; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
990 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
991 if (e->format == c) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
992 if (e->alignment) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
993 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
|
994 / e->alignment) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
995 * e->alignment; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
996 } |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
997 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
998 return size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
999 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1000 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1001 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1002 /* 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
|
1003 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1004 static int |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1005 calcsize(fmt, f) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1006 const char *fmt; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1007 const formatdef *f; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1008 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1009 const formatdef *e; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1010 const char *s; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1011 char c; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1012 int size, num, itemsize, x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1013 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1014 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1015 size = 0; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1016 while ((c = *s++) != '\0') { |
|
7982
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6927
diff
changeset
|
1017 if (isspace((int)c)) |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1018 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1019 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1020 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1021 while ('0' <= (c = *s++) && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1022 x = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1023 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
|
1024 PyErr_SetString( |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1025 StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1026 "overflow in item count"); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1027 return -1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1028 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1029 num = x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1030 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1031 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1032 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1033 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1034 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1035 num = 1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1036 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1037 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1038 if (e == NULL) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1039 return -1; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1040 itemsize = e->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1041 size = align(size, c, e); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1042 x = num * itemsize; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1043 size += x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1044 if (x/itemsize != num || size < 0) { |
| 4705 | 1045 PyErr_SetString(StructError, |
| 1046 "total struct size too long"); | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1047 return -1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1048 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1049 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1050 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1051 return size; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1052 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1053 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1054 |
|
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
|
1055 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
|
1056 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
|
1057 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
|
1058 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
|
1059 |
| 4705 | 1060 static PyObject * |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1061 struct_calcsize(self, args) |
| 4705 | 1062 PyObject *self; /* Not used */ |
| 1063 PyObject *args; | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1064 { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1065 char *fmt; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1066 const formatdef *f; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1067 int size; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1068 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1069 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
|
1070 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1071 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1072 size = calcsize(fmt, f); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1073 if (size < 0) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1074 return NULL; |
| 4705 | 1075 return PyInt_FromLong((long)size); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1076 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1077 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1078 |
|
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
|
1079 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
|
1080 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
|
1081 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
|
1082 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
|
1083 |
| 4705 | 1084 static PyObject * |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1085 struct_pack(self, args) |
| 4705 | 1086 PyObject *self; /* Not used */ |
| 1087 PyObject *args; | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1088 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1089 const formatdef *f, *e; |
| 4705 | 1090 PyObject *format, *result, *v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1091 char *fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1092 int size, num; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1093 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
|
1094 char *s, *res, *restart, *nres; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1095 char c; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1096 |
| 4705 | 1097 if (args == NULL || !PyTuple_Check(args) || |
| 1098 (n = PyTuple_Size(args)) < 1) | |
| 1099 { | |
| 1100 PyErr_BadArgument(); | |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1101 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1102 } |
| 4705 | 1103 format = PyTuple_GetItem(args, 0); |
| 1104 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
|
1105 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1106 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1107 size = calcsize(fmt, f); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1108 if (size < 0) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1109 return NULL; |
| 4705 | 1110 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
|
1111 if (result == NULL) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1112 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1113 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1114 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1115 i = 1; |
| 4705 | 1116 res = restart = PyString_AsString(result); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1117 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1118 while ((c = *s++) != '\0') { |
|
7982
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6927
diff
changeset
|
1119 if (isspace((int)c)) |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1120 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1121 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1122 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1123 while ('0' <= (c = *s++) && c <= '9') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1124 num = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1125 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1126 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1127 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1128 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1129 num = 1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1130 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1131 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1132 if (e == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1133 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
|
1134 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
|
1135 /* 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
|
1136 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
|
1137 *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
|
1138 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
|
1139 continue; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1140 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1141 if (c == 'x') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1142 /* 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
|
1143 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
|
1144 res += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1145 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1146 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1147 if (i >= n) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1148 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1149 "insufficient arguments to pack"); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1150 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1151 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1152 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
|
1153 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1154 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1155 if (c == 's') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1156 /* 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
|
1157 int n; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1158 if (!PyString_Check(v)) { |
| 4705 | 1159 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1160 "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
|
1161 goto fail; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1162 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1163 n = PyString_Size(v); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1164 if (n > num) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1165 n = num; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1166 if (n > 0) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1167 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
|
1168 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
|
1169 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
|
1170 res += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1171 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1172 } |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1173 else if (c == 'p') { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1174 /* num is string size + 1, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1175 to fit in the count byte */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1176 int n; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1177 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
|
1178 if (!PyString_Check(v)) { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1179 PyErr_SetString(StructError, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1180 "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
|
1181 goto fail; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1182 } |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1183 n = PyString_Size(v); |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1184 if (n > num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1185 n = num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1186 if (n > 0) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1187 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
|
1188 if (n < num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1189 /* 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
|
1190 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
|
1191 *res++ = n; /* store the length byte */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1192 res += num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1193 break; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1194 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1195 else { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1196 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
|
1197 goto fail; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1198 res += e->size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1199 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1200 } while (--num > 0); |
|
960
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 if (i < n) { |
| 4705 | 1204 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1205 "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
|
1206 goto fail; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1207 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1208 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1209 return result; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1210 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1211 fail: |
| 4705 | 1212 Py_DECREF(result); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1213 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1214 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1215 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1216 |
|
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
|
1217 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
|
1218 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
|
1219 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
|
1220 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
|
1221 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
|
1222 |
| 4705 | 1223 static PyObject * |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1224 struct_unpack(self, args) |
| 4705 | 1225 PyObject *self; /* Not used */ |
| 1226 PyObject *args; | |
|
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 const formatdef *f, *e; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1229 char *str, *start, *fmt, *s; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1230 char c; |
|
4865
f5073e24147c
Scratch the ears of gcc -Wall.
Barry Warsaw <barry@python.org>
parents:
4861
diff
changeset
|
1231 int len, size, num; |
| 4705 | 1232 PyObject *res, *v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1233 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1234 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
|
1235 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1236 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1237 size = calcsize(fmt, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1238 if (size < 0) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1239 return NULL; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1240 if (size != len) { |
| 4705 | 1241 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1242 "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
|
1243 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1244 } |
| 4705 | 1245 res = PyList_New(0); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1246 if (res == NULL) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1247 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1248 str = start; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1249 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1250 while ((c = *s++) != '\0') { |
|
7982
a909a2869ac5
Make new gcc -Wall happy
Guido van Rossum <guido@python.org>
parents:
6927
diff
changeset
|
1251 if (isspace((int)c)) |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1252 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1253 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1254 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1255 while ('0' <= (c = *s++) && c <= '9') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1256 num = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1257 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1258 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1259 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1260 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1261 num = 1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1262 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1263 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1264 if (e == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1265 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1266 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
|
1267 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
|
1268 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1269 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1270 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1271 if (c == 'x') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1272 str += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1273 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1274 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1275 if (c == 's') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1276 /* 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
|
1277 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
|
1278 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1279 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1280 str += num; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1281 num = 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1282 } |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1283 else if (c == 'p') { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1284 /* num is string buffer size, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1285 not repeat count */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1286 int n = *(unsigned char*)str; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1287 /* first byte (unsigned) is string size */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1288 if (n >= num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1289 n = num-1; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1290 v = PyString_FromStringAndSize(str+1, n); |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1291 if (v == NULL) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1292 goto fail; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1293 str += num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1294 num = 0; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1295 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1296 else { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1297 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
|
1298 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1299 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1300 str += e->size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1301 } |
| 4705 | 1302 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
|
1303 goto fail; |
| 4705 | 1304 Py_DECREF(v); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1305 } while (--num > 0); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1306 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1307 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1308 v = PyList_AsTuple(res); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1309 Py_DECREF(res); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1310 return v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1311 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1312 fail: |
| 4705 | 1313 Py_DECREF(res); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1314 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1315 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1316 |
|
976
31b018bda7e2
unpack() now returns a tuple, not a list
Guido van Rossum <guido@python.org>
parents:
960
diff
changeset
|
1317 |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1318 /* List of functions */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1319 |
| 4705 | 1320 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
|
1321 {"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
|
1322 {"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
|
1323 {"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
|
1324 {NULL, NULL} /* sentinel */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1325 }; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1326 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1327 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1328 /* Module initialization */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1329 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1330 void |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1331 initstruct() |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1332 { |
| 4705 | 1333 PyObject *m, *d; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1334 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1335 /* 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
|
1336 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
|
1337 (PyObject*)NULL, PYTHON_API_VERSION); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1338 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1339 /* Add some symbolic constants to the module */ |
| 4705 | 1340 d = PyModule_GetDict(m); |
|
6367
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
6241
diff
changeset
|
1341 StructError = PyErr_NewException("struct.error", NULL, NULL); |
|
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
6241
diff
changeset
|
1342 if (StructError == NULL) |
|
2c29b8982643
Apply two changes, systematically:
Guido van Rossum <guido@python.org>
parents:
6241
diff
changeset
|
1343 return; |
| 4705 | 1344 PyDict_SetItemString(d, "error", StructError); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1345 } |
