Mercurial > cpython-lru_cache-weakref
annotate Modules/_struct.c @ 49955:878d40468c4e legacy-trunk
Issue #4384: Added logging integration with warnings module using captureWarnings(). This change includes a NullHandler which does nothing; it will be of use to library developers who want to avoid the "No handlers could be found for logger XXX" message which can appear if the library user doesn't configure logging.
| author | Vinay Sajip <vinay_sajip@yahoo.co.uk> |
|---|---|
| date | Wed, 03 Dec 2008 23:22:58 +0000 |
| parents | 6a73134f9603 |
| children | df3923c36ec5 |
| 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 |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
20 /* If PY_STRUCT_OVERFLOW_MASKING is defined, the struct module will wrap all input |
|
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
|
21 numbers for explicit endians such that they fit in the given type, much |
|
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
|
22 like explicit casting in C. A warning will be raised if the number did |
|
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
|
23 not originally fit within the range of the requested type. If it is |
|
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
|
24 not defined, then all range errors and overflow will be struct.error |
|
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
|
25 exceptions. */ |
|
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
|
26 |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
27 #define PY_STRUCT_OVERFLOW_MASKING 1 |
|
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
|
28 |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
29 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
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
|
30 static PyObject *pylong_ulong_mask = NULL; |
|
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
|
31 static PyObject *pyint_zero = NULL; |
|
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
|
32 #endif |
|
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
|
33 |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
34 /* If PY_STRUCT_FLOAT_COERCE is defined, the struct module will allow float |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
35 arguments for integer formats with a warning for backwards |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
36 compatibility. */ |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
37 |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
38 #define PY_STRUCT_FLOAT_COERCE 1 |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
39 |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
40 #ifdef PY_STRUCT_FLOAT_COERCE |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
41 #define FLOAT_COERCE "integer argument expected, got float" |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
42 #endif |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
43 |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
44 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
45 /* 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
|
46 typedef struct _formatdef { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
47 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
|
48 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
|
49 Py_ssize_t alignment; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
50 PyObject* (*unpack)(const char *, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
51 const struct _formatdef *); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
52 int (*pack)(char *, PyObject *, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
53 const struct _formatdef *); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
54 } formatdef; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
55 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
56 typedef struct _formatcode { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
57 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
|
58 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
|
59 Py_ssize_t size; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
60 } formatcode; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
61 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
62 /* Struct object interface */ |
|
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 typedef struct { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
65 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
|
66 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
|
67 Py_ssize_t s_len; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
68 formatcode *s_codes; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
69 PyObject *s_format; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
70 PyObject *weakreflist; /* List of weak references */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
71 } PyStructObject; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
72 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
73 |
| 38267 | 74 #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
|
75 #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
|
76 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
77 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
78 /* Exception */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
79 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
80 static PyObject *StructError; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
81 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
82 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
83 /* 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
|
84 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
85 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
86 typedef struct { char c; short x; } st_short; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
87 typedef struct { char c; int x; } st_int; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
88 typedef struct { char c; long x; } st_long; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
89 typedef struct { char c; float x; } st_float; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
90 typedef struct { char c; double x; } st_double; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
91 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
|
92 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
93 #define SHORT_ALIGN (sizeof(st_short) - sizeof(short)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
94 #define INT_ALIGN (sizeof(st_int) - sizeof(int)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
95 #define LONG_ALIGN (sizeof(st_long) - sizeof(long)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
96 #define FLOAT_ALIGN (sizeof(st_float) - sizeof(float)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
97 #define DOUBLE_ALIGN (sizeof(st_double) - sizeof(double)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
98 #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
|
99 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
100 /* 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
|
101 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
|
102 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
103 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
|
104 #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
|
105 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
106 |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
107 #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
|
108 #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
|
109 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
|
110 #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
|
111 #else |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
112 #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
|
113 #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
|
114 #endif |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
115 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
116 #define STRINGIFY(x) #x |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
117 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
118 #ifdef __powerc |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
119 #pragma options align=reset |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
120 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
121 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
122 /* 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
|
123 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
124 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
125 get_pylong(PyObject *v) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
126 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
127 PyNumberMethods *m; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
128 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
129 assert(v != NULL); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
130 if (PyInt_Check(v)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
131 return PyLong_FromLong(PyInt_AS_LONG(v)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
132 if (PyLong_Check(v)) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
133 Py_INCREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
134 return v; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
135 } |
|
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
|
136 m = Py_TYPE(v)->tp_as_number; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
137 if (m != NULL && m->nb_long != NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
138 v = m->nb_long(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
139 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
140 return NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
141 if (PyLong_Check(v)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
142 return v; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
143 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
144 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
145 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
146 "cannot convert argument to long"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
147 return NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
148 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
149 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
150 /* Helper routine to get a Python integer and raise the appropriate error |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
151 if it isn't one */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
152 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
153 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
154 get_long(PyObject *v, long *p) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
155 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
156 long x = PyInt_AsLong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
157 if (x == -1 && PyErr_Occurred()) { |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
158 #ifdef PY_STRUCT_FLOAT_COERCE |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
159 if (PyFloat_Check(v)) { |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
160 PyObject *o; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
161 int res; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
162 PyErr_Clear(); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
163 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
164 return -1; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
165 o = PyNumber_Int(v); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
166 if (o == NULL) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
167 return -1; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
168 res = get_long(o, p); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
169 Py_DECREF(o); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
170 return res; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
171 } |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
172 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
173 if (PyErr_ExceptionMatches(PyExc_TypeError)) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
174 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
175 "required argument is not an integer"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
176 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
177 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
178 *p = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
179 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
180 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
181 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
182 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
183 /* Same, but handling unsigned long */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
184 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
185 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
186 get_ulong(PyObject *v, unsigned long *p) |
|
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 if (PyLong_Check(v)) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
189 unsigned long x = PyLong_AsUnsignedLong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
190 if (x == (unsigned long)(-1) && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
191 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
192 *p = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
193 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
194 } |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
195 if (get_long(v, (long *)p) < 0) |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
196 return -1; |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
197 if (((long)*p) < 0) { |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
198 PyErr_SetString(StructError, |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
199 "unsigned argument is < 0"); |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
200 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
201 } |
|
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 0; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
203 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
204 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
205 #ifdef HAVE_LONG_LONG |
|
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 /* Same, but handling native 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 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
210 get_longlong(PyObject *v, PY_LONG_LONG *p) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
211 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
212 PY_LONG_LONG x; |
|
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 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
215 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
216 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
217 assert(PyLong_Check(v)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
218 x = PyLong_AsLongLong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
219 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
220 if (x == (PY_LONG_LONG)-1 && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
221 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
222 *p = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
223 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
224 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
225 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
226 /* Same, but handling native unsigned long long. */ |
|
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 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
229 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
|
230 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
231 unsigned PY_LONG_LONG x; |
|
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 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
234 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
235 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
236 assert(PyLong_Check(v)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
237 x = PyLong_AsUnsignedLongLong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
238 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
239 if (x == (unsigned PY_LONG_LONG)-1 && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
240 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
241 *p = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
242 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
243 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
244 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
245 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
246 |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
247 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
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
|
248 |
|
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
|
249 /* Helper routine to get a Python integer and raise the appropriate error |
|
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
|
250 if it isn't one */ |
|
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
|
251 |
|
39301
7dc5f3ede4cd
Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn().
Neal Norwitz <nnorwitz@gmail.com>
parents:
38736
diff
changeset
|
252 #define INT_OVERFLOW "struct integer overflow masking is deprecated" |
|
7dc5f3ede4cd
Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn().
Neal Norwitz <nnorwitz@gmail.com>
parents:
38736
diff
changeset
|
253 |
|
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
|
254 static int |
|
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
|
255 get_wrapped_long(PyObject *v, long *p) |
|
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
|
256 { |
|
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
|
257 if (get_long(v, p) < 0) { |
| 38736 | 258 if (PyLong_Check(v) && |
| 259 PyErr_ExceptionMatches(PyExc_OverflowError)) { | |
|
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
|
260 PyObject *wrapped; |
|
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
|
261 long x; |
|
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
|
262 PyErr_Clear(); |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
263 #ifdef PY_STRUCT_FLOAT_COERCE |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
264 if (PyFloat_Check(v)) { |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
265 PyObject *o; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
266 int res; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
267 PyErr_Clear(); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
268 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
269 return -1; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
270 o = PyNumber_Int(v); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
271 if (o == NULL) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
272 return -1; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
273 res = get_wrapped_long(o, p); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
274 Py_DECREF(o); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
275 return res; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
276 } |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
277 #endif |
|
39301
7dc5f3ede4cd
Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn().
Neal Norwitz <nnorwitz@gmail.com>
parents:
38736
diff
changeset
|
278 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) |
|
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
|
279 return -1; |
|
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
|
280 wrapped = PyNumber_And(v, pylong_ulong_mask); |
|
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
|
281 if (wrapped == NULL) |
|
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
|
282 return -1; |
|
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
|
283 x = (long)PyLong_AsUnsignedLong(wrapped); |
|
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
|
284 Py_DECREF(wrapped); |
|
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
|
285 if (x == -1 && PyErr_Occurred()) |
|
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
|
286 return -1; |
|
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
|
287 *p = x; |
|
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
|
288 } else { |
|
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
|
289 return -1; |
|
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
|
290 } |
|
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
|
291 } |
|
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
|
292 return 0; |
|
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
|
293 } |
|
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
|
294 |
|
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
|
295 static int |
|
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
|
296 get_wrapped_ulong(PyObject *v, unsigned long *p) |
|
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
|
297 { |
|
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
|
298 long x = (long)PyLong_AsUnsignedLong(v); |
|
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
|
299 if (x == -1 && PyErr_Occurred()) { |
|
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
|
300 PyObject *wrapped; |
|
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
|
301 PyErr_Clear(); |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
302 #ifdef PY_STRUCT_FLOAT_COERCE |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
303 if (PyFloat_Check(v)) { |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
304 PyObject *o; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
305 int res; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
306 PyErr_Clear(); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
307 if (PyErr_WarnEx(PyExc_DeprecationWarning, FLOAT_COERCE, 2) < 0) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
308 return -1; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
309 o = PyNumber_Int(v); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
310 if (o == NULL) |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
311 return -1; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
312 res = get_wrapped_ulong(o, p); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
313 Py_DECREF(o); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
314 return res; |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
315 } |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
316 #endif |
|
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
|
317 wrapped = PyNumber_And(v, pylong_ulong_mask); |
|
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
|
318 if (wrapped == NULL) |
|
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
|
319 return -1; |
|
39301
7dc5f3ede4cd
Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn().
Neal Norwitz <nnorwitz@gmail.com>
parents:
38736
diff
changeset
|
320 if (PyErr_WarnEx(PyExc_DeprecationWarning, INT_OVERFLOW, 2) < 0) { |
|
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
|
321 Py_DECREF(wrapped); |
|
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
|
322 return -1; |
|
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
|
323 } |
|
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
|
324 x = (long)PyLong_AsUnsignedLong(wrapped); |
|
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
|
325 Py_DECREF(wrapped); |
|
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
|
326 if (x == -1 && PyErr_Occurred()) |
|
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
|
327 return -1; |
|
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
|
328 } |
|
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
|
329 *p = (unsigned long)x; |
|
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
|
330 return 0; |
|
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
|
331 } |
|
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
|
332 |
|
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
|
333 #define RANGE_ERROR(x, f, flag, mask) \ |
|
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
|
334 do { \ |
|
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
|
335 if (_range_error(f, flag) < 0) \ |
|
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
|
336 return -1; \ |
|
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
|
337 else \ |
|
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
|
338 (x) &= (mask); \ |
|
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
|
339 } while (0) |
|
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
|
340 |
|
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
|
341 #else |
|
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
|
342 |
|
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
|
343 #define get_wrapped_long get_long |
|
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
|
344 #define get_wrapped_ulong get_ulong |
|
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
|
345 #define RANGE_ERROR(x, f, flag, mask) return _range_error(f, flag) |
|
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
|
346 |
|
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
|
347 #endif |
|
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
|
348 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
349 /* Floating point helpers */ |
|
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 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
352 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
|
353 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
|
354 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
355 double x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
356 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
357 x = _PyFloat_Unpack4((unsigned char *)p, le); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
358 if (x == -1.0 && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
359 return NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
360 return PyFloat_FromDouble(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
361 } |
|
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 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
364 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
|
365 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
|
366 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
367 double x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
368 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
369 x = _PyFloat_Unpack8((unsigned char *)p, le); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
370 if (x == -1.0 && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
371 return NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
372 return PyFloat_FromDouble(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
373 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
374 |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
375 /* 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
|
376 static int |
| 38484 | 377 _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
|
378 { |
|
38529
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
379 /* 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
|
380 * 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
|
381 * ((size_t)1 << (f->size * 8)) - 1 |
|
38530
447a94b039de
Repaired error in new comment.
Tim Peters <tim.peters@gmail.com>
parents:
38529
diff
changeset
|
382 * 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
|
383 * 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
|
384 * 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
|
385 * 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
|
386 */ |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
387 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
|
388 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
|
389 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
|
390 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
|
391 "'%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
|
392 f->format, |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
393 ulargest); |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
394 else { |
|
a7f2295a78ac
_range_error(): Speed and simplify (there's no real need for
Tim Peters <tim.peters@gmail.com>
parents:
38528
diff
changeset
|
395 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
|
396 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
|
397 "'%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
|
398 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
|
399 ~ largest, |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
400 largest); |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
401 } |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
402 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
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
|
403 { |
|
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
|
404 PyObject *ptype, *pvalue, *ptraceback; |
|
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
|
405 PyObject *msg; |
|
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
|
406 int rval; |
|
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
|
407 PyErr_Fetch(&ptype, &pvalue, &ptraceback); |
|
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
|
408 assert(pvalue != NULL); |
|
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
|
409 msg = PyObject_Str(pvalue); |
|
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
|
410 Py_XDECREF(ptype); |
|
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
|
411 Py_XDECREF(pvalue); |
|
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
|
412 Py_XDECREF(ptraceback); |
|
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
|
413 if (msg == NULL) |
|
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
|
414 return -1; |
|
39301
7dc5f3ede4cd
Add PyErr_WarnEx() so C code can pass the stacklevel to warnings.warn().
Neal Norwitz <nnorwitz@gmail.com>
parents:
38736
diff
changeset
|
415 rval = PyErr_WarnEx(PyExc_DeprecationWarning, |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
416 PyString_AS_STRING(msg), 2); |
|
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
|
417 Py_DECREF(msg); |
|
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
|
418 if (rval == 0) |
|
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
|
419 return 0; |
|
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
|
420 } |
|
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
|
421 #endif |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
422 return -1; |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
423 } |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
424 |
|
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
425 |
|
38259
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 /* 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
|
428 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
429 [bln][up]_TYPE |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
430 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
431 [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
|
432 [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
|
433 TYPE is one of char, byte, ubyte, etc. |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
434 */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
435 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
436 /* Native mode routines. ****************************************************/ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
437 /* NOTE: |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
438 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
|
439 *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
|
440 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
|
441 compensate for big-endian architectures. |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
442 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
|
443 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
|
444 does this). */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
445 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
446 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
447 nu_char(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
448 { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
449 return PyString_FromStringAndSize(p, 1); |
|
38259
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_byte(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 return PyInt_FromLong((long) *(signed char *)p); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
456 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
457 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
458 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
459 nu_ubyte(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
460 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
461 return PyInt_FromLong((long) *(unsigned char *)p); |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
464 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
465 nu_short(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
466 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
467 short x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
468 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
469 return PyInt_FromLong((long)x); |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
472 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
473 nu_ushort(const char *p, const formatdef *f) |
|
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 unsigned short x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
476 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
477 return PyInt_FromLong((long)x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
478 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
479 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
480 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
481 nu_int(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
482 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
483 int x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
484 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
485 return PyInt_FromLong((long)x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
486 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
487 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
488 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
489 nu_uint(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
490 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
491 unsigned int x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
492 memcpy((char *)&x, p, sizeof x); |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
493 #if (SIZEOF_LONG > SIZEOF_INT) |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
494 return PyInt_FromLong((long)x); |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
495 #else |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
496 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
|
497 return PyInt_FromLong((long)x); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
498 return PyLong_FromUnsignedLong((unsigned long)x); |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
499 #endif |
|
38259
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 PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
503 nu_long(const char *p, 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 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
507 return PyInt_FromLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
508 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
509 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
510 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
511 nu_ulong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
512 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
513 unsigned long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
514 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
|
515 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
|
516 return PyInt_FromLong((long)x); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
517 return PyLong_FromUnsignedLong(x); |
|
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 /* 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
|
521 long long (or, on Windows, __int64). */ |
|
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 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
524 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
525 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
526 nu_longlong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
527 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
528 PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
529 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
|
530 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
|
531 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
|
532 return PyLong_FromLongLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
533 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
534 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
535 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
536 nu_ulonglong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
537 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
538 unsigned PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
539 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
|
540 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
|
541 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
|
542 return PyLong_FromUnsignedLongLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
543 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
544 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
545 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
546 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
547 static PyObject * |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
548 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
|
549 { |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
550 BOOL_TYPE x; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
551 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
|
552 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
|
553 } |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
554 |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
555 |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
556 static PyObject * |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
557 nu_float(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
558 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
559 float x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
560 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
561 return PyFloat_FromDouble((double)x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
562 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
563 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
564 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
565 nu_double(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
566 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
567 double x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
568 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
569 return PyFloat_FromDouble(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
570 } |
|
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 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
573 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
|
574 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
575 void *x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
576 memcpy((char *)&x, p, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
577 return PyLong_FromVoidPtr(x); |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
580 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
581 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
|
582 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
583 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
584 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
585 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
586 if (x < -128 || x > 127){ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
587 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
|
588 "byte format requires -128 <= number <= 127"); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
589 return -1; |
|
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 *p = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
592 return 0; |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
595 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
596 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
|
597 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
598 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
599 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
600 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
601 if (x < 0 || x > 255){ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
602 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
|
603 "ubyte format requires 0 <= number <= 255"); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
604 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
605 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
606 *p = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
607 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
608 } |
|
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 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
611 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
|
612 { |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
613 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
|
614 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
615 "char format require string of length 1"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
616 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
617 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
618 *p = *PyString_AsString(v); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
619 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
620 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
621 |
|
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_short(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 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
626 short y; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
627 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
628 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
629 if (x < SHRT_MIN || x > SHRT_MAX){ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
630 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
631 "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
|
632 " <= number <= " STRINGIFY(SHRT_MAX)); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
633 return -1; |
|
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 y = (short)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
636 memcpy(p, (char *)&y, sizeof y); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
637 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
638 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
639 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
640 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
641 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
|
642 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
643 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
644 unsigned short y; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
645 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
646 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
647 if (x < 0 || x > USHRT_MAX){ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
648 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
|
649 "short format requires 0 <= number <= " STRINGIFY(USHRT_MAX)); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
650 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
651 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
652 y = (unsigned short)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
653 memcpy(p, (char *)&y, sizeof y); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
654 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
655 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
656 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
657 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
658 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
|
659 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
660 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
661 int y; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
662 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
663 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
|
664 #if (SIZEOF_LONG > SIZEOF_INT) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
665 if ((x < ((long)INT_MIN)) || (x > ((long)INT_MAX))) |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
666 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
|
667 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
668 y = (int)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
669 memcpy(p, (char *)&y, sizeof y); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
670 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
671 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
672 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
673 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
674 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
|
675 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
676 unsigned long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
677 unsigned int y; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
678 if (get_ulong(v, &x) < 0) |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
679 return _range_error(f, 1); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
680 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
|
681 #if (SIZEOF_LONG > SIZEOF_INT) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
682 if (x > ((unsigned long)UINT_MAX)) |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
683 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
|
684 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
685 memcpy(p, (char *)&y, sizeof y); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
686 return 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 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
690 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
|
691 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
692 long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
693 if (get_long(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
694 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
695 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
696 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
697 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
698 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
699 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
700 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
|
701 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
702 unsigned long x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
703 if (get_ulong(v, &x) < 0) |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
704 return _range_error(f, 1); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
705 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
706 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
707 } |
|
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 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
710 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
711 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
712 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
|
713 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
714 PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
715 if (get_longlong(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
716 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
717 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
718 return 0; |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
721 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
722 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
|
723 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
724 unsigned PY_LONG_LONG x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
725 if (get_ulonglong(v, &x) < 0) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
726 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
727 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
728 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
729 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
730 #endif |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
731 |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
732 |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
733 static int |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
734 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
|
735 { |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
736 BOOL_TYPE y; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
737 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
|
738 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
|
739 return 0; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
740 } |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
741 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
742 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
743 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
|
744 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
745 float x = (float)PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
746 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
747 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
748 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
749 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
750 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
751 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
752 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
753 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
754 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
755 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
756 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
|
757 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
758 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
759 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
760 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
761 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
762 return -1; |
|
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 memcpy(p, (char *)&x, sizeof(double)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
765 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
766 } |
|
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 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
769 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
|
770 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
771 void *x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
772 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
773 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
774 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
775 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
776 assert(PyLong_Check(v)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
777 x = PyLong_AsVoidPtr(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
778 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
779 if (x == NULL && PyErr_Occurred()) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
780 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
781 memcpy(p, (char *)&x, sizeof x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
782 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
783 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
784 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
785 static formatdef native_table[] = { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
786 {'x', sizeof(char), 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
787 {'b', sizeof(char), 0, nu_byte, np_byte}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
788 {'B', sizeof(char), 0, nu_ubyte, np_ubyte}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
789 {'c', sizeof(char), 0, nu_char, np_char}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
790 {'s', sizeof(char), 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
791 {'p', sizeof(char), 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
792 {'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
|
793 {'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
|
794 {'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
|
795 {'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
|
796 {'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
|
797 {'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
|
798 #ifdef HAVE_LONG_LONG |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
799 {'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
|
800 {'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
|
801 #endif |
|
45672
d41a4206405c
Issue 1872: Changed the struct module typecode from 't' to '?', for
Thomas Heller <theller@ctypes.org>
parents:
45373
diff
changeset
|
802 {'?', 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
|
803 {'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
|
804 {'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
|
805 {'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
|
806 {0} |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
809 /* Big-endian routines. *****************************************************/ |
|
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 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
812 bu_int(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
813 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
814 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
|
815 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
|
816 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
|
817 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
818 x = (x<<8) | *bytes++; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
819 } while (--i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
820 /* Extend the sign bit. */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
821 if (SIZEOF_LONG > f->size) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
822 x |= -(x & (1L << ((8 * f->size) - 1))); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
823 return PyInt_FromLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
824 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
825 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
826 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
827 bu_uint(const char *p, const formatdef *f) |
|
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 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
|
830 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
|
831 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
|
832 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
833 x = (x<<8) | *bytes++; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
834 } 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
|
835 if (x <= LONG_MAX) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
836 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
|
837 return PyLong_FromUnsignedLong(x); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
838 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
839 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
840 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
841 bu_longlong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
842 { |
|
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
|
843 #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
|
844 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
|
845 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
|
846 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
|
847 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
848 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
|
849 } 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
|
850 /* 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
|
851 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
|
852 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
|
853 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
|
854 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
|
855 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
|
856 #else |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
857 return _PyLong_FromByteArray((const unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
858 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
859 0, /* little-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
860 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
|
861 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
862 } |
|
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 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
865 bu_ulonglong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
866 { |
|
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
|
867 #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
|
868 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
|
869 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
|
870 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
|
871 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
872 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
|
873 } 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
|
874 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
|
875 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
|
876 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
|
877 #else |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
878 return _PyLong_FromByteArray((const unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
879 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
880 0, /* little-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
881 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
|
882 #endif |
|
38259
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
885 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
886 bu_float(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
887 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
888 return unpack_float(p, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
889 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
890 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
891 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
892 bu_double(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
893 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
894 return unpack_double(p, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
895 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
896 |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
897 static PyObject * |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
898 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
|
899 { |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
900 char x; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
901 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
|
902 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
|
903 } |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
904 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
905 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
906 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
|
907 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
908 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
|
909 Py_ssize_t i; |
|
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
|
910 if (get_wrapped_long(v, &x) < 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
911 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
912 i = f->size; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
913 if (i != SIZEOF_LONG) { |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
914 if ((i == 2) && (x < -32768 || x > 32767)) |
|
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
|
915 RANGE_ERROR(x, f, 0, 0xffffL); |
|
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
|
916 #if (SIZEOF_LONG != 4) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
917 else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) |
|
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
|
918 RANGE_ERROR(x, f, 0, 0xffffffffL); |
|
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
|
919 #endif |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
920 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
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
|
921 else if ((i == 1) && (x < -128 || x > 127)) |
|
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
|
922 RANGE_ERROR(x, f, 0, 0xffL); |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
923 #endif |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
924 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
925 do { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
926 p[--i] = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
927 x >>= 8; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
928 } while (i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
929 return 0; |
|
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 int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
933 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
|
934 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
935 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
|
936 Py_ssize_t i; |
|
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
|
937 if (get_wrapped_ulong(v, &x) < 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
938 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
939 i = f->size; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
940 if (i != SIZEOF_LONG) { |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
941 unsigned long maxint = 1; |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
942 maxint <<= (unsigned long)(i * 8); |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
943 if (x >= maxint) |
|
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
|
944 RANGE_ERROR(x, f, 1, maxint - 1); |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
945 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
946 do { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
947 p[--i] = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
948 x >>= 8; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
949 } while (i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
950 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
951 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
952 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
953 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
954 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
|
955 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
956 int res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
957 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
958 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
959 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
960 res = _PyLong_AsByteArray((PyLongObject *)v, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
961 (unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
962 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
963 0, /* little_endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
964 1 /* signed */); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
965 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
966 return res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
967 } |
|
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 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
970 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
|
971 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
972 int res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
973 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
974 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
975 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
976 res = _PyLong_AsByteArray((PyLongObject *)v, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
977 (unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
978 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
979 0, /* little_endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
980 0 /* signed */); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
981 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
982 return res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
983 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
984 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
985 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
986 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
|
987 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
988 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
989 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
990 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
991 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
992 return -1; |
|
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 _PyFloat_Pack4(x, (unsigned char *)p, 0); |
|
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 int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
998 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
|
999 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1000 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1001 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1002 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1003 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1004 return -1; |
|
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 return _PyFloat_Pack8(x, (unsigned char *)p, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1007 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1008 |
|
40681
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
1009 static int |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
1010 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
|
1011 { |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
1012 char y; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
1013 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
|
1014 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
|
1015 return 0; |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
1016 } |
|
0bffca64ccc4
Patch #1610575: Add support for _Bool to struct.
Martin v. Löwis <martin@v.loewis.de>
parents:
39380
diff
changeset
|
1017 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1018 static formatdef bigendian_table[] = { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1019 {'x', 1, 0, NULL}, |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
1020 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
1021 /* Native packers do range checking without overflow masking. */ |
|
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
|
1022 {'b', 1, 0, nu_byte, bp_int}, |
|
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
|
1023 {'B', 1, 0, nu_ubyte, bp_uint}, |
|
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
|
1024 #else |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
1025 {'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
|
1026 {'B', 1, 0, nu_ubyte, np_ubyte}, |
|
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
|
1027 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1028 {'c', 1, 0, nu_char, np_char}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1029 {'s', 1, 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1030 {'p', 1, 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1031 {'h', 2, 0, bu_int, bp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1032 {'H', 2, 0, bu_uint, bp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1033 {'i', 4, 0, bu_int, bp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1034 {'I', 4, 0, bu_uint, bp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1035 {'l', 4, 0, bu_int, bp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1036 {'L', 4, 0, bu_uint, bp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1037 {'q', 8, 0, bu_longlong, bp_longlong}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1038 {'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
|
1039 {'?', 1, 0, bu_bool, bp_bool}, |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1040 {'f', 4, 0, bu_float, bp_float}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1041 {'d', 8, 0, bu_double, bp_double}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1042 {0} |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1043 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1044 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1045 /* Little-endian routines. *****************************************************/ |
|
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 PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1048 lu_int(const char *p, 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 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
|
1051 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
|
1052 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
|
1053 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
1054 x = (x<<8) | bytes[--i]; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1055 } while (i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1056 /* Extend the sign bit. */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1057 if (SIZEOF_LONG > f->size) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1058 x |= -(x & (1L << ((8 * f->size) - 1))); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1059 return PyInt_FromLong(x); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1060 } |
|
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 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1063 lu_uint(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1064 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1065 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
|
1066 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
|
1067 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
|
1068 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
1069 x = (x<<8) | bytes[--i]; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1070 } 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
|
1071 if (x <= LONG_MAX) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1072 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
|
1073 return PyLong_FromUnsignedLong((long)x); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1074 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1075 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1076 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1077 lu_longlong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1078 { |
|
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
|
1079 #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
|
1080 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
|
1081 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
|
1082 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
|
1083 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
1084 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
|
1085 } 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
|
1086 /* 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
|
1087 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
|
1088 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
|
1089 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
|
1090 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
|
1091 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
|
1092 #else |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1093 return _PyLong_FromByteArray((const unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1094 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1095 1, /* little-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1096 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
|
1097 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1098 } |
|
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 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1101 lu_ulonglong(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1102 { |
|
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
|
1103 #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
|
1104 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
|
1105 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
|
1106 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
|
1107 do { |
|
38483
fb56a3992604
simplify the struct code a bit (no functional changes)
Bob Ippolito <bob@redivi.com>
parents:
38470
diff
changeset
|
1108 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
|
1109 } 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
|
1110 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
|
1111 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
|
1112 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
|
1113 #else |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1114 return _PyLong_FromByteArray((const unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1115 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1116 1, /* little-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1117 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
|
1118 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1119 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1120 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1121 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1122 lu_float(const char *p, const formatdef *f) |
|
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 return unpack_float(p, 1); |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1127 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1128 lu_double(const char *p, const formatdef *f) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1129 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1130 return unpack_double(p, 1); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1131 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1132 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1133 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1134 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
|
1135 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1136 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
|
1137 Py_ssize_t i; |
|
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
|
1138 if (get_wrapped_long(v, &x) < 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1139 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1140 i = f->size; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1141 if (i != SIZEOF_LONG) { |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1142 if ((i == 2) && (x < -32768 || x > 32767)) |
|
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
|
1143 RANGE_ERROR(x, f, 0, 0xffffL); |
|
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
|
1144 #if (SIZEOF_LONG != 4) |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1145 else if ((i == 4) && (x < -2147483648L || x > 2147483647L)) |
|
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
|
1146 RANGE_ERROR(x, f, 0, 0xffffffffL); |
|
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
|
1147 #endif |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
1148 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
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
|
1149 else if ((i == 1) && (x < -128 || x > 127)) |
|
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
|
1150 RANGE_ERROR(x, f, 0, 0xffL); |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
1151 #endif |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1152 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1153 do { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1154 *p++ = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1155 x >>= 8; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1156 } while (--i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1157 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1158 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1159 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1160 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1161 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
|
1162 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1163 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
|
1164 Py_ssize_t i; |
|
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
|
1165 if (get_wrapped_ulong(v, &x) < 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1166 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1167 i = f->size; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1168 if (i != SIZEOF_LONG) { |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1169 unsigned long maxint = 1; |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1170 maxint <<= (unsigned long)(i * 8); |
|
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1171 if (x >= maxint) |
|
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
|
1172 RANGE_ERROR(x, f, 1, maxint - 1); |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1173 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1174 do { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1175 *p++ = (char)x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1176 x >>= 8; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1177 } while (--i > 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1178 return 0; |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1181 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1182 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
|
1183 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1184 int res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1185 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1186 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1187 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1188 res = _PyLong_AsByteArray((PyLongObject*)v, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1189 (unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1190 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1191 1, /* little_endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1192 1 /* signed */); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1193 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1194 return res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1195 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1196 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1197 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1198 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
|
1199 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1200 int res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1201 v = get_pylong(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1202 if (v == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1203 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1204 res = _PyLong_AsByteArray((PyLongObject*)v, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1205 (unsigned char *)p, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1206 8, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1207 1, /* little_endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1208 0 /* signed */); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1209 Py_DECREF(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1210 return res; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1211 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1212 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1213 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1214 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
|
1215 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1216 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1217 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1218 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1219 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1220 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1221 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1222 return _PyFloat_Pack4(x, (unsigned char *)p, 1); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1223 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1224 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1225 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1226 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
|
1227 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1228 double x = PyFloat_AsDouble(v); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1229 if (x == -1 && PyErr_Occurred()) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1230 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1231 "required argument is not a float"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1232 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1233 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1234 return _PyFloat_Pack8(x, (unsigned char *)p, 1); |
|
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 static formatdef lilendian_table[] = { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1238 {'x', 1, 0, NULL}, |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
1239 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
1240 /* Native packers do range checking without overflow masking. */ |
|
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
|
1241 {'b', 1, 0, nu_byte, lp_int}, |
|
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
|
1242 {'B', 1, 0, nu_ubyte, lp_uint}, |
|
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
|
1243 #else |
|
38363
5213b6065197
fix #1229380 No struct.pack exception for some out of range integers
Bob Ippolito <bob@redivi.com>
parents:
38353
diff
changeset
|
1244 {'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
|
1245 {'B', 1, 0, nu_ubyte, np_ubyte}, |
|
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
|
1246 #endif |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1247 {'c', 1, 0, nu_char, np_char}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1248 {'s', 1, 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1249 {'p', 1, 0, NULL}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1250 {'h', 2, 0, lu_int, lp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1251 {'H', 2, 0, lu_uint, lp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1252 {'i', 4, 0, lu_int, lp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1253 {'I', 4, 0, lu_uint, lp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1254 {'l', 4, 0, lu_int, lp_int}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1255 {'L', 4, 0, lu_uint, lp_uint}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1256 {'q', 8, 0, lu_longlong, lp_longlong}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1257 {'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
|
1258 {'?', 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
|
1259 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
|
1260 {'f', 4, 0, lu_float, lp_float}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1261 {'d', 8, 0, lu_double, lp_double}, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1262 {0} |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1263 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1264 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1265 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1266 static const formatdef * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1267 whichtable(char **pfmt) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1268 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1269 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
|
1270 switch (*fmt) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1271 case '<': |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1272 return lilendian_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1273 case '>': |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1274 case '!': /* Network byte order is big-endian */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1275 return bigendian_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1276 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
|
1277 int n = 1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1278 char *p = (char *) &n; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1279 if (*p == 1) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1280 return lilendian_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1281 else |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1282 return bigendian_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1283 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1284 default: |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1285 --*pfmt; /* Back out of pointer increment */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1286 /* Fall through */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1287 case '@': |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1288 return native_table; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1289 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1290 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1291 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1292 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1293 /* Get the table entry for a format code */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1294 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1295 static const formatdef * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1296 getentry(int c, const formatdef *f) |
|
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 for (; f->format != '\0'; f++) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1299 if (f->format == c) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1300 return f; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1301 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1302 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1303 PyErr_SetString(StructError, "bad char in struct format"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1304 return NULL; |
|
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 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1307 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1308 /* Align a size according to a format code */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1309 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1310 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
|
1311 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
|
1312 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1313 if (e->format == c) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1314 if (e->alignment) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1315 size = ((size + e->alignment - 1) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1316 / e->alignment) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1317 * e->alignment; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1318 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1319 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1320 return size; |
|
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 |
|
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 /* calculate the size of a format string */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1325 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1326 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1327 prepare_s(PyStructObject *self) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1328 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1329 const formatdef *f; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1330 const formatdef *e; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1331 formatcode *codes; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1332 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1333 const char *s; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1334 const char *fmt; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1335 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
|
1336 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
|
1337 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1338 fmt = PyString_AS_STRING(self->s_format); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1339 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1340 f = whichtable((char **)&fmt); |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1341 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1342 s = fmt; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1343 size = 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1344 len = 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1345 while ((c = *s++) != '\0') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1346 if (isspace(Py_CHARMASK(c))) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1347 continue; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1348 if ('0' <= c && c <= '9') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1349 num = c - '0'; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1350 while ('0' <= (c = *s++) && c <= '9') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1351 x = num*10 + (c - '0'); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1352 if (x/10 != num) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1353 PyErr_SetString( |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1354 StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1355 "overflow in item count"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1356 return -1; |
|
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 num = x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1359 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1360 if (c == '\0') |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1361 break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1362 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1363 else |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1364 num = 1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1365 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1366 e = getentry(c, f); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1367 if (e == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1368 return -1; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1369 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1370 switch (c) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1371 case 's': /* fall through */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1372 case 'p': len++; break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1373 case 'x': break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1374 default: len += num; break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1375 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1376 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1377 itemsize = e->size; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1378 size = align(size, c, e); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1379 x = num * itemsize; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1380 size += x; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1381 if (x/itemsize != num || size < 0) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1382 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1383 "total struct size too long"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1384 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1385 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1386 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1387 |
|
47587
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1388 /* check for overflow */ |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1389 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
|
1390 PyErr_NoMemory(); |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1391 return -1; |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1392 } |
|
1c964ed35ef9
Merge in release25-maint r60793:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47527
diff
changeset
|
1393 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1394 self->s_size = size; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1395 self->s_len = len; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1396 codes = PyMem_MALLOC((len + 1) * sizeof(formatcode)); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1397 if (codes == NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1398 PyErr_NoMemory(); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1399 return -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1400 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1401 self->s_codes = codes; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1402 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1403 s = fmt; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1404 size = 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1405 while ((c = *s++) != '\0') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1406 if (isspace(Py_CHARMASK(c))) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1407 continue; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1408 if ('0' <= c && c <= '9') { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1409 num = c - '0'; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1410 while ('0' <= (c = *s++) && c <= '9') |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1411 num = num*10 + (c - '0'); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1412 if (c == '\0') |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1413 break; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1414 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1415 else |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1416 num = 1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1417 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1418 e = getentry(c, f); |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1419 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1420 size = align(size, c, e); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1421 if (c == 's' || c == 'p') { |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1422 codes->offset = size; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1423 codes->size = num; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1424 codes->fmtdef = e; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1425 codes++; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1426 size += num; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1427 } else if (c == 'x') { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1428 size += num; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1429 } else { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1430 while (--num >= 0) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1431 codes->offset = size; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1432 codes->size = e->size; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1433 codes->fmtdef = e; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1434 codes++; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1435 size += e->size; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1436 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1437 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1438 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1439 codes->fmtdef = NULL; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1440 codes->offset = size; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1441 codes->size = 0; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1442 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1443 return 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1444 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1445 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1446 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1447 s_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1448 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1449 PyObject *self; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1450 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1451 assert(type != NULL && type->tp_alloc != NULL); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1452 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1453 self = type->tp_alloc(type, 0); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1454 if (self != NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1455 PyStructObject *s = (PyStructObject*)self; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1456 Py_INCREF(Py_None); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1457 s->s_format = Py_None; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1458 s->s_codes = NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1459 s->s_size = -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1460 s->s_len = -1; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1461 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1462 return self; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1463 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1464 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1465 static int |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1466 s_init(PyObject *self, PyObject *args, PyObject *kwds) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1467 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1468 PyStructObject *soself = (PyStructObject *)self; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1469 PyObject *o_format = NULL; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1470 int ret = 0; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1471 static char *kwlist[] = {"format", 0}; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1472 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1473 assert(PyStruct_Check(self)); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1474 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1475 if (!PyArg_ParseTupleAndKeywords(args, kwds, "S:Struct", kwlist, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1476 &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
|
1477 return -1; |
|
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 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
|
1480 Py_CLEAR(soself->s_format); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1481 soself->s_format = o_format; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1482 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1483 ret = prepare_s(soself); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1484 return ret; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1485 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1486 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1487 static void |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1488 s_dealloc(PyStructObject *s) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1489 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1490 if (s->weakreflist != NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1491 PyObject_ClearWeakRefs((PyObject *)s); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1492 if (s->s_codes != NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1493 PyMem_FREE(s->s_codes); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1494 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1495 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
|
1496 Py_TYPE(s)->tp_free((PyObject *)s); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1497 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1498 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1499 static PyObject * |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1500 s_unpack_internal(PyStructObject *soself, char *startfrom) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1501 formatcode *code; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1502 Py_ssize_t i = 0; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1503 PyObject *result = PyTuple_New(soself->s_len); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1504 if (result == NULL) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1505 return NULL; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1506 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1507 for (code = soself->s_codes; code->fmtdef != NULL; code++) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1508 PyObject *v; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1509 const formatdef *e = code->fmtdef; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1510 const char *res = startfrom + code->offset; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1511 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
|
1512 v = PyString_FromStringAndSize(res, code->size); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1513 } else if (e->format == 'p') { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1514 Py_ssize_t n = *(unsigned char*)res; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1515 if (n >= code->size) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1516 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
|
1517 v = PyString_FromStringAndSize(res + 1, n); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1518 } else { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1519 v = e->unpack(res, e); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1520 } |
| 38736 | 1521 if (v == NULL) |
| 1522 goto fail; | |
| 1523 PyTuple_SET_ITEM(result, i++, v); | |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1524 } |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1525 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1526 return result; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1527 fail: |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1528 Py_DECREF(result); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1529 return NULL; |
|
38445
8daeb9af8422
fix struct regression on 64-bit platforms
Bob Ippolito <bob@redivi.com>
parents:
38430
diff
changeset
|
1530 } |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1531 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1532 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1533 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
|
1534 "S.unpack(str) -> (v1, v2, ...)\n\ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1535 \n\ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1536 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
|
1537 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
|
1538 strings."); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1539 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1540 static PyObject * |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1541 s_unpack(PyObject *self, PyObject *inputstr) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1542 { |
|
41391
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1543 char *start; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1544 Py_ssize_t len; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1545 PyObject *args=NULL, *result; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1546 PyStructObject *soself = (PyStructObject *)self; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1547 assert(PyStruct_Check(self)); |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1548 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
|
1549 if (inputstr == NULL) |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1550 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
|
1551 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
|
1552 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
|
1553 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
|
1554 } |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1555 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
|
1556 if (args == NULL) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1557 return NULL; |
|
41391
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1558 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
|
1559 goto fail; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1560 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
|
1561 goto fail; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1562 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
|
1563 Py_DECREF(args); |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1564 return result; |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1565 |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1566 fail: |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1567 Py_XDECREF(args); |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1568 PyErr_Format(StructError, |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1569 "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
|
1570 soself->s_size); |
|
80ff75fa2754
Bug #1563759: struct.unpack doens't support buffer protocol objects
Raymond Hettinger <python@rcn.com>
parents:
40681
diff
changeset
|
1571 return NULL; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1572 } |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1573 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1574 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
|
1575 "S.unpack_from(buffer[, offset]) -> (v1, v2, ...)\n\ |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1576 \n\ |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1577 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
|
1578 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
|
1579 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
|
1580 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
|
1581 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1582 static PyObject * |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1583 s_unpack_from(PyObject *self, PyObject *args, PyObject *kwds) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1584 { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1585 static char *kwlist[] = {"buffer", "offset", 0}; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1586 #if (PY_VERSION_HEX < 0x02050000) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1587 static char *fmt = "z#|i:unpack_from"; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1588 #else |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1589 static char *fmt = "z#|n:unpack_from"; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1590 #endif |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1591 Py_ssize_t buffer_len = 0, offset = 0; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1592 char *buffer = NULL; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1593 PyStructObject *soself = (PyStructObject *)self; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1594 assert(PyStruct_Check(self)); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1595 assert(soself->s_codes != NULL); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1596 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1597 if (!PyArg_ParseTupleAndKeywords(args, kwds, fmt, kwlist, |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1598 &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
|
1599 return NULL; |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1600 |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1601 if (buffer == NULL) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1602 PyErr_Format(StructError, |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1603 "unpack_from requires a buffer argument"); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1604 return NULL; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1605 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1606 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1607 if (offset < 0) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1608 offset += buffer_len; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1609 |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1610 if (offset < 0 || (buffer_len - offset) < soself->s_size) { |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1611 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
|
1612 "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
|
1613 soself->s_size); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1614 return NULL; |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1615 } |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1616 return s_unpack_internal(soself, buffer + offset); |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1617 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1618 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1619 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1620 /* |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1621 * Guts of the pack function. |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1622 * |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1623 * 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
|
1624 * 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
|
1625 * 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
|
1626 * 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
|
1627 * 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
|
1628 * |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1629 */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1630 static int |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1631 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
|
1632 { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1633 formatcode *code; |
| 38736 | 1634 /* XXX(nnorwitz): why does i need to be a local? can we use |
| 1635 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
|
1636 Py_ssize_t i; |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1637 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1638 memset(buf, '\0', soself->s_size); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1639 i = offset; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1640 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
|
1641 Py_ssize_t n; |
| 38736 | 1642 PyObject *v = PyTuple_GET_ITEM(args, i++); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1643 const formatdef *e = code->fmtdef; |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1644 char *res = buf + code->offset; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1645 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
|
1646 if (!PyString_Check(v)) { |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1647 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1648 "argument for 's' must be a string"); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1649 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1650 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1651 n = PyString_GET_SIZE(v); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1652 if (n > code->size) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1653 n = code->size; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1654 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
|
1655 memcpy(res, PyString_AS_STRING(v), n); |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1656 } 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
|
1657 if (!PyString_Check(v)) { |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1658 PyErr_SetString(StructError, |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1659 "argument for 'p' must be a string"); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1660 return -1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1661 } |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
1662 n = PyString_GET_SIZE(v); |
|
38277
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1663 if (n > (code->size - 1)) |
|
b825e6227366
refactor unpack, add unpack_from
Bob Ippolito <bob@redivi.com>
parents:
38267
diff
changeset
|
1664 n = code->size - 1; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1665 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
|
1666 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
|
1667 if (n > 255) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1668 n = 255; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1669 *res = Py_SAFE_DOWNCAST(n, Py_ssize_t, unsigned char); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1670 } else { |
|
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
|
1671 if (e->pack(res, v, e) < 0) { |
|
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
|
1672 if (PyLong_Check(v) && PyErr_ExceptionMatches(PyExc_OverflowError)) |
|
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
|
1673 PyErr_SetString(StructError, |
|
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
|
1674 "long too large to convert to int"); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1675 return -1; |
|
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
|
1676 } |
|
38259
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 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1679 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1680 /* Success */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1681 return 0; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1682 } |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1683 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1684 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1685 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
|
1686 "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
|
1687 \n\ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1688 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
|
1689 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
|
1690 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1691 static PyObject * |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1692 s_pack(PyObject *self, PyObject *args) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1693 { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1694 PyStructObject *soself; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1695 PyObject *result; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1696 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1697 /* Validate arguments. */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1698 soself = (PyStructObject *)self; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1699 assert(PyStruct_Check(self)); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1700 assert(soself->s_codes != NULL); |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1701 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
|
1702 { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1703 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
|
1704 "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
|
1705 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1706 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1707 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1708 /* 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
|
1709 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
|
1710 if (result == NULL) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1711 return NULL; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1712 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1713 /* 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
|
1714 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
|
1715 Py_DECREF(result); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1716 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1717 } |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1718 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1719 return result; |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1720 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1721 |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1722 PyDoc_STRVAR(s_pack_into__doc__, |
|
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1723 "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
|
1724 \n\ |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1725 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
|
1726 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
|
1727 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
|
1728 more on format strings."); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1729 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1730 static PyObject * |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1731 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
|
1732 { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1733 PyStructObject *soself; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1734 char *buffer; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1735 Py_ssize_t buffer_len, offset; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1736 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1737 /* 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
|
1738 soself = (PyStructObject *)self; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1739 assert(PyStruct_Check(self)); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1740 assert(soself->s_codes != NULL); |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1741 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
|
1742 { |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1743 PyErr_Format(StructError, |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1744 "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
|
1745 (soself->s_len + 2)); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1746 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1747 } |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1748 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1749 /* Extract a writable memory buffer from the first argument */ |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1750 if ( PyObject_AsWriteBuffer(PyTuple_GET_ITEM(args, 0), |
|
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1751 (void**)&buffer, &buffer_len) == -1 ) { |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1752 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
|
1753 } |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1754 assert( buffer_len >= 0 ); |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1755 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1756 /* 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
|
1757 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
|
1758 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
|
1759 return NULL; |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1760 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1761 /* Support negative offsets. */ |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1762 if (offset < 0) |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1763 offset += buffer_len; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1764 |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1765 /* Check boundaries */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1766 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
|
1767 PyErr_Format(StructError, |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1768 "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
|
1769 soself->s_size); |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1770 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1771 } |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
1772 |
|
38353
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1773 /* Call the guts */ |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1774 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
|
1775 return NULL; |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1776 } |
|
36cd4a4ab8f7
Support for buffer protocol for socket and struct.
Martin Blais <blais@furius.ca>
parents:
38322
diff
changeset
|
1777 |
|
38470
e0cea605417e
Fix ref-antileak in _struct.c which eventually lead to deallocating None.
Georg Brandl <georg@python.org>
parents:
38445
diff
changeset
|
1778 Py_RETURN_NONE; |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1779 } |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1780 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1781 static PyObject * |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1782 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
|
1783 { |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1784 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
|
1785 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
|
1786 } |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1787 |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1788 static PyObject * |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1789 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
|
1790 { |
|
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1791 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
|
1792 } |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1793 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1794 /* List of functions */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1795 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1796 static struct PyMethodDef s_methods[] = { |
|
38580
a114003aa3d3
Fixes in struct and socket from merge reviews.
Martin Blais <blais@furius.ca>
parents:
38530
diff
changeset
|
1797 {"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
|
1798 {"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
|
1799 {"unpack", s_unpack, METH_O, s_unpack__doc__}, |
|
41816
ec83d21065aa
Stop using METH_OLDARGS implicitly
Neal Norwitz <nnorwitz@gmail.com>
parents:
41526
diff
changeset
|
1800 {"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
|
1801 s_unpack_from__doc__}, |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1802 {NULL, NULL} /* sentinel */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1803 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1804 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1805 PyDoc_STRVAR(s__doc__, "Compiled struct object"); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1806 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1807 #define OFF(x) offsetof(PyStructObject, x) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1808 |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1809 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
|
1810 {"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
|
1811 {"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
|
1812 {NULL} /* sentinel */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1813 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1814 |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1815 static |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1816 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
|
1817 PyVarObject_HEAD_INIT(NULL, 0) |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1818 "Struct", |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1819 sizeof(PyStructObject), |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1820 0, |
|
38411
631b6648fdf4
enable all of the struct tests, use ssize_t, fix some whitespace
Bob Ippolito <bob@redivi.com>
parents:
38378
diff
changeset
|
1821 (destructor)s_dealloc, /* tp_dealloc */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1822 0, /* tp_print */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1823 0, /* tp_getattr */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1824 0, /* tp_setattr */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1825 0, /* tp_compare */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1826 0, /* tp_repr */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1827 0, /* tp_as_number */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1828 0, /* tp_as_sequence */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1829 0, /* tp_as_mapping */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1830 0, /* tp_hash */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1831 0, /* tp_call */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1832 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
|
1833 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
|
1834 PyObject_GenericSetAttr, /* tp_setattro */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1835 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
|
1836 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
|
1837 s__doc__, /* tp_doc */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1838 0, /* tp_traverse */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1839 0, /* tp_clear */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1840 0, /* tp_richcompare */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1841 offsetof(PyStructObject, weakreflist), /* tp_weaklistoffset */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1842 0, /* tp_iter */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1843 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
|
1844 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
|
1845 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
|
1846 s_getsetlist, /* tp_getset */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1847 0, /* tp_base */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1848 0, /* tp_dict */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1849 0, /* tp_descr_get */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1850 0, /* tp_descr_set */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1851 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
|
1852 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
|
1853 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
|
1854 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
|
1855 PyObject_Del, /* tp_free */ |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1856 }; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
1857 |
|
44446
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 /* ---- Standalone functions ---- */ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1860 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1861 #define MAXCACHE 100 |
|
44459
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1862 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
|
1863 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1864 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1865 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
|
1866 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1867 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
|
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 (cache == NULL) { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1870 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
|
1871 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
|
1872 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1873 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1874 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1875 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
|
1876 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
|
1877 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
|
1878 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
|
1879 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1880 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1881 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
|
1882 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
|
1883 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
|
1884 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
|
1885 /* 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
|
1886 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
|
1887 PyErr_Clear(); |
|
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 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
|
1890 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1891 |
|
44459
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1892 PyDoc_STRVAR(clearcache_doc, |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1893 "Clear the internal cache."); |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1894 |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1895 static PyObject * |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1896 clearcache(PyObject *self) |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1897 { |
|
44733
85cf220f16d8
clearcache() needs to remove the dict as well as clear it.
Raymond Hettinger <python@rcn.com>
parents:
44459
diff
changeset
|
1898 Py_CLEAR(cache); |
|
44459
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1899 Py_RETURN_NONE; |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1900 } |
|
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
1901 |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1902 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
|
1903 "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
|
1904 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1905 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1906 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
|
1907 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1908 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
|
1909 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
|
1910 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
|
1911 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1912 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
|
1913 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
|
1914 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
|
1915 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1916 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1917 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
|
1918 "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
|
1919 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1920 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1921 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
|
1922 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1923 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
|
1924 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
|
1925 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1926 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
|
1927 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
|
1928 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1929 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1930 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
|
1931 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
|
1932 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
|
1933 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1934 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1935 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
|
1936 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
|
1937 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
|
1938 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1939 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1940 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
|
1941 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
|
1942 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
|
1943 return result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1944 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1945 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1946 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
|
1947 "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
|
1948 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
|
1949 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1950 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1951 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
|
1952 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1953 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
|
1954 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
|
1955 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1956 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
|
1957 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
|
1958 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1959 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1960 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
|
1961 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
|
1962 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
|
1963 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1964 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1965 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
|
1966 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
|
1967 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
|
1968 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1969 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1970 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
|
1971 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
|
1972 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
|
1973 return result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1974 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1975 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1976 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
|
1977 "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
|
1978 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
|
1979 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1980 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1981 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
|
1982 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1983 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
|
1984 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1985 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
|
1986 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1987 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1988 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
|
1989 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
|
1990 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1991 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
|
1992 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
|
1993 return result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1994 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1995 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
1996 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
|
1997 "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
|
1998 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
|
1999 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2000 static PyObject * |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2001 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
|
2002 { |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2003 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
|
2004 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
|
2005 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2006 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
|
2007 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
|
2008 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2009 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2010 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
|
2011 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
|
2012 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
|
2013 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2014 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2015 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
|
2016 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
|
2017 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
|
2018 return NULL; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2019 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2020 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
|
2021 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
|
2022 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
|
2023 return result; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2024 } |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2025 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2026 static struct PyMethodDef module_functions[] = { |
|
44459
7643a43f25a1
Added _struct._clearcache() for regression tests
Christian Heimes <christian@cheimes.de>
parents:
44446
diff
changeset
|
2027 {"_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
|
2028 {"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
|
2029 {"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
|
2030 {"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
|
2031 {"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
|
2032 {"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
|
2033 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
|
2034 {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
|
2035 }; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2036 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2037 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2038 /* Module initialization */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2039 |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2040 PyDoc_STRVAR(module_doc, |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2041 "Functions to convert between Python values and C structs.\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2042 Python strings are used to hold the data representing the C struct\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2043 and also as format strings to describe the layout of data in the C struct.\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2044 \n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2045 The optional first format char indicates byte order, size and alignment:\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2046 @: native order, size & alignment (default)\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2047 =: native order, std. size & alignment\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2048 <: little-endian, std. size & alignment\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2049 >: big-endian, std. size & alignment\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2050 !: same as >\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2051 \n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2052 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
|
2053 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
|
2054 x: pad byte (no data); c:char; b:signed byte; B:unsigned byte;\n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2055 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
|
2056 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
|
2057 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
|
2058 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
|
2059 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
|
2060 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
|
2061 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
|
2062 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
|
2063 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
|
2064 \n\ |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2065 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
|
2066 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2067 PyMODINIT_FUNC |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2068 init_struct(void) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2069 { |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2070 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
|
2071 |
|
47527
52294af4b663
This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents:
47294
diff
changeset
|
2072 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
|
2073 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
|
2074 return; |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2075 |
|
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2076 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
|
2077 if (m == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2078 return; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2079 |
|
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
|
2080 Py_TYPE(&PyStructType) = &PyType_Type; |
| 38312 | 2081 if (PyType_Ready(&PyStructType) < 0) |
| 2082 return; | |
| 2083 | |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
2084 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
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
|
2085 if (pyint_zero == NULL) { |
|
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
|
2086 pyint_zero = PyInt_FromLong(0); |
|
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
|
2087 if (pyint_zero == NULL) |
|
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
|
2088 return; |
|
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
|
2089 } |
|
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
|
2090 if (pylong_ulong_mask == NULL) { |
|
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
|
2091 #if (SIZEOF_LONG == 4) |
|
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
|
2092 pylong_ulong_mask = PyLong_FromString("FFFFFFFF", NULL, 16); |
|
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
|
2093 #else |
|
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
|
2094 pylong_ulong_mask = PyLong_FromString("FFFFFFFFFFFFFFFF", NULL, 16); |
|
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
|
2095 #endif |
|
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
|
2096 if (pylong_ulong_mask == NULL) |
|
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
|
2097 return; |
|
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
|
2098 } |
|
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
|
2099 |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
2100 #else |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
2101 /* This speed trick can't be used until overflow masking goes away, because |
|
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
2102 native endian always raises exceptions instead of overflow masking. */ |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
2103 |
|
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
|
2104 /* 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
|
2105 { |
|
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
|
2106 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
|
2107 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
|
2108 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
|
2109 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
|
2110 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
|
2111 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
|
2112 other = bigendian_table; |
|
38322
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
2113 /* 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
|
2114 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
|
2115 native implementations whenever possible |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
2116 (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
|
2117 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
|
2118 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
|
2119 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
|
2120 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
|
2121 /* 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
|
2122 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
|
2123 if (ptr == other) |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
2124 other++; |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
2125 /* 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
|
2126 size matches */ |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
2127 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
|
2128 break; |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
2129 /* 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
|
2130 "unknown" float format */ |
|
c77da2736fb2
fix test_float regression and 64-bit size mismatch issue
Bob Ippolito <bob@redivi.com>
parents:
38317
diff
changeset
|
2131 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
|
2132 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
|
2133 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
|
2134 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
|
2135 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
|
2136 } |
|
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
|
2137 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
|
2138 } |
|
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
|
2139 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
|
2140 } |
|
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
|
2141 } |
|
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
|
2142 #endif |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
2143 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2144 /* Add some symbolic constants to the module */ |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2145 if (StructError == NULL) { |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2146 StructError = PyErr_NewException("struct.error", NULL, NULL); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2147 if (StructError == NULL) |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2148 return; |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2149 } |
|
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
|
2150 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2151 Py_INCREF(StructError); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2152 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
|
2153 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2154 Py_INCREF((PyObject*)&PyStructType); |
|
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2155 PyModule_AddObject(m, "Struct", (PyObject*)&PyStructType); |
|
38528
e0997ffa3572
Trimmed trailing whitespace.
Tim Peters <tim.peters@gmail.com>
parents:
38520
diff
changeset
|
2156 |
|
44446
8d69edd3d290
Finish-up the struct module optimizations started at the Iceland NFS sprint.
Raymond Hettinger <python@rcn.com>
parents:
44350
diff
changeset
|
2157 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
|
2158 |
|
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
|
2159 PyModule_AddIntConstant(m, "_PY_STRUCT_RANGE_CHECKING", 1); |
|
38515
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
2160 #ifdef PY_STRUCT_OVERFLOW_MASKING |
|
bb0d2b955439
Change wrapping terminology to overflow masking
Bob Ippolito <bob@redivi.com>
parents:
38493
diff
changeset
|
2161 PyModule_AddIntConstant(m, "_PY_STRUCT_OVERFLOW_MASKING", 1); |
|
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
|
2162 #endif |
|
39380
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
2163 #ifdef PY_STRUCT_FLOAT_COERCE |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
2164 PyModule_AddIntConstant(m, "_PY_STRUCT_FLOAT_COERCE", 1); |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
2165 #endif |
|
9cb4565e60ca
Fix #1530559, struct.pack raises TypeError where it used to convert.
Bob Ippolito <bob@redivi.com>
parents:
39301
diff
changeset
|
2166 |
|
38259
895da42480f1
Patch #1493701: performance enhancements for struct module.
Bob Ippolito <bob@redivi.com>
parents:
diff
changeset
|
2167 } |
