Mercurial > cpython-lru_cache-weakref
annotate Modules/_struct.c @ 59642:c19b4bfb806a legacy-trunk
- Fix typo in Lib/compileall.py(__all__).
| author | Matthias Klose <doko@ubuntu.com> |
|---|---|
| date | Mon, 15 Mar 2010 18:00:01 +0000 |
| parents | 4a6be864c6b8 |
| children | 0540a9628698 |
| rev | line source |
|---|---|
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1 /* struct module -- pack values into and (out of) strings */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
3 /* New version supporting byte order, alignment and size options, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
4 character strings, and unsigned numbers */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
5 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
6 #define PY_SSIZE_T_CLEAN |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
7 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
8 #include "Python.h" |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
9 #include "structseq.h" |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
10 #include "structmember.h" |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
11 #include <ctype.h> |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
12 |
|
38266
74c82ebc4e44
forward declaration for PyStructType
Bob Ippolito <bob@redivi.com>
parents:
38264
diff
changeset
|
13 static PyTypeObject PyStructType; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
14 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
15 /* compatibility macros */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
16 #if (PY_VERSION_HEX < 0x02050000) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
17 typedef int Py_ssize_t; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
18 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
19 |
|
59436
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
20 /* warning messages */ |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
21 #define FLOAT_COERCE_WARN "integer argument expected, got float" |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
22 #define NON_INTEGER_WARN "integer argument expected, got non-integer " \ |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
23 "(implicit conversion using __int__ is deprecated)" |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
24 |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
25 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
26 /* The translation function for each format character is table driven */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
27 typedef struct _formatdef { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
28 char format; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
29 Py_ssize_t size; |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
30 Py_ssize_t alignment; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
31 PyObject* (*unpack)(const char *, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
32 const struct _formatdef *); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
33 int (*pack)(char *, PyObject *, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
34 const struct _formatdef *); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
35 } formatdef; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
36 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
37 typedef struct _formatcode { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
38 const struct _formatdef *fmtdef; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
39 Py_ssize_t offset; |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
40 Py_ssize_t size; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
41 } formatcode; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
42 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
43 /* Struct object interface */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
44 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
45 typedef struct { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
46 PyObject_HEAD |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
47 Py_ssize_t s_size; |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
48 Py_ssize_t s_len; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
49 formatcode *s_codes; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
50 PyObject *s_format; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
51 PyObject *weakreflist; /* List of weak references */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
52 } PyStructObject; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
53 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
54 |
| 38267 | 55 #define PyStruct_Check(op) PyObject_TypeCheck(op, &PyStructType) |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
56 #define PyStruct_CheckExact(op) (Py_TYPE(op) == &PyStructType) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
57 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
58 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
59 /* Exception */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
60 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
61 static PyObject *StructError; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
62 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
63 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
64 /* Define various structs to figure out the alignments of types */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
65 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
66 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
67 typedef struct { char c; short x; } st_short; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
68 typedef struct { char c; int x; } st_int; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
69 typedef struct { char c; long x; } st_long; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
70 typedef struct { char c; float x; } st_float; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
71 typedef struct { char c; double x; } st_double; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
72 typedef struct { char c; void *x; } st_void_p; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
73 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
74 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
75 #define INT_ALIGN (sizeof(st_int) - sizeof(int)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
76 #define LONG_ALIGN (sizeof(st_long) - sizeof(long)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
77 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
78 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
79 #define VOID_P_ALIGN (sizeof(st_void_p) - sizeof(void *)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
80 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
81 /* We can't support q and Q in native mode unless the compiler does; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
82 in std mode, they're 8 bytes on all platforms. */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
83 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
84 typedef struct { char c; PY_LONG_LONG x; } s_long_long; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
85 #define LONG_LONG_ALIGN (sizeof(s_long_long) - sizeof(PY_LONG_LONG)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
86 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
87 |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
88 #ifdef HAVE_C99_BOOL |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
89 #define BOOL_TYPE _Bool |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
90 typedef struct { char c; _Bool x; } s_bool; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
91 #define BOOL_ALIGN (sizeof(s_bool) - sizeof(BOOL_TYPE)) |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
92 #else |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
93 #define BOOL_TYPE char |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
94 #define BOOL_ALIGN 0 |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
95 #endif |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
96 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
97 #define STRINGIFY(x) #x |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
98 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
99 #ifdef __powerc |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
100 #pragma options align=reset |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
101 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
102 |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
103 static char *integer_codes = "bBhHiIlLqQ"; |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
104 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
105 /* Helper to get a PyLongObject by hook or by crook. Caller should decref. */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
106 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
107 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
108 get_pylong(PyObject *v) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
109 { |
|
59436
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
110 PyObject *r; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
111 assert(v != NULL); |
|
59436
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
112 if (!PyInt_Check(v) && !PyLong_Check(v)) { |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
113 PyNumberMethods *m; |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
114 /* Not an integer; try to use __int__ to convert to an |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
115 integer. This behaviour is deprecated, and is removed in |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
116 Python 3.x. */ |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
117 m = Py_TYPE(v)->tp_as_number; |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
118 if (m != NULL && m->nb_int != NULL) { |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
119 /* Special case warning message for floats, for |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
120 backwards compatibility. */ |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
121 if (PyFloat_Check(v)) { |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
122 if (PyErr_WarnEx(PyExc_DeprecationWarning, |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
123 FLOAT_COERCE_WARN, 1)) |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
124 return NULL; |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
125 } |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
126 else { |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
127 if (PyErr_WarnEx(PyExc_DeprecationWarning, |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
128 NON_INTEGER_WARN, 1)) |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
129 return NULL; |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
130 } |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
131 v = m->nb_int(v); |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
132 if (v == NULL) |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
133 return NULL; |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
134 if (!PyInt_Check(v) && !PyLong_Check(v)) { |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
135 PyErr_SetString(PyExc_TypeError, |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
136 "__int__ method returned non-integer"); |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
137 return NULL; |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
138 } |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
139 } |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
140 else { |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
141 PyErr_SetString(StructError, |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
142 "cannot convert argument to integer"); |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
143 return NULL; |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
144 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
145 } |
|
59436
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
146 else |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
147 /* Ensure we own a reference to v. */ |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
148 Py_INCREF(v); |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
149 |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
150 if (PyInt_Check(v)) { |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
151 r = PyLong_FromLong(PyInt_AS_LONG(v)); |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
152 Py_DECREF(v); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
153 } |
|
59436
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
154 else if (PyLong_Check(v)) { |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
155 assert(PyLong_Check(v)); |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
156 r = v; |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
157 } |
|
59438
4a6be864c6b8
Silence compiler warning.
Mark Dickinson <dickinsm@gmail.com>
parents:
59436
diff
changeset
|
158 else { |
|
4a6be864c6b8
Silence compiler warning.
Mark Dickinson <dickinsm@gmail.com>
parents:
59436
diff
changeset
|
159 r = NULL; /* silence compiler warning about |
|
4a6be864c6b8
Silence compiler warning.
Mark Dickinson <dickinsm@gmail.com>
parents:
59436
diff
changeset
|
160 possibly uninitialized variable */ |
|
59436
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
161 assert(0); /* shouldn't ever get here */ |
|
59438
4a6be864c6b8
Silence compiler warning.
Mark Dickinson <dickinsm@gmail.com>
parents:
59436
diff
changeset
|
162 } |
|
59436
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
163 |
|
cfb442d702cd
Issue #1530559: When packing a non-integer with any integer conversion
Mark Dickinson <dickinsm@gmail.com>
parents:
59368
diff
changeset
|
164 return r; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
165 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
166 |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
167 /* Helper to convert a Python object to a C long. Sets an exception |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
168 (struct.error for an inconvertible type, OverflowError for |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
169 out-of-range values) and returns -1 on error. */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
170 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
171 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
172 get_long(PyObject *v, long *p) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
173 { |
|
55121
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
174 long x; |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
175 |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
176 v = get_pylong(v); |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
177 if (v == NULL) |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
178 return -1; |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
179 assert(PyLong_Check(v)); |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
180 x = PyLong_AsLong(v); |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
181 Py_DECREF(v); |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
182 if (x == (long)-1 && PyErr_Occurred()) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
183 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
184 *p = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
185 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
186 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
187 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
188 /* Same, but handling unsigned long */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
189 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
190 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
191 get_ulong(PyObject *v, unsigned long *p) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
192 { |
|
55121
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
193 unsigned long x; |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
194 |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
195 v = get_pylong(v); |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
196 if (v == NULL) |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
197 return -1; |
|
55121
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
198 assert(PyLong_Check(v)); |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
199 x = PyLong_AsUnsignedLong(v); |
|
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
200 Py_DECREF(v); |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
201 if (x == (unsigned long)-1 && PyErr_Occurred()) |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
202 return -1; |
|
55121
1fc37fecaf8f
Issues #1530559, #1741130: Fix various inconsistencies in struct.pack
Mark Dickinson <dickinsm@gmail.com>
parents:
54412
diff
changeset
|
203 *p = x; |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
204 return 0; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
205 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
206 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
207 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
208 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
209 /* Same, but handling native long long. */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
210 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
211 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
212 get_longlong(PyObject *v, PY_LONG_LONG *p) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
213 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
214 PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
215 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
216 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
217 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
218 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
219 assert(PyLong_Check(v)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
220 x = PyLong_AsLongLong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
221 Py_DECREF(v); |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
222 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
223 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
224 *p = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
225 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
226 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
227 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
228 /* Same, but handling native unsigned long long. */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
229 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
230 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
231 get_ulonglong(PyObject *v, unsigned PY_LONG_LONG *p) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
232 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
233 unsigned PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
234 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
235 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
236 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
237 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
238 assert(PyLong_Check(v)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
239 x = PyLong_AsUnsignedLongLong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
240 Py_DECREF(v); |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
241 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
242 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
243 *p = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
244 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
245 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
246 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
247 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
248 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
249 /* Floating point helpers */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
250 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
251 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
252 unpack_float(const char *p, /* start of 4-byte string */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
253 int le) /* true for little-endian, false for big-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
254 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
255 double x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
256 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
257 x = _PyFloat_Unpack4((unsigned char *)p, le); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
258 if (x == -1.0 && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
259 return NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
260 return PyFloat_FromDouble(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
261 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
262 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
263 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
264 unpack_double(const char *p, /* start of 8-byte string */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
265 int le) /* true for little-endian, false for big-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
266 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
267 double x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
268 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
269 x = _PyFloat_Unpack8((unsigned char *)p, le); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
270 if (x == -1.0 && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
271 return NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
272 return PyFloat_FromDouble(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
273 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
274 |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
275 /* Helper to format the range error exceptions */ |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
276 static int |
| 38484 | 277 _range_error(const formatdef *f, int is_unsigned) |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
278 { |
|
38529
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
279 /* ulargest is the largest unsigned value with f->size bytes. |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
280 * Note that the simpler: |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
281 * ((size_t)1 << (f->size * 8)) - 1 |
|
38530
447a94b039de
Repaired error in new comment.
Tim Peters <tim.peters@gmail.com>
parents:
38529
diff
changeset
|
282 * doesn't work when f->size == sizeof(size_t) because C doesn't |
|
447a94b039de
Repaired error in new comment.
Tim Peters <tim.peters@gmail.com>
parents:
38529
diff
changeset
|
283 * define what happens when a left shift count is >= the number of |
|
447a94b039de
Repaired error in new comment.
Tim Peters <tim.peters@gmail.com>
parents:
38529
diff
changeset
|
284 * bits in the integer being shifted; e.g., on some boxes it doesn't |
|
447a94b039de
Repaired error in new comment.
Tim Peters <tim.peters@gmail.com>
parents:
38529
diff
changeset
|
285 * shift at all when they're equal. |
|
38529
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
286 */ |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
287 const size_t ulargest = (size_t)-1 >> ((SIZEOF_SIZE_T - f->size)*8); |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
288 assert(f->size >= 1 && f->size <= SIZEOF_SIZE_T); |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
289 if (is_unsigned) |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
290 PyErr_Format(StructError, |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
291 "'%c' format requires 0 <= number <= %zu", |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
292 f->format, |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
293 ulargest); |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
294 else { |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
295 const Py_ssize_t largest = (Py_ssize_t)(ulargest >> 1); |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
296 PyErr_Format(StructError, |
|
38520
1a56da86c11a
Calculate smallest properly (it was off by one) and use proper ssize_t types for Win64
Neal Norwitz <nnorwitz@gmail.com>
parents:
38515
diff
changeset
|
297 "'%c' format requires %zd <= number <= %zd", |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
298 f->format, |
|
38529
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
299 ~ largest, |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
300 largest); |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
301 } |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
302 return -1; |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
303 } |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
304 |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
305 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
306 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
307 /* A large number of small routines follow, with names of the form |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
308 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
309 [bln][up]_TYPE |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
310 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
311 [bln] distiguishes among big-endian, little-endian and native. |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
312 [pu] distiguishes between pack (to struct) and unpack (from struct). |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
313 TYPE is one of char, byte, ubyte, etc. |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
314 */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
315 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
316 /* Native mode routines. ****************************************************/ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
317 /* NOTE: |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
318 In all n[up]_<type> routines handling types larger than 1 byte, there is |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
319 *no* guarantee that the p pointer is properly aligned for each type, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
320 therefore memcpy is called. An intermediate variable is used to |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
321 compensate for big-endian architectures. |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
322 Normally both the intermediate variable and the memcpy call will be |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
323 skipped by C optimisation in little-endian architectures (gcc >= 2.91 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
324 does this). */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
325 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
326 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
327 nu_char(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
328 { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
329 return PyString_FromStringAndSize(p, 1); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
330 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
331 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
332 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
333 nu_byte(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
334 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
335 return PyInt_FromLong((long) *(signed char *)p); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
336 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
337 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
338 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
339 nu_ubyte(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
340 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
341 return PyInt_FromLong((long) *(unsigned char *)p); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
342 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
343 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
344 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
345 nu_short(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
346 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
347 short x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
348 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
349 return PyInt_FromLong((long)x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
350 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
351 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
352 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
353 nu_ushort(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
354 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
355 unsigned short x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
356 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
357 return PyInt_FromLong((long)x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
358 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
359 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
360 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
361 nu_int(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
362 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
363 int x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
364 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
365 return PyInt_FromLong((long)x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
366 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
367 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
368 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
369 nu_uint(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
370 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
371 unsigned int x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
372 memcpy((char *)&x, p, sizeof x); |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
373 #if (SIZEOF_LONG > SIZEOF_INT) |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
374 return PyInt_FromLong((long)x); |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
375 #else |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
376 if (x <= ((unsigned int)LONG_MAX)) |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
377 return PyInt_FromLong((long)x); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
378 return PyLong_FromUnsignedLong((unsigned long)x); |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
379 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
380 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
381 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
382 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
383 nu_long(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
384 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
385 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
386 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
387 return PyInt_FromLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
388 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
389 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
390 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
391 nu_ulong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
392 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
393 unsigned long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
394 memcpy((char *)&x, p, sizeof x); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
395 if (x <= LONG_MAX) |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
396 return PyInt_FromLong((long)x); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
397 return PyLong_FromUnsignedLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
398 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
399 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
400 /* Native mode doesn't support q or Q unless the platform C supports |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
401 long long (or, on Windows, __int64). */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
402 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
403 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
404 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
405 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
406 nu_longlong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
407 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
408 PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
409 memcpy((char *)&x, p, sizeof x); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
410 if (x >= LONG_MIN && x <= LONG_MAX) |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
411 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
412 return PyLong_FromLongLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
413 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
414 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
415 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
416 nu_ulonglong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
417 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
418 unsigned PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
419 memcpy((char *)&x, p, sizeof x); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
420 if (x <= LONG_MAX) |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
421 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
422 return PyLong_FromUnsignedLongLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
423 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
424 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
425 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
426 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
427 static PyObject * |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
428 nu_bool(const char *p, const formatdef *f) |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
429 { |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
430 BOOL_TYPE x; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
431 memcpy((char *)&x, p, sizeof x); |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
432 return PyBool_FromLong(x != 0); |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
433 } |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
434 |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
435 |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
436 static PyObject * |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
437 nu_float(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
438 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
439 float x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
440 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
441 return PyFloat_FromDouble((double)x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
442 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
443 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
444 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
445 nu_double(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
446 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
447 double x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
448 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
449 return PyFloat_FromDouble(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
450 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
451 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
452 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
453 nu_void_p(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
454 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
455 void *x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
456 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
457 return PyLong_FromVoidPtr(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
458 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
459 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
460 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
461 np_byte(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
462 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
463 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
464 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
465 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
466 if (x < -128 || x > 127){ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
467 PyErr_SetString(StructError, |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
468 "byte format requires -128 <= number <= 127"); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
469 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
470 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
471 *p = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
472 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
473 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
474 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
475 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
476 np_ubyte(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
477 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
478 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
479 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
480 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
481 if (x < 0 || x > 255){ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
482 PyErr_SetString(StructError, |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
483 "ubyte format requires 0 <= number <= 255"); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
484 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
485 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
486 *p = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
487 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
488 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
489 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
490 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
491 np_char(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
492 { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
493 if (!PyString_Check(v) || PyString_Size(v) != 1) { |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
494 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
495 "char format require string of length 1"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
496 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
497 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
498 *p = *PyString_AsString(v); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
499 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
500 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
501 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
502 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
503 np_short(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
504 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
505 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
506 short y; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
507 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
508 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
509 if (x < SHRT_MIN || x > SHRT_MAX){ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
510 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
511 "short format requires " STRINGIFY(SHRT_MIN) |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
512 " <= number <= " STRINGIFY(SHRT_MAX)); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
513 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
514 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
515 y = (short)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
516 memcpy(p, (char *)&y, sizeof y); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
517 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
518 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
519 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
520 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
521 np_ushort(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
522 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
523 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
524 unsigned short y; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
525 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
526 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
527 if (x < 0 || x > USHRT_MAX){ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
528 PyErr_SetString(StructError, |
|
55142
880b50bca1b1
Typo in error message
Mark Dickinson <dickinsm@gmail.com>
parents:
55121
diff
changeset
|
529 "ushort format requires 0 <= number <= " STRINGIFY(USHRT_MAX)); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
530 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
531 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
532 y = (unsigned short)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
533 memcpy(p, (char *)&y, sizeof y); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
534 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
535 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
536 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
537 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
538 np_int(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
539 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
540 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
541 int y; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
542 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
543 return -1; |
|
38429
003433511e4d
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
Bob Ippolito <bob@redivi.com>
parents:
38411
diff
changeset
|
544 #if (SIZEOF_LONG > SIZEOF_INT) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
545 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX))) |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
546 return _range_error(f, 0); |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
547 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
548 y = (int)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
549 memcpy(p, (char *)&y, sizeof y); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
550 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
551 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
552 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
553 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
554 np_uint(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
555 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
556 unsigned long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
557 unsigned int y; |
|
56246
3dec3b77b787
Eliminate unnecessary get_wrapped_(u)long defines in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents:
55153
diff
changeset
|
558 if (get_ulong(v, &x) < 0) |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49351
diff
changeset
|
559 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
560 y = (unsigned int)x; |
|
38429
003433511e4d
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
Bob Ippolito <bob@redivi.com>
parents:
38411
diff
changeset
|
561 #if (SIZEOF_LONG > SIZEOF_INT) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
562 if (x > ((unsigned long)UINT_MAX)) |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
563 return _range_error(f, 1); |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
564 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
565 memcpy(p, (char *)&y, sizeof y); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
566 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
567 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
568 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
569 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
570 np_long(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
571 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
572 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
573 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
574 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
575 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
576 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
577 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
578 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
579 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
580 np_ulong(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
581 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
582 unsigned long x; |
|
56246
3dec3b77b787
Eliminate unnecessary get_wrapped_(u)long defines in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents:
55153
diff
changeset
|
583 if (get_ulong(v, &x) < 0) |
|
50424
df3923c36ec5
Merged revisions 67952-67953,67955,67957-67958,67960-67961,67963,67965,67967,67970-67971,67973,67982,67988,67990,67995,68014,68016,68030,68057,68061,68112,68115-68118,68120-68121,68123-68128 via svnmerge from
Georg Brandl <georg@python.org>
parents:
49351
diff
changeset
|
584 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
585 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
586 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
587 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
588 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
589 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
590 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
591 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
592 np_longlong(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
593 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
594 PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
595 if (get_longlong(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
596 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
597 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
598 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
599 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
600 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
601 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
602 np_ulonglong(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
603 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
604 unsigned PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
605 if (get_ulonglong(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
606 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
607 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
608 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
609 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
610 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
611 |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
612 |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
613 static int |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
614 np_bool(char *p, PyObject *v, const formatdef *f) |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
615 { |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
616 BOOL_TYPE y; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
617 y = PyObject_IsTrue(v); |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
618 memcpy(p, (char *)&y, sizeof y); |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
619 return 0; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
620 } |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
621 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
622 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
623 np_float(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
624 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
625 float x = (float)PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
626 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
627 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
628 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
629 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
630 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
631 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
632 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
633 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
634 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
635 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
636 np_double(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
637 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
638 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
639 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
640 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
641 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
642 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
643 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
644 memcpy(p, (char *)&x, sizeof(double)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
645 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
646 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
647 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
648 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
649 np_void_p(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
650 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
651 void *x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
652 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
653 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
654 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
655 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
656 assert(PyLong_Check(v)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
657 x = PyLong_AsVoidPtr(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
658 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
659 if (x == NULL && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
660 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
661 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
662 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
663 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
664 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
665 static formatdef native_table[] = { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
666 {'x', sizeof(char), 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
667 {'b', sizeof(char), 0, nu_byte, np_byte}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
668 {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
669 {'c', sizeof(char), 0, nu_char, np_char}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
670 {'s', sizeof(char), 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
671 {'p', sizeof(char), 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
672 {'h', sizeof(short), SHORT_ALIGN, nu_short, np_short}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
673 {'H', sizeof(short), SHORT_ALIGN, nu_ushort, np_ushort}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
674 {'i', sizeof(int), INT_ALIGN, nu_int, np_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
675 {'I', sizeof(int), INT_ALIGN, nu_uint, np_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
676 {'l', sizeof(long), LONG_ALIGN, nu_long, np_long}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
677 {'L', sizeof(long), LONG_ALIGN, nu_ulong, np_ulong}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
678 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
679 {'q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_longlong, np_longlong}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
680 {'Q', sizeof(PY_LONG_LONG), LONG_LONG_ALIGN, nu_ulonglong,np_ulonglong}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
681 #endif |
|
45672
d41a4206405c
Issue 1872: Changed the struct module typecode from 't' to '?', for
Thomas Heller <theller@ctypes.org>
parents:
45373
diff
changeset
|
682 {'?', sizeof(BOOL_TYPE), BOOL_ALIGN, nu_bool, np_bool}, |
|
38317
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
683 {'f', sizeof(float), FLOAT_ALIGN, nu_float, np_float}, |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
684 {'d', sizeof(double), DOUBLE_ALIGN, nu_double, np_double}, |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
685 {'P', sizeof(void *), VOID_P_ALIGN, nu_void_p, np_void_p}, |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
686 {0} |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
687 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
688 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
689 /* Big-endian routines. *****************************************************/ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
690 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
691 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
692 bu_int(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
693 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
694 long x = 0; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
695 Py_ssize_t i = f->size; |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
696 const unsigned char *bytes = (const unsigned char *)p; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
697 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
698 x = (x<<8) | *bytes++; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
699 } while (--i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
700 /* Extend the sign bit. */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
701 if (SIZEOF_LONG > f->size) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
702 x |= -(x & (1L << ((8 * f->size) - 1))); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
703 return PyInt_FromLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
704 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
705 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
706 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
707 bu_uint(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
708 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
709 unsigned long x = 0; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
710 Py_ssize_t i = f->size; |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
711 const unsigned char *bytes = (const unsigned char *)p; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
712 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
713 x = (x<<8) | *bytes++; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
714 } while (--i > 0); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
715 if (x <= LONG_MAX) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
716 return PyInt_FromLong((long)x); |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
717 return PyLong_FromUnsignedLong(x); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
718 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
719 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
720 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
721 bu_longlong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
722 { |
|
38429
003433511e4d
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
Bob Ippolito <bob@redivi.com>
parents:
38411
diff
changeset
|
723 #ifdef HAVE_LONG_LONG |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
724 PY_LONG_LONG x = 0; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
725 Py_ssize_t i = f->size; |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
726 const unsigned char *bytes = (const unsigned char *)p; |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
727 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
728 x = (x<<8) | *bytes++; |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
729 } while (--i > 0); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
730 /* Extend the sign bit. */ |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
731 if (SIZEOF_LONG_LONG > f->size) |
|
41526
708c8c5fc173
Make pythoncore compile cleanly with VisualStudio 2005. Used an explicit typecast to get a 64 bit integer, and undefined the Yield macro that conflicts with winbase.h
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
41391
diff
changeset
|
732 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
733 if (x >= LONG_MIN && x <= LONG_MAX) |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
734 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
735 return PyLong_FromLongLong(x); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
736 #else |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
737 return _PyLong_FromByteArray((const unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
738 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
739 0, /* little-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
740 1 /* signed */); |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
741 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
742 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
743 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
744 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
745 bu_ulonglong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
746 { |
|
38429
003433511e4d
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
Bob Ippolito <bob@redivi.com>
parents:
38411
diff
changeset
|
747 #ifdef HAVE_LONG_LONG |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
748 unsigned PY_LONG_LONG x = 0; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
749 Py_ssize_t i = f->size; |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
750 const unsigned char *bytes = (const unsigned char *)p; |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
751 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
752 x = (x<<8) | *bytes++; |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
753 } while (--i > 0); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
754 if (x <= LONG_MAX) |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
755 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
756 return PyLong_FromUnsignedLongLong(x); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
757 #else |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
758 return _PyLong_FromByteArray((const unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
759 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
760 0, /* little-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
761 0 /* signed */); |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
762 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
763 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
764 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
765 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
766 bu_float(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
767 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
768 return unpack_float(p, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
769 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
770 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
771 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
772 bu_double(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
773 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
774 return unpack_double(p, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
775 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
776 |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
777 static PyObject * |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
778 bu_bool(const char *p, const formatdef *f) |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
779 { |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
780 char x; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
781 memcpy((char *)&x, p, sizeof x); |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
782 return PyBool_FromLong(x != 0); |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
783 } |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
784 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
785 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
786 bp_int(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
787 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
788 long x; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
789 Py_ssize_t i; |
|
56246
3dec3b77b787
Eliminate unnecessary get_wrapped_(u)long defines in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents:
55153
diff
changeset
|
790 if (get_long(v, &x) < 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
791 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
792 i = f->size; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
793 if (i != SIZEOF_LONG) { |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
794 if ((i == 2) && (x < -32768 || x > 32767)) |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
795 return _range_error(f, 0); |
|
38429
003433511e4d
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
Bob Ippolito <bob@redivi.com>
parents:
38411
diff
changeset
|
796 #if (SIZEOF_LONG != 4) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
797 else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
798 return _range_error(f, 0); |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
799 #endif |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
800 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
801 do { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
802 p[--i] = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
803 x >>= 8; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
804 } while (i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
805 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
806 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
807 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
808 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
809 bp_uint(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
810 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
811 unsigned long x; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
812 Py_ssize_t i; |
|
56246
3dec3b77b787
Eliminate unnecessary get_wrapped_(u)long defines in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents:
55153
diff
changeset
|
813 if (get_ulong(v, &x) < 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
814 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
815 i = f->size; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
816 if (i != SIZEOF_LONG) { |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
817 unsigned long maxint = 1; |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
818 maxint <<= (unsigned long)(i * 8); |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
819 if (x >= maxint) |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
820 return _range_error(f, 1); |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
821 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
822 do { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
823 p[--i] = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
824 x >>= 8; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
825 } while (i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
826 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
827 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
828 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
829 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
830 bp_longlong(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
831 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
832 int res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
833 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
834 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
835 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
836 res = _PyLong_AsByteArray((PyLongObject *)v, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
837 (unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
838 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
839 0, /* little_endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
840 1 /* signed */); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
841 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
842 return res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
843 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
844 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
845 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
846 bp_ulonglong(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
847 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
848 int res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
849 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
850 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
851 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
852 res = _PyLong_AsByteArray((PyLongObject *)v, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
853 (unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
854 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
855 0, /* little_endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
856 0 /* signed */); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
857 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
858 return res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
859 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
860 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
861 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
862 bp_float(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
863 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
864 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
865 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
866 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
867 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
868 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
869 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
870 return _PyFloat_Pack4(x, (unsigned char *)p, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
871 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
872 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
873 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
874 bp_double(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
875 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
876 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
877 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
878 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
879 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
880 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
881 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
882 return _PyFloat_Pack8(x, (unsigned char *)p, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
883 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
884 |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
885 static int |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
886 bp_bool(char *p, PyObject *v, const formatdef *f) |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
887 { |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
888 char y; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
889 y = PyObject_IsTrue(v); |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
890 memcpy(p, (char *)&y, sizeof y); |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
891 return 0; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
892 } |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
893 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
894 static formatdef bigendian_table[] = { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
895 {'x', 1, 0, NULL}, |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
896 {'b', 1, 0, nu_byte, np_byte}, |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
897 {'B', 1, 0, nu_ubyte, np_ubyte}, |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
898 {'c', 1, 0, nu_char, np_char}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
899 {'s', 1, 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
900 {'p', 1, 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
901 {'h', 2, 0, bu_int, bp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
902 {'H', 2, 0, bu_uint, bp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
903 {'i', 4, 0, bu_int, bp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
904 {'I', 4, 0, bu_uint, bp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
905 {'l', 4, 0, bu_int, bp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
906 {'L', 4, 0, bu_uint, bp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
907 {'q', 8, 0, bu_longlong, bp_longlong}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
908 {'Q', 8, 0, bu_ulonglong, bp_ulonglong}, |
|
45672
d41a4206405c
Issue 1872: Changed the struct module typecode from 't' to '?', for
Thomas Heller <theller@ctypes.org>
parents:
45373
diff
changeset
|
909 {'?', 1, 0, bu_bool, bp_bool}, |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
910 {'f', 4, 0, bu_float, bp_float}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
911 {'d', 8, 0, bu_double, bp_double}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
912 {0} |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
913 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
914 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
915 /* Little-endian routines. *****************************************************/ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
916 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
917 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
918 lu_int(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
919 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
920 long x = 0; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
921 Py_ssize_t i = f->size; |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
922 const unsigned char *bytes = (const unsigned char *)p; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
923 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
924 x = (x<<8) | bytes[--i]; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
925 } while (i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
926 /* Extend the sign bit. */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
927 if (SIZEOF_LONG > f->size) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
928 x |= -(x & (1L << ((8 * f->size) - 1))); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
929 return PyInt_FromLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
930 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
931 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
932 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
933 lu_uint(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
934 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
935 unsigned long x = 0; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
936 Py_ssize_t i = f->size; |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
937 const unsigned char *bytes = (const unsigned char *)p; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
938 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
939 x = (x<<8) | bytes[--i]; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
940 } while (i > 0); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
941 if (x <= LONG_MAX) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
942 return PyInt_FromLong((long)x); |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
943 return PyLong_FromUnsignedLong((long)x); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
944 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
945 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
946 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
947 lu_longlong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
948 { |
|
38429
003433511e4d
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
Bob Ippolito <bob@redivi.com>
parents:
38411
diff
changeset
|
949 #ifdef HAVE_LONG_LONG |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
950 PY_LONG_LONG x = 0; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
951 Py_ssize_t i = f->size; |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
952 const unsigned char *bytes = (const unsigned char *)p; |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
953 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
954 x = (x<<8) | bytes[--i]; |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
955 } while (i > 0); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
956 /* Extend the sign bit. */ |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
957 if (SIZEOF_LONG_LONG > f->size) |
|
41526
708c8c5fc173
Make pythoncore compile cleanly with VisualStudio 2005. Used an explicit typecast to get a 64 bit integer, and undefined the Yield macro that conflicts with winbase.h
Kristján Valur Jónsson <kristjan@ccpgames.com>
parents:
41391
diff
changeset
|
958 x |= -(x & ((PY_LONG_LONG)1 << ((8 * f->size) - 1))); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
959 if (x >= LONG_MIN && x <= LONG_MAX) |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
960 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, PY_LONG_LONG, long)); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
961 return PyLong_FromLongLong(x); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
962 #else |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
963 return _PyLong_FromByteArray((const unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
964 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
965 1, /* little-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
966 1 /* signed */); |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
967 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
968 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
969 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
970 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
971 lu_ulonglong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
972 { |
|
38429
003433511e4d
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
Bob Ippolito <bob@redivi.com>
parents:
38411
diff
changeset
|
973 #ifdef HAVE_LONG_LONG |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
974 unsigned PY_LONG_LONG x = 0; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
975 Py_ssize_t i = f->size; |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
976 const unsigned char *bytes = (const unsigned char *)p; |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
977 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
978 x = (x<<8) | bytes[--i]; |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
979 } while (i > 0); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
980 if (x <= LONG_MAX) |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
981 return PyInt_FromLong(Py_SAFE_DOWNCAST(x, unsigned PY_LONG_LONG, long)); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
982 return PyLong_FromUnsignedLongLong(x); |
|
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
983 #else |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
984 return _PyLong_FromByteArray((const unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
985 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
986 1, /* little-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
987 0 /* signed */); |
|
38310
850d4f7717b8
Struct now unpacks to PY_LONG_LONG directly when possible, also include #ifdef'ed out code that will return int instead of long when in bounds (not active since it's an API and doc change)
Bob Ippolito <bob@redivi.com>
parents:
38277
diff
changeset
|
988 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
989 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
990 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
991 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
992 lu_float(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
993 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
994 return unpack_float(p, 1); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
995 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
996 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
997 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
998 lu_double(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
999 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1000 return unpack_double(p, 1); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1001 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1002 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1003 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1004 lp_int(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1005 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1006 long x; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1007 Py_ssize_t i; |
|
56246
3dec3b77b787
Eliminate unnecessary get_wrapped_(u)long defines in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents:
55153
diff
changeset
|
1008 if (get_long(v, &x) < 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1009 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1010 i = f->size; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1011 if (i != SIZEOF_LONG) { |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1012 if ((i == 2) && (x < -32768 || x > 32767)) |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1013 return _range_error(f, 0); |
|
38429
003433511e4d
Remove the range checking and int usage #defines from _struct and strip out the now-dead code
Bob Ippolito <bob@redivi.com>
parents:
38411
diff
changeset
|
1014 #if (SIZEOF_LONG != 4) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1015 else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1016 return _range_error(f, 0); |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
1017 #endif |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1018 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1019 do { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1020 *p++ = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1021 x >>= 8; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1022 } while (--i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1023 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1024 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1025 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1026 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1027 lp_uint(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1028 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1029 unsigned long x; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1030 Py_ssize_t i; |
|
56246
3dec3b77b787
Eliminate unnecessary get_wrapped_(u)long defines in struct module.
Mark Dickinson <dickinsm@gmail.com>
parents:
55153
diff
changeset
|
1031 if (get_ulong(v, &x) < 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1032 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1033 i = f->size; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1034 if (i != SIZEOF_LONG) { |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1035 unsigned long maxint = 1; |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1036 maxint <<= (unsigned long)(i * 8); |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1037 if (x >= maxint) |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1038 return _range_error(f, 1); |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1039 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1040 do { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1041 *p++ = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1042 x >>= 8; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1043 } while (--i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1044 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1045 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1046 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1047 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1048 lp_longlong(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1049 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1050 int res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1051 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1052 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1053 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1054 res = _PyLong_AsByteArray((PyLongObject*)v, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1055 (unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1056 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1057 1, /* little_endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1058 1 /* signed */); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1059 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1060 return res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1061 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1062 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1063 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1064 lp_ulonglong(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1065 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1066 int res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1067 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1068 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1069 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1070 res = _PyLong_AsByteArray((PyLongObject*)v, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1071 (unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1072 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1073 1, /* little_endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1074 0 /* signed */); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1075 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1076 return res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1077 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1078 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1079 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1080 lp_float(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1081 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1082 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1083 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1084 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1085 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1086 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1087 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1088 return _PyFloat_Pack4(x, (unsigned char *)p, 1); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1089 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1090 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1091 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1092 lp_double(char *p, PyObject *v, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1093 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1094 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1095 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1096 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1097 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1098 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1099 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1100 return _PyFloat_Pack8(x, (unsigned char *)p, 1); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1101 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1102 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1103 static formatdef lilendian_table[] = { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1104 {'x', 1, 0, NULL}, |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
1105 {'b', 1, 0, nu_byte, np_byte}, |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
1106 {'B', 1, 0, nu_ubyte, np_ubyte}, |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1107 {'c', 1, 0, nu_char, np_char}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1108 {'s', 1, 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1109 {'p', 1, 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1110 {'h', 2, 0, lu_int, lp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1111 {'H', 2, 0, lu_uint, lp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1112 {'i', 4, 0, lu_int, lp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1113 {'I', 4, 0, lu_uint, lp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1114 {'l', 4, 0, lu_int, lp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1115 {'L', 4, 0, lu_uint, lp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1116 {'q', 8, 0, lu_longlong, lp_longlong}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1117 {'Q', 8, 0, lu_ulonglong, lp_ulonglong}, |
|
45672
d41a4206405c
Issue 1872: Changed the struct module typecode from 't' to '?', for
Thomas Heller <theller@ctypes.org>
parents:
45373
diff
changeset
|
1118 {'?', 1, 0, bu_bool, bp_bool}, /* Std rep not endian dep, |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
1119 but potentially different from native rep -- reuse bx_bool funcs. */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1120 {'f', 4, 0, lu_float, lp_float}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1121 {'d', 8, 0, lu_double, lp_double}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1122 {0} |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1123 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1124 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1125 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1126 static const formatdef * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1127 whichtable(char **pfmt) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1128 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1129 const char *fmt = (*pfmt)++; /* May be backed out of later */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1130 switch (*fmt) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1131 case '<': |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1132 return lilendian_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1133 case '>': |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1134 case '!': /* Network byte order is big-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1135 return bigendian_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1136 case '=': { /* Host byte order -- different from native in aligment! */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1137 int n = 1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1138 char *p = (char *) &n; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1139 if (*p == 1) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1140 return lilendian_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1141 else |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1142 return bigendian_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1143 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1144 default: |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1145 --*pfmt; /* Back out of pointer increment */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1146 /* Fall through */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1147 case '@': |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1148 return native_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1149 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1150 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1151 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1152 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1153 /* Get the table entry for a format code */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1154 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1155 static const formatdef * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1156 getentry(int c, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1157 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1158 for (; f->format != '\0'; f++) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1159 if (f->format == c) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1160 return f; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1161 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1162 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1163 PyErr_SetString(StructError, "bad char in struct format"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1164 return NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1165 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1166 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1167 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1168 /* Align a size according to a format code */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1169 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1170 static int |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1171 align(Py_ssize_t size, char c, const formatdef *e) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1172 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1173 if (e->format == c) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1174 if (e->alignment) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1175 size = ((size + e->alignment - 1) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1176 / e->alignment) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1177 * e->alignment; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1178 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1179 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1180 return size; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1181 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1182 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1183 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1184 /* calculate the size of a format string */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1185 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1186 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1187 prepare_s(PyStructObject *self) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1188 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1189 const formatdef *f; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1190 const formatdef *e; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1191 formatcode *codes; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1192 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1193 const char *s; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1194 const char *fmt; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1195 char c; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1196 Py_ssize_t size, len, num, itemsize, x; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1197 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1198 fmt = PyString_AS_STRING(self->s_format); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1199 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1200 f = whichtable((char **)&fmt); |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1201 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1202 s = fmt; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1203 size = 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1204 len = 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1205 while ((c = *s++) != '\0') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1206 if (isspace(Py_CHARMASK(c))) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1207 continue; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1208 if ('0' <= c && c <= '9') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1209 num = c - '0'; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1210 while ('0' <= (c = *s++) && c <= '9') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1211 x = num*10 + (c - '0'); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1212 if (x/10 != num) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1213 PyErr_SetString( |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1214 StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1215 "overflow in item count"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1216 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1217 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1218 num = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1219 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1220 if (c == '\0') |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1221 break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1222 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1223 else |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1224 num = 1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1225 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1226 e = getentry(c, f); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1227 if (e == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1228 return -1; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1229 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1230 switch (c) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1231 case 's': /* fall through */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1232 case 'p': len++; break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1233 case 'x': break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1234 default: len += num; break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1235 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1236 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1237 itemsize = e->size; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1238 size = align(size, c, e); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1239 x = num * itemsize; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1240 size += x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1241 if (x/itemsize != num || size < 0) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1242 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1243 "total struct size too long"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1244 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1245 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1246 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1247 |
|
47587
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1248 /* check for overflow */ |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1249 if ((len + 1) > (PY_SSIZE_T_MAX / sizeof(formatcode))) { |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1250 PyErr_NoMemory(); |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1251 return -1; |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1252 } |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1253 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1254 self->s_size = size; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1255 self->s_len = len; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1256 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1257 if (codes == NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1258 PyErr_NoMemory(); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1259 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1260 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1261 self->s_codes = codes; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1262 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1263 s = fmt; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1264 size = 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1265 while ((c = *s++) != '\0') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1266 if (isspace(Py_CHARMASK(c))) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1267 continue; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1268 if ('0' <= c && c <= '9') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1269 num = c - '0'; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1270 while ('0' <= (c = *s++) && c <= '9') |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1271 num = num*10 + (c - '0'); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1272 if (c == '\0') |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1273 break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1274 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1275 else |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1276 num = 1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1277 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1278 e = getentry(c, f); |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1279 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1280 size = align(size, c, e); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1281 if (c == 's' || c == 'p') { |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1282 codes->offset = size; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1283 codes->size = num; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1284 codes->fmtdef = e; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1285 codes++; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1286 size += num; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1287 } else if (c == 'x') { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1288 size += num; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1289 } else { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1290 while (--num >= 0) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1291 codes->offset = size; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1292 codes->size = e->size; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1293 codes->fmtdef = e; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1294 codes++; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1295 size += e->size; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1296 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1297 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1298 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1299 codes->fmtdef = NULL; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1300 codes->offset = size; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1301 codes->size = 0; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1302 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1303 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1304 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1305 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1306 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1307 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1308 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1309 PyObject *self; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1310 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1311 assert(type != NULL && type->tp_alloc != NULL); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1312 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1313 self = type->tp_alloc(type, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1314 if (self != NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1315 PyStructObject *s = (PyStructObject*)self; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1316 Py_INCREF(Py_None); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1317 s->s_format = Py_None; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1318 s->s_codes = NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1319 s->s_size = -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1320 s->s_len = -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1321 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1322 return self; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1323 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1324 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1325 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1326 s_init(PyObject *self, PyObject *args, PyObject *kwds) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1327 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1328 PyStructObject *soself = (PyStructObject *)self; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1329 PyObject *o_format = NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1330 int ret = 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1331 static char *kwlist[] = {"format", 0}; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1332 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1333 assert(PyStruct_Check(self)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1334 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1335 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1336 &o_format)) |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1337 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1338 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1339 Py_INCREF(o_format); |
|
45373
f378edd21df2
Crashers of the day: Py_CLEAR must be used when there is a chance that the
Amaury Forgeot d'Arc <amauryfa@gmail.com>
parents:
44733
diff
changeset
|
1340 Py_CLEAR(soself->s_format); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1341 soself->s_format = o_format; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1342 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1343 ret = prepare_s(soself); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1344 return ret; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1345 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1346 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1347 static void |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1348 s_dealloc(PyStructObject *s) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1349 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1350 if (s->weakreflist != NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1351 PyObject_ClearWeakRefs((PyObject *)s); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1352 if (s->s_codes != NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1353 PyMem_FREE(s->s_codes); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1354 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1355 Py_XDECREF(s->s_format); |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
1356 Py_TYPE(s)->tp_free((PyObject *)s); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1357 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1358 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1359 static PyObject * |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1360 s_unpack_internal(PyStructObject *soself, char *startfrom) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1361 formatcode *code; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1362 Py_ssize_t i = 0; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1363 PyObject *result = PyTuple_New(soself->s_len); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1364 if (result == NULL) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1365 return NULL; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1366 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1367 for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1368 PyObject *v; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1369 const formatdef *e = code->fmtdef; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1370 const char *res = startfrom + code->offset; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1371 if (e->format == 's') { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1372 v = PyString_FromStringAndSize(res, code->size); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1373 } else if (e->format == 'p') { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1374 Py_ssize_t n = *(unsigned char*)res; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1375 if (n >= code->size) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1376 n = code->size - 1; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1377 v = PyString_FromStringAndSize(res + 1, n); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1378 } else { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1379 v = e->unpack(res, e); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1380 } |
| 38736 | 1381 if (v == NULL) |
| 1382 goto fail; | |
| 1383 PyTuple_SET_ITEM(result, i++, v); | |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1384 } |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1385 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1386 return result; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1387 fail: |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1388 Py_DECREF(result); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1389 return NULL; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1390 } |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1391 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1392 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1393 PyDoc_STRVAR(s_unpack__doc__, |
|
38430
795d1cb1df01
Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents:
38429
diff
changeset
|
1394 "S.unpack(str) -> (v1, v2, ...)\n\ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1395 \n\ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1396 Return tuple containing values unpacked according to this Struct's format.\n\ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1397 Requires len(str) == self.size. See struct.__doc__ for more on format\n\ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1398 strings."); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1399 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1400 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1401 s_unpack(PyObject *self, PyObject *inputstr) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1402 { |
|
41391
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1403 char *start; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1404 Py_ssize_t len; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1405 PyObject *args=NULL, *result; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1406 PyStructObject *soself = (PyStructObject *)self; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1407 assert(PyStruct_Check(self)); |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1408 assert(soself->s_codes != NULL); |
|
41391
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1409 if (inputstr == NULL) |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1410 goto fail; |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1411 if (PyString_Check(inputstr) && |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1412 PyString_GET_SIZE(inputstr) == soself->s_size) { |
|
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1413 return s_unpack_internal(soself, PyString_AS_STRING(inputstr)); |
|
41391
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1414 } |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1415 args = PyTuple_Pack(1, inputstr); |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1416 if (args == NULL) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1417 return NULL; |
|
41391
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1418 if (!PyArg_ParseTuple(args, "s#:unpack", &start, &len)) |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1419 goto fail; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1420 if (soself->s_size != len) |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1421 goto fail; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1422 result = s_unpack_internal(soself, start); |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1423 Py_DECREF(args); |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1424 return result; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1425 |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1426 fail: |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1427 Py_XDECREF(args); |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1428 PyErr_Format(StructError, |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1429 "unpack requires a string argument of length %zd", |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1430 soself->s_size); |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1431 return NULL; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1432 } |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1433 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1434 PyDoc_STRVAR(s_unpack_from__doc__, |
|
38430
795d1cb1df01
Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents:
38429
diff
changeset
|
1435 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\ |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1436 \n\ |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1437 Return tuple containing values unpacked according to this Struct's format.\n\ |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1438 Unlike unpack, unpack_from can unpack values from any object supporting\n\ |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1439 the buffer API, not just str. Requires len(buffer[offset:]) >= self.size.\n\ |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1440 See struct.__doc__ for more on format strings."); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1441 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1442 static PyObject * |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1443 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1444 { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1445 static char *kwlist[] = {"buffer", "offset", 0}; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1446 #if (PY_VERSION_HEX < 0x02050000) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1447 static char *fmt = "z#|i:unpack_from"; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1448 #else |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1449 static char *fmt = "z#|n:unpack_from"; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1450 #endif |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1451 Py_ssize_t buffer_len = 0, offset = 0; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1452 char *buffer = NULL; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1453 PyStructObject *soself = (PyStructObject *)self; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1454 assert(PyStruct_Check(self)); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1455 assert(soself->s_codes != NULL); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1456 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1457 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1458 &buffer, &buffer_len, &offset)) |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1459 return NULL; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1460 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1461 if (buffer == NULL) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1462 PyErr_Format(StructError, |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1463 "unpack_from requires a buffer argument"); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1464 return NULL; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1465 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1466 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1467 if (offset < 0) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1468 offset += buffer_len; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1469 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1470 if (offset < 0 || (buffer_len - offset) < soself->s_size) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1471 PyErr_Format(StructError, |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1472 "unpack_from requires a buffer of at least %zd bytes", |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1473 soself->s_size); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1474 return NULL; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1475 } |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1476 return s_unpack_internal(soself, buffer + offset); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1477 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1478 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1479 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1480 /* |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1481 * Guts of the pack function. |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1482 * |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1483 * Takes a struct object, a tuple of arguments, and offset in that tuple of |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1484 * argument for where to start processing the arguments for packing, and a |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1485 * character buffer for writing the packed string. The caller must insure |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1486 * that the buffer may contain the required length for packing the arguments. |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1487 * 0 is returned on success, 1 is returned if there is an error. |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1488 * |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1489 */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1490 static int |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1491 s_pack_internal(PyStructObject *soself, PyObject *args, int offset, char* buf) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1492 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1493 formatcode *code; |
| 38736 | 1494 /* XXX(nnorwitz): why does i need to be a local? can we use |
| 1495 the offset parameter or do we need the wider width? */ | |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1496 Py_ssize_t i; |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1497 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1498 memset(buf, '\0', soself->s_size); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1499 i = offset; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1500 for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1501 Py_ssize_t n; |
| 38736 | 1502 PyObject *v = PyTuple_GET_ITEM(args, i++); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1503 const formatdef *e = code->fmtdef; |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1504 char *res = buf + code->offset; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1505 if (e->format == 's') { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1506 if (!PyString_Check(v)) { |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1507 PyErr_SetString(StructError, |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1508 "argument for 's' must " |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1509 "be a string"); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1510 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1511 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1512 n = PyString_GET_SIZE(v); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1513 if (n > code->size) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1514 n = code->size; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1515 if (n > 0) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1516 memcpy(res, PyString_AS_STRING(v), n); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1517 } else if (e->format == 'p') { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1518 if (!PyString_Check(v)) { |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1519 PyErr_SetString(StructError, |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1520 "argument for 'p' must " |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1521 "be a string"); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1522 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1523 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1524 n = PyString_GET_SIZE(v); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1525 if (n > (code->size - 1)) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1526 n = code->size - 1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1527 if (n > 0) |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1528 memcpy(res + 1, PyString_AS_STRING(v), n); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1529 if (n > 255) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1530 n = 255; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1531 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1532 } else if (e->pack(res, v, e) < 0) { |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1533 if (strchr(integer_codes, e->format) != NULL && |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1534 PyErr_ExceptionMatches(PyExc_OverflowError)) |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1535 PyErr_Format(StructError, |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1536 "integer out of range for " |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1537 "'%c' format code", |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1538 e->format); |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1539 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1540 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1541 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1542 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1543 /* Success */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1544 return 0; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1545 } |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1546 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1547 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1548 PyDoc_STRVAR(s_pack__doc__, |
|
38430
795d1cb1df01
Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents:
38429
diff
changeset
|
1549 "S.pack(v1, v2, ...) -> string\n\ |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1550 \n\ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1551 Return a string containing values v1, v2, ... packed according to this\n\ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1552 Struct's format. See struct.__doc__ for more on format strings."); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1553 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1554 static PyObject * |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1555 s_pack(PyObject *self, PyObject *args) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1556 { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1557 PyStructObject *soself; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1558 PyObject *result; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1559 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1560 /* Validate arguments. */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1561 soself = (PyStructObject *)self; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1562 assert(PyStruct_Check(self)); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1563 assert(soself->s_codes != NULL); |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1564 if (PyTuple_GET_SIZE(args) != soself->s_len) |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1565 { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1566 PyErr_Format(StructError, |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1567 "pack requires exactly %zd arguments", soself->s_len); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1568 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1569 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1570 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1571 /* Allocate a new string */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1572 result = PyString_FromStringAndSize((char *)NULL, soself->s_size); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1573 if (result == NULL) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1574 return NULL; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1575 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1576 /* Call the guts */ |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1577 if ( s_pack_internal(soself, args, 0, PyString_AS_STRING(result)) != 0 ) { |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1578 Py_DECREF(result); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1579 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1580 } |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1581 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1582 return result; |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1583 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1584 |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1585 PyDoc_STRVAR(s_pack_into__doc__, |
|
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1586 "S.pack_into(buffer, offset, v1, v2, ...)\n\ |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1587 \n\ |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1588 Pack the values v1, v2, ... according to this Struct's format, write \n\ |
|
38430
795d1cb1df01
Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents:
38429
diff
changeset
|
1589 the packed bytes into the writable buffer buf starting at offset. Note\n\ |
|
795d1cb1df01
Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents:
38429
diff
changeset
|
1590 that the offset is not an optional argument. See struct.__doc__ for \n\ |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1591 more on format strings."); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1592 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1593 static PyObject * |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1594 s_pack_into(PyObject *self, PyObject *args) |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1595 { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1596 PyStructObject *soself; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1597 char *buffer; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1598 Py_ssize_t buffer_len, offset; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1599 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1600 /* Validate arguments. +1 is for the first arg as buffer. */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1601 soself = (PyStructObject *)self; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1602 assert(PyStruct_Check(self)); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1603 assert(soself->s_codes != NULL); |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1604 if (PyTuple_GET_SIZE(args) != (soself->s_len + 2)) |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1605 { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1606 PyErr_Format(StructError, |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1607 "pack_into requires exactly %zd arguments", |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1608 (soself->s_len + 2)); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1609 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1610 } |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1611 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1612 /* Extract a writable memory buffer from the first argument */ |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1613 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), |
|
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1614 (void**)&buffer, &buffer_len) == -1 ) { |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1615 return NULL; |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1616 } |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1617 assert( buffer_len >= 0 ); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1618 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1619 /* Extract the offset from the first argument */ |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1620 offset = PyInt_AsSsize_t(PyTuple_GET_ITEM(args, 1)); |
|
49351
6a73134f9603
Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents:
47587
diff
changeset
|
1621 if (offset == -1 && PyErr_Occurred()) |
|
6a73134f9603
Victor Stinner's patches to check the return result of PyLong_Ssize_t
Benjamin Peterson <benjamin@python.org>
parents:
47587
diff
changeset
|
1622 return NULL; |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1623 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1624 /* Support negative offsets. */ |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1625 if (offset < 0) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1626 offset += buffer_len; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1627 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1628 /* Check boundaries */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1629 if (offset < 0 || (buffer_len - offset) < soself->s_size) { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1630 PyErr_Format(StructError, |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1631 "pack_into requires a buffer of at least %zd bytes", |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1632 soself->s_size); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1633 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1634 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1635 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1636 /* Call the guts */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1637 if ( s_pack_internal(soself, args, 2, buffer + offset) != 0 ) { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1638 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1639 } |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1640 |
|
38470
e0cea605417e
Fix ref-antileak in _struct.c which eventually lead to deallocating None.
Georg Brandl <georg@python.org>
parents:
38445
diff
changeset
|
1641 Py_RETURN_NONE; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1642 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1643 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1644 static PyObject * |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1645 s_get_format(PyStructObject *self, void *unused) |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1646 { |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1647 Py_INCREF(self->s_format); |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1648 return self->s_format; |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1649 } |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1650 |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1651 static PyObject * |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1652 s_get_size(PyStructObject *self, void *unused) |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1653 { |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1654 return PyInt_FromSsize_t(self->s_size); |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1655 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1656 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1657 /* List of functions */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1658 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1659 static struct PyMethodDef s_methods[] = { |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1660 {"pack", s_pack, METH_VARARGS, s_pack__doc__}, |
|
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1661 {"pack_into", s_pack_into, METH_VARARGS, s_pack_into__doc__}, |
|
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1662 {"unpack", s_unpack, METH_O, s_unpack__doc__}, |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41526
diff
changeset
|
1663 {"unpack_from", (PyCFunction)s_unpack_from, METH_VARARGS|METH_KEYWORDS, |
|
38583
e00e68dd1ddf
s_methods[]: Stop compiler warnings by casting
Tim Peters <tim.peters@gmail.com>
parents:
38580
diff
changeset
|
1664 s_unpack_from__doc__}, |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1665 {NULL, NULL} /* sentinel */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1666 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1667 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1668 PyDoc_STRVAR(s__doc__, "Compiled struct object"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1669 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1670 #define OFF(x) offsetof(PyStructObject, x) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1671 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1672 static PyGetSetDef s_getsetlist[] = { |
|
38430
795d1cb1df01
Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents:
38429
diff
changeset
|
1673 {"format", (getter)s_get_format, (setter)NULL, "struct format string", NULL}, |
|
795d1cb1df01
Fix up struct docstrings, add struct.pack_to function for symmetry
Bob Ippolito <bob@redivi.com>
parents:
38429
diff
changeset
|
1674 {"size", (getter)s_get_size, (setter)NULL, "struct size in bytes", NULL}, |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1675 {NULL} /* sentinel */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1676 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1677 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1678 static |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1679 PyTypeObject PyStructType = { |
|
42333
6620a510e9a9
PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents:
41816
diff
changeset
|
1680 PyVarObject_HEAD_INIT(NULL, 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1681 "Struct", |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1682 sizeof(PyStructObject), |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1683 0, |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1684 (destructor)s_dealloc, /* tp_dealloc */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1685 0, /* tp_print */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1686 0, /* tp_getattr */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1687 0, /* tp_setattr */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1688 0, /* tp_compare */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1689 0, /* tp_repr */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1690 0, /* tp_as_number */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1691 0, /* tp_as_sequence */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1692 0, /* tp_as_mapping */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1693 0, /* tp_hash */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1694 0, /* tp_call */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1695 0, /* tp_str */ |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1696 PyObject_GenericGetAttr, /* tp_getattro */ |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1697 PyObject_GenericSetAttr, /* tp_setattro */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1698 0, /* tp_as_buffer */ |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1699 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | Py_TPFLAGS_HAVE_WEAKREFS,/* tp_flags */ |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1700 s__doc__, /* tp_doc */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1701 0, /* tp_traverse */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1702 0, /* tp_clear */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1703 0, /* tp_richcompare */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1704 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1705 0, /* tp_iter */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1706 0, /* tp_iternext */ |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1707 s_methods, /* tp_methods */ |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1708 NULL, /* tp_members */ |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1709 s_getsetlist, /* tp_getset */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1710 0, /* tp_base */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1711 0, /* tp_dict */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1712 0, /* tp_descr_get */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1713 0, /* tp_descr_set */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1714 0, /* tp_dictoffset */ |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1715 s_init, /* tp_init */ |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1716 PyType_GenericAlloc,/* tp_alloc */ |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1717 s_new, /* tp_new */ |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1718 PyObject_Del, /* tp_free */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1719 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1720 |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1721 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1722 /* ---- Standalone functions ---- */ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1723 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1724 #define MAXCACHE 100 |
|
44459
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1725 static PyObject *cache = NULL; |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1726 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1727 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1728 cache_struct(PyObject *fmt) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1729 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1730 PyObject * s_object; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1731 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1732 if (cache == NULL) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1733 cache = PyDict_New(); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1734 if (cache == NULL) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1735 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1736 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1737 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1738 s_object = PyDict_GetItem(cache, fmt); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1739 if (s_object != NULL) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1740 Py_INCREF(s_object); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1741 return s_object; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1742 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1743 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1744 s_object = PyObject_CallFunctionObjArgs((PyObject *)(&PyStructType), fmt, NULL); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1745 if (s_object != NULL) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1746 if (PyDict_Size(cache) >= MAXCACHE) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1747 PyDict_Clear(cache); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1748 /* Attempt to cache the result */ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1749 if (PyDict_SetItem(cache, fmt, s_object) == -1) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1750 PyErr_Clear(); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1751 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1752 return s_object; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1753 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1754 |
|
44459
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1755 PyDoc_STRVAR(clearcache_doc, |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1756 "Clear the internal cache."); |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1757 |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1758 static PyObject * |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1759 clearcache(PyObject *self) |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1760 { |
|
44733
85cf220f16d8
clearcache() needs to remove the dict as well as clear it.
Raymond Hettinger <python@rcn.com>
parents:
44459
diff
changeset
|
1761 Py_CLEAR(cache); |
|
44459
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1762 Py_RETURN_NONE; |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1763 } |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1764 |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1765 PyDoc_STRVAR(calcsize_doc, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1766 "Return size of C struct described by format string fmt."); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1767 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1768 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1769 calcsize(PyObject *self, PyObject *fmt) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1770 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1771 Py_ssize_t n; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1772 PyObject *s_object = cache_struct(fmt); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1773 if (s_object == NULL) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1774 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1775 n = ((PyStructObject *)s_object)->s_size; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1776 Py_DECREF(s_object); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1777 return PyInt_FromSsize_t(n); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1778 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1779 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1780 PyDoc_STRVAR(pack_doc, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1781 "Return string containing values v1, v2, ... packed according to fmt."); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1782 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1783 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1784 pack(PyObject *self, PyObject *args) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1785 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1786 PyObject *s_object, *fmt, *newargs, *result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1787 Py_ssize_t n = PyTuple_GET_SIZE(args); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1788 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1789 if (n == 0) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1790 PyErr_SetString(PyExc_TypeError, "missing format argument"); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1791 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1792 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1793 fmt = PyTuple_GET_ITEM(args, 0); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1794 newargs = PyTuple_GetSlice(args, 1, n); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1795 if (newargs == NULL) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1796 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1797 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1798 s_object = cache_struct(fmt); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1799 if (s_object == NULL) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1800 Py_DECREF(newargs); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1801 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1802 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1803 result = s_pack(s_object, newargs); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1804 Py_DECREF(newargs); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1805 Py_DECREF(s_object); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1806 return result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1807 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1808 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1809 PyDoc_STRVAR(pack_into_doc, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1810 "Pack the values v1, v2, ... according to fmt.\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1811 Write the packed bytes into the writable buffer buf starting at offset."); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1812 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1813 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1814 pack_into(PyObject *self, PyObject *args) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1815 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1816 PyObject *s_object, *fmt, *newargs, *result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1817 Py_ssize_t n = PyTuple_GET_SIZE(args); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1818 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1819 if (n == 0) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1820 PyErr_SetString(PyExc_TypeError, "missing format argument"); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1821 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1822 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1823 fmt = PyTuple_GET_ITEM(args, 0); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1824 newargs = PyTuple_GetSlice(args, 1, n); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1825 if (newargs == NULL) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1826 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1827 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1828 s_object = cache_struct(fmt); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1829 if (s_object == NULL) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1830 Py_DECREF(newargs); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1831 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1832 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1833 result = s_pack_into(s_object, newargs); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1834 Py_DECREF(newargs); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1835 Py_DECREF(s_object); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1836 return result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1837 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1838 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1839 PyDoc_STRVAR(unpack_doc, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1840 "Unpack the string containing packed C structure data, according to fmt.\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1841 Requires len(string) == calcsize(fmt)."); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1842 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1843 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1844 unpack(PyObject *self, PyObject *args) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1845 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1846 PyObject *s_object, *fmt, *inputstr, *result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1847 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1848 if (!PyArg_UnpackTuple(args, "unpack", 2, 2, &fmt, &inputstr)) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1849 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1850 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1851 s_object = cache_struct(fmt); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1852 if (s_object == NULL) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1853 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1854 result = s_unpack(s_object, inputstr); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1855 Py_DECREF(s_object); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1856 return result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1857 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1858 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1859 PyDoc_STRVAR(unpack_from_doc, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1860 "Unpack the buffer, containing packed C structure data, according to\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1861 fmt, starting at offset. Requires len(buffer[offset:]) >= calcsize(fmt)."); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1862 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1863 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1864 unpack_from(PyObject *self, PyObject *args, PyObject *kwds) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1865 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1866 PyObject *s_object, *fmt, *newargs, *result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1867 Py_ssize_t n = PyTuple_GET_SIZE(args); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1868 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1869 if (n == 0) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1870 PyErr_SetString(PyExc_TypeError, "missing format argument"); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1871 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1872 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1873 fmt = PyTuple_GET_ITEM(args, 0); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1874 newargs = PyTuple_GetSlice(args, 1, n); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1875 if (newargs == NULL) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1876 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1877 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1878 s_object = cache_struct(fmt); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1879 if (s_object == NULL) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1880 Py_DECREF(newargs); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1881 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1882 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1883 result = s_unpack_from(s_object, newargs, kwds); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1884 Py_DECREF(newargs); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1885 Py_DECREF(s_object); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1886 return result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1887 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1888 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1889 static struct PyMethodDef module_functions[] = { |
|
44459
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1890 {"_clearcache", (PyCFunction)clearcache, METH_NOARGS, clearcache_doc}, |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1891 {"calcsize", calcsize, METH_O, calcsize_doc}, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1892 {"pack", pack, METH_VARARGS, pack_doc}, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1893 {"pack_into", pack_into, METH_VARARGS, pack_into_doc}, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1894 {"unpack", unpack, METH_VARARGS, unpack_doc}, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1895 {"unpack_from", (PyCFunction)unpack_from, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1896 METH_VARARGS|METH_KEYWORDS, unpack_from_doc}, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1897 {NULL, NULL} /* sentinel */ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1898 }; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1899 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1900 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1901 /* Module initialization */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1902 |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1903 PyDoc_STRVAR(module_doc, |
|
56416
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1904 "Functions to convert between Python values and C structs represented\n\ |
|
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1905 as Python strings. It uses format strings (explained below) as compact\n\ |
|
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1906 descriptions of the lay-out of the C structs and the intended conversion\n\ |
|
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1907 to/from Python values.\n\ |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1908 \n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1909 The optional first format char indicates byte order, size and alignment:\n\ |
|
56416
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1910 @: native order, size & alignment (default)\n\ |
|
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1911 =: native order, std. size & alignment\n\ |
|
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1912 <: little-endian, std. size & alignment\n\ |
|
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1913 >: big-endian, std. size & alignment\n\ |
|
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1914 !: same as >\n\ |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1915 \n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1916 The remaining chars indicate types of args and must match exactly;\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1917 these can be preceded by a decimal repeat count:\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1918 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\ |
|
56416
e759ca25701d
Issue #7078: _struct.__doc__ was being ignored. Import it into struct.
Mark Dickinson <dickinsm@gmail.com>
parents:
56246
diff
changeset
|
1919 ?: _Bool (requires C99; if not available, char is used instead)\n\ |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1920 h:short; H:unsigned short; i:int; I:unsigned int;\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1921 l:long; L:unsigned long; f:float; d:double.\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1922 Special cases (preceding decimal count indicates length):\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1923 s:string (array of char); p: pascal string (with count byte).\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1924 Special case (only available in native format):\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1925 P:an integer type that is wide enough to hold a pointer.\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1926 Special case (not in native mode unless 'long long' in platform C):\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1927 q:long long; Q:unsigned long long\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1928 Whitespace between formats is ignored.\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1929 \n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1930 The variable struct.error is an exception raised on errors.\n"); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1931 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1932 PyMODINIT_FUNC |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1933 init_struct(void) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1934 { |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1935 PyObject *ver, *m; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1936 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1937 ver = PyString_FromString("0.2"); |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1938 if (ver == NULL) |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1939 return; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1940 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1941 m = Py_InitModule3("_struct", module_functions, module_doc); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1942 if (m == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1943 return; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1944 |
|
44350
26384b8180c7
#1629: Renamed Py_Size, Py_Type and Py_Refcnt to Py_SIZE, Py_TYPE and Py_REFCNT. Macros for b/w compatibility are available.
Christian Heimes <christian@cheimes.de>
parents:
42333
diff
changeset
|
1945 Py_TYPE(&PyStructType) = &PyType_Type; |
| 38312 | 1946 if (PyType_Ready(&PyStructType) < 0) |
| 1947 return; | |
| 1948 | |
|
55153
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1949 /* This speed trick can't be used until overflow masking goes |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1950 away, because native endian always raises exceptions |
|
b3fb5e6b7e82
Issue #1523: Remove deprecated overflow masking in struct module, and
Mark Dickinson <dickinsm@gmail.com>
parents:
55142
diff
changeset
|
1951 instead of overflow masking. */ |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1952 |
|
38317
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1953 /* Check endian and swap in faster functions */ |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1954 { |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1955 int one = 1; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1956 formatdef *native = native_table; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1957 formatdef *other, *ptr; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1958 if ((int)*(unsigned char*)&one) |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1959 other = lilendian_table; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1960 else |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1961 other = bigendian_table; |
|
38322
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1962 /* Scan through the native table, find a matching |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1963 entry in the endian table and swap in the |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1964 native implementations whenever possible |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1965 (64-bit platforms may not have "standard" sizes) */ |
|
38317
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1966 while (native->format != '\0' && other->format != '\0') { |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1967 ptr = other; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1968 while (ptr->format != '\0') { |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1969 if (ptr->format == native->format) { |
|
38322
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1970 /* Match faster when formats are |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1971 listed in the same order */ |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1972 if (ptr == other) |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1973 other++; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1974 /* Only use the trick if the |
|
38322
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1975 size matches */ |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1976 if (ptr->size != native->size) |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1977 break; |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1978 /* Skip float and double, could be |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1979 "unknown" float format */ |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1980 if (ptr->format == 'd' || ptr->format == 'f') |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
1981 break; |
|
38317
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1982 ptr->pack = native->pack; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1983 ptr->unpack = native->unpack; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1984 break; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1985 } |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1986 ptr++; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1987 } |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1988 native++; |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1989 } |
|
68a348c9868d
Use faster struct pack/unpack functions for the endian table that matches the host's
Bob Ippolito <bob@redivi.com>
parents:
38316
diff
changeset
|
1990 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1991 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1992 /* Add some symbolic constants to the module */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1993 if (StructError == NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1994 StructError = PyErr_NewException("struct.error", NULL, NULL); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1995 if (StructError == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1996 return; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1997 } |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
1998 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1999 Py_INCREF(StructError); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2000 PyModule_AddObject(m, "error", StructError); |
|
38316
2f16e2715223
Use LONG_MIN and LONG_MAX to check Python integer bounds instead of the incorrect INT_MIN and INT_MAX
Bob Ippolito <bob@redivi.com>
parents:
38313
diff
changeset
|
2001 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2002 Py_INCREF((PyObject*)&PyStructType); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2003 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType); |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
2004 |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2005 PyModule_AddObject(m, "__version__", ver); |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2006 |
|
38493
7d510e4f3c98
struct: modulo math plus warning on all endian-explicit formats for compatibility with older struct usage (ugly)
Bob Ippolito <bob@redivi.com>
parents:
38484
diff
changeset
|
2007 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1); |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
2008 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2009 } |
