File: functools.c

package info (click to toggle)
py-postgresql 1.2.1%2Bgit20180803.ef7b9a9-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,620 kB
  • sloc: python: 18,317; ansic: 2,024; sql: 282; sh: 26; makefile: 22
file content (340 lines) | stat: -rw-r--r-- 7,429 bytes parent folder | download | duplicates (3)
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
/*
 * .port.optimized - functools.c
 *
 *//*
 * optimizations for postgresql.python package modules.
 */
/*
 * process the tuple with the associated callables while
 * calling the third object in cases of failure to generalize the exception.
 */
#define include_functools_functions \
	mFUNC(rsetattr, METH_VARARGS, "rsetattr(attr, val, ob) set the attribute to the value *and* return `ob`.") \
	mFUNC(compose, METH_VARARGS, "given a sequence of callables, and an argument for the first call, compose the result.") \
	mFUNC(process_tuple, METH_VARARGS, "process the items in the second argument with the corresponding items in the first argument.") \
	mFUNC(process_chunk, METH_VARARGS, "process the items of the chunk given as the second argument with the corresponding items in the first argument.")

static PyObject *
_process_tuple(PyObject *procs, PyObject *tup, PyObject *fail)
{
	PyObject *rob;
	Py_ssize_t len, i;

	if (!PyTuple_CheckExact(procs))
	{
		PyErr_SetString(
			PyExc_TypeError,
			"process_tuple requires an exact tuple as its first argument"
		);
		return(NULL);
	}

	if (!PyTuple_Check(tup))
	{
		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_TypeError,
			"inconsistent items, %d processors and %d items in row",
			len,
			PyTuple_GET_SIZE(procs)
		);
		return(NULL);
	}
	/* types check out; consistent sizes */
	rob = PyTuple_New(len);

	for (i = 0; i < len; ++i)
	{
		PyObject *p, *o, *ot, *r;
		/* p = processor,
		 * o = source object,
		 * ot = o's tuple (temp for application to p),
		 * r = transformed * output
		 */

		/*
		 * 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);
			/* mmmm, cake! */
			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)
		{
			/* good, set it and move on. */
			PyTuple_SET_ITEM(rob, i, r);
		}
		else
		{
			/*
			 * Exception caused by >>> p(*ot)
			 *
			 * In this case, the failure callback needs to be called
			 * in order to properly generalize the failure. There are numerous,
			 * and (sometimes) inconsistent reasons why a tuple cannot be
			 * processed and therefore a generalized exception raised in the
			 * context of the original is *very* useful.
			 */
			Py_DECREF(rob);
			rob = NULL;

			/*
			 * Don't trap BaseException's.
			 */
			if (PyErr_ExceptionMatches(PyExc_Exception))
			{
				PyObject *cause, *failargs, *failedat;
				PyObject *exc, *tb;

				/* Store exception to set context after handler. */
				PyErr_Fetch(&exc, &cause, &tb);
				PyErr_NormalizeException(&exc, &cause, &tb);
				Py_XDECREF(exc);
				Py_XDECREF(tb);

				failedat = PyLong_FromSsize_t(i);
				if (failedat != NULL)
				{
					failargs = PyTuple_New(4);
					if (failargs != NULL)
					{
						/* args for the exception "generalizer" */
						PyTuple_SET_ITEM(failargs, 0, cause);
						PyTuple_SET_ITEM(failargs, 1, procs);
						Py_INCREF(procs);
						PyTuple_SET_ITEM(failargs, 2, tup);
						Py_INCREF(tup);
						PyTuple_SET_ITEM(failargs, 3, 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);
					}
				}
			}

			/*
			 * Break out of loop to return(NULL);
			 */
			break;
		}
	}

	return(rob);
}

