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