-
Notifications
You must be signed in to change notification settings - Fork 35
Expand file tree
/
Copy pathoptimized.c
More file actions
356 lines (322 loc) · 7.52 KB
/
Copy pathoptimized.c
File metadata and controls
356 lines (322 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/*
* copyright 2009, James William Pye
* http://python.projects.postgresql.org
*
*//*
* Optimizations for protocol modules.
*/
#include <stdint.h>
#ifdef WIN32
#include <winsock.h>
#else
#include <sys/types.h>
#include <netinet/in.h>
#endif
#include <Python.h>
#include <structmember.h>
static PyObject *
parse_tuple_message(PyObject *self, PyObject *args)
{
PyObject *rob;
PyObject *ob;
PyObject *typ;
const char *data;
Py_ssize_t dlen = 0;
uint16_t cnatt = 0, natts = 0;
uint32_t attsize = 0;
uint32_t position = 0;
if (!PyArg_ParseTuple(args, "Oy#", &typ, &data, &dlen))
return(NULL);
/*
* Validate that the given "typ" is in fact a PyTuple_Type subtype.
*/
if (typ != Py_None)
{
if (!PyObject_IsSubclass(typ, (PyObject *) &PyTuple_Type))
{
const char *typname = "<not-a-type>";
if (PyObject_IsInstance(
(PyObject *) typ->ob_type,
(PyObject *) &PyType_Type
))
{
typname = ((PyTypeObject *) typ)->tp_name;
}
PyErr_Format(
PyExc_TypeError,
"cannot instantiate wire tuple into a non-tuple subtype: %s",
typname
);
return(NULL);
}
}
else
{
typ = (PyObject *) &PyTuple_Type;
}
if (dlen < 2)
{
PyErr_Format(PyExc_ValueError,
"invalid tuple message: %d bytes is too small", dlen);
return(NULL);
}
natts = ntohs(*((uint16_t *) (data)));
/*
* FEARME: A bit much for saving a reallocation/copy?
*
* This is expected to be used as a classmethod on a tuple subtype that
* has *no* additional attributes.
*
* If the subtype has a custom __new__ routine, this could
* be problematic, but it *should* only lead to AttributeErrors.
*/
rob = ((PyTypeObject *) typ)->tp_alloc((PyTypeObject *) typ, natts);
if (rob == NULL)
{
return(NULL);
}
position += 2;
while (cnatt < natts)
{
/*
* Need enough data for the attribute size.
*/
if (position + 4 > dlen)
{
PyErr_Format(PyExc_ValueError,
"not enough data available for attribute %d's size header: "
"needed %d bytes, but only %lu remain at position %lu",
cnatt, 4, dlen - position, position
);
goto cleanup;
}
attsize = ntohl(*((uint32_t *) (data + position)));
position += 4;
/*
* NULL.
*/
if (attsize == 0xFFFFFFFFL)
{
Py_INCREF(Py_None);
PyTuple_SET_ITEM(rob, cnatt, Py_None);
}
else
{
if ((position + attsize) < position)
{
/*
* Likely a "limitation" over the pure-Python version, *but*
* the message content size is limited to 0xFFFFFFFF-4 anyways,
* so it is unexpected for an attsize to cause wrap-around.
*/
PyErr_Format(PyExc_ValueError,
"tuple data caused position (uint32_t) "
"to wrap on attribute %d, position %lu + size %lu",
cnatt, position, attsize
);
goto cleanup;
}
if (position + attsize > dlen)
{
PyErr_Format(PyExc_ValueError,
"not enough data for attribute %d, size %lu, "
"as only %lu bytes remain in message",
cnatt, attsize, dlen - position
);
goto cleanup;
}
ob = PyBytes_FromStringAndSize(data + position, attsize);
if (ob == NULL)
{
/*
* Probably an OOM error.
*/
goto cleanup;
}
PyTuple_SET_ITEM(rob, cnatt, ob);
position += attsize;
}
cnatt++;
}
if (position != dlen)
{
PyErr_Format(PyExc_ValueError,
"invalid tuple message, %lu remaining "
"bytes after processing %d attributes",
dlen - position, cnatt
);
goto cleanup;
}
return(rob);
cleanup:
Py_DECREF(rob);
return(NULL);
}
/*
* process the tuple with the associated callables while
* calling the third object in cases of failure to generalize the exception.
*/
static PyObject *
process_tuple(PyObject *self, PyObject *args)
{
PyObject *tup, *procs, *fail, *rob;
Py_ssize_t len, i;
if (!PyArg_ParseTuple(args, "OOO", &procs, &tup, &fail))
return(NULL);
if (!PyObject_IsInstance(procs, (PyObject *) &PyTuple_Type))
{
PyErr_SetString(
PyExc_TypeError,
"process_tuple requires a tuple as its first argument"
);
return(NULL);
}
if (!PyObject_IsInstance(tup, (PyObject *) &PyTuple_Type))
{
PyErr_SetString(
PyExc_TypeError,
"process_tuple requires a tuple as its second argument"
);
return(NULL);
}
len = PyTuple_GET_SIZE(tup);
if (len != PyTuple_GET_SIZE(procs))
{
PyErr_Format(
PyExc_ValueError,
"inconsistent items, %d processors and %d objects",
len,
PyTuple_GET_SIZE(procs)
);
return(NULL);
}
rob = PyTuple_New(len);
for (i = 0; i < len; ++i)
{
PyObject *p, *o, *ot, *r;
/*
* If it's Py_None, that means it's NULL. No processing necessary.
*/
o = PyTuple_GET_ITEM(tup, i);
if (o == Py_None)
{
Py_INCREF(Py_None);
PyTuple_SET_ITEM(rob, i, Py_None);
continue;
}
p = PyTuple_GET_ITEM(procs, i);
/*
* Temp tuple for applying *args to p.
*/
ot = PyTuple_New(1);
PyTuple_SET_ITEM(ot, 0, o);
Py_INCREF(o);
r = PyObject_CallObject(p, ot);
Py_DECREF(ot);
if (r == NULL)
{
/*
* Exception from p(*ot)
*/
Py_DECREF(rob);
rob = NULL;
/*
* Don't trap BaseException's.
*/
if (PyErr_ExceptionMatches(PyExc_Exception))
{
PyObject *failargs, *failedat;
PyObject *exc, *val, *tb;
PyObject *oldexc, *oldval, *oldtb;
/* Store exception to set context after handler. */
PyErr_Fetch(&oldexc, &oldval, &oldtb);
PyErr_NormalizeException(&oldexc, &oldval, &oldtb);
failedat = PyLong_FromSsize_t(i);
if (failedat != NULL)
{
failargs = PyTuple_New(3);
if (failargs != NULL)
{
/* args for the exception "handler" */
PyTuple_SET_ITEM(failargs, 0, procs);
Py_INCREF(procs);
PyTuple_SET_ITEM(failargs, 1, tup);
Py_INCREF(tup);
PyTuple_SET_ITEM(failargs, 2, failedat);
r = PyObject_CallObject(fail, failargs);
Py_DECREF(failargs);
if (r != NULL)
{
PyErr_SetString(PyExc_RuntimeError,
"process_tuple exception handler failed to raise"
);
Py_DECREF(r);
}
}
else
{
Py_DECREF(failedat);
}
}
PyErr_Fetch(&exc, &val, &tb);
PyErr_NormalizeException(&exc, &val, &tb);
/*
* Reference BaseException here as the condition is merely
* *validating* that SetContext can be used.
*/
if (val != NULL && PyObject_IsInstance(val, PyExc_BaseException))
{
/* Steals oldval reference */
PyException_SetContext(val, oldval);
Py_XDECREF(oldexc);
Py_XDECREF(oldtb);
PyErr_Restore(exc, val, tb);
}
else
{
/*
* Fetch & NormalizeException failed somehow.
* Use the old exception...
*/
PyErr_Restore(oldexc, oldval, oldtb);
Py_XDECREF(exc);
Py_XDECREF(val);
Py_XDECREF(tb);
}
}
/*
* Break out of loop to return(NULL);
*/
break;
}
PyTuple_SET_ITEM(rob, i, r);
}
return(rob);
}
static PyMethodDef optimized_methods[] = {
{"parse_tuple_message", (PyCFunction) parse_tuple_message, METH_VARARGS,
PyDoc_STR("parse the given tuple data into a tuple of raw data"),},
{"process_tuple", (PyCFunction) process_tuple, METH_VARARGS,
PyDoc_STR(
"process the items in the second argument "
"with the corresponding items in the first argument."
),
},
{NULL}
};
static struct PyModuleDef optimized_module = {
PyModuleDef_HEAD_INIT,
"optimized",/* name of module */
NULL, /* module documentation, may be NULL */
-1, /* size of per-interpreter state of the module,
or -1 if the module keeps state in global variables. */
optimized_methods,
};
PyMODINIT_FUNC
PyInit_optimized(void)
{
return(PyModule_Create(&optimized_module));
}
/*
* vim: ts=3:sw=3:noet:
*/