/*
 * 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;

	if (!PyArg_ParseTuple(args, "OOO", &procs, &tup, &fail))
		return(NULL);

	return(_process_tuple(procs, tup, fail));
}

static PyObject *
_process_chunk_new_list(PyObject *procs, PyObject *tupc, PyObject *fail)
{
	PyObject *rob;
	Py_ssize_t i, len;

	/*
	 * Turn the iterable into a new list.
	 */
	rob = PyObject_CallFunctionObjArgs((PyObject *) &PyList_Type, tupc, NULL);
	if (rob == NULL)
		return(NULL);
	len = PyList_GET_SIZE(rob);

	for (i = 0; i < len; ++i)
	{
		PyObject *tup, *r;
		/*
		 * If it's Py_None, that means it's NULL. No processing necessary.
		 */
		tup = PyList_GetItem(rob, i); /* borrowed ref from list */
		r = _process_tuple(procs, tup, fail);
		if (r == NULL)
		{
			/* process_tuple failed. assume PyErr_Occurred() */
			Py_DECREF(rob);
			return(NULL);
		}
		PyList_SetItem(rob, i, r);
	}

	return(rob);
}

static PyObject *
_process_chunk_from_list(PyObject *procs, PyObject *tupc, PyObject *fail)
{
	PyObject *rob;
	Py_ssize_t i, len;

	len = PyList_GET_SIZE(tupc);
	rob = PyList_New(len);
	if (rob == NULL)
		return(NULL);

	for (i = 0; i < len; ++i)
	{
		PyObject *tup, *r;
		/*
		 * If it's Py_None, that means it's NULL. No processing necessary.
		 */
		tup = PyList_GET_ITEM(tupc, i);
		r = _process_tuple(procs, tup, fail);
		if (r == NULL)
		{
			Py_DECREF(rob);
			return(NULL);
		}
		PyList_SET_ITEM(rob, i, r);
	}

	return(rob);
}

/*
 * process the chunk of tuples with the associated callables while
 * calling the third object in cases of failure to generalize the exception.
 */
static PyObject *
process_chunk(PyObject *self, PyObject *args)
{
	PyObject *tupc, *procs, *fail;

	if (!PyArg_ParseTuple(args, "OOO", &procs, &tupc, &fail))
		return(NULL);

	if (PyList_Check(tupc))
	{
		return(_process_chunk_from_list(procs, tupc, fail));
	}
	else
	{
		return(_process_chunk_new_list(procs, tupc, fail));
	}
}
static PyObject *
rsetattr(PyObject *self, PyObject *args)
{
	PyObject *ob, *attr, *val;

	if (!PyArg_ParseTuple(args, "OOO", &attr, &val, &ob))
		return(NULL);

	if (PyObject_SetAttr(ob, attr, val) < 0)
		return(NULL);

	Py_INCREF(ob);
	return(ob);
}

/*
 * Override the functools.Composition __call__.
 */
static PyObject *
compose(PyObject *self, PyObject *args)
{
	Py_ssize_t i, len;
	PyObject *rob, *argt, *seq, *x;

	if (!PyArg_ParseTuple(args, "OO", &seq, &rob))
		return(NULL);

	Py_INCREF(rob);
	if (PyObject_IsInstance(seq, (PyObject *) &PyTuple_Type))
	{
		len = PyTuple_GET_SIZE(seq);
		for (i = 0; i < len; ++i)
		{
			x = PyTuple_GET_ITEM(seq, i);
			argt = PyTuple_New(1);
			PyTuple_SET_ITEM(argt, 0, rob);
			rob = PyObject_CallObject(x, argt);
			Py_DECREF(argt);
			if (rob == NULL)
				break;
		}
	}
	else if (PyObject_IsInstance(seq, (PyObject *) &PyList_Type))
	{
		len = PyList_GET_SIZE(seq);
		for (i = 0; i < len; ++i)
		{
			x = PyList_GET_ITEM(seq, i);
			argt = PyTuple_New(1);
			PyTuple_SET_ITEM(argt, 0, rob);
			rob = PyObject_CallObject(x, argt);
			Py_DECREF(argt);
			if (rob == NULL)
				break;
		}
	}
	else
	{
		/*
		 * Arbitrary sequence.
		 */
		len = PySequence_Length(seq);
		for (i = 0; i < len; ++i)
		{
			x = PySequence_GetItem(seq, i);
			argt = PyTuple_New(1);
			PyTuple_SET_ITEM(argt, 0, rob);
			rob = PyObject_CallObject(x, argt);
			Py_DECREF(x);
			Py_DECREF(argt);
			if (rob == NULL)
				break;
		}
	}

	return(rob);
}
/*
 * vim: ts=3:sw=3:noet:
 */