Mercurial > cpython-lru_cache-weakref
annotate Modules/structmodule.c @ 68239:ceec209b26d4 2.4
Close 2.4 branch.
| author | Georg Brandl <georg@python.org> |
|---|---|
| date | Sat, 05 Mar 2011 14:56:41 +0100 |
| parents | 6fb46e87d5f9 |
| children | 9f3f87e920a6 |
| rev | line source |
|---|---|
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1 /* 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
|
2 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
3 /* 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
|
4 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
|
5 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
6 #include "Python.h" |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
7 #include <ctype.h> |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
8 |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
9 PyDoc_STRVAR(struct__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
10 "Functions to convert between Python values and C structs.\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
|
11 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
|
12 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
|
13 \n\ |
|
18077
622867e4d832
Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
14 The optional first format char indicates byte order, size and alignment:\n\ |
|
622867e4d832
Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
15 @: native order, size & alignment (default)\n\ |
|
622867e4d832
Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
16 =: native order, std. size & alignment\n\ |
|
622867e4d832
Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
17 <: little-endian, std. size & alignment\n\ |
|
622867e4d832
Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
18 >: big-endian, std. size & alignment\n\ |
|
622867e4d832
Make clear in the docstring that "std" applies to both size and alignment,
Tim Peters <tim.peters@gmail.com>
parents:
18069
diff
changeset
|
19 !: same as >\n\ |
|
6922
7629c4d985bc
Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents:
6551
diff
changeset
|
20 \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 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
|
22 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
|
23 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
|
24 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
|
25 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
|
26 Special cases (preceding decimal count indicates length):\n\ |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
27 s:string (array of char); p: pascal string (with count byte).\n\ |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
8580
diff
changeset
|
28 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
|
29 P:an integer type that is wide enough to hold a pointer.\n\ |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
30 Special case (not in native mode unless 'long long' in platform C):\n\ |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
31 q:long long; Q:unsigned long long\n\ |
|
6922
7629c4d985bc
Added doc strings, suggested by Charles G. Waldman (but massaged quite a bit).
Guido van Rossum <guido@python.org>
parents:
6551
diff
changeset
|
32 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
|
33 \n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
34 The variable struct.error is an exception raised on errors."); |
|
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 |
|
20654
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
45 typedef struct { char c; short x; } st_short; |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
46 typedef struct { char c; int x; } st_int; |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
47 typedef struct { char c; long x; } st_long; |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
48 typedef struct { char c; float x; } st_float; |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
49 typedef struct { char c; double x; } st_double; |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
50 typedef struct { char c; void *x; } st_void_p; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
51 |
|
20654
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
52 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short)) |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
53 #define INT_ALIGN (sizeof(st_int) - sizeof(int)) |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
54 #define LONG_ALIGN (sizeof(st_long) - sizeof(long)) |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
55 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float)) |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
56 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double)) |
|
edd9d0b3c47e
SF patch #474590 -- RISC OS support
Guido van Rossum <guido@python.org>
parents:
19924
diff
changeset
|
57 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *)) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
58 |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
59 /* We can't support q and Q in native mode unless the compiler does; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
60 in std mode, they're 8 bytes on all platforms. */ |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
61 #ifdef HAVE_LONG_LONG |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
62 typedef struct { char c; PY_LONG_LONG x; } s_long_long; |
|
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
63 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG)) |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
64 #endif |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
65 |
|
14587
e8d8758adfdb
Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents:
14583
diff
changeset
|
66 #define STRINGIFY(x) #x |
|
e8d8758adfdb
Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents:
14583
diff
changeset
|
67 |
|
2278
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
68 #ifdef __powerc |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
69 #pragma options align=reset |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
70 #endif |
|
65644766158a
Quick hack so PowerPC macs can access system structures
Jack Jansen <jack.jansen@cwi.nl>
parents:
2061
diff
changeset
|
71 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
72 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
73 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
74 static PyObject * |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
75 get_pylong(PyObject *v) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
76 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
77 PyNumberMethods *m; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
78 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
79 assert(v != NULL); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
80 if (PyInt_Check(v)) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
81 return PyLong_FromLong(PyInt_AS_LONG(v)); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
82 if (PyLong_Check(v)) { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
83 Py_INCREF(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
84 return v; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
85 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
86 m = v->ob_type->tp_as_number; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
87 if (m != NULL && m->nb_long != NULL) { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
88 v = m->nb_long(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
89 if (v == NULL) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
90 return NULL; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
91 if (PyLong_Check(v)) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
92 return v; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
93 Py_DECREF(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
94 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
95 PyErr_SetString(StructError, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
96 "cannot convert argument to long"); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
97 return NULL; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
98 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
99 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
100 /* Helper routine to get a Python integer and raise the appropriate error |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
101 if it isn't one */ |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
102 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
103 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
104 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
|
105 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
106 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
|
107 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
|
108 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
|
109 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
110 "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
|
111 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
112 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
113 *p = x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
114 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
115 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
116 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
117 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
118 /* 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
|
119 |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
120 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
121 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
|
122 { |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
123 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
|
124 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
|
125 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
|
126 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
|
127 *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
|
128 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
|
129 } |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
130 else { |
|
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
131 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
|
132 } |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
133 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
134 |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
135 #ifdef HAVE_LONG_LONG |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
136 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
137 /* Same, but handling native long long. */ |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
138 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
139 static int |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
140 get_longlong(PyObject *v, PY_LONG_LONG *p) |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
141 { |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
142 PY_LONG_LONG x; |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
143 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
144 v = get_pylong(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
145 if (v == NULL) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
146 return -1; |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
147 assert(PyLong_Check(v)); |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
148 x = PyLong_AsLongLong(v); |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
149 Py_DECREF(v); |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
150 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
151 return -1; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
152 *p = x; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
153 return 0; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
154 } |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
155 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
156 /* Same, but handling native unsigned long long. */ |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
157 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
158 static int |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
159 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
160 { |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
161 unsigned PY_LONG_LONG x; |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
162 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
163 v = get_pylong(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
164 if (v == NULL) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
165 return -1; |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
166 assert(PyLong_Check(v)); |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
167 x = PyLong_AsUnsignedLongLong(v); |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
168 Py_DECREF(v); |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
169 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
170 return -1; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
171 *p = x; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
172 return 0; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
173 } |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
174 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
175 #endif |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
176 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
177 /* Floating point helpers */ |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
178 |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
179 static PyObject * |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
180 unpack_float(const char *p, /* start of 4-byte string */ |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
181 int le) /* true for little-endian, false for big-endian */ |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
182 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
183 double x; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
184 |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
185 x = _PyFloat_Unpack4((unsigned char *)p, le); |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
186 if (x == -1.0 && PyErr_Occurred()) |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
187 return NULL; |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
188 return PyFloat_FromDouble(x); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
189 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
190 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
191 static PyObject * |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
192 unpack_double(const char *p, /* start of 8-byte string */ |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
193 int le) /* true for little-endian, false for big-endian */ |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
194 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
195 double x; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
196 |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
197 x = _PyFloat_Unpack8((unsigned char *)p, le); |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
198 if (x == -1.0 && PyErr_Occurred()) |
|
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
199 return NULL; |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
200 return PyFloat_FromDouble(x); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
201 } |
|
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 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
204 /* 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
|
205 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
206 typedef struct _formatdef { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
207 char format; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
208 int size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
209 int alignment; |
|
13447
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
210 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
|
211 const struct _formatdef *); |
|
a89f7f268de4
Nuke all remaining occurrences of Py_PROTO and Py_FPROTO.
Tim Peters <tim.peters@gmail.com>
parents:
13256
diff
changeset
|
212 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
|
213 const struct _formatdef *); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
214 } formatdef; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
215 |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
216 /* A large number of small routines follow, with names of the form |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
217 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
218 [bln][up]_TYPE |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
219 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
220 [bln] distiguishes among big-endian, little-endian and native. |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
221 [pu] distiguishes between pack (to struct) and unpack (from struct). |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
222 TYPE is one of char, byte, ubyte, etc. |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
223 */ |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
224 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
225 /* Native mode routines. ****************************************************/ |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
226 /* NOTE: |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
227 In all n[up]_<type> routines handling types larger than 1 byte, there is |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
228 *no* guarantee that the p pointer is properly aligned for each type, |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
229 therefore memcpy is called. An intermediate variable is used to |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
230 compensate for big-endian architectures. |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
231 Normally both the intermediate variable and the memcpy call will be |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
232 skipped by C optimisation in little-endian architectures (gcc >= 2.91 |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
233 does this). */ |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
234 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
235 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
236 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
|
237 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
238 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
|
239 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
240 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
241 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
242 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
|
243 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
244 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
|
245 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
246 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
247 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
248 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
|
249 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
250 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
|
251 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
252 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
253 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
254 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
|
255 { |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
256 short x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
257 memcpy((char *)&x, p, sizeof x); |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
258 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
|
259 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
260 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
261 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
262 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
|
263 { |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
264 unsigned short x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
265 memcpy((char *)&x, p, sizeof x); |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
266 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
|
267 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
268 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
269 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
270 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
|
271 { |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
272 int x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
273 memcpy((char *)&x, p, sizeof x); |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
274 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
|
275 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
276 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
277 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
278 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
|
279 { |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
280 unsigned int x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
281 memcpy((char *)&x, p, sizeof x); |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
282 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
|
283 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
284 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
285 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
286 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
|
287 { |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
288 long x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
289 memcpy((char *)&x, p, sizeof x); |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
290 return PyInt_FromLong(x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
291 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
292 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
293 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
294 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
|
295 { |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
296 unsigned long x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
297 memcpy((char *)&x, p, sizeof x); |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
298 return PyLong_FromUnsignedLong(x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
299 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
300 |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
301 /* Native mode doesn't support q or Q unless the platform C supports |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
302 long long (or, on Windows, __int64). */ |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
303 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
304 #ifdef HAVE_LONG_LONG |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
305 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
306 static PyObject * |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
307 nu_longlong(const char *p, const formatdef *f) |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
308 { |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
309 PY_LONG_LONG x; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
310 memcpy((char *)&x, p, sizeof x); |
|
18705
8df77415a61d
SF bug #442520: test_struct fails on SPARC.
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
311 return PyLong_FromLongLong(x); |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
312 } |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
313 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
314 static PyObject * |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
315 nu_ulonglong(const char *p, const formatdef *f) |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
316 { |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
317 unsigned PY_LONG_LONG x; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
318 memcpy((char *)&x, p, sizeof x); |
|
18705
8df77415a61d
SF bug #442520: test_struct fails on SPARC.
Tim Peters <tim.peters@gmail.com>
parents:
18094
diff
changeset
|
319 return PyLong_FromUnsignedLongLong(x); |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
320 } |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
321 |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
322 #endif |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
323 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
324 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
325 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
|
326 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
327 float x; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
328 memcpy((char *)&x, p, sizeof x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
329 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
|
330 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
331 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
332 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
333 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
|
334 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
335 double x; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
336 memcpy((char *)&x, p, sizeof x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
337 return PyFloat_FromDouble(x); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
338 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
339 |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
8580
diff
changeset
|
340 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
341 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
|
342 { |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
343 void *x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
344 memcpy((char *)&x, p, sizeof x); |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
345 return PyLong_FromVoidPtr(x); |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
8580
diff
changeset
|
346 } |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
8580
diff
changeset
|
347 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
348 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
349 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
|
350 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
351 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
352 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
|
353 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
|
354 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
|
355 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
|
356 "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
|
357 return -1; |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
358 } |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
359 *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
|
360 return 0; |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
361 } |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
362 |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
363 static int |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
364 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
|
365 { |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
366 long x; |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
367 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
|
368 return -1; |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
369 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
|
370 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
|
371 "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
|
372 return -1; |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
373 } |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
374 *p = (char)x; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
375 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
376 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
377 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
378 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
379 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
|
380 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
381 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
|
382 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
383 "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
|
384 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
385 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
386 *p = *PyString_AsString(v); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
387 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
388 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
389 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
390 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
391 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
|
392 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
393 long x; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
394 short y; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
395 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
|
396 return -1; |
|
14587
e8d8758adfdb
Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents:
14583
diff
changeset
|
397 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
|
398 PyErr_SetString(StructError, |
|
14587
e8d8758adfdb
Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents:
14583
diff
changeset
|
399 "short format requires " STRINGIFY(SHRT_MIN) |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
400 "<=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
|
401 return -1; |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
402 } |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
403 y = (short)x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
404 memcpy(p, (char *)&y, sizeof y); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
405 return 0; |
|
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 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
408 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
|
409 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
|
410 { |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
411 long x; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
412 unsigned short y; |
|
14583
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
413 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
|
414 return -1; |
|
14587
e8d8758adfdb
Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents:
14583
diff
changeset
|
415 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
|
416 PyErr_SetString(StructError, |
|
14587
e8d8758adfdb
Use symbolic constants for allowable short ranges.
Martin v. Löwis <martin@v.loewis.de>
parents:
14583
diff
changeset
|
417 "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
|
418 return -1; |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
419 } |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
420 y = (unsigned short)x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
421 memcpy(p, (char *)&y, sizeof y); |
|
14583
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
422 return 0; |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
423 } |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
424 |
|
213dbc880159
Check range for bytes and shorts. Closes bug #110845.
Martin v. Löwis <martin@v.loewis.de>
parents:
14375
diff
changeset
|
425 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
426 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
|
427 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
428 long x; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
429 int y; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
430 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
|
431 return -1; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
432 y = (int)x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
433 memcpy(p, (char *)&y, sizeof y); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
434 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
435 } |
|
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 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
438 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
|
439 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
440 unsigned long x; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
441 unsigned int y; |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
442 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
|
443 return -1; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
444 y = (unsigned int)x; |
|
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
445 memcpy(p, (char *)&y, sizeof y); |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
446 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
447 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
448 |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
449 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
450 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
|
451 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
452 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
453 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
|
454 return -1; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
455 memcpy(p, (char *)&x, sizeof x); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
456 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
457 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
458 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
459 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
460 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
|
461 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
462 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
|
463 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
|
464 return -1; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
465 memcpy(p, (char *)&x, sizeof x); |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
466 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
467 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
468 |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
469 #ifdef HAVE_LONG_LONG |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
470 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
471 static int |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
472 np_longlong(char *p, PyObject *v, const formatdef *f) |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
473 { |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
474 PY_LONG_LONG x; |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
475 if (get_longlong(v, &x) < 0) |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
476 return -1; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
477 memcpy(p, (char *)&x, sizeof x); |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
478 return 0; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
479 } |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
480 |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
481 static int |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
482 np_ulonglong(char *p, PyObject *v, const formatdef *f) |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
483 { |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
484 unsigned PY_LONG_LONG x; |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
485 if (get_ulonglong(v, &x) < 0) |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
486 return -1; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
487 memcpy(p, (char *)&x, sizeof x); |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
488 return 0; |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
489 } |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
490 #endif |
|
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
491 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
492 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
493 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
|
494 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
495 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
|
496 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
|
497 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
498 "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
|
499 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
500 } |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
501 memcpy(p, (char *)&x, sizeof 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_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
|
507 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
508 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
|
509 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
|
510 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
511 "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
|
512 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
513 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
514 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
|
515 return 0; |
|
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 |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
8580
diff
changeset
|
518 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
519 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
|
520 { |
|
33599
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
521 void *x; |
|
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
522 |
|
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
523 v = get_pylong(v); |
|
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
524 if (v == NULL) |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
8580
diff
changeset
|
525 return -1; |
|
33599
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
526 assert(PyLong_Check(v)); |
|
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
527 x = PyLong_AsVoidPtr(v); |
|
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
528 Py_DECREF(v); |
|
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
529 if (x == NULL && PyErr_Occurred()) |
|
32df1d05cb2d
Patch #1011240: SystemError generated by struct.pack('P', 'foo').
Armin Rigo <arigo@tunes.org>
parents:
30978
diff
changeset
|
530 return -1; |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
531 memcpy(p, (char *)&x, sizeof x); |
|
9123
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
8580
diff
changeset
|
532 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
|
533 } |
|
17d0c51e0887
Patches from Greg Stein to support 'P' format in struct module's
Guido van Rossum <guido@python.org>
parents:
8580
diff
changeset
|
534 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
535 static formatdef native_table[] = { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
536 {'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
|
537 {'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
|
538 {'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
|
539 {'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
|
540 {'s', sizeof(char), 0, NULL}, |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
541 {'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
|
542 {'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
|
543 {'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
|
544 {'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
|
545 {'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
|
546 {'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
|
547 {'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
|
548 {'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
|
549 {'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
|
550 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
551 #ifdef HAVE_LONG_LONG |
|
28437
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
552 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong}, |
|
871da703e4c0
Rename LONG_LONG to PY_LONG_LONG. Fixes #710285.
Martin v. Löwis <martin@v.loewis.de>
parents:
28335
diff
changeset
|
553 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong}, |
|
18069
8e187740aa96
Initial support for 'q' and 'Q' struct format codes: for now, only in
Tim Peters <tim.peters@gmail.com>
parents:
17343
diff
changeset
|
554 #endif |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
555 {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 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
558 /* Big-endian routines. *****************************************************/ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
559 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
560 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
561 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
|
562 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
563 long x = 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
564 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
565 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
566 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
|
567 } while (--i > 0); |
|
17343
de9858530a55
Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
568 /* Extend the sign bit. */ |
|
de9858530a55
Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
569 if (SIZEOF_LONG > f->size) |
|
de9858530a55
Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
570 x |= -(x & (1L << (8*f->size - 1))); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
571 return PyInt_FromLong(x); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
572 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
573 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
574 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
575 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
|
576 { |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
577 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
|
578 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
579 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
580 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
|
581 } 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
|
582 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
|
583 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
|
584 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
|
585 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
|
586 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
587 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
588 static PyObject * |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
589 bu_longlong(const char *p, const formatdef *f) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
590 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
591 return _PyLong_FromByteArray((const unsigned char *)p, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
592 8, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
593 0, /* little-endian */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
594 1 /* signed */); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
595 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
596 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
597 static PyObject * |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
598 bu_ulonglong(const char *p, const formatdef *f) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
599 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
600 return _PyLong_FromByteArray((const unsigned char *)p, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
601 8, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
602 0, /* little-endian */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
603 0 /* signed */); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
604 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
605 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
606 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
607 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
|
608 { |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
609 return unpack_float(p, 0); |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
610 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
611 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
612 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
613 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
|
614 { |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
615 return unpack_double(p, 0); |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
616 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
617 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
618 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
619 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
|
620 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
621 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
622 int i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
623 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
|
624 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
625 i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
626 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
627 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
|
628 x >>= 8; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
629 } while (i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
630 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
631 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
632 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
633 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
634 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
|
635 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
636 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
|
637 int i; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
638 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
|
639 return -1; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
640 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
|
641 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
642 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
|
643 x >>= 8; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
644 } 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
|
645 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
646 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
647 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
648 static int |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
649 bp_longlong(char *p, PyObject *v, const formatdef *f) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
650 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
651 int res; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
652 v = get_pylong(v); |
|
18094
2367b7a001a7
The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
653 if (v == NULL) |
|
2367b7a001a7
The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
654 return -1; |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
655 res = _PyLong_AsByteArray((PyLongObject *)v, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
656 (unsigned char *)p, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
657 8, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
658 0, /* little_endian */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
659 1 /* signed */); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
660 Py_DECREF(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
661 return res; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
662 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
663 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
664 static int |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
665 bp_ulonglong(char *p, PyObject *v, const formatdef *f) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
666 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
667 int res; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
668 v = get_pylong(v); |
|
18094
2367b7a001a7
The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
669 if (v == NULL) |
|
2367b7a001a7
The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
670 return -1; |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
671 res = _PyLong_AsByteArray((PyLongObject *)v, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
672 (unsigned char *)p, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
673 8, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
674 0, /* little_endian */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
675 0 /* signed */); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
676 Py_DECREF(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
677 return res; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
678 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
679 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
680 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
681 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
|
682 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
683 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
684 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
685 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
686 "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
|
687 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
688 } |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
689 return _PyFloat_Pack4(x, (unsigned char *)p, 0); |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
690 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
691 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
692 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
693 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
|
694 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
695 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
696 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
697 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
698 "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
|
699 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
700 } |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
701 return _PyFloat_Pack8(x, (unsigned char *)p, 0); |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
702 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
703 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
704 static formatdef bigendian_table[] = { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
705 {'x', 1, 0, NULL}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
706 {'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
|
707 {'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
|
708 {'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
|
709 {'s', 1, 0, NULL}, |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
710 {'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
|
711 {'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
|
712 {'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
|
713 {'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
|
714 {'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
|
715 {'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
|
716 {'L', 4, 0, bu_uint, bp_uint}, |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
717 {'q', 8, 0, bu_longlong, bp_longlong}, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
718 {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
719 {'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
|
720 {'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
|
721 {0} |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
722 }; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
723 |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
724 /* Little-endian routines. *****************************************************/ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
725 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
726 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
727 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
|
728 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
729 long x = 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
730 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
731 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
732 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
|
733 } while (i > 0); |
|
17343
de9858530a55
Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
734 /* Extend the sign bit. */ |
|
de9858530a55
Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
735 if (SIZEOF_LONG > f->size) |
|
de9858530a55
Repair portability of sign extension when reading signed ints on boxes
Tim Peters <tim.peters@gmail.com>
parents:
14869
diff
changeset
|
736 x |= -(x & (1L << (8*f->size - 1))); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
737 return PyInt_FromLong(x); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
738 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
739 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
740 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
741 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
|
742 { |
|
4874
03bcdd22f6a4
Changed the ``add/sub_offset'' hacks for dealing with C's unsigned
Guido van Rossum <guido@python.org>
parents:
4867
diff
changeset
|
743 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
|
744 int i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
745 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
746 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
|
747 } 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
|
748 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
|
749 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
|
750 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
|
751 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
|
752 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
753 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
754 static PyObject * |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
755 lu_longlong(const char *p, const formatdef *f) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
756 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
757 return _PyLong_FromByteArray((const unsigned char *)p, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
758 8, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
759 1, /* little-endian */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
760 1 /* signed */); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
761 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
762 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
763 static PyObject * |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
764 lu_ulonglong(const char *p, const formatdef *f) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
765 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
766 return _PyLong_FromByteArray((const unsigned char *)p, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
767 8, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
768 1, /* little-endian */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
769 0 /* signed */); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
770 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
771 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
772 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
773 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
|
774 { |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
775 return unpack_float(p, 1); |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
776 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
777 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
778 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
779 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
|
780 { |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
781 return unpack_double(p, 1); |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
782 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
783 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
784 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
785 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
|
786 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
787 long x; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
788 int i; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
789 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
|
790 return -1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
791 i = f->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
792 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
793 *p++ = (char)x; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
794 x >>= 8; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
795 } while (--i > 0); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
796 return 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
797 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
798 |
|
4846
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
799 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
800 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
|
801 { |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
802 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
|
803 int i; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
804 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
|
805 return -1; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
806 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
|
807 do { |
|
5318
2c4b802a3b5d
Keep Microsoft VC happy.
Guido van Rossum <guido@python.org>
parents:
4874
diff
changeset
|
808 *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
|
809 x >>= 8; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
810 } 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
|
811 return 0; |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
812 } |
|
972026d47274
Added better handling of unsigned longs -- a Python long returned by
Guido van Rossum <guido@python.org>
parents:
4842
diff
changeset
|
813 |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
814 static int |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
815 lp_longlong(char *p, PyObject *v, const formatdef *f) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
816 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
817 int res; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
818 v = get_pylong(v); |
|
18094
2367b7a001a7
The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
819 if (v == NULL) |
|
2367b7a001a7
The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
820 return -1; |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
821 res = _PyLong_AsByteArray((PyLongObject*)v, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
822 (unsigned char *)p, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
823 8, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
824 1, /* little_endian */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
825 1 /* signed */); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
826 Py_DECREF(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
827 return res; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
828 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
829 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
830 static int |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
831 lp_ulonglong(char *p, PyObject *v, const formatdef *f) |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
832 { |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
833 int res; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
834 v = get_pylong(v); |
|
18094
2367b7a001a7
The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
835 if (v == NULL) |
|
2367b7a001a7
The new {b,l}p_{u,}longlong() didn't check get_pylong()'s return for NULL.
Tim Peters <tim.peters@gmail.com>
parents:
18084
diff
changeset
|
836 return -1; |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
837 res = _PyLong_AsByteArray((PyLongObject*)v, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
838 (unsigned char *)p, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
839 8, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
840 1, /* little_endian */ |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
841 0 /* signed */); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
842 Py_DECREF(v); |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
843 return res; |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
844 } |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
845 |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
846 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
847 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
|
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 } |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
855 return _PyFloat_Pack4(x, (unsigned char *)p, 1); |
|
4858
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 |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
858 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
859 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
|
860 { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
861 double x = PyFloat_AsDouble(v); |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
862 if (x == -1 && PyErr_Occurred()) { |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
863 PyErr_SetString(StructError, |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
864 "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
|
865 return -1; |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
866 } |
|
28335
70003b0cb837
New private API functions _PyFloat_{Pack,Unpack}(4,8}. This is a
Tim Peters <tim.peters@gmail.com>
parents:
28334
diff
changeset
|
867 return _PyFloat_Pack8(x, (unsigned char *)p, 1); |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
868 } |
|
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
869 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
870 static formatdef lilendian_table[] = { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
871 {'x', 1, 0, NULL}, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
872 {'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
|
873 {'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
|
874 {'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
|
875 {'s', 1, 0, NULL}, |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
876 {'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
|
877 {'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
|
878 {'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
|
879 {'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
|
880 {'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
|
881 {'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
|
882 {'L', 4, 0, lu_uint, lp_uint}, |
|
18084
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
883 {'q', 8, 0, lu_longlong, lp_longlong}, |
|
bbcdbd8f4469
Added q/Q standard (x-platform 8-byte ints) mode in struct module.
Tim Peters <tim.peters@gmail.com>
parents:
18079
diff
changeset
|
884 {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, |
|
4858
198989ee36d5
Support float and double in non-native formats.
Guido van Rossum <guido@python.org>
parents:
4846
diff
changeset
|
885 {'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
|
886 {'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
|
887 {0} |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
888 }; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
889 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
890 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
891 static const formatdef * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
892 whichtable(char **pfmt) |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
893 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
894 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
|
895 switch (*fmt) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
896 case '<': |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
897 return lilendian_table; |
|
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 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
|
900 return bigendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
901 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
|
902 int n = 1; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
903 char *p = (char *) &n; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
904 if (*p == 1) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
905 return lilendian_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
906 else |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
907 return bigendian_table; |
|
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 default: |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
910 --*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
|
911 /* Fall through */ |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
912 case '@': |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
913 return native_table; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
914 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
915 } |
|
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 /* 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
|
919 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
920 static const formatdef * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
921 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
|
922 { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
923 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
|
924 if (f->format == c) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
925 return f; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
926 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
927 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
928 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
|
929 return NULL; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
930 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
931 |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
932 |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
933 /* 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
|
934 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
935 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
936 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
|
937 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
938 if (e->format == c) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
939 if (e->alignment) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
940 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
|
941 / e->alignment) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
942 * e->alignment; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
943 } |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
944 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
945 return size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
946 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
947 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
948 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
949 /* 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
|
950 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
951 static int |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
952 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
|
953 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
954 const formatdef *e; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
955 const char *s; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
956 char c; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
957 int size, num, itemsize, x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
958 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
959 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
960 size = 0; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
961 while ((c = *s++) != '\0') { |
|
35455
8a3d7f9f02cc
SF bug #1072182: bad arg type to isspace in struct module
Raymond Hettinger <python@rcn.com>
parents:
33599
diff
changeset
|
962 if (isspace(Py_CHARMASK(c))) |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
963 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
964 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
965 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
966 while ('0' <= (c = *s++) && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
967 x = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
968 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
|
969 PyErr_SetString( |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
970 StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
971 "overflow in item count"); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
972 return -1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
973 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
974 num = x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
975 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
976 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
977 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
978 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
979 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
980 num = 1; |
|
18079
bc05b0b5e057
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
18078
diff
changeset
|
981 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
982 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
983 if (e == NULL) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
984 return -1; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
985 itemsize = e->size; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
986 size = align(size, c, e); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
987 x = num * itemsize; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
988 size += x; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
989 if (x/itemsize != num || size < 0) { |
| 4705 | 990 PyErr_SetString(StructError, |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
991 "total struct size too long"); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
992 return -1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
993 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
994 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
995 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
996 return size; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
997 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
998 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
999 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1000 PyDoc_STRVAR(calcsize__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1001 "calcsize(fmt) -> int\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
|
1002 Return size of C struct described by format string fmt.\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1003 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
|
1004 |
| 4705 | 1005 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
1006 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
|
1007 { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1008 char *fmt; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1009 const formatdef *f; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1010 int size; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1011 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
1012 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
|
1013 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1014 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1015 size = calcsize(fmt, f); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1016 if (size < 0) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1017 return NULL; |
| 4705 | 1018 return PyInt_FromLong((long)size); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1019 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1020 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1021 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1022 PyDoc_STRVAR(pack__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1023 "pack(fmt, v1, v2, ...) -> string\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
|
1024 Return string containing values v1, v2, ... packed according to fmt.\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1025 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
|
1026 |
| 4705 | 1027 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
1028 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
|
1029 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1030 const formatdef *f, *e; |
| 4705 | 1031 PyObject *format, *result, *v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1032 char *fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1033 int size, num; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1034 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
|
1035 char *s, *res, *restart, *nres; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1036 char c; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1037 |
| 4705 | 1038 if (args == NULL || !PyTuple_Check(args) || |
| 1039 (n = PyTuple_Size(args)) < 1) | |
|
25104
4b580a462508
(Most of) SF patch 601369 (Christos Georgiou): obmalloc,structmodule:
Guido van Rossum <guido@python.org>
parents:
24471
diff
changeset
|
1040 { |
|
18079
bc05b0b5e057
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
18078
diff
changeset
|
1041 PyErr_SetString(PyExc_TypeError, |
|
12814
b1b260a2de98
Michael Hudson <mwh21@cam.ac.uk>:
Fred Drake <fdrake@acm.org>
parents:
11882
diff
changeset
|
1042 "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
|
1043 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1044 } |
| 4705 | 1045 format = PyTuple_GetItem(args, 0); |
|
22771
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21945
diff
changeset
|
1046 fmt = PyString_AsString(format); |
|
260a1720f629
Get rid of more PyArg_Parse & METH_OLDARGS.
Neal Norwitz <nnorwitz@gmail.com>
parents:
21945
diff
changeset
|
1047 if (!fmt) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1048 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1049 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1050 size = calcsize(fmt, f); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1051 if (size < 0) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1052 return NULL; |
| 4705 | 1053 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
|
1054 if (result == NULL) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1055 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1056 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1057 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1058 i = 1; |
| 4705 | 1059 res = restart = PyString_AsString(result); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1060 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1061 while ((c = *s++) != '\0') { |
|
35455
8a3d7f9f02cc
SF bug #1072182: bad arg type to isspace in struct module
Raymond Hettinger <python@rcn.com>
parents:
33599
diff
changeset
|
1062 if (isspace(Py_CHARMASK(c))) |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1063 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1064 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1065 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1066 while ('0' <= (c = *s++) && c <= '9') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1067 num = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1068 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1069 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1070 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1071 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1072 num = 1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1073 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1074 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1075 if (e == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1076 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
|
1077 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
|
1078 /* 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
|
1079 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
|
1080 *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
|
1081 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
|
1082 continue; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1083 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1084 if (c == 'x') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1085 /* 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
|
1086 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
|
1087 res += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1088 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1089 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1090 if (i >= n) { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1091 PyErr_SetString(StructError, |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1092 "insufficient arguments to pack"); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1093 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1094 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1095 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
|
1096 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1097 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1098 if (c == 's') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1099 /* 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
|
1100 int n; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1101 if (!PyString_Check(v)) { |
| 4705 | 1102 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1103 "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
|
1104 goto fail; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1105 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1106 n = PyString_Size(v); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1107 if (n > num) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1108 n = num; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1109 if (n > 0) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1110 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
|
1111 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
|
1112 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
|
1113 res += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1114 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1115 } |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1116 else if (c == 'p') { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1117 /* num is string size + 1, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1118 to fit in the count byte */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1119 int n; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1120 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
|
1121 if (!PyString_Check(v)) { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1122 PyErr_SetString(StructError, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1123 "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
|
1124 goto fail; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1125 } |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1126 n = PyString_Size(v); |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1127 if (n > num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1128 n = num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1129 if (n > 0) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1130 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
|
1131 if (n < num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1132 /* 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
|
1133 memset(res+1+n, '\0', num-n); |
|
19924
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18705
diff
changeset
|
1134 if (n > 255) |
|
01f66f5db326
The 'p' (Pascal string) pack code acts unreasonably when the string size
Tim Peters <tim.peters@gmail.com>
parents:
18705
diff
changeset
|
1135 n = 255; |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1136 *res++ = n; /* store the length byte */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1137 res += num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1138 break; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1139 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1140 else { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1141 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
|
1142 goto fail; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1143 res += e->size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1144 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1145 } while (--num > 0); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1146 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1147 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1148 if (i < n) { |
| 4705 | 1149 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1150 "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
|
1151 goto fail; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1152 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1153 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1154 return result; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1155 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1156 fail: |
| 4705 | 1157 Py_DECREF(result); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1158 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1159 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1160 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1161 |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1162 PyDoc_STRVAR(unpack__doc__, |
|
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1163 "unpack(fmt, string) -> (v1, v2, ...)\n\ |
|
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
|
1164 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
|
1165 to fmt. Requires len(string)==calcsize(fmt).\n\ |
|
23832
ed1ba37a46b7
Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents:
22771
diff
changeset
|
1166 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
|
1167 |
| 4705 | 1168 static PyObject * |
|
13502
7348cceee8fa
ANSI-fication
Peter Schneider-Kamp <nowonder@nowonder.de>
parents:
13447
diff
changeset
|
1169 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
|
1170 { |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1171 const formatdef *f, *e; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1172 char *str, *start, *fmt, *s; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1173 char c; |
|
4865
f5073e24147c
Scratch the ears of gcc -Wall.
Barry Warsaw <barry@python.org>
parents:
4861
diff
changeset
|
1174 int len, size, num; |
| 4705 | 1175 PyObject *res, *v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1176 |
|
11882
dc65c5f6ad7c
Massive patch by Skip Montanaro to add ":name" to as many
Guido van Rossum <guido@python.org>
parents:
9588
diff
changeset
|
1177 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
|
1178 return NULL; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1179 f = whichtable(&fmt); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1180 size = calcsize(fmt, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1181 if (size < 0) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1182 return NULL; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1183 if (size != len) { |
| 4705 | 1184 PyErr_SetString(StructError, |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1185 "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
|
1186 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1187 } |
| 4705 | 1188 res = PyList_New(0); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1189 if (res == NULL) |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1190 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1191 str = start; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1192 s = fmt; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1193 while ((c = *s++) != '\0') { |
|
35455
8a3d7f9f02cc
SF bug #1072182: bad arg type to isspace in struct module
Raymond Hettinger <python@rcn.com>
parents:
33599
diff
changeset
|
1194 if (isspace(Py_CHARMASK(c))) |
|
6167
9e432468f756
Ignore whitespace between formats (not internal to a count+format).
Guido van Rossum <guido@python.org>
parents:
5318
diff
changeset
|
1195 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1196 if ('0' <= c && c <= '9') { |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1197 num = c - '0'; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1198 while ('0' <= (c = *s++) && c <= '9') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1199 num = num*10 + (c - '0'); |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1200 if (c == '\0') |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1201 break; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1202 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1203 else |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1204 num = 1; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1205 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1206 e = getentry(c, f); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1207 if (e == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1208 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1209 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
|
1210 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
|
1211 continue; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1212 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1213 do { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1214 if (c == 'x') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1215 str += num; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1216 break; |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1217 } |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1218 if (c == 's') { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1219 /* 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
|
1220 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
|
1221 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1222 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1223 str += num; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1224 num = 0; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1225 } |
|
6241
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1226 else if (c == 'p') { |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1227 /* num is string buffer size, |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1228 not repeat count */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1229 int n = *(unsigned char*)str; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1230 /* first byte (unsigned) is string size */ |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1231 if (n >= num) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1232 n = num-1; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1233 v = PyString_FromStringAndSize(str+1, n); |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1234 if (v == NULL) |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1235 goto fail; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1236 str += num; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1237 num = 0; |
|
ab64344e87db
Contribution by Hannu Krosing (with some changes).
Guido van Rossum <guido@python.org>
parents:
6167
diff
changeset
|
1238 } |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1239 else { |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1240 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
|
1241 if (v == NULL) |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1242 goto fail; |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1243 str += e->size; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1244 } |
| 4705 | 1245 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
|
1246 goto fail; |
| 4705 | 1247 Py_DECREF(v); |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1248 } while (--num > 0); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1249 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1250 |
|
4841
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1251 v = PyList_AsTuple(res); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1252 Py_DECREF(res); |
|
a7b9c7c81337
Pretty much rewritten to fulfull several long-standing wishes:
Guido van Rossum <guido@python.org>
parents:
4705
diff
changeset
|
1253 return v; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1254 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1255 fail: |
| 4705 | 1256 Py_DECREF(res); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1257 return NULL; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1258 } |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1259 |
|
976
31b018bda7e2
unpack() now returns a tuple, not a list
Guido van Rossum <guido@python.org>
parents:
960
diff
changeset
|
1260 |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1261 /* List of functions */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1262 |
| 4705 | 1263 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
|
1264 {"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
|
1265 {"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
|
1266 {"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
|
1267 {NULL, NULL} /* sentinel */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1268 }; |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1269 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1270 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1271 /* Module initialization */ |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1272 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
1273 PyMODINIT_FUNC |
|
13651
8a063423e0b7
Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents:
13502
diff
changeset
|
1274 initstruct(void) |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1275 { |
|
21944
f39213405e05
Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents:
20654
diff
changeset
|
1276 PyObject *m; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1277 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1278 /* 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
|
1279 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
|
1280 (PyObject*)NULL, PYTHON_API_VERSION); |
|
39893
6fb46e87d5f9
[Backport of rev. 42093 by neal.norwitz]
Andrew M. Kuchling <amk@amk.ca>
parents:
35455
diff
changeset
|
1281 if (m == NULL) |
|
6fb46e87d5f9
[Backport of rev. 42093 by neal.norwitz]
Andrew M. Kuchling <amk@amk.ca>
parents:
35455
diff
changeset
|
1282 return; |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1283 |
|
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1284 /* Add some symbolic constants to the module */ |
|
21944
f39213405e05
Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents:
20654
diff
changeset
|
1285 if (StructError == NULL) { |
|
f39213405e05
Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents:
20654
diff
changeset
|
1286 StructError = PyErr_NewException("struct.error", NULL, NULL); |
|
f39213405e05
Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents:
20654
diff
changeset
|
1287 if (StructError == NULL) |
|
f39213405e05
Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents:
20654
diff
changeset
|
1288 return; |
|
f39213405e05
Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents:
20654
diff
changeset
|
1289 } |
|
f39213405e05
Use PyModule_AddObject() instead of accessing the module dict directly.
Fred Drake <fdrake@acm.org>
parents:
20654
diff
changeset
|
1290 Py_INCREF(StructError); |
| 21945 | 1291 PyModule_AddObject(m, "error", StructError); |
|
960
7d3a053c6765
struct: pack/unpack binary structs; fcntl: fcntl(), ioctl().
Guido van Rossum <guido@python.org>
parents:
diff
changeset
|
1292 } |
