/src/cpython/Modules/_operator.c
Line | Count | Source |
1 | | #include "Python.h" |
2 | | #include "pycore_modsupport.h" // _PyArg_NoKwnames() |
3 | | #include "pycore_moduleobject.h" // _PyModule_GetState() |
4 | | #include "pycore_pystate.h" // _PyInterpreterState_GET() |
5 | | #include "pycore_tuple.h" // _PyTuple_ITEMS() |
6 | | #include "pycore_typeobject.h" // _PyType_GetModuleState() |
7 | | #include "pycore_unicodeobject.h" // _PyUnicode_InternMortal() |
8 | | |
9 | | |
10 | | #include "clinic/_operator.c.h" |
11 | | |
12 | | typedef struct { |
13 | | PyObject *itemgetter_type; |
14 | | PyObject *attrgetter_type; |
15 | | PyObject *methodcaller_type; |
16 | | } _operator_state; |
17 | | |
18 | | static inline _operator_state* |
19 | | get_operator_state(PyObject *module) |
20 | 2.02k | { |
21 | 2.02k | void *state = _PyModule_GetState(module); |
22 | 2.02k | assert(state != NULL); |
23 | 2.02k | return (_operator_state *)state; |
24 | 2.02k | } |
25 | | |
26 | | /*[clinic input] |
27 | | module _operator |
28 | | [clinic start generated code]*/ |
29 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=672ecf48487521e7]*/ |
30 | | |
31 | | /*[clinic input] |
32 | | class _operator.itemgetter "itemgetterobject *" "type" |
33 | | [clinic start generated code]*/ |
34 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=419d278df02ea948]*/ |
35 | | |
36 | | /*[clinic input] |
37 | | class _operator.attrgetter "attrgetterobject *" "type" |
38 | | [clinic start generated code]*/ |
39 | | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=c6efaf5380908852]*/ |
40 | | |
41 | | PyDoc_STRVAR(operator_doc, |
42 | | "Operator interface.\n\ |
43 | | \n\ |
44 | | This module exports a set of functions implemented in C corresponding\n\ |
45 | | to the intrinsic operators of Python. For example, operator.add(x, y)\n\ |
46 | | is equivalent to the expression x+y. The function names are those\n\ |
47 | | used for special methods; variants without leading and trailing\n\ |
48 | | '__' are also provided for convenience."); |
49 | | |
50 | | |
51 | | /*[clinic input] |
52 | | _operator.truth -> bool |
53 | | |
54 | | a: object |
55 | | / |
56 | | |
57 | | Return True if a is true, False otherwise. |
58 | | [clinic start generated code]*/ |
59 | | |
60 | | static int |
61 | | _operator_truth_impl(PyObject *module, PyObject *a) |
62 | | /*[clinic end generated code: output=eaf87767234fa5d7 input=bc74a4cd90235875]*/ |
63 | 0 | { |
64 | 0 | return PyObject_IsTrue(a); |
65 | 0 | } |
66 | | |
67 | | /*[clinic input] |
68 | | _operator.add |
69 | | |
70 | | a: object |
71 | | b: object |
72 | | / |
73 | | |
74 | | Same as a + b. |
75 | | [clinic start generated code]*/ |
76 | | |
77 | | static PyObject * |
78 | | _operator_add_impl(PyObject *module, PyObject *a, PyObject *b) |
79 | | /*[clinic end generated code: output=8292984204f45164 input=5efe3bff856ac215]*/ |
80 | 0 | { |
81 | 0 | return PyNumber_Add(a, b); |
82 | 0 | } |
83 | | |
84 | | /*[clinic input] |
85 | | _operator.sub = _operator.add |
86 | | |
87 | | Same as a - b. |
88 | | [clinic start generated code]*/ |
89 | | |
90 | | static PyObject * |
91 | | _operator_sub_impl(PyObject *module, PyObject *a, PyObject *b) |
92 | | /*[clinic end generated code: output=4adfc3b888c1ee2e input=6494c6b100b8e795]*/ |
93 | 0 | { |
94 | 0 | return PyNumber_Subtract(a, b); |
95 | 0 | } |
96 | | |
97 | | /*[clinic input] |
98 | | _operator.mul = _operator.add |
99 | | |
100 | | Same as a * b. |
101 | | [clinic start generated code]*/ |
102 | | |
103 | | static PyObject * |
104 | | _operator_mul_impl(PyObject *module, PyObject *a, PyObject *b) |
105 | | /*[clinic end generated code: output=d24d66f55a01944c input=2368615b4358b70d]*/ |
106 | 0 | { |
107 | 0 | return PyNumber_Multiply(a, b); |
108 | 0 | } |
109 | | |
110 | | /*[clinic input] |
111 | | _operator.matmul = _operator.add |
112 | | |
113 | | Same as a @ b. |
114 | | [clinic start generated code]*/ |
115 | | |
116 | | static PyObject * |
117 | | _operator_matmul_impl(PyObject *module, PyObject *a, PyObject *b) |
118 | | /*[clinic end generated code: output=a20d917eb35d0101 input=9ab304e37fb42dd4]*/ |
119 | 0 | { |
120 | 0 | return PyNumber_MatrixMultiply(a, b); |
121 | 0 | } |
122 | | |
123 | | /*[clinic input] |
124 | | _operator.floordiv = _operator.add |
125 | | |
126 | | Same as a // b. |
127 | | [clinic start generated code]*/ |
128 | | |
129 | | static PyObject * |
130 | | _operator_floordiv_impl(PyObject *module, PyObject *a, PyObject *b) |
131 | | /*[clinic end generated code: output=df26b71a60589f99 input=bb2e88ba446c612c]*/ |
132 | 0 | { |
133 | 0 | return PyNumber_FloorDivide(a, b); |
134 | 0 | } |
135 | | |
136 | | /*[clinic input] |
137 | | _operator.truediv = _operator.add |
138 | | |
139 | | Same as a / b. |
140 | | [clinic start generated code]*/ |
141 | | |
142 | | static PyObject * |
143 | | _operator_truediv_impl(PyObject *module, PyObject *a, PyObject *b) |
144 | | /*[clinic end generated code: output=0e6a959944d77719 input=ecbb947673f4eb1f]*/ |
145 | 0 | { |
146 | 0 | return PyNumber_TrueDivide(a, b); |
147 | 0 | } |
148 | | |
149 | | /*[clinic input] |
150 | | _operator.mod = _operator.add |
151 | | |
152 | | Same as a % b. |
153 | | [clinic start generated code]*/ |
154 | | |
155 | | static PyObject * |
156 | | _operator_mod_impl(PyObject *module, PyObject *a, PyObject *b) |
157 | | /*[clinic end generated code: output=9519822f0bbec166 input=102e19b422342ac1]*/ |
158 | 0 | { |
159 | 0 | return PyNumber_Remainder(a, b); |
160 | 0 | } |
161 | | |
162 | | /*[clinic input] |
163 | | _operator.neg |
164 | | |
165 | | a: object |
166 | | / |
167 | | |
168 | | Same as -a. |
169 | | [clinic start generated code]*/ |
170 | | |
171 | | static PyObject * |
172 | | _operator_neg(PyObject *module, PyObject *a) |
173 | | /*[clinic end generated code: output=36e08ecfc6a1c08c input=84f09bdcf27c96ec]*/ |
174 | 0 | { |
175 | 0 | return PyNumber_Negative(a); |
176 | 0 | } |
177 | | |
178 | | /*[clinic input] |
179 | | _operator.pos = _operator.neg |
180 | | |
181 | | Same as +a. |
182 | | [clinic start generated code]*/ |
183 | | |
184 | | static PyObject * |
185 | | _operator_pos(PyObject *module, PyObject *a) |
186 | | /*[clinic end generated code: output=dad7a126221dd091 input=b6445b63fddb8772]*/ |
187 | 0 | { |
188 | 0 | return PyNumber_Positive(a); |
189 | 0 | } |
190 | | |
191 | | /*[clinic input] |
192 | | _operator.abs = _operator.neg |
193 | | |
194 | | Same as abs(a). |
195 | | [clinic start generated code]*/ |
196 | | |
197 | | static PyObject * |
198 | | _operator_abs(PyObject *module, PyObject *a) |
199 | | /*[clinic end generated code: output=1389a93ba053ea3e input=341d07ba86f58039]*/ |
200 | 0 | { |
201 | 0 | return PyNumber_Absolute(a); |
202 | 0 | } |
203 | | |
204 | | /*[clinic input] |
205 | | _operator.inv = _operator.neg |
206 | | |
207 | | Same as ~a. |
208 | | [clinic start generated code]*/ |
209 | | |
210 | | static PyObject * |
211 | | _operator_inv(PyObject *module, PyObject *a) |
212 | | /*[clinic end generated code: output=a56875ba075ee06d input=b01a4677739f6eb2]*/ |
213 | 0 | { |
214 | 0 | return PyNumber_Invert(a); |
215 | 0 | } |
216 | | |
217 | | /*[clinic input] |
218 | | _operator.invert = _operator.neg |
219 | | |
220 | | Same as ~a. |
221 | | [clinic start generated code]*/ |
222 | | |
223 | | static PyObject * |
224 | | _operator_invert(PyObject *module, PyObject *a) |
225 | | /*[clinic end generated code: output=406b5aa030545fcc input=7f2d607176672e55]*/ |
226 | 0 | { |
227 | 0 | return PyNumber_Invert(a); |
228 | 0 | } |
229 | | |
230 | | /*[clinic input] |
231 | | _operator.lshift = _operator.add |
232 | | |
233 | | Same as a << b. |
234 | | [clinic start generated code]*/ |
235 | | |
236 | | static PyObject * |
237 | | _operator_lshift_impl(PyObject *module, PyObject *a, PyObject *b) |
238 | | /*[clinic end generated code: output=37f7e52c41435bd8 input=746e8a160cbbc9eb]*/ |
239 | 0 | { |
240 | 0 | return PyNumber_Lshift(a, b); |
241 | 0 | } |
242 | | |
243 | | /*[clinic input] |
244 | | _operator.rshift = _operator.add |
245 | | |
246 | | Same as a >> b. |
247 | | [clinic start generated code]*/ |
248 | | |
249 | | static PyObject * |
250 | | _operator_rshift_impl(PyObject *module, PyObject *a, PyObject *b) |
251 | | /*[clinic end generated code: output=4593c7ef30ec2ee3 input=d2c85bb5a64504c2]*/ |
252 | 0 | { |
253 | 0 | return PyNumber_Rshift(a, b); |
254 | 0 | } |
255 | | |
256 | | /*[clinic input] |
257 | | _operator.not_ = _operator.truth |
258 | | |
259 | | Same as not a. |
260 | | [clinic start generated code]*/ |
261 | | |
262 | | static int |
263 | | _operator_not__impl(PyObject *module, PyObject *a) |
264 | | /*[clinic end generated code: output=743f9c24a09759ef input=854156d50804d9b8]*/ |
265 | 0 | { |
266 | 0 | return PyObject_Not(a); |
267 | 0 | } |
268 | | |
269 | | /*[clinic input] |
270 | | _operator.and_ = _operator.add |
271 | | |
272 | | Same as a & b. |
273 | | [clinic start generated code]*/ |
274 | | |
275 | | static PyObject * |
276 | | _operator_and__impl(PyObject *module, PyObject *a, PyObject *b) |
277 | | /*[clinic end generated code: output=93c4fe88f7b76d9e input=4f3057c90ec4c99f]*/ |
278 | 0 | { |
279 | 0 | return PyNumber_And(a, b); |
280 | 0 | } |
281 | | |
282 | | /*[clinic input] |
283 | | _operator.xor = _operator.add |
284 | | |
285 | | Same as a ^ b. |
286 | | [clinic start generated code]*/ |
287 | | |
288 | | static PyObject * |
289 | | _operator_xor_impl(PyObject *module, PyObject *a, PyObject *b) |
290 | | /*[clinic end generated code: output=b24cd8b79fde0004 input=3c5cfa7253d808dd]*/ |
291 | 0 | { |
292 | 0 | return PyNumber_Xor(a, b); |
293 | 0 | } |
294 | | |
295 | | /*[clinic input] |
296 | | _operator.or_ = _operator.add |
297 | | |
298 | | Same as a | b. |
299 | | [clinic start generated code]*/ |
300 | | |
301 | | static PyObject * |
302 | | _operator_or__impl(PyObject *module, PyObject *a, PyObject *b) |
303 | | /*[clinic end generated code: output=58024867b8d90461 input=b40c6c44f7c79c09]*/ |
304 | 0 | { |
305 | 0 | return PyNumber_Or(a, b); |
306 | 0 | } |
307 | | |
308 | | /*[clinic input] |
309 | | _operator.iadd = _operator.add |
310 | | |
311 | | Same as a += b. |
312 | | [clinic start generated code]*/ |
313 | | |
314 | | static PyObject * |
315 | | _operator_iadd_impl(PyObject *module, PyObject *a, PyObject *b) |
316 | | /*[clinic end generated code: output=07dc627832526eb5 input=d22a91c07ac69227]*/ |
317 | 0 | { |
318 | 0 | return PyNumber_InPlaceAdd(a, b); |
319 | 0 | } |
320 | | |
321 | | /*[clinic input] |
322 | | _operator.isub = _operator.add |
323 | | |
324 | | Same as a -= b. |
325 | | [clinic start generated code]*/ |
326 | | |
327 | | static PyObject * |
328 | | _operator_isub_impl(PyObject *module, PyObject *a, PyObject *b) |
329 | | /*[clinic end generated code: output=4513467d23b5e0b1 input=4591b00d0a0ccafd]*/ |
330 | 0 | { |
331 | 0 | return PyNumber_InPlaceSubtract(a, b); |
332 | 0 | } |
333 | | |
334 | | /*[clinic input] |
335 | | _operator.imul = _operator.add |
336 | | |
337 | | Same as a *= b. |
338 | | [clinic start generated code]*/ |
339 | | |
340 | | static PyObject * |
341 | | _operator_imul_impl(PyObject *module, PyObject *a, PyObject *b) |
342 | | /*[clinic end generated code: output=5e87dacd19a71eab input=0e01fb8631e1b76f]*/ |
343 | 0 | { |
344 | 0 | return PyNumber_InPlaceMultiply(a, b); |
345 | 0 | } |
346 | | |
347 | | /*[clinic input] |
348 | | _operator.imatmul = _operator.add |
349 | | |
350 | | Same as a @= b. |
351 | | [clinic start generated code]*/ |
352 | | |
353 | | static PyObject * |
354 | | _operator_imatmul_impl(PyObject *module, PyObject *a, PyObject *b) |
355 | | /*[clinic end generated code: output=d603cbdf716ce519 input=bb614026372cd542]*/ |
356 | 0 | { |
357 | 0 | return PyNumber_InPlaceMatrixMultiply(a, b); |
358 | 0 | } |
359 | | |
360 | | /*[clinic input] |
361 | | _operator.ifloordiv = _operator.add |
362 | | |
363 | | Same as a //= b. |
364 | | [clinic start generated code]*/ |
365 | | |
366 | | static PyObject * |
367 | | _operator_ifloordiv_impl(PyObject *module, PyObject *a, PyObject *b) |
368 | | /*[clinic end generated code: output=535336048c681794 input=9df3b5021cff4ca1]*/ |
369 | 0 | { |
370 | 0 | return PyNumber_InPlaceFloorDivide(a, b); |
371 | 0 | } |
372 | | |
373 | | /*[clinic input] |
374 | | _operator.itruediv = _operator.add |
375 | | |
376 | | Same as a /= b. |
377 | | [clinic start generated code]*/ |
378 | | |
379 | | static PyObject * |
380 | | _operator_itruediv_impl(PyObject *module, PyObject *a, PyObject *b) |
381 | | /*[clinic end generated code: output=28017fbd3563952f input=9a1ee01608f5f590]*/ |
382 | 0 | { |
383 | 0 | return PyNumber_InPlaceTrueDivide(a, b); |
384 | 0 | } |
385 | | |
386 | | /*[clinic input] |
387 | | _operator.imod = _operator.add |
388 | | |
389 | | Same as a %= b. |
390 | | [clinic start generated code]*/ |
391 | | |
392 | | static PyObject * |
393 | | _operator_imod_impl(PyObject *module, PyObject *a, PyObject *b) |
394 | | /*[clinic end generated code: output=f7c540ae0fc70904 input=d0c384a3ce38e1dd]*/ |
395 | 0 | { |
396 | 0 | return PyNumber_InPlaceRemainder(a, b); |
397 | 0 | } |
398 | | |
399 | | /*[clinic input] |
400 | | _operator.ilshift = _operator.add |
401 | | |
402 | | Same as a <<= b. |
403 | | [clinic start generated code]*/ |
404 | | |
405 | | static PyObject * |
406 | | _operator_ilshift_impl(PyObject *module, PyObject *a, PyObject *b) |
407 | | /*[clinic end generated code: output=e73a8fee1ac18749 input=e21b6b310f54572e]*/ |
408 | 0 | { |
409 | 0 | return PyNumber_InPlaceLshift(a, b); |
410 | 0 | } |
411 | | |
412 | | /*[clinic input] |
413 | | _operator.irshift = _operator.add |
414 | | |
415 | | Same as a >>= b. |
416 | | [clinic start generated code]*/ |
417 | | |
418 | | static PyObject * |
419 | | _operator_irshift_impl(PyObject *module, PyObject *a, PyObject *b) |
420 | | /*[clinic end generated code: output=97f2af6b5ff2ed81 input=6778dbd0f6e1ec16]*/ |
421 | 0 | { |
422 | 0 | return PyNumber_InPlaceRshift(a, b); |
423 | 0 | } |
424 | | |
425 | | /*[clinic input] |
426 | | _operator.iand = _operator.add |
427 | | |
428 | | Same as a &= b. |
429 | | [clinic start generated code]*/ |
430 | | |
431 | | static PyObject * |
432 | | _operator_iand_impl(PyObject *module, PyObject *a, PyObject *b) |
433 | | /*[clinic end generated code: output=4599e9d40cbf7d00 input=71dfd8e70c156a7b]*/ |
434 | 0 | { |
435 | 0 | return PyNumber_InPlaceAnd(a, b); |
436 | 0 | } |
437 | | |
438 | | /*[clinic input] |
439 | | _operator.ixor = _operator.add |
440 | | |
441 | | Same as a ^= b. |
442 | | [clinic start generated code]*/ |
443 | | |
444 | | static PyObject * |
445 | | _operator_ixor_impl(PyObject *module, PyObject *a, PyObject *b) |
446 | | /*[clinic end generated code: output=5ff881766872be03 input=695c32bec0604d86]*/ |
447 | 0 | { |
448 | 0 | return PyNumber_InPlaceXor(a, b); |
449 | 0 | } |
450 | | |
451 | | /*[clinic input] |
452 | | _operator.ior = _operator.add |
453 | | |
454 | | Same as a |= b. |
455 | | [clinic start generated code]*/ |
456 | | |
457 | | static PyObject * |
458 | | _operator_ior_impl(PyObject *module, PyObject *a, PyObject *b) |
459 | | /*[clinic end generated code: output=48aac319445bf759 input=8f01d03eda9920cf]*/ |
460 | 0 | { |
461 | 0 | return PyNumber_InPlaceOr(a, b); |
462 | 0 | } |
463 | | |
464 | | /*[clinic input] |
465 | | _operator.concat = _operator.add |
466 | | |
467 | | Same as a + b, for a and b sequences. |
468 | | [clinic start generated code]*/ |
469 | | |
470 | | static PyObject * |
471 | | _operator_concat_impl(PyObject *module, PyObject *a, PyObject *b) |
472 | | /*[clinic end generated code: output=80028390942c5f11 input=8544ccd5341a3658]*/ |
473 | 0 | { |
474 | 0 | return PySequence_Concat(a, b); |
475 | 0 | } |
476 | | |
477 | | /*[clinic input] |
478 | | _operator.iconcat = _operator.add |
479 | | |
480 | | Same as a += b, for a and b sequences. |
481 | | [clinic start generated code]*/ |
482 | | |
483 | | static PyObject * |
484 | | _operator_iconcat_impl(PyObject *module, PyObject *a, PyObject *b) |
485 | | /*[clinic end generated code: output=3ea0a162ebb2e26d input=8f5fe5722fcd837e]*/ |
486 | 0 | { |
487 | 0 | return PySequence_InPlaceConcat(a, b); |
488 | 0 | } |
489 | | |
490 | | /*[clinic input] |
491 | | _operator.contains -> bool |
492 | | |
493 | | a: object |
494 | | b: object |
495 | | / |
496 | | |
497 | | Same as b in a (note reversed operands). |
498 | | [clinic start generated code]*/ |
499 | | |
500 | | static int |
501 | | _operator_contains_impl(PyObject *module, PyObject *a, PyObject *b) |
502 | | /*[clinic end generated code: output=413b4dbe82b6ffc1 input=9122a69b505fde13]*/ |
503 | 0 | { |
504 | 0 | return PySequence_Contains(a, b); |
505 | 0 | } |
506 | | |
507 | | /*[clinic input] |
508 | | _operator.indexOf -> Py_ssize_t |
509 | | |
510 | | a: object |
511 | | b: object |
512 | | / |
513 | | |
514 | | Return the first index of b in a. |
515 | | [clinic start generated code]*/ |
516 | | |
517 | | static Py_ssize_t |
518 | | _operator_indexOf_impl(PyObject *module, PyObject *a, PyObject *b) |
519 | | /*[clinic end generated code: output=c6226d8e0fb60fa6 input=8be2e43b6a6fffe3]*/ |
520 | 0 | { |
521 | 0 | return PySequence_Index(a, b); |
522 | 0 | } |
523 | | |
524 | | /*[clinic input] |
525 | | _operator.countOf = _operator.indexOf |
526 | | |
527 | | Return the number of items in a which are, or which equal, b. |
528 | | [clinic start generated code]*/ |
529 | | |
530 | | static Py_ssize_t |
531 | | _operator_countOf_impl(PyObject *module, PyObject *a, PyObject *b) |
532 | | /*[clinic end generated code: output=9e1623197daf3382 input=93ea57f170f3f0bb]*/ |
533 | 0 | { |
534 | 0 | return PySequence_Count(a, b); |
535 | 0 | } |
536 | | |
537 | | /*[clinic input] |
538 | | _operator.getitem |
539 | | |
540 | | a: object |
541 | | b: object |
542 | | / |
543 | | |
544 | | Same as a[b]. |
545 | | [clinic start generated code]*/ |
546 | | |
547 | | static PyObject * |
548 | | _operator_getitem_impl(PyObject *module, PyObject *a, PyObject *b) |
549 | | /*[clinic end generated code: output=6c8d8101a676e594 input=6682797320e48845]*/ |
550 | 0 | { |
551 | 0 | return PyObject_GetItem(a, b); |
552 | 0 | } |
553 | | |
554 | | /*[clinic input] |
555 | | _operator.setitem |
556 | | |
557 | | a: object |
558 | | b: object |
559 | | c: object |
560 | | / |
561 | | |
562 | | Same as a[b] = c. |
563 | | [clinic start generated code]*/ |
564 | | |
565 | | static PyObject * |
566 | | _operator_setitem_impl(PyObject *module, PyObject *a, PyObject *b, |
567 | | PyObject *c) |
568 | | /*[clinic end generated code: output=1324f9061ae99e25 input=ceaf453c4d3a58df]*/ |
569 | 0 | { |
570 | 0 | if (-1 == PyObject_SetItem(a, b, c)) |
571 | 0 | return NULL; |
572 | 0 | Py_RETURN_NONE; |
573 | 0 | } |
574 | | |
575 | | /*[clinic input] |
576 | | _operator.delitem = _operator.getitem |
577 | | |
578 | | Same as del a[b]. |
579 | | [clinic start generated code]*/ |
580 | | |
581 | | static PyObject * |
582 | | _operator_delitem_impl(PyObject *module, PyObject *a, PyObject *b) |
583 | | /*[clinic end generated code: output=db18f61506295799 input=991bec56a0d3ec7f]*/ |
584 | 0 | { |
585 | 0 | if (-1 == PyObject_DelItem(a, b)) |
586 | 0 | return NULL; |
587 | 0 | Py_RETURN_NONE; |
588 | 0 | } |
589 | | |
590 | | /*[clinic input] |
591 | | _operator.eq |
592 | | |
593 | | a: object |
594 | | b: object |
595 | | / |
596 | | |
597 | | Same as a == b. |
598 | | [clinic start generated code]*/ |
599 | | |
600 | | static PyObject * |
601 | | _operator_eq_impl(PyObject *module, PyObject *a, PyObject *b) |
602 | | /*[clinic end generated code: output=8d7d46ed4135677c input=586fca687a95a83f]*/ |
603 | 16 | { |
604 | 16 | return PyObject_RichCompare(a, b, Py_EQ); |
605 | 16 | } |
606 | | |
607 | | /*[clinic input] |
608 | | _operator.ne = _operator.eq |
609 | | |
610 | | Same as a != b. |
611 | | [clinic start generated code]*/ |
612 | | |
613 | | static PyObject * |
614 | | _operator_ne_impl(PyObject *module, PyObject *a, PyObject *b) |
615 | | /*[clinic end generated code: output=c99bd0c3a4c01297 input=5d88f23d35e9abac]*/ |
616 | 0 | { |
617 | 0 | return PyObject_RichCompare(a, b, Py_NE); |
618 | 0 | } |
619 | | |
620 | | /*[clinic input] |
621 | | _operator.lt = _operator.eq |
622 | | |
623 | | Same as a < b. |
624 | | [clinic start generated code]*/ |
625 | | |
626 | | static PyObject * |
627 | | _operator_lt_impl(PyObject *module, PyObject *a, PyObject *b) |
628 | | /*[clinic end generated code: output=082d7c45c440e535 input=34a59ad6d39d3a2b]*/ |
629 | 0 | { |
630 | 0 | return PyObject_RichCompare(a, b, Py_LT); |
631 | 0 | } |
632 | | |
633 | | /*[clinic input] |
634 | | _operator.le = _operator.eq |
635 | | |
636 | | Same as a <= b. |
637 | | [clinic start generated code]*/ |
638 | | |
639 | | static PyObject * |
640 | | _operator_le_impl(PyObject *module, PyObject *a, PyObject *b) |
641 | | /*[clinic end generated code: output=00970a2923d0ae17 input=b812a7860a0bef44]*/ |
642 | 0 | { |
643 | 0 | return PyObject_RichCompare(a, b, Py_LE); |
644 | 0 | } |
645 | | |
646 | | /*[clinic input] |
647 | | _operator.gt = _operator.eq |
648 | | |
649 | | Same as a > b. |
650 | | [clinic start generated code]*/ |
651 | | |
652 | | static PyObject * |
653 | | _operator_gt_impl(PyObject *module, PyObject *a, PyObject *b) |
654 | | /*[clinic end generated code: output=8d373349ecf25641 input=9bdb45b995ada35b]*/ |
655 | 0 | { |
656 | 0 | return PyObject_RichCompare(a, b, Py_GT); |
657 | 0 | } |
658 | | |
659 | | /*[clinic input] |
660 | | _operator.ge = _operator.eq |
661 | | |
662 | | Same as a >= b. |
663 | | [clinic start generated code]*/ |
664 | | |
665 | | static PyObject * |
666 | | _operator_ge_impl(PyObject *module, PyObject *a, PyObject *b) |
667 | | /*[clinic end generated code: output=7ce3882256d4b137 input=cf1dc4a5ca9c35f5]*/ |
668 | 0 | { |
669 | 0 | return PyObject_RichCompare(a, b, Py_GE); |
670 | 0 | } |
671 | | |
672 | | /*[clinic input] |
673 | | _operator.pow = _operator.add |
674 | | |
675 | | Same as a ** b. |
676 | | [clinic start generated code]*/ |
677 | | |
678 | | static PyObject * |
679 | | _operator_pow_impl(PyObject *module, PyObject *a, PyObject *b) |
680 | | /*[clinic end generated code: output=09e668ad50036120 input=690b40f097ab1637]*/ |
681 | 0 | { |
682 | 0 | return PyNumber_Power(a, b, Py_None); |
683 | 0 | } |
684 | | |
685 | | /*[clinic input] |
686 | | _operator.ipow = _operator.add |
687 | | |
688 | | Same as a **= b. |
689 | | [clinic start generated code]*/ |
690 | | |
691 | | static PyObject * |
692 | | _operator_ipow_impl(PyObject *module, PyObject *a, PyObject *b) |
693 | | /*[clinic end generated code: output=7189ff4d4367c808 input=f00623899d07499a]*/ |
694 | 0 | { |
695 | 0 | return PyNumber_InPlacePower(a, b, Py_None); |
696 | 0 | } |
697 | | |
698 | | /*[clinic input] |
699 | | _operator.index |
700 | | |
701 | | a: object |
702 | | / |
703 | | |
704 | | Same as a.__index__() |
705 | | [clinic start generated code]*/ |
706 | | |
707 | | static PyObject * |
708 | | _operator_index(PyObject *module, PyObject *a) |
709 | | /*[clinic end generated code: output=d972b0764ac305fc input=6f54d50ea64a579c]*/ |
710 | 422 | { |
711 | 422 | return PyNumber_Index(a); |
712 | 422 | } |
713 | | |
714 | | /*[clinic input] |
715 | | _operator.is_ = _operator.add |
716 | | |
717 | | Same as a is b. |
718 | | [clinic start generated code]*/ |
719 | | |
720 | | static PyObject * |
721 | | _operator_is__impl(PyObject *module, PyObject *a, PyObject *b) |
722 | | /*[clinic end generated code: output=bcd47a402e482e1d input=5fa9b97df03c427f]*/ |
723 | 0 | { |
724 | 0 | PyObject *result = Py_Is(a, b) ? Py_True : Py_False; |
725 | 0 | return Py_NewRef(result); |
726 | 0 | } |
727 | | |
728 | | /*[clinic input] |
729 | | _operator.is_not = _operator.add |
730 | | |
731 | | Same as a is not b. |
732 | | [clinic start generated code]*/ |
733 | | |
734 | | static PyObject * |
735 | | _operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b) |
736 | | /*[clinic end generated code: output=491a1f2f81f6c7f9 input=5a93f7e1a93535f1]*/ |
737 | 0 | { |
738 | 0 | PyObject *result; |
739 | 0 | result = (a != b) ? Py_True : Py_False; |
740 | 0 | return Py_NewRef(result); |
741 | 0 | } |
742 | | |
743 | | /*[clinic input] |
744 | | _operator.is_none = _operator.neg |
745 | | |
746 | | Same as a is None. |
747 | | [clinic start generated code]*/ |
748 | | |
749 | | static PyObject * |
750 | | _operator_is_none(PyObject *module, PyObject *a) |
751 | | /*[clinic end generated code: output=07159cc102261dec input=0448b38af7b8533d]*/ |
752 | 0 | { |
753 | 0 | PyObject *result = Py_IsNone(a) ? Py_True : Py_False; |
754 | 0 | return Py_NewRef(result); |
755 | 0 | } |
756 | | |
757 | | /*[clinic input] |
758 | | _operator.is_not_none = _operator.neg |
759 | | |
760 | | Same as a is not None. |
761 | | [clinic start generated code]*/ |
762 | | |
763 | | static PyObject * |
764 | | _operator_is_not_none(PyObject *module, PyObject *a) |
765 | | /*[clinic end generated code: output=b0168a51451d9140 input=7587f38ebac51688]*/ |
766 | 0 | { |
767 | 0 | PyObject *result = Py_IsNone(a) ? Py_False : Py_True; |
768 | 0 | return Py_NewRef(result); |
769 | 0 | } |
770 | | |
771 | | /* compare_digest **********************************************************/ |
772 | | |
773 | | /* |
774 | | * timing safe compare |
775 | | * |
776 | | * Returns 1 if the strings are equal. |
777 | | * In case of len(a) != len(b) the function tries to keep the timing |
778 | | * dependent on the length of b. CPU cache locality may still alter timing |
779 | | * a bit. |
780 | | */ |
781 | | static int |
782 | | _tscmp(const unsigned char *a, const unsigned char *b, |
783 | | Py_ssize_t len_a, Py_ssize_t len_b) |
784 | 0 | { |
785 | | /* The volatile type declarations make sure that the compiler has no |
786 | | * chance to optimize and fold the code in any way that may change |
787 | | * the timing. |
788 | | */ |
789 | 0 | volatile Py_ssize_t length; |
790 | 0 | volatile const unsigned char *left; |
791 | 0 | volatile const unsigned char *right; |
792 | 0 | Py_ssize_t i; |
793 | 0 | volatile unsigned char result; |
794 | | |
795 | | /* loop count depends on length of b */ |
796 | 0 | length = len_b; |
797 | 0 | left = NULL; |
798 | 0 | right = b; |
799 | | |
800 | | /* don't use else here to keep the amount of CPU instructions constant, |
801 | | * volatile forces re-evaluation |
802 | | * */ |
803 | 0 | if (len_a == length) { |
804 | 0 | left = *((volatile const unsigned char**)&a); |
805 | 0 | result = 0; |
806 | 0 | } |
807 | 0 | if (len_a != length) { |
808 | 0 | left = b; |
809 | 0 | result = 1; |
810 | 0 | } |
811 | |
|
812 | 0 | for (i=0; i < length; i++) { |
813 | 0 | result |= *left++ ^ *right++; |
814 | 0 | } |
815 | |
|
816 | 0 | return (result == 0); |
817 | 0 | } |
818 | | |
819 | | /*[clinic input] |
820 | | _operator.length_hint -> Py_ssize_t |
821 | | |
822 | | obj: object |
823 | | default: Py_ssize_t = 0 |
824 | | / |
825 | | |
826 | | Return an estimate of the number of items in obj. |
827 | | |
828 | | This is useful for presizing containers when building from an iterable. |
829 | | |
830 | | If the object supports len(), the result will be exact. |
831 | | Otherwise, it may over- or under-estimate by an arbitrary amount. |
832 | | The result will be an integer >= 0. |
833 | | [clinic start generated code]*/ |
834 | | |
835 | | static Py_ssize_t |
836 | | _operator_length_hint_impl(PyObject *module, PyObject *obj, |
837 | | Py_ssize_t default_value) |
838 | | /*[clinic end generated code: output=01d469edc1d612ad input=65ed29f04401e96a]*/ |
839 | 0 | { |
840 | 0 | return PyObject_LengthHint(obj, default_value); |
841 | 0 | } |
842 | | |
843 | | /* NOTE: Keep in sync with _hashopenssl.c implementation. */ |
844 | | |
845 | | /*[clinic input] |
846 | | _operator._compare_digest = _operator.eq |
847 | | |
848 | | Return 'a == b'. |
849 | | |
850 | | This function uses an approach designed to prevent |
851 | | timing analysis, making it appropriate for cryptography. |
852 | | |
853 | | a and b must both be of the same type: either str (ASCII only), |
854 | | or any bytes-like object. |
855 | | |
856 | | Note: If a and b are of different lengths, or if an error occurs, |
857 | | a timing attack could theoretically reveal information about the |
858 | | types and lengths of a and b--but not their values. |
859 | | [clinic start generated code]*/ |
860 | | |
861 | | static PyObject * |
862 | | _operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b) |
863 | | /*[clinic end generated code: output=11d452bdd3a23cbc input=9ac7e2c4e30bc356]*/ |
864 | 0 | { |
865 | 0 | int rc; |
866 | | |
867 | | /* ASCII unicode string */ |
868 | 0 | if(PyUnicode_Check(a) && PyUnicode_Check(b)) { |
869 | 0 | if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) { |
870 | 0 | PyErr_SetString(PyExc_TypeError, |
871 | 0 | "comparing strings with non-ASCII characters is " |
872 | 0 | "not supported"); |
873 | 0 | return NULL; |
874 | 0 | } |
875 | | |
876 | 0 | rc = _tscmp(PyUnicode_DATA(a), |
877 | 0 | PyUnicode_DATA(b), |
878 | 0 | PyUnicode_GET_LENGTH(a), |
879 | 0 | PyUnicode_GET_LENGTH(b)); |
880 | 0 | } |
881 | | /* fallback to buffer interface for bytes, bytearray and other */ |
882 | 0 | else { |
883 | 0 | Py_buffer view_a; |
884 | 0 | Py_buffer view_b; |
885 | |
|
886 | 0 | if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) { |
887 | 0 | PyErr_Format(PyExc_TypeError, |
888 | 0 | "unsupported operand types(s) or combination of types: " |
889 | 0 | "'%.100s' and '%.100s'", |
890 | 0 | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
891 | 0 | return NULL; |
892 | 0 | } |
893 | | |
894 | 0 | if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) { |
895 | 0 | return NULL; |
896 | 0 | } |
897 | 0 | if (view_a.ndim > 1) { |
898 | 0 | PyErr_SetString(PyExc_BufferError, |
899 | 0 | "Buffer must be single dimension"); |
900 | 0 | PyBuffer_Release(&view_a); |
901 | 0 | return NULL; |
902 | 0 | } |
903 | | |
904 | 0 | if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) { |
905 | 0 | PyBuffer_Release(&view_a); |
906 | 0 | return NULL; |
907 | 0 | } |
908 | 0 | if (view_b.ndim > 1) { |
909 | 0 | PyErr_SetString(PyExc_BufferError, |
910 | 0 | "Buffer must be single dimension"); |
911 | 0 | PyBuffer_Release(&view_a); |
912 | 0 | PyBuffer_Release(&view_b); |
913 | 0 | return NULL; |
914 | 0 | } |
915 | | |
916 | 0 | rc = _tscmp((const unsigned char*)view_a.buf, |
917 | 0 | (const unsigned char*)view_b.buf, |
918 | 0 | view_a.len, |
919 | 0 | view_b.len); |
920 | |
|
921 | 0 | PyBuffer_Release(&view_a); |
922 | 0 | PyBuffer_Release(&view_b); |
923 | 0 | } |
924 | | |
925 | 0 | return PyBool_FromLong(rc); |
926 | 0 | } |
927 | | |
928 | | PyDoc_STRVAR(_operator_call__doc__, |
929 | | "call($module, obj, /, *args, **kwargs)\n" |
930 | | "--\n" |
931 | | "\n" |
932 | | "Same as obj(*args, **kwargs)."); |
933 | | |
934 | | #define _OPERATOR_CALL_METHODDEF \ |
935 | | {"call", _PyCFunction_CAST(_operator_call), METH_FASTCALL | METH_KEYWORDS, _operator_call__doc__}, |
936 | | |
937 | | static PyObject * |
938 | | _operator_call(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
939 | 0 | { |
940 | 0 | if (!_PyArg_CheckPositional("call", nargs, 1, PY_SSIZE_T_MAX)) { |
941 | 0 | return NULL; |
942 | 0 | } |
943 | 0 | return PyObject_Vectorcall( |
944 | 0 | args[0], |
945 | 0 | &args[1], (PyVectorcall_NARGS(nargs) - 1) | PY_VECTORCALL_ARGUMENTS_OFFSET, |
946 | 0 | kwnames); |
947 | 0 | } |
948 | | |
949 | | /* operator methods **********************************************************/ |
950 | | |
951 | | static struct PyMethodDef operator_methods[] = { |
952 | | |
953 | | _OPERATOR_TRUTH_METHODDEF |
954 | | _OPERATOR_CONTAINS_METHODDEF |
955 | | _OPERATOR_INDEXOF_METHODDEF |
956 | | _OPERATOR_COUNTOF_METHODDEF |
957 | | _OPERATOR_IS__METHODDEF |
958 | | _OPERATOR_IS_NOT_METHODDEF |
959 | | _OPERATOR_IS_NONE_METHODDEF |
960 | | _OPERATOR_IS_NOT_NONE_METHODDEF |
961 | | _OPERATOR_INDEX_METHODDEF |
962 | | _OPERATOR_ADD_METHODDEF |
963 | | _OPERATOR_SUB_METHODDEF |
964 | | _OPERATOR_MUL_METHODDEF |
965 | | _OPERATOR_MATMUL_METHODDEF |
966 | | _OPERATOR_FLOORDIV_METHODDEF |
967 | | _OPERATOR_TRUEDIV_METHODDEF |
968 | | _OPERATOR_MOD_METHODDEF |
969 | | _OPERATOR_NEG_METHODDEF |
970 | | _OPERATOR_POS_METHODDEF |
971 | | _OPERATOR_ABS_METHODDEF |
972 | | _OPERATOR_INV_METHODDEF |
973 | | _OPERATOR_INVERT_METHODDEF |
974 | | _OPERATOR_LSHIFT_METHODDEF |
975 | | _OPERATOR_RSHIFT_METHODDEF |
976 | | _OPERATOR_NOT__METHODDEF |
977 | | _OPERATOR_AND__METHODDEF |
978 | | _OPERATOR_XOR_METHODDEF |
979 | | _OPERATOR_OR__METHODDEF |
980 | | _OPERATOR_IADD_METHODDEF |
981 | | _OPERATOR_ISUB_METHODDEF |
982 | | _OPERATOR_IMUL_METHODDEF |
983 | | _OPERATOR_IMATMUL_METHODDEF |
984 | | _OPERATOR_IFLOORDIV_METHODDEF |
985 | | _OPERATOR_ITRUEDIV_METHODDEF |
986 | | _OPERATOR_IMOD_METHODDEF |
987 | | _OPERATOR_ILSHIFT_METHODDEF |
988 | | _OPERATOR_IRSHIFT_METHODDEF |
989 | | _OPERATOR_IAND_METHODDEF |
990 | | _OPERATOR_IXOR_METHODDEF |
991 | | _OPERATOR_IOR_METHODDEF |
992 | | _OPERATOR_CONCAT_METHODDEF |
993 | | _OPERATOR_ICONCAT_METHODDEF |
994 | | _OPERATOR_GETITEM_METHODDEF |
995 | | _OPERATOR_SETITEM_METHODDEF |
996 | | _OPERATOR_DELITEM_METHODDEF |
997 | | _OPERATOR_POW_METHODDEF |
998 | | _OPERATOR_IPOW_METHODDEF |
999 | | _OPERATOR_EQ_METHODDEF |
1000 | | _OPERATOR_NE_METHODDEF |
1001 | | _OPERATOR_LT_METHODDEF |
1002 | | _OPERATOR_LE_METHODDEF |
1003 | | _OPERATOR_GT_METHODDEF |
1004 | | _OPERATOR_GE_METHODDEF |
1005 | | _OPERATOR__COMPARE_DIGEST_METHODDEF |
1006 | | _OPERATOR_LENGTH_HINT_METHODDEF |
1007 | | _OPERATOR_CALL_METHODDEF |
1008 | | {NULL, NULL} /* sentinel */ |
1009 | | |
1010 | | }; |
1011 | | |
1012 | | |
1013 | | static PyObject * |
1014 | | text_signature(PyObject *Py_UNUSED(self), void *Py_UNUSED(closure)) |
1015 | 0 | { |
1016 | 0 | return PyUnicode_FromString("(obj, /)"); |
1017 | 0 | } |
1018 | | |
1019 | | static PyGetSetDef common_getset[] = { |
1020 | | {"__text_signature__", text_signature, NULL}, |
1021 | | {NULL} |
1022 | | }; |
1023 | | |
1024 | | /* itemgetter object **********************************************************/ |
1025 | | |
1026 | | typedef struct { |
1027 | | PyObject_HEAD |
1028 | | Py_ssize_t nitems; |
1029 | | PyObject *item; |
1030 | | Py_ssize_t index; // -1 unless *item* is a single non-negative integer index |
1031 | | vectorcallfunc vectorcall; |
1032 | | } itemgetterobject; |
1033 | | |
1034 | 1.44M | #define itemgetterobject_CAST(op) ((itemgetterobject *)(op)) |
1035 | | |
1036 | | // Forward declarations |
1037 | | static PyObject * |
1038 | | itemgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *); |
1039 | | static PyObject * |
1040 | | itemgetter_call_impl(itemgetterobject *, PyObject *); |
1041 | | |
1042 | | /*[clinic input] |
1043 | | @vectorcall |
1044 | | @classmethod |
1045 | | _operator.itemgetter.__new__ as itemgetter_type_new |
1046 | | *items: array |
1047 | | [clinic start generated code]*/ |
1048 | | |
1049 | | static PyObject * |
1050 | | itemgetter_type_new_impl(PyTypeObject *type, PyObject * const *items, |
1051 | | Py_ssize_t items_length) |
1052 | | /*[clinic end generated code: output=b449203c8dbfacf8 input=af4aec330543c134]*/ |
1053 | 346k | { |
1054 | 346k | itemgetterobject *ig; |
1055 | 346k | PyObject *item; |
1056 | 346k | Py_ssize_t index; |
1057 | | |
1058 | 346k | if (items_length < 1) { |
1059 | 0 | PyErr_SetString(PyExc_TypeError, "itemgetter expected 1 argument, got 0"); |
1060 | 0 | return NULL; |
1061 | 0 | } |
1062 | 346k | if (items_length == 1) { |
1063 | 346k | item = Py_NewRef(items[0]); |
1064 | 346k | } |
1065 | 0 | else { |
1066 | 0 | item = PyTuple_FromArray(items, items_length); |
1067 | 0 | if (item == NULL) { |
1068 | 0 | return NULL; |
1069 | 0 | } |
1070 | 0 | } |
1071 | 346k | _operator_state *state = _PyType_GetModuleState(type); |
1072 | | /* create itemgetterobject structure */ |
1073 | 346k | ig = PyObject_GC_New(itemgetterobject, (PyTypeObject *) state->itemgetter_type); |
1074 | 346k | if (ig == NULL) { |
1075 | 0 | Py_DECREF(item); |
1076 | 0 | return NULL; |
1077 | 0 | } |
1078 | | |
1079 | 346k | ig->item = item; |
1080 | 346k | ig->nitems = items_length; |
1081 | 346k | ig->index = -1; |
1082 | 346k | if (PyLong_CheckExact(item)) { |
1083 | 346k | index = PyLong_AsSsize_t(item); |
1084 | 346k | if (index < 0) { |
1085 | | /* If we get here, then either the index conversion failed |
1086 | | * due to being out of range, or the index was a negative |
1087 | | * integer. Either way, we clear any possible exception |
1088 | | * and fall back to the slow path, where ig->index is -1. |
1089 | | */ |
1090 | 0 | PyErr_Clear(); |
1091 | 0 | } |
1092 | 346k | else { |
1093 | 346k | ig->index = index; |
1094 | 346k | } |
1095 | 346k | } |
1096 | | |
1097 | 346k | ig->vectorcall = itemgetter_vectorcall; |
1098 | 346k | PyObject_GC_Track(ig); |
1099 | 346k | return (PyObject *)ig; |
1100 | 346k | } |
1101 | | |
1102 | | static int |
1103 | | itemgetter_clear(PyObject *op) |
1104 | 346k | { |
1105 | 346k | itemgetterobject *ig = itemgetterobject_CAST(op); |
1106 | 346k | Py_CLEAR(ig->item); |
1107 | 346k | return 0; |
1108 | 346k | } |
1109 | | |
1110 | | static void |
1111 | | itemgetter_dealloc(PyObject *op) |
1112 | 346k | { |
1113 | 346k | PyTypeObject *tp = Py_TYPE(op); |
1114 | 346k | PyObject_GC_UnTrack(op); |
1115 | 346k | (void)itemgetter_clear(op); |
1116 | 346k | tp->tp_free(op); |
1117 | 346k | Py_DECREF(tp); |
1118 | 346k | } |
1119 | | |
1120 | | static int |
1121 | | itemgetter_traverse(PyObject *op, visitproc visit, void *arg) |
1122 | 184 | { |
1123 | 184 | itemgetterobject *ig = itemgetterobject_CAST(op); |
1124 | 184 | Py_VISIT(Py_TYPE(ig)); |
1125 | 184 | Py_VISIT(ig->item); |
1126 | 184 | return 0; |
1127 | 184 | } |
1128 | | |
1129 | | static PyObject * |
1130 | | itemgetter_call(PyObject *op, PyObject *args, PyObject *kw) |
1131 | 0 | { |
1132 | 0 | assert(PyTuple_CheckExact(args)); |
1133 | 0 | if (!_PyArg_NoKeywords("itemgetter", kw)) |
1134 | 0 | return NULL; |
1135 | 0 | if (!_PyArg_CheckPositional("itemgetter", PyTuple_GET_SIZE(args), 1, 1)) |
1136 | 0 | return NULL; |
1137 | 0 | itemgetterobject *ig = itemgetterobject_CAST(op); |
1138 | 0 | return itemgetter_call_impl(ig, PyTuple_GET_ITEM(args, 0)); |
1139 | 0 | } |
1140 | | |
1141 | | static PyObject * |
1142 | | itemgetter_vectorcall(PyObject *op, PyObject *const *args, |
1143 | | size_t nargsf, PyObject *kwnames) |
1144 | 1.09M | { |
1145 | 1.09M | if (!_PyArg_NoKwnames("itemgetter", kwnames)) { |
1146 | 0 | return NULL; |
1147 | 0 | } |
1148 | 1.09M | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
1149 | 1.09M | if (!_PyArg_CheckPositional("itemgetter", nargs, 1, 1)) { |
1150 | 0 | return NULL; |
1151 | 0 | } |
1152 | 1.09M | itemgetterobject *ig = itemgetterobject_CAST(op); |
1153 | 1.09M | return itemgetter_call_impl(ig, args[0]); |
1154 | 1.09M | } |
1155 | | |
1156 | | static PyObject * |
1157 | | itemgetter_call_impl(itemgetterobject *ig, PyObject *obj) |
1158 | 1.09M | { |
1159 | 1.09M | PyObject *result; |
1160 | 1.09M | Py_ssize_t i, nitems=ig->nitems; |
1161 | 1.09M | if (nitems == 1) { |
1162 | 1.09M | if (ig->index >= 0 |
1163 | 1.09M | && PyTuple_CheckExact(obj) |
1164 | 1.09M | && ig->index < PyTuple_GET_SIZE(obj)) |
1165 | 1.09M | { |
1166 | 1.09M | result = PyTuple_GET_ITEM(obj, ig->index); |
1167 | 1.09M | return Py_NewRef(result); |
1168 | 1.09M | } |
1169 | 0 | return PyObject_GetItem(obj, ig->item); |
1170 | 1.09M | } |
1171 | | |
1172 | 1.09M | assert(PyTuple_Check(ig->item)); |
1173 | 0 | assert(PyTuple_GET_SIZE(ig->item) == nitems); |
1174 | |
|
1175 | 0 | result = PyTuple_New(nitems); |
1176 | 0 | if (result == NULL) |
1177 | 0 | return NULL; |
1178 | | |
1179 | 0 | for (i=0 ; i < nitems ; i++) { |
1180 | 0 | PyObject *item, *val; |
1181 | 0 | item = PyTuple_GET_ITEM(ig->item, i); |
1182 | 0 | val = PyObject_GetItem(obj, item); |
1183 | 0 | if (val == NULL) { |
1184 | 0 | Py_DECREF(result); |
1185 | 0 | return NULL; |
1186 | 0 | } |
1187 | 0 | PyTuple_SET_ITEM(result, i, val); |
1188 | 0 | } |
1189 | 0 | return result; |
1190 | 0 | } |
1191 | | |
1192 | | static PyObject * |
1193 | | itemgetter_repr(PyObject *op) |
1194 | 0 | { |
1195 | 0 | PyObject *repr; |
1196 | 0 | const char *reprfmt; |
1197 | 0 | itemgetterobject *ig = itemgetterobject_CAST(op); |
1198 | |
|
1199 | 0 | int status = Py_ReprEnter(op); |
1200 | 0 | if (status != 0) { |
1201 | 0 | if (status < 0) |
1202 | 0 | return NULL; |
1203 | 0 | return PyUnicode_FromFormat("%s(...)", Py_TYPE(ig)->tp_name); |
1204 | 0 | } |
1205 | | |
1206 | 0 | reprfmt = ig->nitems == 1 ? "%s(%R)" : "%s%R"; |
1207 | 0 | repr = PyUnicode_FromFormat(reprfmt, Py_TYPE(ig)->tp_name, ig->item); |
1208 | 0 | Py_ReprLeave(op); |
1209 | 0 | return repr; |
1210 | 0 | } |
1211 | | |
1212 | | static PyObject * |
1213 | | itemgetter_reduce(PyObject *op, PyObject *Py_UNUSED(dummy)) |
1214 | 0 | { |
1215 | 0 | itemgetterobject *ig = itemgetterobject_CAST(op); |
1216 | 0 | if (ig->nitems == 1) |
1217 | 0 | return Py_BuildValue("O(O)", Py_TYPE(ig), ig->item); |
1218 | 0 | return _PyTuple_FromPair((PyObject *)Py_TYPE(ig), ig->item); |
1219 | 0 | } |
1220 | | |
1221 | | PyDoc_STRVAR(reduce_doc, "Return state information for pickling"); |
1222 | | |
1223 | | static PyMethodDef itemgetter_methods[] = { |
1224 | | {"__reduce__", itemgetter_reduce, METH_NOARGS, |
1225 | | reduce_doc}, |
1226 | | {NULL} |
1227 | | }; |
1228 | | |
1229 | | static PyMemberDef itemgetter_members[] = { |
1230 | | {"__vectorcalloffset__", Py_T_PYSSIZET, offsetof(itemgetterobject, vectorcall), Py_READONLY}, |
1231 | | {NULL} /* Sentinel */ |
1232 | | }; |
1233 | | |
1234 | | PyDoc_STRVAR(itemgetter_doc, |
1235 | | "itemgetter(item, /, *items)\n--\n\n\ |
1236 | | Return a callable object that fetches the given item(s) from its operand.\n\ |
1237 | | After f = itemgetter(2), the call f(r) returns r[2].\n\ |
1238 | | After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])"); |
1239 | | |
1240 | | static PyType_Slot itemgetter_type_slots[] = { |
1241 | | {Py_tp_doc, (void *)itemgetter_doc}, |
1242 | | {Py_tp_dealloc, itemgetter_dealloc}, |
1243 | | {Py_tp_call, itemgetter_call}, |
1244 | | {Py_tp_traverse, itemgetter_traverse}, |
1245 | | {Py_tp_clear, itemgetter_clear}, |
1246 | | {Py_tp_methods, itemgetter_methods}, |
1247 | | {Py_tp_members, itemgetter_members}, |
1248 | | {Py_tp_getset, common_getset}, |
1249 | | {Py_tp_new, itemgetter_type_new}, |
1250 | | {Py_tp_vectorcall, itemgetter_type_vectorcall}, |
1251 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
1252 | | {Py_tp_repr, itemgetter_repr}, |
1253 | | {0, 0} |
1254 | | }; |
1255 | | |
1256 | | static PyType_Spec itemgetter_type_spec = { |
1257 | | .name = "operator.itemgetter", |
1258 | | .basicsize = sizeof(itemgetterobject), |
1259 | | .itemsize = 0, |
1260 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
1261 | | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_VECTORCALL), |
1262 | | .slots = itemgetter_type_slots, |
1263 | | }; |
1264 | | |
1265 | | /* attrgetter object **********************************************************/ |
1266 | | |
1267 | | typedef struct { |
1268 | | PyObject_HEAD |
1269 | | Py_ssize_t nattrs; |
1270 | | PyObject *attr; |
1271 | | vectorcallfunc vectorcall; |
1272 | | } attrgetterobject; |
1273 | | |
1274 | 34 | #define attrgetterobject_CAST(op) ((attrgetterobject *)(op)) |
1275 | | |
1276 | | // Forward declarations |
1277 | | static PyObject * |
1278 | | attrgetter_vectorcall(PyObject *, PyObject *const *, size_t, PyObject *); |
1279 | | static PyObject * |
1280 | | attrgetter_call_impl(attrgetterobject *, PyObject *); |
1281 | | |
1282 | | /*[clinic input] |
1283 | | @vectorcall |
1284 | | @classmethod |
1285 | | _operator.attrgetter.__new__ as attrgetter_type_new |
1286 | | *attrs: array |
1287 | | [clinic start generated code]*/ |
1288 | | |
1289 | | static PyObject * |
1290 | | attrgetter_type_new_impl(PyTypeObject *type, PyObject * const *attrs, |
1291 | | Py_ssize_t attrs_length) |
1292 | | /*[clinic end generated code: output=5d014e36ea7aba23 input=b3a21a0100fbeba8]*/ |
1293 | 4 | { |
1294 | 4 | attrgetterobject *ag; |
1295 | 4 | PyObject *attr; |
1296 | 4 | Py_ssize_t idx, char_idx; |
1297 | | |
1298 | 4 | if (attrs_length < 1) { |
1299 | 0 | PyErr_SetString(PyExc_TypeError, "attrgetter expected 1 argument, got 0"); |
1300 | 0 | return NULL; |
1301 | 0 | } |
1302 | | |
1303 | 4 | attr = PyTuple_New(attrs_length); |
1304 | 4 | if (attr == NULL) |
1305 | 0 | return NULL; |
1306 | | |
1307 | | /* prepare attr while checking args */ |
1308 | 4 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
1309 | 8 | for (idx = 0; idx < attrs_length; ++idx) { |
1310 | 4 | PyObject *item = attrs[idx]; |
1311 | 4 | int dot_count; |
1312 | | |
1313 | 4 | if (!PyUnicode_Check(item)) { |
1314 | 0 | PyErr_SetString(PyExc_TypeError, |
1315 | 0 | "attribute name must be a string"); |
1316 | 0 | Py_DECREF(attr); |
1317 | 0 | return NULL; |
1318 | 0 | } |
1319 | 4 | Py_ssize_t item_len = PyUnicode_GET_LENGTH(item); |
1320 | 4 | int kind = PyUnicode_KIND(item); |
1321 | 4 | const void *data = PyUnicode_DATA(item); |
1322 | | |
1323 | | /* check whether the string is dotted */ |
1324 | 4 | dot_count = 0; |
1325 | 68 | for (char_idx = 0; char_idx < item_len; ++char_idx) { |
1326 | 64 | if (PyUnicode_READ(kind, data, char_idx) == '.') |
1327 | 0 | ++dot_count; |
1328 | 64 | } |
1329 | | |
1330 | 4 | if (dot_count == 0) { |
1331 | 4 | Py_INCREF(item); |
1332 | 4 | _PyUnicode_InternMortal(interp, &item); |
1333 | 4 | PyTuple_SET_ITEM(attr, idx, item); |
1334 | 4 | } else { /* make it a tuple of non-dotted attrnames */ |
1335 | 0 | PyObject *attr_chain = PyTuple_New(dot_count + 1); |
1336 | 0 | PyObject *attr_chain_item; |
1337 | 0 | Py_ssize_t unibuff_from = 0; |
1338 | 0 | Py_ssize_t unibuff_till = 0; |
1339 | 0 | Py_ssize_t attr_chain_idx = 0; |
1340 | |
|
1341 | 0 | if (attr_chain == NULL) { |
1342 | 0 | Py_DECREF(attr); |
1343 | 0 | return NULL; |
1344 | 0 | } |
1345 | | |
1346 | 0 | for (; dot_count > 0; --dot_count) { |
1347 | 0 | while (PyUnicode_READ(kind, data, unibuff_till) != '.') { |
1348 | 0 | ++unibuff_till; |
1349 | 0 | } |
1350 | 0 | attr_chain_item = PyUnicode_Substring(item, |
1351 | 0 | unibuff_from, |
1352 | 0 | unibuff_till); |
1353 | 0 | if (attr_chain_item == NULL) { |
1354 | 0 | Py_DECREF(attr_chain); |
1355 | 0 | Py_DECREF(attr); |
1356 | 0 | return NULL; |
1357 | 0 | } |
1358 | 0 | _PyUnicode_InternMortal(interp, &attr_chain_item); |
1359 | 0 | PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item); |
1360 | 0 | ++attr_chain_idx; |
1361 | 0 | unibuff_till = unibuff_from = unibuff_till + 1; |
1362 | 0 | } |
1363 | | |
1364 | | /* now add the last dotless name */ |
1365 | 0 | attr_chain_item = PyUnicode_Substring(item, |
1366 | 0 | unibuff_from, item_len); |
1367 | 0 | if (attr_chain_item == NULL) { |
1368 | 0 | Py_DECREF(attr_chain); |
1369 | 0 | Py_DECREF(attr); |
1370 | 0 | return NULL; |
1371 | 0 | } |
1372 | 0 | _PyUnicode_InternMortal(interp, &attr_chain_item); |
1373 | 0 | PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item); |
1374 | |
|
1375 | 0 | PyTuple_SET_ITEM(attr, idx, attr_chain); |
1376 | 0 | } |
1377 | 4 | } |
1378 | | |
1379 | 4 | _operator_state *state = _PyType_GetModuleState(type); |
1380 | | /* create attrgetterobject structure */ |
1381 | 4 | ag = PyObject_GC_New(attrgetterobject, (PyTypeObject *)state->attrgetter_type); |
1382 | 4 | if (ag == NULL) { |
1383 | 0 | Py_DECREF(attr); |
1384 | 0 | return NULL; |
1385 | 0 | } |
1386 | | |
1387 | 4 | ag->attr = attr; |
1388 | 4 | ag->nattrs = attrs_length; |
1389 | 4 | ag->vectorcall = attrgetter_vectorcall; |
1390 | | |
1391 | 4 | PyObject_GC_Track(ag); |
1392 | 4 | return (PyObject *)ag; |
1393 | 4 | } |
1394 | | |
1395 | | static int |
1396 | | attrgetter_clear(PyObject *op) |
1397 | 0 | { |
1398 | 0 | attrgetterobject *ag = attrgetterobject_CAST(op); |
1399 | 0 | Py_CLEAR(ag->attr); |
1400 | 0 | return 0; |
1401 | 0 | } |
1402 | | |
1403 | | static void |
1404 | | attrgetter_dealloc(PyObject *op) |
1405 | 0 | { |
1406 | 0 | PyTypeObject *tp = Py_TYPE(op); |
1407 | 0 | PyObject_GC_UnTrack(op); |
1408 | 0 | (void)attrgetter_clear(op); |
1409 | 0 | tp->tp_free(op); |
1410 | 0 | Py_DECREF(tp); |
1411 | 0 | } |
1412 | | |
1413 | | static int |
1414 | | attrgetter_traverse(PyObject *op, visitproc visit, void *arg) |
1415 | 22 | { |
1416 | 22 | attrgetterobject *ag = attrgetterobject_CAST(op); |
1417 | 22 | Py_VISIT(ag->attr); |
1418 | 22 | Py_VISIT(Py_TYPE(ag)); |
1419 | 22 | return 0; |
1420 | 22 | } |
1421 | | |
1422 | | static PyObject * |
1423 | | dotted_getattr(PyObject *obj, PyObject *attr) |
1424 | 12 | { |
1425 | 12 | PyObject *newobj; |
1426 | | |
1427 | | /* attr is either a tuple or instance of str. |
1428 | | Ensured by the setup code of attrgetter_new */ |
1429 | 12 | if (PyTuple_CheckExact(attr)) { /* chained getattr */ |
1430 | 0 | Py_ssize_t name_idx = 0, name_count; |
1431 | 0 | PyObject *attr_name; |
1432 | |
|
1433 | 0 | name_count = PyTuple_GET_SIZE(attr); |
1434 | 0 | Py_INCREF(obj); |
1435 | 0 | for (name_idx = 0; name_idx < name_count; ++name_idx) { |
1436 | 0 | attr_name = PyTuple_GET_ITEM(attr, name_idx); |
1437 | 0 | newobj = PyObject_GetAttr(obj, attr_name); |
1438 | 0 | Py_DECREF(obj); |
1439 | 0 | if (newobj == NULL) { |
1440 | 0 | return NULL; |
1441 | 0 | } |
1442 | | /* here */ |
1443 | 0 | obj = newobj; |
1444 | 0 | } |
1445 | 12 | } else { /* single getattr */ |
1446 | 12 | newobj = PyObject_GetAttr(obj, attr); |
1447 | 12 | if (newobj == NULL) |
1448 | 0 | return NULL; |
1449 | 12 | obj = newobj; |
1450 | 12 | } |
1451 | | |
1452 | 12 | return obj; |
1453 | 12 | } |
1454 | | |
1455 | | static PyObject * |
1456 | | attrgetter_call(PyObject *op, PyObject *args, PyObject *kw) |
1457 | 0 | { |
1458 | 0 | if (!_PyArg_NoKeywords("attrgetter", kw)) |
1459 | 0 | return NULL; |
1460 | 0 | if (!_PyArg_CheckPositional("attrgetter", PyTuple_GET_SIZE(args), 1, 1)) |
1461 | 0 | return NULL; |
1462 | 0 | attrgetterobject *ag = attrgetterobject_CAST(op); |
1463 | 0 | return attrgetter_call_impl(ag, PyTuple_GET_ITEM(args, 0)); |
1464 | 0 | } |
1465 | | |
1466 | | static PyObject * |
1467 | | attrgetter_vectorcall(PyObject *op, PyObject *const *args, |
1468 | | size_t nargsf, PyObject *kwnames) |
1469 | 12 | { |
1470 | 12 | if (!_PyArg_NoKwnames("attrgetter", kwnames)) { |
1471 | 0 | return NULL; |
1472 | 0 | } |
1473 | 12 | Py_ssize_t nargs = PyVectorcall_NARGS(nargsf); |
1474 | 12 | if (!_PyArg_CheckPositional("attrgetter", nargs, 1, 1)) { |
1475 | 0 | return NULL; |
1476 | 0 | } |
1477 | 12 | attrgetterobject *ag = attrgetterobject_CAST(op); |
1478 | 12 | return attrgetter_call_impl(ag, args[0]); |
1479 | 12 | } |
1480 | | |
1481 | | static PyObject * |
1482 | | attrgetter_call_impl(attrgetterobject *ag, PyObject *obj) |
1483 | 12 | { |
1484 | 12 | PyObject *result; |
1485 | 12 | Py_ssize_t i, nattrs=ag->nattrs; |
1486 | | |
1487 | 12 | if (ag->nattrs == 1) { |
1488 | | /* ag->attr is always a tuple */ |
1489 | 12 | return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0)); |
1490 | 12 | } |
1491 | | |
1492 | 12 | assert(PyTuple_Check(ag->attr)); |
1493 | 0 | assert(PyTuple_GET_SIZE(ag->attr) == nattrs); |
1494 | |
|
1495 | 0 | result = PyTuple_New(nattrs); |
1496 | 0 | if (result == NULL) |
1497 | 0 | return NULL; |
1498 | | |
1499 | 0 | for (i=0 ; i < nattrs ; i++) { |
1500 | 0 | PyObject *attr, *val; |
1501 | 0 | attr = PyTuple_GET_ITEM(ag->attr, i); |
1502 | 0 | val = dotted_getattr(obj, attr); |
1503 | 0 | if (val == NULL) { |
1504 | 0 | Py_DECREF(result); |
1505 | 0 | return NULL; |
1506 | 0 | } |
1507 | 0 | PyTuple_SET_ITEM(result, i, val); |
1508 | 0 | } |
1509 | 0 | return result; |
1510 | 0 | } |
1511 | | |
1512 | | static PyObject * |
1513 | | dotjoinattr(PyObject *attr, PyObject **attrsep) |
1514 | 0 | { |
1515 | 0 | if (PyTuple_CheckExact(attr)) { |
1516 | 0 | if (*attrsep == NULL) { |
1517 | 0 | *attrsep = PyUnicode_FromString("."); |
1518 | 0 | if (*attrsep == NULL) |
1519 | 0 | return NULL; |
1520 | 0 | } |
1521 | 0 | return PyUnicode_Join(*attrsep, attr); |
1522 | 0 | } else { |
1523 | 0 | return Py_NewRef(attr); |
1524 | 0 | } |
1525 | 0 | } |
1526 | | |
1527 | | static PyObject * |
1528 | | attrgetter_args(attrgetterobject *ag) |
1529 | 0 | { |
1530 | 0 | Py_ssize_t i; |
1531 | 0 | PyObject *attrsep = NULL; |
1532 | 0 | PyObject *attrstrings = PyTuple_New(ag->nattrs); |
1533 | 0 | if (attrstrings == NULL) |
1534 | 0 | return NULL; |
1535 | | |
1536 | 0 | for (i = 0; i < ag->nattrs; ++i) { |
1537 | 0 | PyObject *attr = PyTuple_GET_ITEM(ag->attr, i); |
1538 | 0 | PyObject *attrstr = dotjoinattr(attr, &attrsep); |
1539 | 0 | if (attrstr == NULL) { |
1540 | 0 | Py_XDECREF(attrsep); |
1541 | 0 | Py_DECREF(attrstrings); |
1542 | 0 | return NULL; |
1543 | 0 | } |
1544 | 0 | PyTuple_SET_ITEM(attrstrings, i, attrstr); |
1545 | 0 | } |
1546 | 0 | Py_XDECREF(attrsep); |
1547 | 0 | return attrstrings; |
1548 | 0 | } |
1549 | | |
1550 | | static PyObject * |
1551 | | attrgetter_repr(PyObject *op) |
1552 | 0 | { |
1553 | 0 | PyObject *repr = NULL; |
1554 | 0 | attrgetterobject *ag = attrgetterobject_CAST(op); |
1555 | 0 | int status = Py_ReprEnter(op); |
1556 | 0 | if (status != 0) { |
1557 | 0 | if (status < 0) |
1558 | 0 | return NULL; |
1559 | 0 | return PyUnicode_FromFormat("%s(...)", Py_TYPE(ag)->tp_name); |
1560 | 0 | } |
1561 | | |
1562 | 0 | if (ag->nattrs == 1) { |
1563 | 0 | PyObject *attrsep = NULL; |
1564 | 0 | PyObject *attr = dotjoinattr(PyTuple_GET_ITEM(ag->attr, 0), &attrsep); |
1565 | 0 | if (attr != NULL) { |
1566 | 0 | repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(ag)->tp_name, attr); |
1567 | 0 | Py_DECREF(attr); |
1568 | 0 | } |
1569 | 0 | Py_XDECREF(attrsep); |
1570 | 0 | } |
1571 | 0 | else { |
1572 | 0 | PyObject *attrstrings = attrgetter_args(ag); |
1573 | 0 | if (attrstrings != NULL) { |
1574 | 0 | repr = PyUnicode_FromFormat("%s%R", |
1575 | 0 | Py_TYPE(ag)->tp_name, attrstrings); |
1576 | 0 | Py_DECREF(attrstrings); |
1577 | 0 | } |
1578 | 0 | } |
1579 | 0 | Py_ReprLeave(op); |
1580 | 0 | return repr; |
1581 | 0 | } |
1582 | | |
1583 | | static PyObject * |
1584 | | attrgetter_reduce(PyObject *op, PyObject *Py_UNUSED(dummy)) |
1585 | 0 | { |
1586 | 0 | attrgetterobject *ag = attrgetterobject_CAST(op); |
1587 | 0 | PyObject *attrstrings = attrgetter_args(ag); |
1588 | 0 | if (attrstrings == NULL) |
1589 | 0 | return NULL; |
1590 | 0 | return Py_BuildValue("ON", Py_TYPE(ag), attrstrings); |
1591 | 0 | } |
1592 | | |
1593 | | static PyMethodDef attrgetter_methods[] = { |
1594 | | {"__reduce__", attrgetter_reduce, METH_NOARGS, |
1595 | | reduce_doc}, |
1596 | | {NULL} |
1597 | | }; |
1598 | | |
1599 | | static PyMemberDef attrgetter_members[] = { |
1600 | | {"__vectorcalloffset__", Py_T_PYSSIZET, offsetof(attrgetterobject, vectorcall), Py_READONLY}, |
1601 | | {NULL} /* Sentinel*/ |
1602 | | }; |
1603 | | |
1604 | | PyDoc_STRVAR(attrgetter_doc, |
1605 | | "attrgetter(attr, /, *attrs)\n--\n\n\ |
1606 | | Return a callable object that fetches the given attribute(s) from its operand.\n\ |
1607 | | After f = attrgetter('name'), the call f(r) returns r.name.\n\ |
1608 | | After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\ |
1609 | | After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\ |
1610 | | (r.name.first, r.name.last)."); |
1611 | | |
1612 | | static PyType_Slot attrgetter_type_slots[] = { |
1613 | | {Py_tp_doc, (void *)attrgetter_doc}, |
1614 | | {Py_tp_dealloc, attrgetter_dealloc}, |
1615 | | {Py_tp_call, attrgetter_call}, |
1616 | | {Py_tp_traverse, attrgetter_traverse}, |
1617 | | {Py_tp_clear, attrgetter_clear}, |
1618 | | {Py_tp_methods, attrgetter_methods}, |
1619 | | {Py_tp_members, attrgetter_members}, |
1620 | | {Py_tp_getset, common_getset}, |
1621 | | {Py_tp_new, attrgetter_type_new}, |
1622 | | {Py_tp_vectorcall, attrgetter_type_vectorcall}, |
1623 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
1624 | | {Py_tp_repr, attrgetter_repr}, |
1625 | | {0, 0} |
1626 | | }; |
1627 | | |
1628 | | static PyType_Spec attrgetter_type_spec = { |
1629 | | .name = "operator.attrgetter", |
1630 | | .basicsize = sizeof(attrgetterobject), |
1631 | | .itemsize = 0, |
1632 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
1633 | | Py_TPFLAGS_IMMUTABLETYPE | Py_TPFLAGS_HAVE_VECTORCALL), |
1634 | | .slots = attrgetter_type_slots, |
1635 | | }; |
1636 | | |
1637 | | |
1638 | | /* methodcaller object **********************************************************/ |
1639 | | |
1640 | | typedef struct { |
1641 | | PyObject_HEAD |
1642 | | PyObject *name; |
1643 | | PyObject *args; |
1644 | | PyObject *kwds; |
1645 | | PyObject *vectorcall_args; |
1646 | | PyObject *vectorcall_kwnames; |
1647 | | vectorcallfunc vectorcall; |
1648 | | } methodcallerobject; |
1649 | | |
1650 | 0 | #define methodcallerobject_CAST(op) ((methodcallerobject *)(op)) |
1651 | | |
1652 | 0 | #define _METHODCALLER_MAX_ARGS 8 |
1653 | | |
1654 | | static PyObject * |
1655 | | methodcaller_vectorcall(PyObject *op, PyObject *const *args, |
1656 | | size_t nargsf, PyObject* kwnames) |
1657 | 0 | { |
1658 | 0 | methodcallerobject *mc = methodcallerobject_CAST(op); |
1659 | 0 | if (!_PyArg_CheckPositional("methodcaller", PyVectorcall_NARGS(nargsf), 1, 1) |
1660 | 0 | || !_PyArg_NoKwnames("methodcaller", kwnames)) { |
1661 | 0 | return NULL; |
1662 | 0 | } |
1663 | 0 | assert(mc->vectorcall_args != NULL); |
1664 | |
|
1665 | 0 | PyObject *tmp_args[_METHODCALLER_MAX_ARGS]; |
1666 | 0 | tmp_args[0] = args[0]; |
1667 | 0 | assert(1 + PyTuple_GET_SIZE(mc->vectorcall_args) <= _METHODCALLER_MAX_ARGS); |
1668 | 0 | memcpy(tmp_args + 1, _PyTuple_ITEMS(mc->vectorcall_args), sizeof(PyObject *) * PyTuple_GET_SIZE(mc->vectorcall_args)); |
1669 | |
|
1670 | 0 | return PyObject_VectorcallMethod(mc->name, tmp_args, |
1671 | 0 | (1 + PyTuple_GET_SIZE(mc->args)) | PY_VECTORCALL_ARGUMENTS_OFFSET, |
1672 | 0 | mc->vectorcall_kwnames); |
1673 | 0 | } |
1674 | | |
1675 | | static int |
1676 | | _methodcaller_initialize_vectorcall(methodcallerobject* mc) |
1677 | 0 | { |
1678 | 0 | PyObject* args = mc->args; |
1679 | 0 | PyObject* kwds = mc->kwds; |
1680 | |
|
1681 | 0 | if (kwds && PyDict_Size(kwds)) { |
1682 | 0 | PyObject *values = PyDict_Values(kwds); |
1683 | 0 | if (!values) { |
1684 | 0 | return -1; |
1685 | 0 | } |
1686 | 0 | PyObject *values_tuple = PySequence_Tuple(values); |
1687 | 0 | Py_DECREF(values); |
1688 | 0 | if (!values_tuple) { |
1689 | 0 | return -1; |
1690 | 0 | } |
1691 | 0 | if (PyTuple_GET_SIZE(args)) { |
1692 | 0 | mc->vectorcall_args = PySequence_Concat(args, values_tuple); |
1693 | 0 | Py_DECREF(values_tuple); |
1694 | 0 | if (mc->vectorcall_args == NULL) { |
1695 | 0 | return -1; |
1696 | 0 | } |
1697 | 0 | } |
1698 | 0 | else { |
1699 | 0 | mc->vectorcall_args = values_tuple; |
1700 | 0 | } |
1701 | 0 | mc->vectorcall_kwnames = PySequence_Tuple(kwds); |
1702 | 0 | if (!mc->vectorcall_kwnames) { |
1703 | 0 | return -1; |
1704 | 0 | } |
1705 | 0 | } |
1706 | 0 | else { |
1707 | 0 | mc->vectorcall_args = Py_NewRef(args); |
1708 | 0 | mc->vectorcall_kwnames = NULL; |
1709 | 0 | } |
1710 | | |
1711 | 0 | mc->vectorcall = methodcaller_vectorcall; |
1712 | 0 | return 0; |
1713 | 0 | } |
1714 | | |
1715 | | /* AC 3.5: variable number of arguments, not currently support by AC */ |
1716 | | static PyObject * |
1717 | | methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
1718 | 0 | { |
1719 | 0 | methodcallerobject *mc; |
1720 | 0 | PyObject *name; |
1721 | |
|
1722 | 0 | if (PyTuple_GET_SIZE(args) < 1) { |
1723 | 0 | PyErr_SetString(PyExc_TypeError, "methodcaller needs at least " |
1724 | 0 | "one argument, the method name"); |
1725 | 0 | return NULL; |
1726 | 0 | } |
1727 | | |
1728 | 0 | name = PyTuple_GET_ITEM(args, 0); |
1729 | 0 | if (!PyUnicode_Check(name)) { |
1730 | 0 | PyErr_SetString(PyExc_TypeError, |
1731 | 0 | "method name must be a string"); |
1732 | 0 | return NULL; |
1733 | 0 | } |
1734 | | |
1735 | 0 | _operator_state *state = _PyType_GetModuleState(type); |
1736 | | /* create methodcallerobject structure */ |
1737 | 0 | mc = PyObject_GC_New(methodcallerobject, (PyTypeObject *)state->methodcaller_type); |
1738 | 0 | if (mc == NULL) { |
1739 | 0 | return NULL; |
1740 | 0 | } |
1741 | 0 | mc->vectorcall = NULL; |
1742 | 0 | mc->vectorcall_args = NULL; |
1743 | 0 | mc->vectorcall_kwnames = NULL; |
1744 | 0 | mc->kwds = Py_XNewRef(kwds); |
1745 | |
|
1746 | 0 | Py_INCREF(name); |
1747 | 0 | PyInterpreterState *interp = _PyInterpreterState_GET(); |
1748 | 0 | _PyUnicode_InternMortal(interp, &name); |
1749 | 0 | mc->name = name; |
1750 | |
|
1751 | 0 | mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
1752 | 0 | if (mc->args == NULL) { |
1753 | 0 | Py_DECREF(mc); |
1754 | 0 | return NULL; |
1755 | 0 | } |
1756 | | |
1757 | 0 | Py_ssize_t vectorcall_size = PyTuple_GET_SIZE(args) |
1758 | 0 | + (kwds ? PyDict_Size(kwds) : 0); |
1759 | 0 | if (vectorcall_size < (_METHODCALLER_MAX_ARGS)) { |
1760 | 0 | if (_methodcaller_initialize_vectorcall(mc) < 0) { |
1761 | 0 | Py_DECREF(mc); |
1762 | 0 | return NULL; |
1763 | 0 | } |
1764 | 0 | } |
1765 | | |
1766 | 0 | PyObject_GC_Track(mc); |
1767 | 0 | return (PyObject *)mc; |
1768 | 0 | } |
1769 | | |
1770 | | static int |
1771 | | methodcaller_clear(PyObject *op) |
1772 | 0 | { |
1773 | 0 | methodcallerobject *mc = methodcallerobject_CAST(op); |
1774 | 0 | Py_CLEAR(mc->name); |
1775 | 0 | Py_CLEAR(mc->args); |
1776 | 0 | Py_CLEAR(mc->kwds); |
1777 | 0 | Py_CLEAR(mc->vectorcall_args); |
1778 | 0 | Py_CLEAR(mc->vectorcall_kwnames); |
1779 | 0 | return 0; |
1780 | 0 | } |
1781 | | |
1782 | | static void |
1783 | | methodcaller_dealloc(PyObject *op) |
1784 | 0 | { |
1785 | 0 | PyTypeObject *tp = Py_TYPE(op); |
1786 | 0 | PyObject_GC_UnTrack(op); |
1787 | 0 | (void)methodcaller_clear(op); |
1788 | 0 | tp->tp_free(op); |
1789 | 0 | Py_DECREF(tp); |
1790 | 0 | } |
1791 | | |
1792 | | static int |
1793 | | methodcaller_traverse(PyObject *op, visitproc visit, void *arg) |
1794 | 0 | { |
1795 | 0 | methodcallerobject *mc = methodcallerobject_CAST(op); |
1796 | 0 | Py_VISIT(mc->name); |
1797 | 0 | Py_VISIT(mc->args); |
1798 | 0 | Py_VISIT(mc->kwds); |
1799 | 0 | Py_VISIT(mc->vectorcall_args); |
1800 | 0 | Py_VISIT(mc->vectorcall_kwnames); |
1801 | 0 | Py_VISIT(Py_TYPE(mc)); |
1802 | 0 | return 0; |
1803 | 0 | } |
1804 | | |
1805 | | static PyObject * |
1806 | | methodcaller_call(PyObject *op, PyObject *args, PyObject *kw) |
1807 | 0 | { |
1808 | 0 | PyObject *method, *obj, *result; |
1809 | 0 | methodcallerobject *mc = methodcallerobject_CAST(op); |
1810 | |
|
1811 | 0 | if (!_PyArg_NoKeywords("methodcaller", kw)) |
1812 | 0 | return NULL; |
1813 | 0 | if (!_PyArg_CheckPositional("methodcaller", PyTuple_GET_SIZE(args), 1, 1)) |
1814 | 0 | return NULL; |
1815 | 0 | obj = PyTuple_GET_ITEM(args, 0); |
1816 | 0 | method = PyObject_GetAttr(obj, mc->name); |
1817 | 0 | if (method == NULL) |
1818 | 0 | return NULL; |
1819 | | |
1820 | 0 | result = PyObject_Call(method, mc->args, mc->kwds); |
1821 | 0 | Py_DECREF(method); |
1822 | 0 | return result; |
1823 | 0 | } |
1824 | | |
1825 | | static PyObject * |
1826 | | methodcaller_repr(PyObject *op) |
1827 | 0 | { |
1828 | 0 | methodcallerobject *mc = methodcallerobject_CAST(op); |
1829 | 0 | PyObject *argreprs, *repr = NULL, *sep, *joinedargreprs; |
1830 | 0 | Py_ssize_t numtotalargs, numposargs, numkwdargs, i; |
1831 | 0 | int status = Py_ReprEnter(op); |
1832 | 0 | if (status != 0) { |
1833 | 0 | if (status < 0) |
1834 | 0 | return NULL; |
1835 | 0 | return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name); |
1836 | 0 | } |
1837 | | |
1838 | 0 | numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0; |
1839 | 0 | numposargs = PyTuple_GET_SIZE(mc->args); |
1840 | 0 | numtotalargs = numposargs + numkwdargs; |
1841 | |
|
1842 | 0 | if (numtotalargs == 0) { |
1843 | 0 | repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(mc)->tp_name, mc->name); |
1844 | 0 | Py_ReprLeave(op); |
1845 | 0 | return repr; |
1846 | 0 | } |
1847 | | |
1848 | 0 | argreprs = PyTuple_New(numtotalargs); |
1849 | 0 | if (argreprs == NULL) { |
1850 | 0 | Py_ReprLeave(op); |
1851 | 0 | return NULL; |
1852 | 0 | } |
1853 | | |
1854 | 0 | for (i = 0; i < numposargs; ++i) { |
1855 | 0 | PyObject *onerepr = PyObject_Repr(PyTuple_GET_ITEM(mc->args, i)); |
1856 | 0 | if (onerepr == NULL) |
1857 | 0 | goto done; |
1858 | 0 | PyTuple_SET_ITEM(argreprs, i, onerepr); |
1859 | 0 | } |
1860 | | |
1861 | 0 | if (numkwdargs != 0) { |
1862 | 0 | PyObject *key, *value; |
1863 | 0 | Py_ssize_t pos = 0; |
1864 | 0 | while (PyDict_Next(mc->kwds, &pos, &key, &value)) { |
1865 | 0 | PyObject *onerepr = PyUnicode_FromFormat("%U=%R", key, value); |
1866 | 0 | if (onerepr == NULL) |
1867 | 0 | goto done; |
1868 | 0 | if (i >= numtotalargs) { |
1869 | 0 | i = -1; |
1870 | 0 | Py_DECREF(onerepr); |
1871 | 0 | break; |
1872 | 0 | } |
1873 | 0 | PyTuple_SET_ITEM(argreprs, i, onerepr); |
1874 | 0 | ++i; |
1875 | 0 | } |
1876 | 0 | if (i != numtotalargs) { |
1877 | 0 | PyErr_SetString(PyExc_RuntimeError, |
1878 | 0 | "keywords dict changed size during iteration"); |
1879 | 0 | goto done; |
1880 | 0 | } |
1881 | 0 | } |
1882 | | |
1883 | 0 | sep = PyUnicode_FromString(", "); |
1884 | 0 | if (sep == NULL) |
1885 | 0 | goto done; |
1886 | | |
1887 | 0 | joinedargreprs = PyUnicode_Join(sep, argreprs); |
1888 | 0 | Py_DECREF(sep); |
1889 | 0 | if (joinedargreprs == NULL) |
1890 | 0 | goto done; |
1891 | | |
1892 | 0 | repr = PyUnicode_FromFormat("%s(%R, %U)", Py_TYPE(mc)->tp_name, |
1893 | 0 | mc->name, joinedargreprs); |
1894 | 0 | Py_DECREF(joinedargreprs); |
1895 | |
|
1896 | 0 | done: |
1897 | 0 | Py_DECREF(argreprs); |
1898 | 0 | Py_ReprLeave(op); |
1899 | 0 | return repr; |
1900 | 0 | } |
1901 | | |
1902 | | static PyObject * |
1903 | | methodcaller_reduce(PyObject *op, PyObject *Py_UNUSED(dummy)) |
1904 | 0 | { |
1905 | 0 | methodcallerobject *mc = methodcallerobject_CAST(op); |
1906 | 0 | if (!mc->kwds || PyDict_GET_SIZE(mc->kwds) == 0) { |
1907 | 0 | Py_ssize_t i; |
1908 | 0 | Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args); |
1909 | 0 | PyObject *newargs = PyTuple_New(1 + callargcount); |
1910 | 0 | if (newargs == NULL) |
1911 | 0 | return NULL; |
1912 | 0 | PyTuple_SET_ITEM(newargs, 0, Py_NewRef(mc->name)); |
1913 | 0 | for (i = 0; i < callargcount; ++i) { |
1914 | 0 | PyObject *arg = PyTuple_GET_ITEM(mc->args, i); |
1915 | 0 | PyTuple_SET_ITEM(newargs, i + 1, Py_NewRef(arg)); |
1916 | 0 | } |
1917 | 0 | return Py_BuildValue("ON", Py_TYPE(mc), newargs); |
1918 | 0 | } |
1919 | 0 | else { |
1920 | 0 | PyObject *partial; |
1921 | 0 | PyObject *constructor; |
1922 | 0 | PyObject *newargs[2]; |
1923 | |
|
1924 | 0 | partial = PyImport_ImportModuleAttrString("functools", "partial"); |
1925 | 0 | if (!partial) |
1926 | 0 | return NULL; |
1927 | | |
1928 | 0 | newargs[0] = (PyObject *)Py_TYPE(mc); |
1929 | 0 | newargs[1] = mc->name; |
1930 | 0 | constructor = PyObject_VectorcallDict(partial, newargs, 2, mc->kwds); |
1931 | |
|
1932 | 0 | Py_DECREF(partial); |
1933 | 0 | return Py_BuildValue("NO", constructor, mc->args); |
1934 | 0 | } |
1935 | 0 | } |
1936 | | |
1937 | | static PyMethodDef methodcaller_methods[] = { |
1938 | | {"__reduce__", methodcaller_reduce, METH_NOARGS, |
1939 | | reduce_doc}, |
1940 | | {NULL} |
1941 | | }; |
1942 | | |
1943 | | static PyMemberDef methodcaller_members[] = { |
1944 | | {"__vectorcalloffset__", Py_T_PYSSIZET, offsetof(methodcallerobject, vectorcall), Py_READONLY}, |
1945 | | {NULL} |
1946 | | }; |
1947 | | |
1948 | | PyDoc_STRVAR(methodcaller_doc, |
1949 | | "methodcaller(name, /, *args, **kwargs)\n--\n\n\ |
1950 | | Return a callable object that calls the given method on its operand.\n\ |
1951 | | After f = methodcaller('name'), the call f(r) returns r.name().\n\ |
1952 | | After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\ |
1953 | | r.name('date', foo=1)."); |
1954 | | |
1955 | | static PyType_Slot methodcaller_type_slots[] = { |
1956 | | {Py_tp_doc, (void *)methodcaller_doc}, |
1957 | | {Py_tp_dealloc, methodcaller_dealloc}, |
1958 | | {Py_tp_call, methodcaller_call}, |
1959 | | {Py_tp_traverse, methodcaller_traverse}, |
1960 | | {Py_tp_clear, methodcaller_clear}, |
1961 | | {Py_tp_methods, methodcaller_methods}, |
1962 | | {Py_tp_members, methodcaller_members}, |
1963 | | {Py_tp_getset, common_getset}, |
1964 | | {Py_tp_new, methodcaller_new}, |
1965 | | {Py_tp_getattro, PyObject_GenericGetAttr}, |
1966 | | {Py_tp_repr, methodcaller_repr}, |
1967 | | {0, 0} |
1968 | | }; |
1969 | | |
1970 | | static PyType_Spec methodcaller_type_spec = { |
1971 | | .name = "operator.methodcaller", |
1972 | | .basicsize = sizeof(methodcallerobject), |
1973 | | .itemsize = 0, |
1974 | | .flags = (Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC | |
1975 | | Py_TPFLAGS_HAVE_VECTORCALL | Py_TPFLAGS_IMMUTABLETYPE), |
1976 | | .slots = methodcaller_type_slots, |
1977 | | }; |
1978 | | |
1979 | | static int |
1980 | | operator_exec(PyObject *module) |
1981 | 29 | { |
1982 | 29 | _operator_state *state = get_operator_state(module); |
1983 | 29 | state->attrgetter_type = PyType_FromModuleAndSpec(module, &attrgetter_type_spec, NULL); |
1984 | 29 | if (state->attrgetter_type == NULL) { |
1985 | 0 | return -1; |
1986 | 0 | } |
1987 | 29 | if (PyModule_AddType(module, (PyTypeObject *)state->attrgetter_type) < 0) { |
1988 | 0 | return -1; |
1989 | 0 | } |
1990 | | |
1991 | 29 | state->itemgetter_type = PyType_FromModuleAndSpec(module, &itemgetter_type_spec, NULL); |
1992 | 29 | if (state->itemgetter_type == NULL) { |
1993 | 0 | return -1; |
1994 | 0 | } |
1995 | 29 | if (PyModule_AddType(module, (PyTypeObject *)state->itemgetter_type) < 0) { |
1996 | 0 | return -1; |
1997 | 0 | } |
1998 | | |
1999 | 29 | state->methodcaller_type = PyType_FromModuleAndSpec(module, &methodcaller_type_spec, NULL); |
2000 | 29 | if (state->methodcaller_type == NULL) { |
2001 | 0 | return -1; |
2002 | 0 | } |
2003 | 29 | if (PyModule_AddType(module, (PyTypeObject *)state->methodcaller_type) < 0) { |
2004 | 0 | return -1; |
2005 | 0 | } |
2006 | | |
2007 | 29 | return 0; |
2008 | 29 | } |
2009 | | |
2010 | | |
2011 | | static struct PyModuleDef_Slot operator_slots[] = { |
2012 | | _Py_ABI_SLOT, |
2013 | | {Py_mod_exec, operator_exec}, |
2014 | | {Py_mod_multiple_interpreters, Py_MOD_PER_INTERPRETER_GIL_SUPPORTED}, |
2015 | | {Py_mod_gil, Py_MOD_GIL_NOT_USED}, |
2016 | | {0, NULL} |
2017 | | }; |
2018 | | |
2019 | | static int |
2020 | | operator_traverse(PyObject *module, visitproc visit, void *arg) |
2021 | 1.99k | { |
2022 | 1.99k | _operator_state *state = get_operator_state(module); |
2023 | 1.99k | Py_VISIT(state->attrgetter_type); |
2024 | 1.99k | Py_VISIT(state->itemgetter_type); |
2025 | 1.99k | Py_VISIT(state->methodcaller_type); |
2026 | 1.99k | return 0; |
2027 | 1.99k | } |
2028 | | |
2029 | | static int |
2030 | | operator_clear(PyObject *module) |
2031 | 0 | { |
2032 | 0 | _operator_state *state = get_operator_state(module); |
2033 | 0 | Py_CLEAR(state->attrgetter_type); |
2034 | 0 | Py_CLEAR(state->itemgetter_type); |
2035 | 0 | Py_CLEAR(state->methodcaller_type); |
2036 | 0 | return 0; |
2037 | 0 | } |
2038 | | |
2039 | | static void |
2040 | | operator_free(void *module) |
2041 | 0 | { |
2042 | 0 | (void)operator_clear((PyObject *)module); |
2043 | 0 | } |
2044 | | |
2045 | | static struct PyModuleDef operatormodule = { |
2046 | | PyModuleDef_HEAD_INIT, |
2047 | | .m_name = "_operator", |
2048 | | .m_doc = operator_doc, |
2049 | | .m_size = sizeof(_operator_state), |
2050 | | .m_methods = operator_methods, |
2051 | | .m_slots = operator_slots, |
2052 | | .m_traverse = operator_traverse, |
2053 | | .m_clear = operator_clear, |
2054 | | .m_free = operator_free, |
2055 | | }; |
2056 | | |
2057 | | PyMODINIT_FUNC |
2058 | | PyInit__operator(void) |
2059 | 29 | { |
2060 | 29 | return PyModuleDef_Init(&operatormodule); |
2061 | 29 | } |