annotate Modules/_struct.c @ 62449:53a152b400a7 legacy-trunk

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