1 |
|
---|
2 | #include "Python.h"
|
---|
3 |
|
---|
4 | PyDoc_STRVAR(operator_doc,
|
---|
5 | "Operator interface.\n\
|
---|
6 | \n\
|
---|
7 | This module exports a set of functions implemented in C corresponding\n\
|
---|
8 | to the intrinsic operators of Python. For example, operator.add(x, y)\n\
|
---|
9 | is equivalent to the expression x+y. The function names are those\n\
|
---|
10 | used for special class methods; variants without leading and trailing\n\
|
---|
11 | '__' are also provided for convenience.");
|
---|
12 |
|
---|
13 | #define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
|
---|
14 | return AOP(a1); }
|
---|
15 |
|
---|
16 | #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
|
---|
17 | PyObject *a1, *a2; \
|
---|
18 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
|
---|
19 | return AOP(a1,a2); }
|
---|
20 |
|
---|
21 | #define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
|
---|
22 | PyObject *a1; int a2; \
|
---|
23 | if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \
|
---|
24 | return AOP(a1,a2); }
|
---|
25 |
|
---|
26 | #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
|
---|
27 | PyObject *a1, *a2; \
|
---|
28 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
|
---|
29 | if(-1 == AOP(a1,a2)) return NULL; \
|
---|
30 | Py_INCREF(Py_None); \
|
---|
31 | return Py_None; }
|
---|
32 |
|
---|
33 | #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
|
---|
34 | PyObject *a1, *a2, *a3; \
|
---|
35 | if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \
|
---|
36 | if(-1 == AOP(a1,a2,a3)) return NULL; \
|
---|
37 | Py_INCREF(Py_None); \
|
---|
38 | return Py_None; }
|
---|
39 |
|
---|
40 | #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
|
---|
41 | long r; \
|
---|
42 | if(-1 == (r=AOP(a1))) return NULL; \
|
---|
43 | return PyBool_FromLong(r); }
|
---|
44 |
|
---|
45 | #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
|
---|
46 | PyObject *a1, *a2; long r; \
|
---|
47 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
|
---|
48 | if(-1 == (r=AOP(a1,a2))) return NULL; \
|
---|
49 | return PyInt_FromLong(r); }
|
---|
50 |
|
---|
51 | #define spamn2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
|
---|
52 | PyObject *a1, *a2; Py_ssize_t r; \
|
---|
53 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
|
---|
54 | if(-1 == (r=AOP(a1,a2))) return NULL; \
|
---|
55 | return PyInt_FromSsize_t(r); }
|
---|
56 |
|
---|
57 | #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
|
---|
58 | PyObject *a1, *a2; long r; \
|
---|
59 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
|
---|
60 | if(-1 == (r=AOP(a1,a2))) return NULL; \
|
---|
61 | return PyBool_FromLong(r); }
|
---|
62 |
|
---|
63 | #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
|
---|
64 | PyObject *a1, *a2; \
|
---|
65 | if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
|
---|
66 | return PyObject_RichCompare(a1,a2,A); }
|
---|
67 |
|
---|
68 | spami(isCallable , PyCallable_Check)
|
---|
69 | spami(isNumberType , PyNumber_Check)
|
---|
70 | spami(truth , PyObject_IsTrue)
|
---|
71 | spam2(op_add , PyNumber_Add)
|
---|
72 | spam2(op_sub , PyNumber_Subtract)
|
---|
73 | spam2(op_mul , PyNumber_Multiply)
|
---|
74 | spam2(op_div , PyNumber_Divide)
|
---|
75 | spam2(op_floordiv , PyNumber_FloorDivide)
|
---|
76 | spam2(op_truediv , PyNumber_TrueDivide)
|
---|
77 | spam2(op_mod , PyNumber_Remainder)
|
---|
78 | spam1(op_neg , PyNumber_Negative)
|
---|
79 | spam1(op_pos , PyNumber_Positive)
|
---|
80 | spam1(op_abs , PyNumber_Absolute)
|
---|
81 | spam1(op_inv , PyNumber_Invert)
|
---|
82 | spam1(op_invert , PyNumber_Invert)
|
---|
83 | spam2(op_lshift , PyNumber_Lshift)
|
---|
84 | spam2(op_rshift , PyNumber_Rshift)
|
---|
85 | spami(op_not_ , PyObject_Not)
|
---|
86 | spam2(op_and_ , PyNumber_And)
|
---|
87 | spam2(op_xor , PyNumber_Xor)
|
---|
88 | spam2(op_or_ , PyNumber_Or)
|
---|
89 | spam2(op_iadd , PyNumber_InPlaceAdd)
|
---|
90 | spam2(op_isub , PyNumber_InPlaceSubtract)
|
---|
91 | spam2(op_imul , PyNumber_InPlaceMultiply)
|
---|
92 | spam2(op_idiv , PyNumber_InPlaceDivide)
|
---|
93 | spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
|
---|
94 | spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
|
---|
95 | spam2(op_imod , PyNumber_InPlaceRemainder)
|
---|
96 | spam2(op_ilshift , PyNumber_InPlaceLshift)
|
---|
97 | spam2(op_irshift , PyNumber_InPlaceRshift)
|
---|
98 | spam2(op_iand , PyNumber_InPlaceAnd)
|
---|
99 | spam2(op_ixor , PyNumber_InPlaceXor)
|
---|
100 | spam2(op_ior , PyNumber_InPlaceOr)
|
---|
101 | spami(isSequenceType , PySequence_Check)
|
---|
102 | spam2(op_concat , PySequence_Concat)
|
---|
103 | spamoi(op_repeat , PySequence_Repeat)
|
---|
104 | spam2(op_iconcat , PySequence_InPlaceConcat)
|
---|
105 | spamoi(op_irepeat , PySequence_InPlaceRepeat)
|
---|
106 | spami2b(op_contains , PySequence_Contains)
|
---|
107 | spami2b(sequenceIncludes, PySequence_Contains)
|
---|
108 | spamn2(indexOf , PySequence_Index)
|
---|
109 | spamn2(countOf , PySequence_Count)
|
---|
110 | spami(isMappingType , PyMapping_Check)
|
---|
111 | spam2(op_getitem , PyObject_GetItem)
|
---|
112 | spam2n(op_delitem , PyObject_DelItem)
|
---|
113 | spam3n(op_setitem , PyObject_SetItem)
|
---|
114 | spamrc(op_lt , Py_LT)
|
---|
115 | spamrc(op_le , Py_LE)
|
---|
116 | spamrc(op_eq , Py_EQ)
|
---|
117 | spamrc(op_ne , Py_NE)
|
---|
118 | spamrc(op_gt , Py_GT)
|
---|
119 | spamrc(op_ge , Py_GE)
|
---|
120 |
|
---|
121 | static PyObject*
|
---|
122 | op_pow(PyObject *s, PyObject *a)
|
---|
123 | {
|
---|
124 | PyObject *a1, *a2;
|
---|
125 | if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
|
---|
126 | return PyNumber_Power(a1, a2, Py_None);
|
---|
127 | return NULL;
|
---|
128 | }
|
---|
129 |
|
---|
130 | static PyObject*
|
---|
131 | op_ipow(PyObject *s, PyObject *a)
|
---|
132 | {
|
---|
133 | PyObject *a1, *a2;
|
---|
134 | if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
|
---|
135 | return PyNumber_InPlacePower(a1, a2, Py_None);
|
---|
136 | return NULL;
|
---|
137 | }
|
---|
138 |
|
---|
139 | static PyObject *
|
---|
140 | op_index(PyObject *s, PyObject *a)
|
---|
141 | {
|
---|
142 | return PyNumber_Index(a);
|
---|
143 | }
|
---|
144 |
|
---|
145 | static PyObject*
|
---|
146 | is_(PyObject *s, PyObject *a)
|
---|
147 | {
|
---|
148 | PyObject *a1, *a2, *result = NULL;
|
---|
149 | if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
|
---|
150 | result = (a1 == a2) ? Py_True : Py_False;
|
---|
151 | Py_INCREF(result);
|
---|
152 | }
|
---|
153 | return result;
|
---|
154 | }
|
---|
155 |
|
---|
156 | static PyObject*
|
---|
157 | is_not(PyObject *s, PyObject *a)
|
---|
158 | {
|
---|
159 | PyObject *a1, *a2, *result = NULL;
|
---|
160 | if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
|
---|
161 | result = (a1 != a2) ? Py_True : Py_False;
|
---|
162 | Py_INCREF(result);
|
---|
163 | }
|
---|
164 | return result;
|
---|
165 | }
|
---|
166 |
|
---|
167 | static PyObject*
|
---|
168 | op_getslice(PyObject *s, PyObject *a)
|
---|
169 | {
|
---|
170 | PyObject *a1;
|
---|
171 | int a2,a3;
|
---|
172 |
|
---|
173 | if (!PyArg_ParseTuple(a,"Oii:getslice",&a1,&a2,&a3))
|
---|
174 | return NULL;
|
---|
175 | return PySequence_GetSlice(a1,a2,a3);
|
---|
176 | }
|
---|
177 |
|
---|
178 | static PyObject*
|
---|
179 | op_setslice(PyObject *s, PyObject *a)
|
---|
180 | {
|
---|
181 | PyObject *a1, *a4;
|
---|
182 | int a2,a3;
|
---|
183 |
|
---|
184 | if (!PyArg_ParseTuple(a,"OiiO:setslice",&a1,&a2,&a3,&a4))
|
---|
185 | return NULL;
|
---|
186 |
|
---|
187 | if (-1 == PySequence_SetSlice(a1,a2,a3,a4))
|
---|
188 | return NULL;
|
---|
189 |
|
---|
190 | Py_INCREF(Py_None);
|
---|
191 | return Py_None;
|
---|
192 | }
|
---|
193 |
|
---|
194 | static PyObject*
|
---|
195 | op_delslice(PyObject *s, PyObject *a)
|
---|
196 | {
|
---|
197 | PyObject *a1;
|
---|
198 | int a2,a3;
|
---|
199 |
|
---|
200 | if(! PyArg_ParseTuple(a,"Oii:delslice",&a1,&a2,&a3))
|
---|
201 | return NULL;
|
---|
202 |
|
---|
203 | if (-1 == PySequence_DelSlice(a1,a2,a3))
|
---|
204 | return NULL;
|
---|
205 |
|
---|
206 | Py_INCREF(Py_None);
|
---|
207 | return Py_None;
|
---|
208 | }
|
---|
209 |
|
---|
210 | #undef spam1
|
---|
211 | #undef spam2
|
---|
212 | #undef spam1o
|
---|
213 | #undef spam1o
|
---|
214 | #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
|
---|
215 | #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
|
---|
216 | {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
|
---|
217 | #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
|
---|
218 | #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
|
---|
219 | {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
|
---|
220 |
|
---|
221 | static struct PyMethodDef operator_methods[] = {
|
---|
222 |
|
---|
223 | spam1o(isCallable,
|
---|
224 | "isCallable(a) -- Same as callable(a).")
|
---|
225 | spam1o(isNumberType,
|
---|
226 | "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
|
---|
227 | spam1o(isSequenceType,
|
---|
228 | "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
|
---|
229 | spam1o(truth,
|
---|
230 | "truth(a) -- Return True if a is true, False otherwise.")
|
---|
231 | spam2(contains,__contains__,
|
---|
232 | "contains(a, b) -- Same as b in a (note reversed operands).")
|
---|
233 | spam1(sequenceIncludes,
|
---|
234 | "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
|
---|
235 | spam1(indexOf,
|
---|
236 | "indexOf(a, b) -- Return the first index of b in a.")
|
---|
237 | spam1(countOf,
|
---|
238 | "countOf(a, b) -- Return the number of times b occurs in a.")
|
---|
239 | spam1o(isMappingType,
|
---|
240 | "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
|
---|
241 |
|
---|
242 | spam1(is_, "is_(a, b) -- Same as a is b.")
|
---|
243 | spam1(is_not, "is_not(a, b) -- Same as a is not b.")
|
---|
244 | spam2o(index, __index__, "index(a) -- Same as a.__index__()")
|
---|
245 | spam2(add,__add__, "add(a, b) -- Same as a + b.")
|
---|
246 | spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
|
---|
247 | spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
|
---|
248 | spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
|
---|
249 | spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
|
---|
250 | spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
|
---|
251 | spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
|
---|
252 | spam2o(neg,__neg__, "neg(a) -- Same as -a.")
|
---|
253 | spam2o(pos,__pos__, "pos(a) -- Same as +a.")
|
---|
254 | spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
|
---|
255 | spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
|
---|
256 | spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
|
---|
257 | spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
|
---|
258 | spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
|
---|
259 | spam2o(not_,__not__, "not_(a) -- Same as not a.")
|
---|
260 | spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
|
---|
261 | spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
|
---|
262 | spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
|
---|
263 | spam2(iadd,__iadd__, "iadd(a, b) -- Same as a += b.")
|
---|
264 | spam2(isub,__isub__, "isub(a, b) -- Same as a -= b.")
|
---|
265 | spam2(imul,__imul__, "imul(a, b) -- Same as a *= b.")
|
---|
266 | spam2(idiv,__idiv__, "idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
|
---|
267 | spam2(ifloordiv,__ifloordiv__, "ifloordiv(a, b) -- Same as a //= b.")
|
---|
268 | spam2(itruediv,__itruediv__, "itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
|
---|
269 | spam2(imod,__imod__, "imod(a, b) -- Same as a %= b.")
|
---|
270 | spam2(ilshift,__ilshift__, "ilshift(a, b) -- Same as a <<= b.")
|
---|
271 | spam2(irshift,__irshift__, "irshift(a, b) -- Same as a >>= b.")
|
---|
272 | spam2(iand,__iand__, "iand(a, b) -- Same as a &= b.")
|
---|
273 | spam2(ixor,__ixor__, "ixor(a, b) -- Same as a ^= b.")
|
---|
274 | spam2(ior,__ior__, "ior(a, b) -- Same as a |= b.")
|
---|
275 | spam2(concat,__concat__,
|
---|
276 | "concat(a, b) -- Same as a + b, for a and b sequences.")
|
---|
277 | spam2(repeat,__repeat__,
|
---|
278 | "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
|
---|
279 | spam2(iconcat,__iconcat__,
|
---|
280 | "iconcat(a, b) -- Same as a += b, for a and b sequences.")
|
---|
281 | spam2(irepeat,__irepeat__,
|
---|
282 | "irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
|
---|
283 | spam2(getitem,__getitem__,
|
---|
284 | "getitem(a, b) -- Same as a[b].")
|
---|
285 | spam2(setitem,__setitem__,
|
---|
286 | "setitem(a, b, c) -- Same as a[b] = c.")
|
---|
287 | spam2(delitem,__delitem__,
|
---|
288 | "delitem(a, b) -- Same as del a[b].")
|
---|
289 | spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
|
---|
290 | spam2(ipow,__ipow__, "ipow(a, b) -- Same as a **= b.")
|
---|
291 | spam2(getslice,__getslice__,
|
---|
292 | "getslice(a, b, c) -- Same as a[b:c].")
|
---|
293 | spam2(setslice,__setslice__,
|
---|
294 | "setslice(a, b, c, d) -- Same as a[b:c] = d.")
|
---|
295 | spam2(delslice,__delslice__,
|
---|
296 | "delslice(a, b, c) -- Same as del a[b:c].")
|
---|
297 | spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
|
---|
298 | spam2(le,__le__, "le(a, b) -- Same as a<=b.")
|
---|
299 | spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
|
---|
300 | spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
|
---|
301 | spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
|
---|
302 | spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
|
---|
303 |
|
---|
304 | {NULL, NULL} /* sentinel */
|
---|
305 |
|
---|
306 | };
|
---|
307 |
|
---|
308 | /* itemgetter object **********************************************************/
|
---|
309 |
|
---|
310 | typedef struct {
|
---|
311 | PyObject_HEAD
|
---|
312 | Py_ssize_t nitems;
|
---|
313 | PyObject *item;
|
---|
314 | } itemgetterobject;
|
---|
315 |
|
---|
316 | static PyTypeObject itemgetter_type;
|
---|
317 |
|
---|
318 | static PyObject *
|
---|
319 | itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
---|
320 | {
|
---|
321 | itemgetterobject *ig;
|
---|
322 | PyObject *item;
|
---|
323 | Py_ssize_t nitems;
|
---|
324 |
|
---|
325 | if (!_PyArg_NoKeywords("itemgetter()", kwds))
|
---|
326 | return NULL;
|
---|
327 |
|
---|
328 | nitems = PyTuple_GET_SIZE(args);
|
---|
329 | if (nitems <= 1) {
|
---|
330 | if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
|
---|
331 | return NULL;
|
---|
332 | } else
|
---|
333 | item = args;
|
---|
334 |
|
---|
335 | /* create itemgetterobject structure */
|
---|
336 | ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
|
---|
337 | if (ig == NULL)
|
---|
338 | return NULL;
|
---|
339 |
|
---|
340 | Py_INCREF(item);
|
---|
341 | ig->item = item;
|
---|
342 | ig->nitems = nitems;
|
---|
343 |
|
---|
344 | PyObject_GC_Track(ig);
|
---|
345 | return (PyObject *)ig;
|
---|
346 | }
|
---|
347 |
|
---|
348 | static void
|
---|
349 | itemgetter_dealloc(itemgetterobject *ig)
|
---|
350 | {
|
---|
351 | PyObject_GC_UnTrack(ig);
|
---|
352 | Py_XDECREF(ig->item);
|
---|
353 | PyObject_GC_Del(ig);
|
---|
354 | }
|
---|
355 |
|
---|
356 | static int
|
---|
357 | itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
|
---|
358 | {
|
---|
359 | Py_VISIT(ig->item);
|
---|
360 | return 0;
|
---|
361 | }
|
---|
362 |
|
---|
363 | static PyObject *
|
---|
364 | itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
|
---|
365 | {
|
---|
366 | PyObject *obj, *result;
|
---|
367 | Py_ssize_t i, nitems=ig->nitems;
|
---|
368 |
|
---|
369 | if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
|
---|
370 | return NULL;
|
---|
371 | if (nitems == 1)
|
---|
372 | return PyObject_GetItem(obj, ig->item);
|
---|
373 |
|
---|
374 | assert(PyTuple_Check(ig->item));
|
---|
375 | assert(PyTuple_GET_SIZE(ig->item) == nitems);
|
---|
376 |
|
---|
377 | result = PyTuple_New(nitems);
|
---|
378 | if (result == NULL)
|
---|
379 | return NULL;
|
---|
380 |
|
---|
381 | for (i=0 ; i < nitems ; i++) {
|
---|
382 | PyObject *item, *val;
|
---|
383 | item = PyTuple_GET_ITEM(ig->item, i);
|
---|
384 | val = PyObject_GetItem(obj, item);
|
---|
385 | if (val == NULL) {
|
---|
386 | Py_DECREF(result);
|
---|
387 | return NULL;
|
---|
388 | }
|
---|
389 | PyTuple_SET_ITEM(result, i, val);
|
---|
390 | }
|
---|
391 | return result;
|
---|
392 | }
|
---|
393 |
|
---|
394 | PyDoc_STRVAR(itemgetter_doc,
|
---|
395 | "itemgetter(item, ...) --> itemgetter object\n\
|
---|
396 | \n\
|
---|
397 | Return a callable object that fetches the given item(s) from its operand.\n\
|
---|
398 | After, f=itemgetter(2), the call f(r) returns r[2].\n\
|
---|
399 | After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
|
---|
400 |
|
---|
401 | static PyTypeObject itemgetter_type = {
|
---|
402 | PyObject_HEAD_INIT(NULL)
|
---|
403 | 0, /* ob_size */
|
---|
404 | "operator.itemgetter", /* tp_name */
|
---|
405 | sizeof(itemgetterobject), /* tp_basicsize */
|
---|
406 | 0, /* tp_itemsize */
|
---|
407 | /* methods */
|
---|
408 | (destructor)itemgetter_dealloc, /* tp_dealloc */
|
---|
409 | 0, /* tp_print */
|
---|
410 | 0, /* tp_getattr */
|
---|
411 | 0, /* tp_setattr */
|
---|
412 | 0, /* tp_compare */
|
---|
413 | 0, /* tp_repr */
|
---|
414 | 0, /* tp_as_number */
|
---|
415 | 0, /* tp_as_sequence */
|
---|
416 | 0, /* tp_as_mapping */
|
---|
417 | 0, /* tp_hash */
|
---|
418 | (ternaryfunc)itemgetter_call, /* tp_call */
|
---|
419 | 0, /* tp_str */
|
---|
420 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
421 | 0, /* tp_setattro */
|
---|
422 | 0, /* tp_as_buffer */
|
---|
423 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
424 | itemgetter_doc, /* tp_doc */
|
---|
425 | (traverseproc)itemgetter_traverse, /* tp_traverse */
|
---|
426 | 0, /* tp_clear */
|
---|
427 | 0, /* tp_richcompare */
|
---|
428 | 0, /* tp_weaklistoffset */
|
---|
429 | 0, /* tp_iter */
|
---|
430 | 0, /* tp_iternext */
|
---|
431 | 0, /* tp_methods */
|
---|
432 | 0, /* tp_members */
|
---|
433 | 0, /* tp_getset */
|
---|
434 | 0, /* tp_base */
|
---|
435 | 0, /* tp_dict */
|
---|
436 | 0, /* tp_descr_get */
|
---|
437 | 0, /* tp_descr_set */
|
---|
438 | 0, /* tp_dictoffset */
|
---|
439 | 0, /* tp_init */
|
---|
440 | 0, /* tp_alloc */
|
---|
441 | itemgetter_new, /* tp_new */
|
---|
442 | 0, /* tp_free */
|
---|
443 | };
|
---|
444 |
|
---|
445 |
|
---|
446 | /* attrgetter object **********************************************************/
|
---|
447 |
|
---|
448 | typedef struct {
|
---|
449 | PyObject_HEAD
|
---|
450 | Py_ssize_t nattrs;
|
---|
451 | PyObject *attr;
|
---|
452 | } attrgetterobject;
|
---|
453 |
|
---|
454 | static PyTypeObject attrgetter_type;
|
---|
455 |
|
---|
456 | static PyObject *
|
---|
457 | attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
|
---|
458 | {
|
---|
459 | attrgetterobject *ag;
|
---|
460 | PyObject *attr;
|
---|
461 | Py_ssize_t nattrs;
|
---|
462 |
|
---|
463 | if (!_PyArg_NoKeywords("attrgetter()", kwds))
|
---|
464 | return NULL;
|
---|
465 |
|
---|
466 | nattrs = PyTuple_GET_SIZE(args);
|
---|
467 | if (nattrs <= 1) {
|
---|
468 | if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
|
---|
469 | return NULL;
|
---|
470 | } else
|
---|
471 | attr = args;
|
---|
472 |
|
---|
473 | /* create attrgetterobject structure */
|
---|
474 | ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
|
---|
475 | if (ag == NULL)
|
---|
476 | return NULL;
|
---|
477 |
|
---|
478 | Py_INCREF(attr);
|
---|
479 | ag->attr = attr;
|
---|
480 | ag->nattrs = nattrs;
|
---|
481 |
|
---|
482 | PyObject_GC_Track(ag);
|
---|
483 | return (PyObject *)ag;
|
---|
484 | }
|
---|
485 |
|
---|
486 | static void
|
---|
487 | attrgetter_dealloc(attrgetterobject *ag)
|
---|
488 | {
|
---|
489 | PyObject_GC_UnTrack(ag);
|
---|
490 | Py_XDECREF(ag->attr);
|
---|
491 | PyObject_GC_Del(ag);
|
---|
492 | }
|
---|
493 |
|
---|
494 | static int
|
---|
495 | attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
|
---|
496 | {
|
---|
497 | Py_VISIT(ag->attr);
|
---|
498 | return 0;
|
---|
499 | }
|
---|
500 |
|
---|
501 | static PyObject *
|
---|
502 | attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
|
---|
503 | {
|
---|
504 | PyObject *obj, *result;
|
---|
505 | Py_ssize_t i, nattrs=ag->nattrs;
|
---|
506 |
|
---|
507 | if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
|
---|
508 | return NULL;
|
---|
509 | if (ag->nattrs == 1)
|
---|
510 | return PyObject_GetAttr(obj, ag->attr);
|
---|
511 |
|
---|
512 | assert(PyTuple_Check(ag->attr));
|
---|
513 | assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
|
---|
514 |
|
---|
515 | result = PyTuple_New(nattrs);
|
---|
516 | if (result == NULL)
|
---|
517 | return NULL;
|
---|
518 |
|
---|
519 | for (i=0 ; i < nattrs ; i++) {
|
---|
520 | PyObject *attr, *val;
|
---|
521 | attr = PyTuple_GET_ITEM(ag->attr, i);
|
---|
522 | val = PyObject_GetAttr(obj, attr);
|
---|
523 | if (val == NULL) {
|
---|
524 | Py_DECREF(result);
|
---|
525 | return NULL;
|
---|
526 | }
|
---|
527 | PyTuple_SET_ITEM(result, i, val);
|
---|
528 | }
|
---|
529 | return result;
|
---|
530 | }
|
---|
531 |
|
---|
532 | PyDoc_STRVAR(attrgetter_doc,
|
---|
533 | "attrgetter(attr, ...) --> attrgetter object\n\
|
---|
534 | \n\
|
---|
535 | Return a callable object that fetches the given attribute(s) from its operand.\n\
|
---|
536 | After, f=attrgetter('name'), the call f(r) returns r.name.\n\
|
---|
537 | After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).");
|
---|
538 |
|
---|
539 | static PyTypeObject attrgetter_type = {
|
---|
540 | PyObject_HEAD_INIT(NULL)
|
---|
541 | 0, /* ob_size */
|
---|
542 | "operator.attrgetter", /* tp_name */
|
---|
543 | sizeof(attrgetterobject), /* tp_basicsize */
|
---|
544 | 0, /* tp_itemsize */
|
---|
545 | /* methods */
|
---|
546 | (destructor)attrgetter_dealloc, /* tp_dealloc */
|
---|
547 | 0, /* tp_print */
|
---|
548 | 0, /* tp_getattr */
|
---|
549 | 0, /* tp_setattr */
|
---|
550 | 0, /* tp_compare */
|
---|
551 | 0, /* tp_repr */
|
---|
552 | 0, /* tp_as_number */
|
---|
553 | 0, /* tp_as_sequence */
|
---|
554 | 0, /* tp_as_mapping */
|
---|
555 | 0, /* tp_hash */
|
---|
556 | (ternaryfunc)attrgetter_call, /* tp_call */
|
---|
557 | 0, /* tp_str */
|
---|
558 | PyObject_GenericGetAttr, /* tp_getattro */
|
---|
559 | 0, /* tp_setattro */
|
---|
560 | 0, /* tp_as_buffer */
|
---|
561 | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
|
---|
562 | attrgetter_doc, /* tp_doc */
|
---|
563 | (traverseproc)attrgetter_traverse, /* tp_traverse */
|
---|
564 | 0, /* tp_clear */
|
---|
565 | 0, /* tp_richcompare */
|
---|
566 | 0, /* tp_weaklistoffset */
|
---|
567 | 0, /* tp_iter */
|
---|
568 | 0, /* tp_iternext */
|
---|
569 | 0, /* tp_methods */
|
---|
570 | 0, /* tp_members */
|
---|
571 | 0, /* tp_getset */
|
---|
572 | 0, /* tp_base */
|
---|
573 | 0, /* tp_dict */
|
---|
574 | 0, /* tp_descr_get */
|
---|
575 | 0, /* tp_descr_set */
|
---|
576 | 0, /* tp_dictoffset */
|
---|
577 | 0, /* tp_init */
|
---|
578 | 0, /* tp_alloc */
|
---|
579 | attrgetter_new, /* tp_new */
|
---|
580 | 0, /* tp_free */
|
---|
581 | };
|
---|
582 | /* Initialization function for the module (*must* be called initoperator) */
|
---|
583 |
|
---|
584 | PyMODINIT_FUNC
|
---|
585 | initoperator(void)
|
---|
586 | {
|
---|
587 | PyObject *m;
|
---|
588 |
|
---|
589 | /* Create the module and add the functions */
|
---|
590 | m = Py_InitModule4("operator", operator_methods, operator_doc,
|
---|
591 | (PyObject*)NULL, PYTHON_API_VERSION);
|
---|
592 | if (m == NULL)
|
---|
593 | return;
|
---|
594 |
|
---|
595 | if (PyType_Ready(&itemgetter_type) < 0)
|
---|
596 | return;
|
---|
597 | Py_INCREF(&itemgetter_type);
|
---|
598 | PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
|
---|
599 |
|
---|
600 | if (PyType_Ready(&attrgetter_type) < 0)
|
---|
601 | return;
|
---|
602 | Py_INCREF(&attrgetter_type);
|
---|
603 | PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
|
---|
604 | }
|
---|