Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(22)

Delta Between Two Patch Sets: Modules/_operator.c

Issue 29259: Add tp_fastcall to PyTypeObject: support FASTCALL calling convention for all callable objects
Left Patch Set: Created 3 years ago
Right Patch Set: Created 3 years ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
LEFTRIGHT
1 1
2 #include "Python.h" 2 #include "Python.h"
3
4 #include "clinic/_operator.c.h"
5
6 /*[clinic input]
7 module _operator
8 [clinic start generated code]*/
9 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=672ecf48487521e7]*/
3 10
4 PyDoc_STRVAR(operator_doc, 11 PyDoc_STRVAR(operator_doc,
5 "Operator interface.\n\ 12 "Operator interface.\n\
6 \n\ 13 \n\
7 This module exports a set of functions implemented in C corresponding\n\ 14 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\ 15 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\ 16 is equivalent to the expression x+y. The function names are those\n\
10 used for special methods; variants without leading and trailing\n\ 17 used for special methods; variants without leading and trailing\n\
11 '__' are also provided for convenience."); 18 '__' are also provided for convenience.");
12 19
13 #define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \ 20
14 return AOP(a1); } 21 /*[clinic input]
15 22 _operator.truth -> bool
16 #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ 23
17 PyObject *a1, *a2; \ 24 a: object
18 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ 25 /
19 return AOP(a1,a2); } 26
20 27 Return True if a is true, False otherwise.
21 #define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ 28 [clinic start generated code]*/
22 PyObject *a1; int a2; \ 29
23 if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \ 30 static int
24 return AOP(a1,a2); } 31 _operator_truth_impl(PyObject *module, PyObject *a)
25 32 /*[clinic end generated code: output=eaf87767234fa5d7 input=bc74a4cd90235875]*/
26 #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ 33 {
27 PyObject *a1, *a2; \ 34 return PyObject_IsTrue(a);
28 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ 35 }
29 if(-1 == AOP(a1,a2)) return NULL; \ 36
30 Py_INCREF(Py_None); \ 37 /*[clinic input]
31 return Py_None; } 38 _operator.add
32 39
33 #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ 40 a: object
34 PyObject *a1, *a2, *a3; \ 41 b: object
35 if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \ 42 /
36 if(-1 == AOP(a1,a2,a3)) return NULL; \ 43
37 Py_INCREF(Py_None); \ 44 Same as a + b.
38 return Py_None; } 45 [clinic start generated code]*/
39 46
40 #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \ 47 static PyObject *
41 long r; \ 48 _operator_add_impl(PyObject *module, PyObject *a, PyObject *b)
42 if(-1 == (r=AOP(a1))) return NULL; \ 49 /*[clinic end generated code: output=8292984204f45164 input=5efe3bff856ac215]*/
43 return PyBool_FromLong(r); } 50 {
44 51 return PyNumber_Add(a, b);
45 #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ 52 }
46 PyObject *a1, *a2; long r; \ 53
47 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ 54 /*[clinic input]
48 if(-1 == (r=AOP(a1,a2))) return NULL; \ 55 _operator.sub = _operator.add
49 return PyLong_FromLong(r); } 56
50 57 Same as a - b.
51 #define spamn2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ 58 [clinic start generated code]*/
52 PyObject *a1, *a2; Py_ssize_t r; \ 59
53 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ 60 static PyObject *
54 if(-1 == (r=AOP(a1,a2))) return NULL; \ 61 _operator_sub_impl(PyObject *module, PyObject *a, PyObject *b)
55 return PyLong_FromSsize_t(r); } 62 /*[clinic end generated code: output=4adfc3b888c1ee2e input=6494c6b100b8e795]*/
56 63 {
57 #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ 64 return PyNumber_Subtract(a, b);
58 PyObject *a1, *a2; long r; \ 65 }
59 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ 66
60 if(-1 == (r=AOP(a1,a2))) return NULL; \ 67 /*[clinic input]
61 return PyBool_FromLong(r); } 68 _operator.mul = _operator.add
62 69
63 #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \ 70 Same as a * b.
64 PyObject *a1, *a2; \ 71 [clinic start generated code]*/
65 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ 72
66 return PyObject_RichCompare(a1,a2,A); } 73 static PyObject *
67 74 _operator_mul_impl(PyObject *module, PyObject *a, PyObject *b)
68 spami(truth , PyObject_IsTrue) 75 /*[clinic end generated code: output=d24d66f55a01944c input=2368615b4358b70d]*/
69 spam2(op_add , PyNumber_Add) 76 {
70 spam2(op_sub , PyNumber_Subtract) 77 return PyNumber_Multiply(a, b);
71 spam2(op_mul , PyNumber_Multiply) 78 }
72 spam2(op_matmul , PyNumber_MatrixMultiply) 79
73 spam2(op_floordiv , PyNumber_FloorDivide) 80 /*[clinic input]
74 spam2(op_truediv , PyNumber_TrueDivide) 81 _operator.matmul = _operator.add
75 spam2(op_mod , PyNumber_Remainder) 82
76 spam1(op_neg , PyNumber_Negative) 83 Same as a @ b.
77 spam1(op_pos , PyNumber_Positive) 84 [clinic start generated code]*/
78 spam1(op_abs , PyNumber_Absolute) 85
79 spam1(op_inv , PyNumber_Invert) 86 static PyObject *
80 spam1(op_invert , PyNumber_Invert) 87 _operator_matmul_impl(PyObject *module, PyObject *a, PyObject *b)
81 spam2(op_lshift , PyNumber_Lshift) 88 /*[clinic end generated code: output=a20d917eb35d0101 input=9ab304e37fb42dd4]*/
82 spam2(op_rshift , PyNumber_Rshift) 89 {
83 spami(op_not_ , PyObject_Not) 90 return PyNumber_MatrixMultiply(a, b);
84 spam2(op_and_ , PyNumber_And) 91 }
85 spam2(op_xor , PyNumber_Xor) 92
86 spam2(op_or_ , PyNumber_Or) 93 /*[clinic input]
87 spam2(op_iadd , PyNumber_InPlaceAdd) 94 _operator.floordiv = _operator.add
88 spam2(op_isub , PyNumber_InPlaceSubtract) 95
89 spam2(op_imul , PyNumber_InPlaceMultiply) 96 Same as a // b.
90 spam2(op_imatmul , PyNumber_InPlaceMatrixMultiply) 97 [clinic start generated code]*/
91 spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide) 98
92 spam2(op_itruediv , PyNumber_InPlaceTrueDivide) 99 static PyObject *
93 spam2(op_imod , PyNumber_InPlaceRemainder) 100 _operator_floordiv_impl(PyObject *module, PyObject *a, PyObject *b)
94 spam2(op_ilshift , PyNumber_InPlaceLshift) 101 /*[clinic end generated code: output=df26b71a60589f99 input=bb2e88ba446c612c]*/
95 spam2(op_irshift , PyNumber_InPlaceRshift) 102 {
96 spam2(op_iand , PyNumber_InPlaceAnd) 103 return PyNumber_FloorDivide(a, b);
97 spam2(op_ixor , PyNumber_InPlaceXor) 104 }
98 spam2(op_ior , PyNumber_InPlaceOr) 105
99 spam2(op_concat , PySequence_Concat) 106 /*[clinic input]
100 spam2(op_iconcat , PySequence_InPlaceConcat) 107 _operator.truediv = _operator.add
101 spami2b(op_contains , PySequence_Contains) 108
102 spamn2(indexOf , PySequence_Index) 109 Same as a / b.
103 spamn2(countOf , PySequence_Count) 110 [clinic start generated code]*/
104 spam2(op_getitem , PyObject_GetItem) 111
105 spam2n(op_delitem , PyObject_DelItem) 112 static PyObject *
106 spam3n(op_setitem , PyObject_SetItem) 113 _operator_truediv_impl(PyObject *module, PyObject *a, PyObject *b)
107 spamrc(op_lt , Py_LT) 114 /*[clinic end generated code: output=0e6a959944d77719 input=ecbb947673f4eb1f]*/
108 spamrc(op_le , Py_LE) 115 {
109 spamrc(op_eq , Py_EQ) 116 return PyNumber_TrueDivide(a, b);
110 spamrc(op_ne , Py_NE) 117 }
111 spamrc(op_gt , Py_GT) 118
112 spamrc(op_ge , Py_GE) 119 /*[clinic input]
113 120 _operator.mod = _operator.add
114 static PyObject* 121
115 op_pow(PyObject *s, PyObject *a) 122 Same as a % b.
116 { 123 [clinic start generated code]*/
117 PyObject *a1, *a2; 124
118 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2)) 125 static PyObject *
119 return PyNumber_Power(a1, a2, Py_None); 126 _operator_mod_impl(PyObject *module, PyObject *a, PyObject *b)
120 return NULL; 127 /*[clinic end generated code: output=9519822f0bbec166 input=102e19b422342ac1]*/
121 } 128 {
122 129 return PyNumber_Remainder(a, b);
123 static PyObject* 130 }
124 op_ipow(PyObject *s, PyObject *a) 131
125 { 132 /*[clinic input]
126 PyObject *a1, *a2; 133 _operator.neg
127 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2)) 134
128 return PyNumber_InPlacePower(a1, a2, Py_None); 135 a: object
129 return NULL; 136 /
130 } 137
131 138 Same as -a.
132 static PyObject * 139 [clinic start generated code]*/
133 op_index(PyObject *s, PyObject *a) 140
141 static PyObject *
142 _operator_neg(PyObject *module, PyObject *a)
143 /*[clinic end generated code: output=36e08ecfc6a1c08c input=84f09bdcf27c96ec]*/
144 {
145 return PyNumber_Negative(a);
146 }
147
148 /*[clinic input]
149 _operator.pos = _operator.neg
150
151 Same as +a.
152 [clinic start generated code]*/
153
154 static PyObject *
155 _operator_pos(PyObject *module, PyObject *a)
156 /*[clinic end generated code: output=dad7a126221dd091 input=b6445b63fddb8772]*/
157 {
158 return PyNumber_Positive(a);
159 }
160
161 /*[clinic input]
162 _operator.abs = _operator.neg
163
164 Same as abs(a).
165 [clinic start generated code]*/
166
167 static PyObject *
168 _operator_abs(PyObject *module, PyObject *a)
169 /*[clinic end generated code: output=1389a93ba053ea3e input=341d07ba86f58039]*/
170 {
171 return PyNumber_Absolute(a);
172 }
173
174 /*[clinic input]
175 _operator.inv = _operator.neg
176
177 Same as ~a.
178 [clinic start generated code]*/
179
180 static PyObject *
181 _operator_inv(PyObject *module, PyObject *a)
182 /*[clinic end generated code: output=a56875ba075ee06d input=b01a4677739f6eb2]*/
183 {
184 return PyNumber_Invert(a);
185 }
186
187 /*[clinic input]
188 _operator.invert = _operator.neg
189
190 Same as ~a.
191 [clinic start generated code]*/
192
193 static PyObject *
194 _operator_invert(PyObject *module, PyObject *a)
195 /*[clinic end generated code: output=406b5aa030545fcc input=7f2d607176672e55]*/
196 {
197 return PyNumber_Invert(a);
198 }
199
200 /*[clinic input]
201 _operator.lshift = _operator.add
202
203 Same as a << b.
204 [clinic start generated code]*/
205
206 static PyObject *
207 _operator_lshift_impl(PyObject *module, PyObject *a, PyObject *b)
208 /*[clinic end generated code: output=37f7e52c41435bd8 input=746e8a160cbbc9eb]*/
209 {
210 return PyNumber_Lshift(a, b);
211 }
212
213 /*[clinic input]
214 _operator.rshift = _operator.add
215
216 Same as a >> b.
217 [clinic start generated code]*/
218
219 static PyObject *
220 _operator_rshift_impl(PyObject *module, PyObject *a, PyObject *b)
221 /*[clinic end generated code: output=4593c7ef30ec2ee3 input=d2c85bb5a64504c2]*/
222 {
223 return PyNumber_Rshift(a, b);
224 }
225
226 /*[clinic input]
227 _operator.not_ = _operator.truth
228
229 Same as not a.
230 [clinic start generated code]*/
231
232 static int
233 _operator_not__impl(PyObject *module, PyObject *a)
234 /*[clinic end generated code: output=743f9c24a09759ef input=854156d50804d9b8]*/
235 {
236 return PyObject_Not(a);
237 }
238
239 /*[clinic input]
240 _operator.and_ = _operator.add
241
242 Same as a & b.
243 [clinic start generated code]*/
244
245 static PyObject *
246 _operator_and__impl(PyObject *module, PyObject *a, PyObject *b)
247 /*[clinic end generated code: output=93c4fe88f7b76d9e input=4f3057c90ec4c99f]*/
248 {
249 return PyNumber_And(a, b);
250 }
251
252 /*[clinic input]
253 _operator.xor = _operator.add
254
255 Same as a ^ b.
256 [clinic start generated code]*/
257
258 static PyObject *
259 _operator_xor_impl(PyObject *module, PyObject *a, PyObject *b)
260 /*[clinic end generated code: output=b24cd8b79fde0004 input=3c5cfa7253d808dd]*/
261 {
262 return PyNumber_Xor(a, b);
263 }
264
265 /*[clinic input]
266 _operator.or_ = _operator.add
267
268 Same as a | b.
269 [clinic start generated code]*/
270
271 static PyObject *
272 _operator_or__impl(PyObject *module, PyObject *a, PyObject *b)
273 /*[clinic end generated code: output=58024867b8d90461 input=b40c6c44f7c79c09]*/
274 {
275 return PyNumber_Or(a, b);
276 }
277
278 /*[clinic input]
279 _operator.iadd = _operator.add
280
281 Same as a += b.
282 [clinic start generated code]*/
283
284 static PyObject *
285 _operator_iadd_impl(PyObject *module, PyObject *a, PyObject *b)
286 /*[clinic end generated code: output=07dc627832526eb5 input=d22a91c07ac69227]*/
287 {
288 return PyNumber_InPlaceAdd(a, b);
289 }
290
291 /*[clinic input]
292 _operator.isub = _operator.add
293
294 Same as a -= b.
295 [clinic start generated code]*/
296
297 static PyObject *
298 _operator_isub_impl(PyObject *module, PyObject *a, PyObject *b)
299 /*[clinic end generated code: output=4513467d23b5e0b1 input=4591b00d0a0ccafd]*/
300 {
301 return PyNumber_InPlaceSubtract(a, b);
302 }
303
304 /*[clinic input]
305 _operator.imul = _operator.add
306
307 Same as a *= b.
308 [clinic start generated code]*/
309
310 static PyObject *
311 _operator_imul_impl(PyObject *module, PyObject *a, PyObject *b)
312 /*[clinic end generated code: output=5e87dacd19a71eab input=0e01fb8631e1b76f]*/
313 {
314 return PyNumber_InPlaceMultiply(a, b);
315 }
316
317 /*[clinic input]
318 _operator.imatmul = _operator.add
319
320 Same as a @= b.
321 [clinic start generated code]*/
322
323 static PyObject *
324 _operator_imatmul_impl(PyObject *module, PyObject *a, PyObject *b)
325 /*[clinic end generated code: output=d603cbdf716ce519 input=bb614026372cd542]*/
326 {
327 return PyNumber_InPlaceMatrixMultiply(a, b);
328 }
329
330 /*[clinic input]
331 _operator.ifloordiv = _operator.add
332
333 Same as a //= b.
334 [clinic start generated code]*/
335
336 static PyObject *
337 _operator_ifloordiv_impl(PyObject *module, PyObject *a, PyObject *b)
338 /*[clinic end generated code: output=535336048c681794 input=9df3b5021cff4ca1]*/
339 {
340 return PyNumber_InPlaceFloorDivide(a, b);
341 }
342
343 /*[clinic input]
344 _operator.itruediv = _operator.add
345
346 Same as a /= b.
347 [clinic start generated code]*/
348
349 static PyObject *
350 _operator_itruediv_impl(PyObject *module, PyObject *a, PyObject *b)
351 /*[clinic end generated code: output=28017fbd3563952f input=9a1ee01608f5f590]*/
352 {
353 return PyNumber_InPlaceTrueDivide(a, b);
354 }
355
356 /*[clinic input]
357 _operator.imod = _operator.add
358
359 Same as a %= b.
360 [clinic start generated code]*/
361
362 static PyObject *
363 _operator_imod_impl(PyObject *module, PyObject *a, PyObject *b)
364 /*[clinic end generated code: output=f7c540ae0fc70904 input=d0c384a3ce38e1dd]*/
365 {
366 return PyNumber_InPlaceRemainder(a, b);
367 }
368
369 /*[clinic input]
370 _operator.ilshift = _operator.add
371
372 Same as a <<= b.
373 [clinic start generated code]*/
374
375 static PyObject *
376 _operator_ilshift_impl(PyObject *module, PyObject *a, PyObject *b)
377 /*[clinic end generated code: output=e73a8fee1ac18749 input=e21b6b310f54572e]*/
378 {
379 return PyNumber_InPlaceLshift(a, b);
380 }
381
382 /*[clinic input]
383 _operator.irshift = _operator.add
384
385 Same as a >>= b.
386 [clinic start generated code]*/
387
388 static PyObject *
389 _operator_irshift_impl(PyObject *module, PyObject *a, PyObject *b)
390 /*[clinic end generated code: output=97f2af6b5ff2ed81 input=6778dbd0f6e1ec16]*/
391 {
392 return PyNumber_InPlaceRshift(a, b);
393 }
394
395 /*[clinic input]
396 _operator.iand = _operator.add
397
398 Same as a &= b.
399 [clinic start generated code]*/
400
401 static PyObject *
402 _operator_iand_impl(PyObject *module, PyObject *a, PyObject *b)
403 /*[clinic end generated code: output=4599e9d40cbf7d00 input=71dfd8e70c156a7b]*/
404 {
405 return PyNumber_InPlaceAnd(a, b);
406 }
407
408 /*[clinic input]
409 _operator.ixor = _operator.add
410
411 Same as a ^= b.
412 [clinic start generated code]*/
413
414 static PyObject *
415 _operator_ixor_impl(PyObject *module, PyObject *a, PyObject *b)
416 /*[clinic end generated code: output=5ff881766872be03 input=695c32bec0604d86]*/
417 {
418 return PyNumber_InPlaceXor(a, b);
419 }
420
421 /*[clinic input]
422 _operator.ior = _operator.add
423
424 Same as a |= b.
425 [clinic start generated code]*/
426
427 static PyObject *
428 _operator_ior_impl(PyObject *module, PyObject *a, PyObject *b)
429 /*[clinic end generated code: output=48aac319445bf759 input=8f01d03eda9920cf]*/
430 {
431 return PyNumber_InPlaceOr(a, b);
432 }
433
434 /*[clinic input]
435 _operator.concat = _operator.add
436
437 Same as a + b, for a and b sequences.
438 [clinic start generated code]*/
439
440 static PyObject *
441 _operator_concat_impl(PyObject *module, PyObject *a, PyObject *b)
442 /*[clinic end generated code: output=80028390942c5f11 input=8544ccd5341a3658]*/
443 {
444 return PySequence_Concat(a, b);
445 }
446
447 /*[clinic input]
448 _operator.iconcat = _operator.add
449
450 Same as a += b, for a and b sequences.
451 [clinic start generated code]*/
452
453 static PyObject *
454 _operator_iconcat_impl(PyObject *module, PyObject *a, PyObject *b)
455 /*[clinic end generated code: output=3ea0a162ebb2e26d input=8f5fe5722fcd837e]*/
456 {
457 return PySequence_InPlaceConcat(a, b);
458 }
459
460 /*[clinic input]
461 _operator.contains -> bool
462
463 a: object
464 b: object
465 /
466
467 Same as b in a (note reversed operands).
468 [clinic start generated code]*/
469
470 static int
471 _operator_contains_impl(PyObject *module, PyObject *a, PyObject *b)
472 /*[clinic end generated code: output=413b4dbe82b6ffc1 input=9122a69b505fde13]*/
473 {
474 return PySequence_Contains(a, b);
475 }
476
477 /*[clinic input]
478 _operator.indexOf -> Py_ssize_t
479
480 a: object
481 b: object
482 /
483
484 Return the first index of b in a.
485 [clinic start generated code]*/
486
487 static Py_ssize_t
488 _operator_indexOf_impl(PyObject *module, PyObject *a, PyObject *b)
489 /*[clinic end generated code: output=c6226d8e0fb60fa6 input=8be2e43b6a6fffe3]*/
490 {
491 return PySequence_Index(a, b);
492 }
493
494 /*[clinic input]
495 _operator.countOf = _operator.indexOf
496
497 Return the number of times b occurs in a.
498 [clinic start generated code]*/
499
500 static Py_ssize_t
501 _operator_countOf_impl(PyObject *module, PyObject *a, PyObject *b)
502 /*[clinic end generated code: output=9e1623197daf3382 input=0c3a2656add252db]*/
503 {
504 return PySequence_Count(a, b);
505 }
506
507 /*[clinic input]
508 _operator.getitem
509
510 a: object
511 b: object
512 /
513
514 Same as a[b].
515 [clinic start generated code]*/
516
517 static PyObject *
518 _operator_getitem_impl(PyObject *module, PyObject *a, PyObject *b)
519 /*[clinic end generated code: output=6c8d8101a676e594 input=6682797320e48845]*/
520 {
521 return PyObject_GetItem(a, b);
522 }
523
524 /*[clinic input]
525 _operator.setitem
526
527 a: object
528 b: object
529 c: object
530 /
531
532 Same as a[b] = c.
533 [clinic start generated code]*/
534
535 static PyObject *
536 _operator_setitem_impl(PyObject *module, PyObject *a, PyObject *b,
537 PyObject *c)
538 /*[clinic end generated code: output=1324f9061ae99e25 input=ceaf453c4d3a58df]*/
539 {
540 if (-1 == PyObject_SetItem(a, b, c))
541 return NULL;
542 Py_RETURN_NONE;
543 }
544
545 /*[clinic input]
546 _operator.delitem = _operator.getitem
547
548 Same as del a[b].
549 [clinic start generated code]*/
550
551 static PyObject *
552 _operator_delitem_impl(PyObject *module, PyObject *a, PyObject *b)
553 /*[clinic end generated code: output=db18f61506295799 input=991bec56a0d3ec7f]*/
554 {
555 if (-1 == PyObject_DelItem(a, b))
556 return NULL;
557 Py_RETURN_NONE;
558 }
559
560 /*[clinic input]
561 _operator.eq
562
563 a: object
564 b: object
565 /
566
567 Same as a == b.
568 [clinic start generated code]*/
569
570 static PyObject *
571 _operator_eq_impl(PyObject *module, PyObject *a, PyObject *b)
572 /*[clinic end generated code: output=8d7d46ed4135677c input=586fca687a95a83f]*/
573 {
574 return PyObject_RichCompare(a, b, Py_EQ);
575 }
576
577 /*[clinic input]
578 _operator.ne = _operator.eq
579
580 Same as a != b.
581 [clinic start generated code]*/
582
583 static PyObject *
584 _operator_ne_impl(PyObject *module, PyObject *a, PyObject *b)
585 /*[clinic end generated code: output=c99bd0c3a4c01297 input=5d88f23d35e9abac]*/
586 {
587 return PyObject_RichCompare(a, b, Py_NE);
588 }
589
590 /*[clinic input]
591 _operator.lt = _operator.eq
592
593 Same as a < b.
594 [clinic start generated code]*/
595
596 static PyObject *
597 _operator_lt_impl(PyObject *module, PyObject *a, PyObject *b)
598 /*[clinic end generated code: output=082d7c45c440e535 input=34a59ad6d39d3a2b]*/
599 {
600 return PyObject_RichCompare(a, b, Py_LT);
601 }
602
603 /*[clinic input]
604 _operator.le = _operator.eq
605
606 Same as a <= b.
607 [clinic start generated code]*/
608
609 static PyObject *
610 _operator_le_impl(PyObject *module, PyObject *a, PyObject *b)
611 /*[clinic end generated code: output=00970a2923d0ae17 input=b812a7860a0bef44]*/
612 {
613 return PyObject_RichCompare(a, b, Py_LE);
614 }
615
616 /*[clinic input]
617 _operator.gt = _operator.eq
618
619 Same as a > b.
620 [clinic start generated code]*/
621
622 static PyObject *
623 _operator_gt_impl(PyObject *module, PyObject *a, PyObject *b)
624 /*[clinic end generated code: output=8d373349ecf25641 input=9bdb45b995ada35b]*/
625 {
626 return PyObject_RichCompare(a, b, Py_GT);
627 }
628
629 /*[clinic input]
630 _operator.ge = _operator.eq
631
632 Same as a >= b.
633 [clinic start generated code]*/
634
635 static PyObject *
636 _operator_ge_impl(PyObject *module, PyObject *a, PyObject *b)
637 /*[clinic end generated code: output=7ce3882256d4b137 input=cf1dc4a5ca9c35f5]*/
638 {
639 return PyObject_RichCompare(a, b, Py_GE);
640 }
641
642 /*[clinic input]
643 _operator.pow = _operator.add
644
645 Same as a ** b.
646 [clinic start generated code]*/
647
648 static PyObject *
649 _operator_pow_impl(PyObject *module, PyObject *a, PyObject *b)
650 /*[clinic end generated code: output=09e668ad50036120 input=690b40f097ab1637]*/
651 {
652 return PyNumber_Power(a, b, Py_None);
653 }
654
655 /*[clinic input]
656 _operator.ipow = _operator.add
657
658 Same as a **= b.
659 [clinic start generated code]*/
660
661 static PyObject *
662 _operator_ipow_impl(PyObject *module, PyObject *a, PyObject *b)
663 /*[clinic end generated code: output=7189ff4d4367c808 input=f00623899d07499a]*/
664 {
665 return PyNumber_InPlacePower(a, b, Py_None);
666 }
667
668 /*[clinic input]
669 _operator.index
670
671 a: object
672 /
673
674 Same as a.__index__()
675 [clinic start generated code]*/
676
677 static PyObject *
678 _operator_index(PyObject *module, PyObject *a)
679 /*[clinic end generated code: output=d972b0764ac305fc input=6f54d50ea64a579c]*/
134 { 680 {
135 return PyNumber_Index(a); 681 return PyNumber_Index(a);
136 } 682 }
137 683
138 static PyObject* 684 /*[clinic input]
139 is_(PyObject *s, PyObject *a) 685 _operator.is_ = _operator.add
140 { 686
141 PyObject *a1, *a2, *result = NULL; 687 Same as a is b.
142 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) { 688 [clinic start generated code]*/
143 result = (a1 == a2) ? Py_True : Py_False; 689
144 Py_INCREF(result); 690 static PyObject *
145 } 691 _operator_is__impl(PyObject *module, PyObject *a, PyObject *b)
692 /*[clinic end generated code: output=bcd47a402e482e1d input=5fa9b97df03c427f]*/
693 {
694 PyObject *result;
695 result = (a == b) ? Py_True : Py_False;
696 Py_INCREF(result);
146 return result; 697 return result;
147 } 698 }
148 699
149 static PyObject* 700 /*[clinic input]
150 is_not(PyObject *s, PyObject *a) 701 _operator.is_not = _operator.add
151 { 702
152 PyObject *a1, *a2, *result = NULL; 703 Same as a is not b.
153 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) { 704 [clinic start generated code]*/
154 result = (a1 != a2) ? Py_True : Py_False; 705
155 Py_INCREF(result); 706 static PyObject *
156 } 707 _operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b)
708 /*[clinic end generated code: output=491a1f2f81f6c7f9 input=5a93f7e1a93535f1]*/
709 {
710 PyObject *result;
711 result = (a != b) ? Py_True : Py_False;
712 Py_INCREF(result);
157 return result; 713 return result;
158 } 714 }
159
160 #undef spam1
161 #undef spam2
162 #undef spam1o
163 #undef spam1o
164 715
165 /* compare_digest **********************************************************/ 716 /* compare_digest **********************************************************/
166 717
167 /* 718 /*
168 * timing safe compare 719 * timing safe compare
169 * 720 *
170 * Returns 1 of the strings are equal. 721 * Returns 1 of the strings are equal.
171 * In case of len(a) != len(b) the function tries to keep the timing 722 * In case of len(a) != len(b) the function tries to keep the timing
172 * dependent on the length of b. CPU cache locally may still alter timing 723 * dependent on the length of b. CPU cache locally may still alter timing
173 * a bit. 724 * a bit.
(...skipping 29 matching lines...) Expand all
203 result = 1; 754 result = 1;
204 } 755 }
205 756
206 for (i=0; i < length; i++) { 757 for (i=0; i < length; i++) {
207 result |= *left++ ^ *right++; 758 result |= *left++ ^ *right++;
208 } 759 }
209 760
210 return (result == 0); 761 return (result == 0);
211 } 762 }
212 763
213 PyDoc_STRVAR(length_hint__doc__, 764 /*[clinic input]
214 "length_hint(obj, default=0) -> int\n" 765 _operator.length_hint -> Py_ssize_t
215 "Return an estimate of the number of items in obj.\n" 766
216 "This is useful for presizing containers when building from an\n" 767 obj: object
217 "iterable.\n" 768 default: Py_ssize_t = 0
218 "\n" 769 /
219 "If the object supports len(), the result will be\n" 770
220 "exact. Otherwise, it may over- or under-estimate by an\n" 771 Return an estimate of the number of items in obj.
221 "arbitrary amount. The result will be an integer >= 0."); 772
222 773 This is useful for presizing containers when building from an iterable.
223 static PyObject *length_hint(PyObject *self, PyObject *args) 774
224 { 775 If the object supports len(), the result will be exact.
225 PyObject *obj; 776 Otherwise, it may over- or under-estimate by an arbitrary amount.
226 Py_ssize_t defaultvalue = 0, res; 777 The result will be an integer >= 0.
227 if (!PyArg_ParseTuple(args, "O|n:length_hint", &obj, &defaultvalue)) { 778 [clinic start generated code]*/
228 return NULL; 779
229 } 780 static Py_ssize_t
230 res = PyObject_LengthHint(obj, defaultvalue); 781 _operator_length_hint_impl(PyObject *module, PyObject *obj,
231 if (res == -1 && PyErr_Occurred()) { 782 Py_ssize_t default_value)
232 return NULL; 783 /*[clinic end generated code: output=01d469edc1d612ad input=65ed29f04401e96a]*/
233 } 784 {
234 return PyLong_FromSsize_t(res); 785 return PyObject_LengthHint(obj, default_value);
235 } 786 }
236 787
237 788 /*[clinic input]
238 PyDoc_STRVAR(compare_digest__doc__, 789 _operator._compare_digest = _operator.eq
239 "compare_digest(a, b) -> bool\n" 790
240 "\n" 791 Return 'a == b'.
241 "Return 'a == b'. This function uses an approach designed to prevent\n" 792
242 "timing analysis, making it appropriate for cryptography.\n" 793 This function uses an approach designed to prevent
243 "a and b must both be of the same type: either str (ASCII only),\n" 794 timing analysis, making it appropriate for cryptography.
244 "or any bytes-like object.\n" 795
245 "\n" 796 a and b must both be of the same type: either str (ASCII only),
246 "Note: If a and b are of different lengths, or if an error occurs,\n" 797 or any bytes-like object.
247 "a timing attack could theoretically reveal information about the\n" 798
248 "types and lengths of a and b--but not their values.\n"); 799 Note: If a and b are of different lengths, or if an error occurs,
249 800 a timing attack could theoretically reveal information about the
250 static PyObject* 801 types and lengths of a and b--but not their values.
251 compare_digest(PyObject *self, PyObject *args) 802 [clinic start generated code]*/
252 { 803
253 PyObject *a, *b; 804 static PyObject *
805 _operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)
806 /*[clinic end generated code: output=11d452bdd3a23cbc input=9ac7e2c4e30bc356]*/
807 {
254 int rc; 808 int rc;
255
256 if (!PyArg_ParseTuple(args, "OO:compare_digest", &a, &b)) {
257 return NULL;
258 }
259 809
260 /* ASCII unicode string */ 810 /* ASCII unicode string */
261 if(PyUnicode_Check(a) && PyUnicode_Check(b)) { 811 if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
262 if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) { 812 if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
263 return NULL; 813 return NULL;
264 } 814 }
265 if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) { 815 if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
266 PyErr_SetString(PyExc_TypeError, 816 PyErr_SetString(PyExc_TypeError,
267 "comparing strings with non-ASCII characters is " 817 "comparing strings with non-ASCII characters is "
268 "not supported"); 818 "not supported");
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
316 866
317 PyBuffer_Release(&view_a); 867 PyBuffer_Release(&view_a);
318 PyBuffer_Release(&view_b); 868 PyBuffer_Release(&view_b);
319 } 869 }
320 870
321 return PyBool_FromLong(rc); 871 return PyBool_FromLong(rc);
322 } 872 }
323 873
324 /* operator methods **********************************************************/ 874 /* operator methods **********************************************************/
325 875
326 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
327 #define spam2(OP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
328 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
329 #define spam2o(OP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)},
330
331 static struct PyMethodDef operator_methods[] = { 876 static struct PyMethodDef operator_methods[] = {
332 877
333 spam1o(truth, 878 _OPERATOR_TRUTH_METHODDEF
334 "truth(a) -- Return True if a is true, False otherwise.") 879 _OPERATOR_CONTAINS_METHODDEF
335 spam2(contains, 880 _OPERATOR_INDEXOF_METHODDEF
336 "contains(a, b) -- Same as b in a (note reversed operands).") 881 _OPERATOR_COUNTOF_METHODDEF
337 spam1(indexOf, 882 _OPERATOR_IS__METHODDEF
338 "indexOf(a, b) -- Return the first index of b in a.") 883 _OPERATOR_IS_NOT_METHODDEF
339 spam1(countOf, 884 _OPERATOR_INDEX_METHODDEF
340 "countOf(a, b) -- Return the number of times b occurs in a.") 885 _OPERATOR_ADD_METHODDEF
341 886 _OPERATOR_SUB_METHODDEF
342 spam1(is_, "is_(a, b) -- Same as a is b.") 887 _OPERATOR_MUL_METHODDEF
343 spam1(is_not, "is_not(a, b) -- Same as a is not b.") 888 _OPERATOR_MATMUL_METHODDEF
344 spam2o(index, "index(a) -- Same as a.__index__()") 889 _OPERATOR_FLOORDIV_METHODDEF
345 spam2(add, "add(a, b) -- Same as a + b.") 890 _OPERATOR_TRUEDIV_METHODDEF
346 spam2(sub, "sub(a, b) -- Same as a - b.") 891 _OPERATOR_MOD_METHODDEF
347 spam2(mul, "mul(a, b) -- Same as a * b.") 892 _OPERATOR_NEG_METHODDEF
348 spam2(matmul, "matmul(a, b) -- Same as a @ b.") 893 _OPERATOR_POS_METHODDEF
349 spam2(floordiv, "floordiv(a, b) -- Same as a // b.") 894 _OPERATOR_ABS_METHODDEF
350 spam2(truediv, "truediv(a, b) -- Same as a / b.") 895 _OPERATOR_INV_METHODDEF
351 spam2(mod, "mod(a, b) -- Same as a % b.") 896 _OPERATOR_INVERT_METHODDEF
352 spam2o(neg, "neg(a) -- Same as -a.") 897 _OPERATOR_LSHIFT_METHODDEF
353 spam2o(pos, "pos(a) -- Same as +a.") 898 _OPERATOR_RSHIFT_METHODDEF
354 spam2o(abs, "abs(a) -- Same as abs(a).") 899 _OPERATOR_NOT__METHODDEF
355 spam2o(inv, "inv(a) -- Same as ~a.") 900 _OPERATOR_AND__METHODDEF
356 spam2o(invert, "invert(a) -- Same as ~a.") 901 _OPERATOR_XOR_METHODDEF
357 spam2(lshift, "lshift(a, b) -- Same as a << b.") 902 _OPERATOR_OR__METHODDEF
358 spam2(rshift, "rshift(a, b) -- Same as a >> b.") 903 _OPERATOR_IADD_METHODDEF
359 spam2o(not_, "not_(a) -- Same as not a.") 904 _OPERATOR_ISUB_METHODDEF
360 spam2(and_, "and_(a, b) -- Same as a & b.") 905 _OPERATOR_IMUL_METHODDEF
361 spam2(xor, "xor(a, b) -- Same as a ^ b.") 906 _OPERATOR_IMATMUL_METHODDEF
362 spam2(or_, "or_(a, b) -- Same as a | b.") 907 _OPERATOR_IFLOORDIV_METHODDEF
363 spam2(iadd, "a = iadd(a, b) -- Same as a += b.") 908 _OPERATOR_ITRUEDIV_METHODDEF
364 spam2(isub, "a = isub(a, b) -- Same as a -= b.") 909 _OPERATOR_IMOD_METHODDEF
365 spam2(imul, "a = imul(a, b) -- Same as a *= b.") 910 _OPERATOR_ILSHIFT_METHODDEF
366 spam2(imatmul, "a = imatmul(a, b) -- Same as a @= b.") 911 _OPERATOR_IRSHIFT_METHODDEF
367 spam2(ifloordiv, "a = ifloordiv(a, b) -- Same as a //= b.") 912 _OPERATOR_IAND_METHODDEF
368 spam2(itruediv, "a = itruediv(a, b) -- Same as a /= b") 913 _OPERATOR_IXOR_METHODDEF
369 spam2(imod, "a = imod(a, b) -- Same as a %= b.") 914 _OPERATOR_IOR_METHODDEF
370 spam2(ilshift, "a = ilshift(a, b) -- Same as a <<= b.") 915 _OPERATOR_CONCAT_METHODDEF
371 spam2(irshift, "a = irshift(a, b) -- Same as a >>= b.") 916 _OPERATOR_ICONCAT_METHODDEF
372 spam2(iand, "a = iand(a, b) -- Same as a &= b.") 917 _OPERATOR_GETITEM_METHODDEF
373 spam2(ixor, "a = ixor(a, b) -- Same as a ^= b.") 918 _OPERATOR_SETITEM_METHODDEF
374 spam2(ior, "a = ior(a, b) -- Same as a |= b.") 919 _OPERATOR_DELITEM_METHODDEF
375 spam2(concat, 920 _OPERATOR_POW_METHODDEF
376 "concat(a, b) -- Same as a + b, for a and b sequences.") 921 _OPERATOR_IPOW_METHODDEF
377 spam2(iconcat, 922 _OPERATOR_EQ_METHODDEF
378 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.") 923 _OPERATOR_NE_METHODDEF
379 spam2(getitem, 924 _OPERATOR_LT_METHODDEF
380 "getitem(a, b) -- Same as a[b].") 925 _OPERATOR_LE_METHODDEF
381 spam2(setitem, 926 _OPERATOR_GT_METHODDEF
382 "setitem(a, b, c) -- Same as a[b] = c.") 927 _OPERATOR_GE_METHODDEF
383 spam2(delitem, 928 _OPERATOR__COMPARE_DIGEST_METHODDEF
384 "delitem(a, b) -- Same as del a[b].") 929 _OPERATOR_LENGTH_HINT_METHODDEF
385 spam2(pow, "pow(a, b) -- Same as a ** b.")
386 spam2(ipow, "a = ipow(a, b) -- Same as a **= b.")
387 spam2(lt, "lt(a, b) -- Same as a<b.")
388 spam2(le, "le(a, b) -- Same as a<=b.")
389 spam2(eq, "eq(a, b) -- Same as a==b.")
390 spam2(ne, "ne(a, b) -- Same as a!=b.")
391 spam2(gt, "gt(a, b) -- Same as a>b.")
392 spam2(ge, "ge(a, b) -- Same as a>=b.")
393
394 {"_compare_digest", (PyCFunction)compare_digest, METH_VARARGS,
395 compare_digest__doc__},
396 {"length_hint", (PyCFunction)length_hint, METH_VARARGS,
397 length_hint__doc__},
398 {NULL, NULL} /* sentinel */ 930 {NULL, NULL} /* sentinel */
399 931
400 }; 932 };
401 933
402 /* itemgetter object **********************************************************/ 934 /* itemgetter object **********************************************************/
403 935
404 typedef struct { 936 typedef struct {
405 PyObject_HEAD 937 PyObject_HEAD
406 Py_ssize_t nitems; 938 Py_ssize_t nitems;
407 PyObject *item; 939 PyObject *item;
408 } itemgetterobject; 940 } itemgetterobject;
409 941
410 static PyTypeObject itemgetter_type; 942 static PyTypeObject itemgetter_type;
411 943
944 /* AC 3.5: treats first argument as an iterable, otherwise uses *args */
412 static PyObject * 945 static PyObject *
413 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 946 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
414 { 947 {
415 itemgetterobject *ig; 948 itemgetterobject *ig;
416 PyObject *item; 949 PyObject *item;
417 Py_ssize_t nitems; 950 Py_ssize_t nitems;
418 951
419 if (!_PyArg_NoKeywords("itemgetter()", kwds)) 952 if (!_PyArg_NoKeywords("itemgetter()", kwds))
420 return NULL; 953 return NULL;
421 954
(...skipping 160 matching lines...) Expand 10 before | Expand all | Expand 10 after
582 /* attrgetter object **********************************************************/ 1115 /* attrgetter object **********************************************************/
583 1116
584 typedef struct { 1117 typedef struct {
585 PyObject_HEAD 1118 PyObject_HEAD
586 Py_ssize_t nattrs; 1119 Py_ssize_t nattrs;
587 PyObject *attr; 1120 PyObject *attr;
588 } attrgetterobject; 1121 } attrgetterobject;
589 1122
590 static PyTypeObject attrgetter_type; 1123 static PyTypeObject attrgetter_type;
591 1124
1125 /* AC 3.5: treats first argument as an iterable, otherwise uses *args */
592 static PyObject * 1126 static PyObject *
593 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 1127 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
594 { 1128 {
595 attrgetterobject *ag; 1129 attrgetterobject *ag;
596 PyObject *attr; 1130 PyObject *attr;
597 Py_ssize_t nattrs, idx, char_idx; 1131 Py_ssize_t nattrs, idx, char_idx;
598 1132
599 if (!_PyArg_NoKeywords("attrgetter()", kwds)) 1133 if (!_PyArg_NoKeywords("attrgetter()", kwds))
600 return NULL; 1134 return NULL;
601 1135
(...skipping 332 matching lines...) Expand 10 before | Expand all | Expand 10 after
934 1468
935 typedef struct { 1469 typedef struct {
936 PyObject_HEAD 1470 PyObject_HEAD
937 PyObject *name; 1471 PyObject *name;
938 PyObject *args; 1472 PyObject *args;
939 PyObject *kwds; 1473 PyObject *kwds;
940 } methodcallerobject; 1474 } methodcallerobject;
941 1475
942 static PyTypeObject methodcaller_type; 1476 static PyTypeObject methodcaller_type;
943 1477
1478 /* AC 3.5: variable number of arguments, not currently support by AC */
944 static PyObject * 1479 static PyObject *
945 methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) 1480 methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
946 { 1481 {
947 methodcallerobject *mc; 1482 methodcallerobject *mc;
948 PyObject *name; 1483 PyObject *name;
949 1484
950 if (PyTuple_GET_SIZE(args) < 1) { 1485 if (PyTuple_GET_SIZE(args) < 1) {
951 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least " 1486 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
952 "one argument, the method name"); 1487 "one argument, the method name");
953 return NULL; 1488 return NULL;
(...skipping 275 matching lines...) Expand 10 before | Expand all | Expand 10 after
1229 return NULL; 1764 return NULL;
1230 Py_INCREF(&attrgetter_type); 1765 Py_INCREF(&attrgetter_type);
1231 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type); 1766 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
1232 1767
1233 if (PyType_Ready(&methodcaller_type) < 0) 1768 if (PyType_Ready(&methodcaller_type) < 0)
1234 return NULL; 1769 return NULL;
1235 Py_INCREF(&methodcaller_type); 1770 Py_INCREF(&methodcaller_type);
1236 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type); 1771 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
1237 return m; 1772 return m;
1238 } 1773 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+