blob: f55c2f1d1f415e552eca2c2c95a1ae53d164153d [file] [log] [blame]
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +00001#include "Python.h"
Victor Stinnercdad2722021-04-22 00:52:52 +02002#include "pycore_moduleobject.h" // _PyModule_GetState()
Serhiy Storchakab813a0e2017-01-19 17:44:13 +02003#include "clinic/_operator.c.h"
4
Dong-hee Na31967fd2020-08-26 17:22:27 +00005typedef struct {
6 PyObject *itemgetter_type;
7 PyObject *attrgetter_type;
8 PyObject *methodcaller_type;
9} _operator_state;
10
11static inline _operator_state*
12get_operator_state(PyObject *module)
13{
Victor Stinnercdad2722021-04-22 00:52:52 +020014 void *state = _PyModule_GetState(module);
Dong-hee Na31967fd2020-08-26 17:22:27 +000015 assert(state != NULL);
16 return (_operator_state *)state;
17}
18
Serhiy Storchakab813a0e2017-01-19 17:44:13 +020019/*[clinic input]
20module _operator
21[clinic start generated code]*/
22/*[clinic end generated code: output=da39a3ee5e6b4b0d input=672ecf48487521e7]*/
23
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000024PyDoc_STRVAR(operator_doc,
25"Operator interface.\n\
Guido van Rossum037b9401996-07-30 16:55:54 +000026\n\
27This module exports a set of functions implemented in C corresponding\n\
28to the intrinsic operators of Python. For example, operator.add(x, y)\n\
29is equivalent to the expression x+y. The function names are those\n\
Benjamin Petersona0dfa822009-11-13 02:25:08 +000030used for special methods; variants without leading and trailing\n\
Martin v. Löwis14f8b4c2002-06-13 20:33:02 +000031'__' are also provided for convenience.");
Guido van Rossum037b9401996-07-30 16:55:54 +000032
Guido van Rossum037b9401996-07-30 16:55:54 +000033
Serhiy Storchakab813a0e2017-01-19 17:44:13 +020034/*[clinic input]
35_operator.truth -> bool
Guido van Rossum037b9401996-07-30 16:55:54 +000036
Serhiy Storchakab813a0e2017-01-19 17:44:13 +020037 a: object
38 /
Guido van Rossum037b9401996-07-30 16:55:54 +000039
Serhiy Storchakab813a0e2017-01-19 17:44:13 +020040Return True if a is true, False otherwise.
41[clinic start generated code]*/
Guido van Rossum037b9401996-07-30 16:55:54 +000042
Serhiy Storchakab813a0e2017-01-19 17:44:13 +020043static int
44_operator_truth_impl(PyObject *module, PyObject *a)
45/*[clinic end generated code: output=eaf87767234fa5d7 input=bc74a4cd90235875]*/
Raymond Hettinger5959c552002-08-19 03:19:09 +000046{
Serhiy Storchakab813a0e2017-01-19 17:44:13 +020047 return PyObject_IsTrue(a);
Raymond Hettinger5959c552002-08-19 03:19:09 +000048}
49
Serhiy Storchakab813a0e2017-01-19 17:44:13 +020050/*[clinic input]
51_operator.add
52
53 a: object
54 b: object
55 /
56
57Same as a + b.
58[clinic start generated code]*/
Armin Rigof5bd3b42005-12-29 16:50:42 +000059
Guido van Rossum38fff8c2006-03-07 18:50:55 +000060static PyObject *
Serhiy Storchakab813a0e2017-01-19 17:44:13 +020061_operator_add_impl(PyObject *module, PyObject *a, PyObject *b)
62/*[clinic end generated code: output=8292984204f45164 input=5efe3bff856ac215]*/
63{
64 return PyNumber_Add(a, b);
65}
66
67/*[clinic input]
68_operator.sub = _operator.add
69
70Same as a - b.
71[clinic start generated code]*/
72
73static PyObject *
74_operator_sub_impl(PyObject *module, PyObject *a, PyObject *b)
75/*[clinic end generated code: output=4adfc3b888c1ee2e input=6494c6b100b8e795]*/
76{
77 return PyNumber_Subtract(a, b);
78}
79
80/*[clinic input]
81_operator.mul = _operator.add
82
83Same as a * b.
84[clinic start generated code]*/
85
86static PyObject *
87_operator_mul_impl(PyObject *module, PyObject *a, PyObject *b)
88/*[clinic end generated code: output=d24d66f55a01944c input=2368615b4358b70d]*/
89{
90 return PyNumber_Multiply(a, b);
91}
92
93/*[clinic input]
94_operator.matmul = _operator.add
95
96Same as a @ b.
97[clinic start generated code]*/
98
99static PyObject *
100_operator_matmul_impl(PyObject *module, PyObject *a, PyObject *b)
101/*[clinic end generated code: output=a20d917eb35d0101 input=9ab304e37fb42dd4]*/
102{
103 return PyNumber_MatrixMultiply(a, b);
104}
105
106/*[clinic input]
107_operator.floordiv = _operator.add
108
109Same as a // b.
110[clinic start generated code]*/
111
112static PyObject *
113_operator_floordiv_impl(PyObject *module, PyObject *a, PyObject *b)
114/*[clinic end generated code: output=df26b71a60589f99 input=bb2e88ba446c612c]*/
115{
116 return PyNumber_FloorDivide(a, b);
117}
118
119/*[clinic input]
120_operator.truediv = _operator.add
121
122Same as a / b.
123[clinic start generated code]*/
124
125static PyObject *
126_operator_truediv_impl(PyObject *module, PyObject *a, PyObject *b)
127/*[clinic end generated code: output=0e6a959944d77719 input=ecbb947673f4eb1f]*/
128{
129 return PyNumber_TrueDivide(a, b);
130}
131
132/*[clinic input]
133_operator.mod = _operator.add
134
135Same as a % b.
136[clinic start generated code]*/
137
138static PyObject *
139_operator_mod_impl(PyObject *module, PyObject *a, PyObject *b)
140/*[clinic end generated code: output=9519822f0bbec166 input=102e19b422342ac1]*/
141{
142 return PyNumber_Remainder(a, b);
143}
144
145/*[clinic input]
146_operator.neg
147
148 a: object
149 /
150
151Same as -a.
152[clinic start generated code]*/
153
154static PyObject *
155_operator_neg(PyObject *module, PyObject *a)
156/*[clinic end generated code: output=36e08ecfc6a1c08c input=84f09bdcf27c96ec]*/
157{
158 return PyNumber_Negative(a);
159}
160
161/*[clinic input]
162_operator.pos = _operator.neg
163
164Same as +a.
165[clinic start generated code]*/
166
167static PyObject *
168_operator_pos(PyObject *module, PyObject *a)
169/*[clinic end generated code: output=dad7a126221dd091 input=b6445b63fddb8772]*/
170{
171 return PyNumber_Positive(a);
172}
173
174/*[clinic input]
175_operator.abs = _operator.neg
176
177Same as abs(a).
178[clinic start generated code]*/
179
180static PyObject *
181_operator_abs(PyObject *module, PyObject *a)
182/*[clinic end generated code: output=1389a93ba053ea3e input=341d07ba86f58039]*/
183{
184 return PyNumber_Absolute(a);
185}
186
187/*[clinic input]
188_operator.inv = _operator.neg
189
190Same as ~a.
191[clinic start generated code]*/
192
193static PyObject *
194_operator_inv(PyObject *module, PyObject *a)
195/*[clinic end generated code: output=a56875ba075ee06d input=b01a4677739f6eb2]*/
196{
197 return PyNumber_Invert(a);
198}
199
200/*[clinic input]
201_operator.invert = _operator.neg
202
203Same as ~a.
204[clinic start generated code]*/
205
206static PyObject *
207_operator_invert(PyObject *module, PyObject *a)
208/*[clinic end generated code: output=406b5aa030545fcc input=7f2d607176672e55]*/
209{
210 return PyNumber_Invert(a);
211}
212
213/*[clinic input]
214_operator.lshift = _operator.add
215
216Same as a << b.
217[clinic start generated code]*/
218
219static PyObject *
220_operator_lshift_impl(PyObject *module, PyObject *a, PyObject *b)
221/*[clinic end generated code: output=37f7e52c41435bd8 input=746e8a160cbbc9eb]*/
222{
223 return PyNumber_Lshift(a, b);
224}
225
226/*[clinic input]
227_operator.rshift = _operator.add
228
229Same as a >> b.
230[clinic start generated code]*/
231
232static PyObject *
233_operator_rshift_impl(PyObject *module, PyObject *a, PyObject *b)
234/*[clinic end generated code: output=4593c7ef30ec2ee3 input=d2c85bb5a64504c2]*/
235{
236 return PyNumber_Rshift(a, b);
237}
238
239/*[clinic input]
240_operator.not_ = _operator.truth
241
242Same as not a.
243[clinic start generated code]*/
244
245static int
246_operator_not__impl(PyObject *module, PyObject *a)
247/*[clinic end generated code: output=743f9c24a09759ef input=854156d50804d9b8]*/
248{
249 return PyObject_Not(a);
250}
251
252/*[clinic input]
253_operator.and_ = _operator.add
254
255Same as a & b.
256[clinic start generated code]*/
257
258static PyObject *
259_operator_and__impl(PyObject *module, PyObject *a, PyObject *b)
260/*[clinic end generated code: output=93c4fe88f7b76d9e input=4f3057c90ec4c99f]*/
261{
262 return PyNumber_And(a, b);
263}
264
265/*[clinic input]
266_operator.xor = _operator.add
267
268Same as a ^ b.
269[clinic start generated code]*/
270
271static PyObject *
272_operator_xor_impl(PyObject *module, PyObject *a, PyObject *b)
273/*[clinic end generated code: output=b24cd8b79fde0004 input=3c5cfa7253d808dd]*/
274{
275 return PyNumber_Xor(a, b);
276}
277
278/*[clinic input]
279_operator.or_ = _operator.add
280
281Same as a | b.
282[clinic start generated code]*/
283
284static PyObject *
285_operator_or__impl(PyObject *module, PyObject *a, PyObject *b)
286/*[clinic end generated code: output=58024867b8d90461 input=b40c6c44f7c79c09]*/
287{
288 return PyNumber_Or(a, b);
289}
290
291/*[clinic input]
292_operator.iadd = _operator.add
293
294Same as a += b.
295[clinic start generated code]*/
296
297static PyObject *
298_operator_iadd_impl(PyObject *module, PyObject *a, PyObject *b)
299/*[clinic end generated code: output=07dc627832526eb5 input=d22a91c07ac69227]*/
300{
301 return PyNumber_InPlaceAdd(a, b);
302}
303
304/*[clinic input]
305_operator.isub = _operator.add
306
307Same as a -= b.
308[clinic start generated code]*/
309
310static PyObject *
311_operator_isub_impl(PyObject *module, PyObject *a, PyObject *b)
312/*[clinic end generated code: output=4513467d23b5e0b1 input=4591b00d0a0ccafd]*/
313{
314 return PyNumber_InPlaceSubtract(a, b);
315}
316
317/*[clinic input]
318_operator.imul = _operator.add
319
320Same as a *= b.
321[clinic start generated code]*/
322
323static PyObject *
324_operator_imul_impl(PyObject *module, PyObject *a, PyObject *b)
325/*[clinic end generated code: output=5e87dacd19a71eab input=0e01fb8631e1b76f]*/
326{
327 return PyNumber_InPlaceMultiply(a, b);
328}
329
330/*[clinic input]
331_operator.imatmul = _operator.add
332
333Same as a @= b.
334[clinic start generated code]*/
335
336static PyObject *
337_operator_imatmul_impl(PyObject *module, PyObject *a, PyObject *b)
338/*[clinic end generated code: output=d603cbdf716ce519 input=bb614026372cd542]*/
339{
340 return PyNumber_InPlaceMatrixMultiply(a, b);
341}
342
343/*[clinic input]
344_operator.ifloordiv = _operator.add
345
346Same as a //= b.
347[clinic start generated code]*/
348
349static PyObject *
350_operator_ifloordiv_impl(PyObject *module, PyObject *a, PyObject *b)
351/*[clinic end generated code: output=535336048c681794 input=9df3b5021cff4ca1]*/
352{
353 return PyNumber_InPlaceFloorDivide(a, b);
354}
355
356/*[clinic input]
357_operator.itruediv = _operator.add
358
359Same as a /= b.
360[clinic start generated code]*/
361
362static PyObject *
363_operator_itruediv_impl(PyObject *module, PyObject *a, PyObject *b)
364/*[clinic end generated code: output=28017fbd3563952f input=9a1ee01608f5f590]*/
365{
366 return PyNumber_InPlaceTrueDivide(a, b);
367}
368
369/*[clinic input]
370_operator.imod = _operator.add
371
372Same as a %= b.
373[clinic start generated code]*/
374
375static PyObject *
376_operator_imod_impl(PyObject *module, PyObject *a, PyObject *b)
377/*[clinic end generated code: output=f7c540ae0fc70904 input=d0c384a3ce38e1dd]*/
378{
379 return PyNumber_InPlaceRemainder(a, b);
380}
381
382/*[clinic input]
383_operator.ilshift = _operator.add
384
385Same as a <<= b.
386[clinic start generated code]*/
387
388static PyObject *
389_operator_ilshift_impl(PyObject *module, PyObject *a, PyObject *b)
390/*[clinic end generated code: output=e73a8fee1ac18749 input=e21b6b310f54572e]*/
391{
392 return PyNumber_InPlaceLshift(a, b);
393}
394
395/*[clinic input]
396_operator.irshift = _operator.add
397
398Same as a >>= b.
399[clinic start generated code]*/
400
401static PyObject *
402_operator_irshift_impl(PyObject *module, PyObject *a, PyObject *b)
403/*[clinic end generated code: output=97f2af6b5ff2ed81 input=6778dbd0f6e1ec16]*/
404{
405 return PyNumber_InPlaceRshift(a, b);
406}
407
408/*[clinic input]
409_operator.iand = _operator.add
410
411Same as a &= b.
412[clinic start generated code]*/
413
414static PyObject *
415_operator_iand_impl(PyObject *module, PyObject *a, PyObject *b)
416/*[clinic end generated code: output=4599e9d40cbf7d00 input=71dfd8e70c156a7b]*/
417{
418 return PyNumber_InPlaceAnd(a, b);
419}
420
421/*[clinic input]
422_operator.ixor = _operator.add
423
424Same as a ^= b.
425[clinic start generated code]*/
426
427static PyObject *
428_operator_ixor_impl(PyObject *module, PyObject *a, PyObject *b)
429/*[clinic end generated code: output=5ff881766872be03 input=695c32bec0604d86]*/
430{
431 return PyNumber_InPlaceXor(a, b);
432}
433
434/*[clinic input]
435_operator.ior = _operator.add
436
437Same as a |= b.
438[clinic start generated code]*/
439
440static PyObject *
441_operator_ior_impl(PyObject *module, PyObject *a, PyObject *b)
442/*[clinic end generated code: output=48aac319445bf759 input=8f01d03eda9920cf]*/
443{
444 return PyNumber_InPlaceOr(a, b);
445}
446
447/*[clinic input]
448_operator.concat = _operator.add
449
450Same as a + b, for a and b sequences.
451[clinic start generated code]*/
452
453static PyObject *
454_operator_concat_impl(PyObject *module, PyObject *a, PyObject *b)
455/*[clinic end generated code: output=80028390942c5f11 input=8544ccd5341a3658]*/
456{
457 return PySequence_Concat(a, b);
458}
459
460/*[clinic input]
461_operator.iconcat = _operator.add
462
463Same as a += b, for a and b sequences.
464[clinic start generated code]*/
465
466static PyObject *
467_operator_iconcat_impl(PyObject *module, PyObject *a, PyObject *b)
468/*[clinic end generated code: output=3ea0a162ebb2e26d input=8f5fe5722fcd837e]*/
469{
470 return PySequence_InPlaceConcat(a, b);
471}
472
473/*[clinic input]
474_operator.contains -> bool
475
476 a: object
477 b: object
478 /
479
480Same as b in a (note reversed operands).
481[clinic start generated code]*/
482
483static int
484_operator_contains_impl(PyObject *module, PyObject *a, PyObject *b)
485/*[clinic end generated code: output=413b4dbe82b6ffc1 input=9122a69b505fde13]*/
486{
487 return PySequence_Contains(a, b);
488}
489
490/*[clinic input]
491_operator.indexOf -> Py_ssize_t
492
493 a: object
494 b: object
495 /
496
497Return the first index of b in a.
498[clinic start generated code]*/
499
500static Py_ssize_t
501_operator_indexOf_impl(PyObject *module, PyObject *a, PyObject *b)
502/*[clinic end generated code: output=c6226d8e0fb60fa6 input=8be2e43b6a6fffe3]*/
503{
504 return PySequence_Index(a, b);
505}
506
507/*[clinic input]
508_operator.countOf = _operator.indexOf
509
510Return the number of times b occurs in a.
511[clinic start generated code]*/
512
513static Py_ssize_t
514_operator_countOf_impl(PyObject *module, PyObject *a, PyObject *b)
515/*[clinic end generated code: output=9e1623197daf3382 input=0c3a2656add252db]*/
516{
517 return PySequence_Count(a, b);
518}
519
520/*[clinic input]
521_operator.getitem
522
523 a: object
524 b: object
525 /
526
527Same as a[b].
528[clinic start generated code]*/
529
530static PyObject *
531_operator_getitem_impl(PyObject *module, PyObject *a, PyObject *b)
532/*[clinic end generated code: output=6c8d8101a676e594 input=6682797320e48845]*/
533{
534 return PyObject_GetItem(a, b);
535}
536
537/*[clinic input]
538_operator.setitem
539
540 a: object
541 b: object
542 c: object
543 /
544
545Same as a[b] = c.
546[clinic start generated code]*/
547
548static PyObject *
549_operator_setitem_impl(PyObject *module, PyObject *a, PyObject *b,
550 PyObject *c)
551/*[clinic end generated code: output=1324f9061ae99e25 input=ceaf453c4d3a58df]*/
552{
553 if (-1 == PyObject_SetItem(a, b, c))
554 return NULL;
555 Py_RETURN_NONE;
556}
557
558/*[clinic input]
559_operator.delitem = _operator.getitem
560
561Same as del a[b].
562[clinic start generated code]*/
563
564static PyObject *
565_operator_delitem_impl(PyObject *module, PyObject *a, PyObject *b)
566/*[clinic end generated code: output=db18f61506295799 input=991bec56a0d3ec7f]*/
567{
568 if (-1 == PyObject_DelItem(a, b))
569 return NULL;
570 Py_RETURN_NONE;
571}
572
573/*[clinic input]
574_operator.eq
575
576 a: object
577 b: object
578 /
579
580Same as a == b.
581[clinic start generated code]*/
582
583static PyObject *
584_operator_eq_impl(PyObject *module, PyObject *a, PyObject *b)
585/*[clinic end generated code: output=8d7d46ed4135677c input=586fca687a95a83f]*/
586{
587 return PyObject_RichCompare(a, b, Py_EQ);
588}
589
590/*[clinic input]
591_operator.ne = _operator.eq
592
593Same as a != b.
594[clinic start generated code]*/
595
596static PyObject *
597_operator_ne_impl(PyObject *module, PyObject *a, PyObject *b)
598/*[clinic end generated code: output=c99bd0c3a4c01297 input=5d88f23d35e9abac]*/
599{
600 return PyObject_RichCompare(a, b, Py_NE);
601}
602
603/*[clinic input]
604_operator.lt = _operator.eq
605
606Same as a < b.
607[clinic start generated code]*/
608
609static PyObject *
610_operator_lt_impl(PyObject *module, PyObject *a, PyObject *b)
611/*[clinic end generated code: output=082d7c45c440e535 input=34a59ad6d39d3a2b]*/
612{
613 return PyObject_RichCompare(a, b, Py_LT);
614}
615
616/*[clinic input]
617_operator.le = _operator.eq
618
619Same as a <= b.
620[clinic start generated code]*/
621
622static PyObject *
623_operator_le_impl(PyObject *module, PyObject *a, PyObject *b)
624/*[clinic end generated code: output=00970a2923d0ae17 input=b812a7860a0bef44]*/
625{
626 return PyObject_RichCompare(a, b, Py_LE);
627}
628
629/*[clinic input]
630_operator.gt = _operator.eq
631
632Same as a > b.
633[clinic start generated code]*/
634
635static PyObject *
636_operator_gt_impl(PyObject *module, PyObject *a, PyObject *b)
637/*[clinic end generated code: output=8d373349ecf25641 input=9bdb45b995ada35b]*/
638{
639 return PyObject_RichCompare(a, b, Py_GT);
640}
641
642/*[clinic input]
643_operator.ge = _operator.eq
644
645Same as a >= b.
646[clinic start generated code]*/
647
648static PyObject *
649_operator_ge_impl(PyObject *module, PyObject *a, PyObject *b)
650/*[clinic end generated code: output=7ce3882256d4b137 input=cf1dc4a5ca9c35f5]*/
651{
652 return PyObject_RichCompare(a, b, Py_GE);
653}
654
655/*[clinic input]
656_operator.pow = _operator.add
657
658Same as a ** b.
659[clinic start generated code]*/
660
661static PyObject *
662_operator_pow_impl(PyObject *module, PyObject *a, PyObject *b)
663/*[clinic end generated code: output=09e668ad50036120 input=690b40f097ab1637]*/
664{
665 return PyNumber_Power(a, b, Py_None);
666}
667
668/*[clinic input]
669_operator.ipow = _operator.add
670
671Same as a **= b.
672[clinic start generated code]*/
673
674static PyObject *
675_operator_ipow_impl(PyObject *module, PyObject *a, PyObject *b)
676/*[clinic end generated code: output=7189ff4d4367c808 input=f00623899d07499a]*/
677{
678 return PyNumber_InPlacePower(a, b, Py_None);
679}
680
681/*[clinic input]
682_operator.index
683
684 a: object
685 /
686
687Same as a.__index__()
688[clinic start generated code]*/
689
690static PyObject *
691_operator_index(PyObject *module, PyObject *a)
692/*[clinic end generated code: output=d972b0764ac305fc input=6f54d50ea64a579c]*/
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000693{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000694 return PyNumber_Index(a);
Guido van Rossum38fff8c2006-03-07 18:50:55 +0000695}
696
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200697/*[clinic input]
698_operator.is_ = _operator.add
699
700Same as a is b.
701[clinic start generated code]*/
702
703static PyObject *
704_operator_is__impl(PyObject *module, PyObject *a, PyObject *b)
705/*[clinic end generated code: output=bcd47a402e482e1d input=5fa9b97df03c427f]*/
Raymond Hettinger9543b342003-01-18 23:22:20 +0000706{
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200707 PyObject *result;
708 result = (a == b) ? Py_True : Py_False;
709 Py_INCREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 return result;
Raymond Hettinger9543b342003-01-18 23:22:20 +0000711}
712
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200713/*[clinic input]
714_operator.is_not = _operator.add
715
716Same as a is not b.
717[clinic start generated code]*/
718
719static PyObject *
720_operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b)
721/*[clinic end generated code: output=491a1f2f81f6c7f9 input=5a93f7e1a93535f1]*/
Raymond Hettinger9543b342003-01-18 23:22:20 +0000722{
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200723 PyObject *result;
724 result = (a != b) ? Py_True : Py_False;
725 Py_INCREF(result);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000726 return result;
Raymond Hettinger9543b342003-01-18 23:22:20 +0000727}
728
Christian Heimes6cea6552012-06-24 13:48:32 +0200729/* compare_digest **********************************************************/
730
731/*
732 * timing safe compare
733 *
734 * Returns 1 of the strings are equal.
735 * In case of len(a) != len(b) the function tries to keep the timing
736 * dependent on the length of b. CPU cache locally may still alter timing
737 * a bit.
738 */
739static int
740_tscmp(const unsigned char *a, const unsigned char *b,
741 Py_ssize_t len_a, Py_ssize_t len_b)
742{
743 /* The volatile type declarations make sure that the compiler has no
744 * chance to optimize and fold the code in any way that may change
745 * the timing.
746 */
747 volatile Py_ssize_t length;
748 volatile const unsigned char *left;
749 volatile const unsigned char *right;
750 Py_ssize_t i;
Devin Jeanpierre31729362020-11-21 01:55:23 -0700751 volatile unsigned char result;
Christian Heimes6cea6552012-06-24 13:48:32 +0200752
753 /* loop count depends on length of b */
754 length = len_b;
755 left = NULL;
756 right = b;
757
758 /* don't use else here to keep the amount of CPU instructions constant,
759 * volatile forces re-evaluation
760 * */
761 if (len_a == length) {
762 left = *((volatile const unsigned char**)&a);
763 result = 0;
764 }
765 if (len_a != length) {
766 left = b;
767 result = 1;
768 }
769
770 for (i=0; i < length; i++) {
771 result |= *left++ ^ *right++;
772 }
773
774 return (result == 0);
775}
776
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200777/*[clinic input]
778_operator.length_hint -> Py_ssize_t
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200779
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200780 obj: object
781 default: Py_ssize_t = 0
782 /
783
784Return an estimate of the number of items in obj.
785
786This is useful for presizing containers when building from an iterable.
787
788If the object supports len(), the result will be exact.
789Otherwise, it may over- or under-estimate by an arbitrary amount.
790The result will be an integer >= 0.
791[clinic start generated code]*/
792
793static Py_ssize_t
794_operator_length_hint_impl(PyObject *module, PyObject *obj,
795 Py_ssize_t default_value)
796/*[clinic end generated code: output=01d469edc1d612ad input=65ed29f04401e96a]*/
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200797{
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200798 return PyObject_LengthHint(obj, default_value);
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200799}
800
Christian Heimesdb5aed92020-05-27 21:50:06 +0200801/* NOTE: Keep in sync with _hashopenssl.c implementation. */
802
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200803/*[clinic input]
804_operator._compare_digest = _operator.eq
Armin Ronacheraa9a79d2012-10-06 14:03:24 +0200805
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200806Return 'a == b'.
Christian Heimes6cea6552012-06-24 13:48:32 +0200807
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200808This function uses an approach designed to prevent
809timing analysis, making it appropriate for cryptography.
810
811a and b must both be of the same type: either str (ASCII only),
812or any bytes-like object.
813
814Note: If a and b are of different lengths, or if an error occurs,
815a timing attack could theoretically reveal information about the
816types and lengths of a and b--but not their values.
817[clinic start generated code]*/
818
819static PyObject *
820_operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)
821/*[clinic end generated code: output=11d452bdd3a23cbc input=9ac7e2c4e30bc356]*/
Christian Heimes6cea6552012-06-24 13:48:32 +0200822{
Christian Heimes6cea6552012-06-24 13:48:32 +0200823 int rc;
Christian Heimes6cea6552012-06-24 13:48:32 +0200824
Christian Heimes6cea6552012-06-24 13:48:32 +0200825 /* ASCII unicode string */
826 if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
827 if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
828 return NULL;
829 }
830 if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
831 PyErr_SetString(PyExc_TypeError,
832 "comparing strings with non-ASCII characters is "
833 "not supported");
834 return NULL;
835 }
836
837 rc = _tscmp(PyUnicode_DATA(a),
838 PyUnicode_DATA(b),
839 PyUnicode_GET_LENGTH(a),
840 PyUnicode_GET_LENGTH(b));
841 }
842 /* fallback to buffer interface for bytes, bytesarray and other */
843 else {
844 Py_buffer view_a;
845 Py_buffer view_b;
846
Benjamin Peterson23a192d2014-05-11 16:17:02 -0700847 if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {
Christian Heimes6cea6552012-06-24 13:48:32 +0200848 PyErr_Format(PyExc_TypeError,
849 "unsupported operand types(s) or combination of types: "
850 "'%.100s' and '%.100s'",
851 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
852 return NULL;
853 }
854
855 if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
856 return NULL;
857 }
858 if (view_a.ndim > 1) {
859 PyErr_SetString(PyExc_BufferError,
860 "Buffer must be single dimension");
861 PyBuffer_Release(&view_a);
862 return NULL;
863 }
864
865 if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
866 PyBuffer_Release(&view_a);
867 return NULL;
868 }
869 if (view_b.ndim > 1) {
870 PyErr_SetString(PyExc_BufferError,
871 "Buffer must be single dimension");
872 PyBuffer_Release(&view_a);
873 PyBuffer_Release(&view_b);
874 return NULL;
875 }
876
877 rc = _tscmp((const unsigned char*)view_a.buf,
878 (const unsigned char*)view_b.buf,
879 view_a.len,
880 view_b.len);
881
882 PyBuffer_Release(&view_a);
883 PyBuffer_Release(&view_b);
884 }
885
Georg Brandl93b7d7e2012-06-24 13:54:51 +0200886 return PyBool_FromLong(rc);
Christian Heimes6cea6552012-06-24 13:48:32 +0200887}
888
889/* operator methods **********************************************************/
890
Guido van Rossum037b9401996-07-30 16:55:54 +0000891static struct PyMethodDef operator_methods[] = {
Guido van Rossum037b9401996-07-30 16:55:54 +0000892
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200893 _OPERATOR_TRUTH_METHODDEF
894 _OPERATOR_CONTAINS_METHODDEF
895 _OPERATOR_INDEXOF_METHODDEF
896 _OPERATOR_COUNTOF_METHODDEF
897 _OPERATOR_IS__METHODDEF
898 _OPERATOR_IS_NOT_METHODDEF
899 _OPERATOR_INDEX_METHODDEF
900 _OPERATOR_ADD_METHODDEF
901 _OPERATOR_SUB_METHODDEF
902 _OPERATOR_MUL_METHODDEF
903 _OPERATOR_MATMUL_METHODDEF
904 _OPERATOR_FLOORDIV_METHODDEF
905 _OPERATOR_TRUEDIV_METHODDEF
906 _OPERATOR_MOD_METHODDEF
907 _OPERATOR_NEG_METHODDEF
908 _OPERATOR_POS_METHODDEF
909 _OPERATOR_ABS_METHODDEF
910 _OPERATOR_INV_METHODDEF
911 _OPERATOR_INVERT_METHODDEF
912 _OPERATOR_LSHIFT_METHODDEF
913 _OPERATOR_RSHIFT_METHODDEF
914 _OPERATOR_NOT__METHODDEF
915 _OPERATOR_AND__METHODDEF
916 _OPERATOR_XOR_METHODDEF
917 _OPERATOR_OR__METHODDEF
918 _OPERATOR_IADD_METHODDEF
919 _OPERATOR_ISUB_METHODDEF
920 _OPERATOR_IMUL_METHODDEF
921 _OPERATOR_IMATMUL_METHODDEF
922 _OPERATOR_IFLOORDIV_METHODDEF
923 _OPERATOR_ITRUEDIV_METHODDEF
924 _OPERATOR_IMOD_METHODDEF
925 _OPERATOR_ILSHIFT_METHODDEF
926 _OPERATOR_IRSHIFT_METHODDEF
927 _OPERATOR_IAND_METHODDEF
928 _OPERATOR_IXOR_METHODDEF
929 _OPERATOR_IOR_METHODDEF
930 _OPERATOR_CONCAT_METHODDEF
931 _OPERATOR_ICONCAT_METHODDEF
932 _OPERATOR_GETITEM_METHODDEF
933 _OPERATOR_SETITEM_METHODDEF
934 _OPERATOR_DELITEM_METHODDEF
935 _OPERATOR_POW_METHODDEF
936 _OPERATOR_IPOW_METHODDEF
937 _OPERATOR_EQ_METHODDEF
938 _OPERATOR_NE_METHODDEF
939 _OPERATOR_LT_METHODDEF
940 _OPERATOR_LE_METHODDEF
941 _OPERATOR_GT_METHODDEF
942 _OPERATOR_GE_METHODDEF
943 _OPERATOR__COMPARE_DIGEST_METHODDEF
944 _OPERATOR_LENGTH_HINT_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000945 {NULL, NULL} /* sentinel */
Guido van Rossum037b9401996-07-30 16:55:54 +0000946
Guido van Rossum037b9401996-07-30 16:55:54 +0000947};
948
Raymond Hettinger166958b2003-12-01 13:18:39 +0000949/* itemgetter object **********************************************************/
Guido van Rossum037b9401996-07-30 16:55:54 +0000950
Raymond Hettinger166958b2003-12-01 13:18:39 +0000951typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000952 PyObject_HEAD
953 Py_ssize_t nitems;
954 PyObject *item;
Raymond Hettinger2d53bed2019-01-07 09:38:41 -0700955 Py_ssize_t index; // -1 unless *item* is a single non-negative integer index
Raymond Hettinger166958b2003-12-01 13:18:39 +0000956} itemgetterobject;
957
Serhiy Storchakab813a0e2017-01-19 17:44:13 +0200958/* AC 3.5: treats first argument as an iterable, otherwise uses *args */
Raymond Hettinger166958b2003-12-01 13:18:39 +0000959static PyObject *
960itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
961{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 itemgetterobject *ig;
963 PyObject *item;
964 Py_ssize_t nitems;
Raymond Hettinger2d53bed2019-01-07 09:38:41 -0700965 Py_ssize_t index;
Raymond Hettinger166958b2003-12-01 13:18:39 +0000966
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +0300967 if (!_PyArg_NoKeywords("itemgetter", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +0000969
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000970 nitems = PyTuple_GET_SIZE(args);
971 if (nitems <= 1) {
972 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
973 return NULL;
Dong-hee Na31967fd2020-08-26 17:22:27 +0000974 } else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000975 item = args;
Dong-hee Na31967fd2020-08-26 17:22:27 +0000976 }
977 _operator_state *state = PyType_GetModuleState(type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000978 /* create itemgetterobject structure */
Dong-hee Na31967fd2020-08-26 17:22:27 +0000979 ig = PyObject_GC_New(itemgetterobject, (PyTypeObject *) state->itemgetter_type);
980 if (ig == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000981 return NULL;
Dong-hee Na31967fd2020-08-26 17:22:27 +0000982 }
Raymond Hettinger166958b2003-12-01 13:18:39 +0000983
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000984 Py_INCREF(item);
985 ig->item = item;
986 ig->nitems = nitems;
Raymond Hettinger2d53bed2019-01-07 09:38:41 -0700987 ig->index = -1;
988 if (PyLong_CheckExact(item)) {
989 index = PyLong_AsSsize_t(item);
990 if (index < 0) {
991 /* If we get here, then either the index conversion failed
992 * due to being out of range, or the index was a negative
993 * integer. Either way, we clear any possible exception
994 * and fall back to the slow path, where ig->index is -1.
995 */
996 PyErr_Clear();
997 }
998 else {
999 ig->index = index;
1000 }
1001 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001002
1003 PyObject_GC_Track(ig);
1004 return (PyObject *)ig;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001005}
1006
1007static void
1008itemgetter_dealloc(itemgetterobject *ig)
1009{
Dong-hee Na31967fd2020-08-26 17:22:27 +00001010 PyTypeObject *tp = Py_TYPE(ig);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001011 PyObject_GC_UnTrack(ig);
1012 Py_XDECREF(ig->item);
Dong-hee Na31967fd2020-08-26 17:22:27 +00001013 tp->tp_free(ig);
1014 Py_DECREF(tp);
Raymond Hettinger166958b2003-12-01 13:18:39 +00001015}
1016
1017static int
1018itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
1019{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001020 Py_VISIT(ig->item);
1021 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001022}
1023
1024static PyObject *
1025itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
1026{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001027 PyObject *obj, *result;
1028 Py_ssize_t i, nitems=ig->nitems;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001029
Raymond Hettinger2d53bed2019-01-07 09:38:41 -07001030 assert(PyTuple_CheckExact(args));
Serhiy Storchaka79342662019-01-12 08:25:41 +02001031 if (!_PyArg_NoKeywords("itemgetter", kw))
1032 return NULL;
1033 if (!_PyArg_CheckPositional("itemgetter", PyTuple_GET_SIZE(args), 1, 1))
1034 return NULL;
1035
1036 obj = PyTuple_GET_ITEM(args, 0);
Raymond Hettinger2d53bed2019-01-07 09:38:41 -07001037 if (nitems == 1) {
1038 if (ig->index >= 0
1039 && PyTuple_CheckExact(obj)
1040 && ig->index < PyTuple_GET_SIZE(obj))
1041 {
1042 result = PyTuple_GET_ITEM(obj, ig->index);
1043 Py_INCREF(result);
1044 return result;
1045 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001046 return PyObject_GetItem(obj, ig->item);
Raymond Hettinger2d53bed2019-01-07 09:38:41 -07001047 }
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001048
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 assert(PyTuple_Check(ig->item));
1050 assert(PyTuple_GET_SIZE(ig->item) == nitems);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001051
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001052 result = PyTuple_New(nitems);
1053 if (result == NULL)
1054 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001055
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001056 for (i=0 ; i < nitems ; i++) {
1057 PyObject *item, *val;
1058 item = PyTuple_GET_ITEM(ig->item, i);
1059 val = PyObject_GetItem(obj, item);
1060 if (val == NULL) {
1061 Py_DECREF(result);
1062 return NULL;
1063 }
1064 PyTuple_SET_ITEM(result, i, val);
1065 }
1066 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001067}
1068
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001069static PyObject *
1070itemgetter_repr(itemgetterobject *ig)
1071{
1072 PyObject *repr;
1073 const char *reprfmt;
1074
1075 int status = Py_ReprEnter((PyObject *)ig);
1076 if (status != 0) {
1077 if (status < 0)
1078 return NULL;
1079 return PyUnicode_FromFormat("%s(...)", Py_TYPE(ig)->tp_name);
1080 }
1081
1082 reprfmt = ig->nitems == 1 ? "%s(%R)" : "%s%R";
1083 repr = PyUnicode_FromFormat(reprfmt, Py_TYPE(ig)->tp_name, ig->item);
1084 Py_ReprLeave((PyObject *)ig);
1085 return repr;
1086}
1087
1088static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301089itemgetter_reduce(itemgetterobject *ig, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001090{
1091 if (ig->nitems == 1)
1092 return Py_BuildValue("O(O)", Py_TYPE(ig), ig->item);
1093 return PyTuple_Pack(2, Py_TYPE(ig), ig->item);
1094}
1095
1096PyDoc_STRVAR(reduce_doc, "Return state information for pickling");
1097
1098static PyMethodDef itemgetter_methods[] = {
1099 {"__reduce__", (PyCFunction)itemgetter_reduce, METH_NOARGS,
1100 reduce_doc},
1101 {NULL}
1102};
1103
Raymond Hettinger166958b2003-12-01 13:18:39 +00001104PyDoc_STRVAR(itemgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001105"itemgetter(item, ...) --> itemgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +00001106\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001107Return a callable object that fetches the given item(s) from its operand.\n\
Ezio Melottibabc8222013-05-08 10:53:11 +03001108After f = itemgetter(2), the call f(r) returns r[2].\n\
1109After g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");
Raymond Hettinger166958b2003-12-01 13:18:39 +00001110
Dong-hee Na31967fd2020-08-26 17:22:27 +00001111static PyType_Slot itemgetter_type_slots[] = {
1112 {Py_tp_doc, (void *)itemgetter_doc},
1113 {Py_tp_dealloc, itemgetter_dealloc},
1114 {Py_tp_call, itemgetter_call},
1115 {Py_tp_traverse, itemgetter_traverse},
1116 {Py_tp_methods, itemgetter_methods},
1117 {Py_tp_new, itemgetter_new},
1118 {Py_tp_getattro, PyObject_GenericGetAttr},
1119 {Py_tp_repr, itemgetter_repr},
1120 {0, 0}
Raymond Hettinger166958b2003-12-01 13:18:39 +00001121};
1122
Dong-hee Na31967fd2020-08-26 17:22:27 +00001123static PyType_Spec itemgetter_type_spec = {
1124 .name = "operator.itemgetter",
1125 .basicsize = sizeof(itemgetterobject),
1126 .itemsize = 0,
1127 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
1128 .slots = itemgetter_type_slots,
1129};
Raymond Hettinger166958b2003-12-01 13:18:39 +00001130
1131/* attrgetter object **********************************************************/
1132
1133typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001134 PyObject_HEAD
1135 Py_ssize_t nattrs;
1136 PyObject *attr;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001137} attrgetterobject;
1138
Serhiy Storchakab813a0e2017-01-19 17:44:13 +02001139/* AC 3.5: treats first argument as an iterable, otherwise uses *args */
Raymond Hettinger166958b2003-12-01 13:18:39 +00001140static PyObject *
1141attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1142{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001143 attrgetterobject *ag;
1144 PyObject *attr;
Antoine Pitroue9745712010-10-31 15:26:04 +00001145 Py_ssize_t nattrs, idx, char_idx;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001146
Serhiy Storchaka6cca5c82017-06-08 14:41:19 +03001147 if (!_PyArg_NoKeywords("attrgetter", kwds))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001148 return NULL;
Georg Brandl02c42872005-08-26 06:42:30 +00001149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001150 nattrs = PyTuple_GET_SIZE(args);
1151 if (nattrs <= 1) {
1152 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
1153 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +00001154 }
1155
1156 attr = PyTuple_New(nattrs);
1157 if (attr == NULL)
1158 return NULL;
1159
1160 /* prepare attr while checking args */
1161 for (idx = 0; idx < nattrs; ++idx) {
1162 PyObject *item = PyTuple_GET_ITEM(args, idx);
1163 Py_ssize_t item_len;
Serhiy Storchakacd8295f2020-04-11 10:48:40 +03001164 const void *data;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001165 unsigned int kind;
Antoine Pitroue9745712010-10-31 15:26:04 +00001166 int dot_count;
1167
1168 if (!PyUnicode_Check(item)) {
1169 PyErr_SetString(PyExc_TypeError,
1170 "attribute name must be a string");
1171 Py_DECREF(attr);
1172 return NULL;
1173 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001174 if (PyUnicode_READY(item)) {
1175 Py_DECREF(attr);
1176 return NULL;
1177 }
1178 item_len = PyUnicode_GET_LENGTH(item);
1179 kind = PyUnicode_KIND(item);
1180 data = PyUnicode_DATA(item);
Antoine Pitroue9745712010-10-31 15:26:04 +00001181
1182 /* check whethere the string is dotted */
1183 dot_count = 0;
1184 for (char_idx = 0; char_idx < item_len; ++char_idx) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001185 if (PyUnicode_READ(kind, data, char_idx) == '.')
Antoine Pitroue9745712010-10-31 15:26:04 +00001186 ++dot_count;
1187 }
1188
1189 if (dot_count == 0) {
1190 Py_INCREF(item);
1191 PyUnicode_InternInPlace(&item);
1192 PyTuple_SET_ITEM(attr, idx, item);
1193 } else { /* make it a tuple of non-dotted attrnames */
1194 PyObject *attr_chain = PyTuple_New(dot_count + 1);
1195 PyObject *attr_chain_item;
Antoine Pitrou87298c42010-10-31 21:03:01 +00001196 Py_ssize_t unibuff_from = 0;
1197 Py_ssize_t unibuff_till = 0;
1198 Py_ssize_t attr_chain_idx = 0;
Antoine Pitroue9745712010-10-31 15:26:04 +00001199
1200 if (attr_chain == NULL) {
1201 Py_DECREF(attr);
1202 return NULL;
1203 }
1204
Antoine Pitroue9745712010-10-31 15:26:04 +00001205 for (; dot_count > 0; --dot_count) {
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001206 while (PyUnicode_READ(kind, data, unibuff_till) != '.') {
Antoine Pitroue9745712010-10-31 15:26:04 +00001207 ++unibuff_till;
1208 }
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001209 attr_chain_item = PyUnicode_Substring(item,
1210 unibuff_from,
1211 unibuff_till);
Antoine Pitroue9745712010-10-31 15:26:04 +00001212 if (attr_chain_item == NULL) {
1213 Py_DECREF(attr_chain);
1214 Py_DECREF(attr);
1215 return NULL;
1216 }
1217 PyUnicode_InternInPlace(&attr_chain_item);
1218 PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
1219 ++attr_chain_idx;
1220 unibuff_till = unibuff_from = unibuff_till + 1;
1221 }
1222
1223 /* now add the last dotless name */
Martin v. Löwisd63a3b82011-09-28 07:41:54 +02001224 attr_chain_item = PyUnicode_Substring(item,
1225 unibuff_from, item_len);
Antoine Pitroue9745712010-10-31 15:26:04 +00001226 if (attr_chain_item == NULL) {
1227 Py_DECREF(attr_chain);
1228 Py_DECREF(attr);
1229 return NULL;
1230 }
1231 PyUnicode_InternInPlace(&attr_chain_item);
1232 PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
1233
1234 PyTuple_SET_ITEM(attr, idx, attr_chain);
1235 }
1236 }
Raymond Hettinger166958b2003-12-01 13:18:39 +00001237
Dong-hee Na31967fd2020-08-26 17:22:27 +00001238 _operator_state *state = PyType_GetModuleState(type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001239 /* create attrgetterobject structure */
Dong-hee Na31967fd2020-08-26 17:22:27 +00001240 ag = PyObject_GC_New(attrgetterobject, (PyTypeObject *)state->attrgetter_type);
Antoine Pitroue9745712010-10-31 15:26:04 +00001241 if (ag == NULL) {
1242 Py_DECREF(attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001243 return NULL;
Antoine Pitroue9745712010-10-31 15:26:04 +00001244 }
Raymond Hettinger166958b2003-12-01 13:18:39 +00001245
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001246 ag->attr = attr;
1247 ag->nattrs = nattrs;
1248
1249 PyObject_GC_Track(ag);
1250 return (PyObject *)ag;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001251}
1252
1253static void
1254attrgetter_dealloc(attrgetterobject *ag)
1255{
Dong-hee Na31967fd2020-08-26 17:22:27 +00001256 PyTypeObject *tp = Py_TYPE(ag);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001257 PyObject_GC_UnTrack(ag);
1258 Py_XDECREF(ag->attr);
Dong-hee Na31967fd2020-08-26 17:22:27 +00001259 tp->tp_free(ag);
1260 Py_DECREF(tp);
Raymond Hettinger166958b2003-12-01 13:18:39 +00001261}
1262
1263static int
1264attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
1265{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001266 Py_VISIT(ag->attr);
1267 return 0;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001268}
1269
1270static PyObject *
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001271dotted_getattr(PyObject *obj, PyObject *attr)
1272{
Antoine Pitroue9745712010-10-31 15:26:04 +00001273 PyObject *newobj;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001274
Antoine Pitroue9745712010-10-31 15:26:04 +00001275 /* attr is either a tuple or instance of str.
1276 Ensured by the setup code of attrgetter_new */
1277 if (PyTuple_CheckExact(attr)) { /* chained getattr */
1278 Py_ssize_t name_idx = 0, name_count;
1279 PyObject *attr_name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001280
Antoine Pitroue9745712010-10-31 15:26:04 +00001281 name_count = PyTuple_GET_SIZE(attr);
1282 Py_INCREF(obj);
1283 for (name_idx = 0; name_idx < name_count; ++name_idx) {
1284 attr_name = PyTuple_GET_ITEM(attr, name_idx);
1285 newobj = PyObject_GetAttr(obj, attr_name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001286 Py_DECREF(obj);
Antoine Pitroue9745712010-10-31 15:26:04 +00001287 if (newobj == NULL) {
1288 return NULL;
1289 }
1290 /* here */
1291 obj = newobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001292 }
Antoine Pitroue9745712010-10-31 15:26:04 +00001293 } else { /* single getattr */
1294 newobj = PyObject_GetAttr(obj, attr);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001295 if (newobj == NULL)
1296 return NULL;
1297 obj = newobj;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001298 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001299
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001300 return obj;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001301}
1302
1303static PyObject *
Raymond Hettinger166958b2003-12-01 13:18:39 +00001304attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
1305{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001306 PyObject *obj, *result;
1307 Py_ssize_t i, nattrs=ag->nattrs;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001308
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02001309 if (!_PyArg_NoKeywords("attrgetter", kw))
Serhiy Storchakac2a2a752016-04-23 10:51:39 +03001310 return NULL;
Serhiy Storchaka79342662019-01-12 08:25:41 +02001311 if (!_PyArg_CheckPositional("attrgetter", PyTuple_GET_SIZE(args), 1, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001312 return NULL;
Serhiy Storchaka79342662019-01-12 08:25:41 +02001313 obj = PyTuple_GET_ITEM(args, 0);
Antoine Pitroue9745712010-10-31 15:26:04 +00001314 if (ag->nattrs == 1) /* ag->attr is always a tuple */
1315 return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0));
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001316
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001317 assert(PyTuple_Check(ag->attr));
1318 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001319
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001320 result = PyTuple_New(nattrs);
1321 if (result == NULL)
1322 return NULL;
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001323
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001324 for (i=0 ; i < nattrs ; i++) {
1325 PyObject *attr, *val;
1326 attr = PyTuple_GET_ITEM(ag->attr, i);
1327 val = dotted_getattr(obj, attr);
1328 if (val == NULL) {
1329 Py_DECREF(result);
1330 return NULL;
1331 }
1332 PyTuple_SET_ITEM(result, i, val);
1333 }
1334 return result;
Raymond Hettinger166958b2003-12-01 13:18:39 +00001335}
1336
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001337static PyObject *
1338dotjoinattr(PyObject *attr, PyObject **attrsep)
1339{
1340 if (PyTuple_CheckExact(attr)) {
1341 if (*attrsep == NULL) {
1342 *attrsep = PyUnicode_FromString(".");
1343 if (*attrsep == NULL)
1344 return NULL;
1345 }
1346 return PyUnicode_Join(*attrsep, attr);
1347 } else {
1348 Py_INCREF(attr);
1349 return attr;
1350 }
1351}
1352
1353static PyObject *
1354attrgetter_args(attrgetterobject *ag)
1355{
1356 Py_ssize_t i;
1357 PyObject *attrsep = NULL;
1358 PyObject *attrstrings = PyTuple_New(ag->nattrs);
1359 if (attrstrings == NULL)
1360 return NULL;
1361
1362 for (i = 0; i < ag->nattrs; ++i) {
1363 PyObject *attr = PyTuple_GET_ITEM(ag->attr, i);
1364 PyObject *attrstr = dotjoinattr(attr, &attrsep);
1365 if (attrstr == NULL) {
1366 Py_XDECREF(attrsep);
1367 Py_DECREF(attrstrings);
1368 return NULL;
1369 }
1370 PyTuple_SET_ITEM(attrstrings, i, attrstr);
1371 }
1372 Py_XDECREF(attrsep);
1373 return attrstrings;
1374}
1375
1376static PyObject *
1377attrgetter_repr(attrgetterobject *ag)
1378{
1379 PyObject *repr = NULL;
1380 int status = Py_ReprEnter((PyObject *)ag);
1381 if (status != 0) {
1382 if (status < 0)
1383 return NULL;
1384 return PyUnicode_FromFormat("%s(...)", Py_TYPE(ag)->tp_name);
1385 }
1386
1387 if (ag->nattrs == 1) {
1388 PyObject *attrsep = NULL;
1389 PyObject *attr = dotjoinattr(PyTuple_GET_ITEM(ag->attr, 0), &attrsep);
Serhiy Storchaka548de2b2015-05-21 14:19:20 +03001390 if (attr != NULL) {
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001391 repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(ag)->tp_name, attr);
Serhiy Storchaka548de2b2015-05-21 14:19:20 +03001392 Py_DECREF(attr);
1393 }
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001394 Py_XDECREF(attrsep);
1395 }
1396 else {
1397 PyObject *attrstrings = attrgetter_args(ag);
1398 if (attrstrings != NULL) {
1399 repr = PyUnicode_FromFormat("%s%R",
1400 Py_TYPE(ag)->tp_name, attrstrings);
1401 Py_DECREF(attrstrings);
1402 }
1403 }
1404 Py_ReprLeave((PyObject *)ag);
1405 return repr;
1406}
1407
1408static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301409attrgetter_reduce(attrgetterobject *ag, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001410{
1411 PyObject *attrstrings = attrgetter_args(ag);
1412 if (attrstrings == NULL)
1413 return NULL;
1414
1415 return Py_BuildValue("ON", Py_TYPE(ag), attrstrings);
1416}
1417
1418static PyMethodDef attrgetter_methods[] = {
1419 {"__reduce__", (PyCFunction)attrgetter_reduce, METH_NOARGS,
1420 reduce_doc},
1421 {NULL}
1422};
1423
Raymond Hettinger166958b2003-12-01 13:18:39 +00001424PyDoc_STRVAR(attrgetter_doc,
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001425"attrgetter(attr, ...) --> attrgetter object\n\
Raymond Hettinger166958b2003-12-01 13:18:39 +00001426\n\
Raymond Hettinger984f9bb2005-03-09 16:38:48 +00001427Return a callable object that fetches the given attribute(s) from its operand.\n\
Ezio Melottibabc8222013-05-08 10:53:11 +03001428After f = attrgetter('name'), the call f(r) returns r.name.\n\
1429After g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
1430After h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001431(r.name.first, r.name.last).");
Raymond Hettinger166958b2003-12-01 13:18:39 +00001432
Dong-hee Na31967fd2020-08-26 17:22:27 +00001433static PyType_Slot attrgetter_type_slots[] = {
1434 {Py_tp_doc, (void *)attrgetter_doc},
1435 {Py_tp_dealloc, attrgetter_dealloc},
1436 {Py_tp_call, attrgetter_call},
1437 {Py_tp_traverse, attrgetter_traverse},
1438 {Py_tp_methods, attrgetter_methods},
1439 {Py_tp_new, attrgetter_new},
1440 {Py_tp_getattro, PyObject_GenericGetAttr},
1441 {Py_tp_repr, attrgetter_repr},
1442 {0, 0}
1443};
1444
1445static PyType_Spec attrgetter_type_spec = {
1446 .name = "operator.attrgetter",
1447 .basicsize = sizeof(attrgetterobject),
1448 .itemsize = 0,
1449 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
1450 .slots = attrgetter_type_slots,
Raymond Hettinger166958b2003-12-01 13:18:39 +00001451};
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001452
1453
1454/* methodcaller object **********************************************************/
1455
1456typedef struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001457 PyObject_HEAD
1458 PyObject *name;
1459 PyObject *args;
1460 PyObject *kwds;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001461} methodcallerobject;
1462
Serhiy Storchakab813a0e2017-01-19 17:44:13 +02001463/* AC 3.5: variable number of arguments, not currently support by AC */
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001464static PyObject *
1465methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1466{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001467 methodcallerobject *mc;
Benjamin Peterson1f0e7c92016-08-16 23:35:35 -07001468 PyObject *name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001469
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001470 if (PyTuple_GET_SIZE(args) < 1) {
1471 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
1472 "one argument, the method name");
1473 return NULL;
1474 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001475
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001476 name = PyTuple_GET_ITEM(args, 0);
1477 if (!PyUnicode_Check(name)) {
1478 PyErr_SetString(PyExc_TypeError,
1479 "method name must be a string");
1480 return NULL;
1481 }
1482
Dong-hee Na31967fd2020-08-26 17:22:27 +00001483 _operator_state *state = PyType_GetModuleState(type);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001484 /* create methodcallerobject structure */
Dong-hee Na31967fd2020-08-26 17:22:27 +00001485 mc = PyObject_GC_New(methodcallerobject, (PyTypeObject *)state->methodcaller_type);
1486 if (mc == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001487 return NULL;
Dong-hee Na31967fd2020-08-26 17:22:27 +00001488 }
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001489
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001490 name = PyTuple_GET_ITEM(args, 0);
1491 Py_INCREF(name);
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001492 PyUnicode_InternInPlace(&name);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001493 mc->name = name;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001494
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001495 Py_XINCREF(kwds);
1496 mc->kwds = kwds;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001497
Benjamin Peterson1f0e7c92016-08-16 23:35:35 -07001498 mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
1499 if (mc->args == NULL) {
1500 Py_DECREF(mc);
1501 return NULL;
1502 }
1503
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001504 PyObject_GC_Track(mc);
1505 return (PyObject *)mc;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001506}
1507
1508static void
1509methodcaller_dealloc(methodcallerobject *mc)
1510{
Dong-hee Na31967fd2020-08-26 17:22:27 +00001511 PyTypeObject *tp = Py_TYPE(mc);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001512 PyObject_GC_UnTrack(mc);
1513 Py_XDECREF(mc->name);
1514 Py_XDECREF(mc->args);
1515 Py_XDECREF(mc->kwds);
Dong-hee Na31967fd2020-08-26 17:22:27 +00001516 tp->tp_free(mc);
1517 Py_DECREF(tp);
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001518}
1519
1520static int
1521methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
1522{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001523 Py_VISIT(mc->args);
1524 Py_VISIT(mc->kwds);
1525 return 0;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001526}
1527
1528static PyObject *
1529methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
1530{
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001531 PyObject *method, *obj, *result;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001532
Serhiy Storchaka68a001d2017-02-06 10:41:46 +02001533 if (!_PyArg_NoKeywords("methodcaller", kw))
Serhiy Storchakac2a2a752016-04-23 10:51:39 +03001534 return NULL;
Serhiy Storchaka79342662019-01-12 08:25:41 +02001535 if (!_PyArg_CheckPositional("methodcaller", PyTuple_GET_SIZE(args), 1, 1))
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001536 return NULL;
Serhiy Storchaka79342662019-01-12 08:25:41 +02001537 obj = PyTuple_GET_ITEM(args, 0);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001538 method = PyObject_GetAttr(obj, mc->name);
1539 if (method == NULL)
1540 return NULL;
1541 result = PyObject_Call(method, mc->args, mc->kwds);
1542 Py_DECREF(method);
1543 return result;
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001544}
1545
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001546static PyObject *
1547methodcaller_repr(methodcallerobject *mc)
1548{
1549 PyObject *argreprs, *repr = NULL, *sep, *joinedargreprs;
1550 Py_ssize_t numtotalargs, numposargs, numkwdargs, i;
1551 int status = Py_ReprEnter((PyObject *)mc);
1552 if (status != 0) {
1553 if (status < 0)
1554 return NULL;
1555 return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name);
1556 }
1557
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001558 numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0;
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001559 numposargs = PyTuple_GET_SIZE(mc->args);
1560 numtotalargs = numposargs + numkwdargs;
1561
1562 if (numtotalargs == 0) {
1563 repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(mc)->tp_name, mc->name);
1564 Py_ReprLeave((PyObject *)mc);
1565 return repr;
1566 }
1567
1568 argreprs = PyTuple_New(numtotalargs);
1569 if (argreprs == NULL) {
1570 Py_ReprLeave((PyObject *)mc);
1571 return NULL;
1572 }
1573
1574 for (i = 0; i < numposargs; ++i) {
1575 PyObject *onerepr = PyObject_Repr(PyTuple_GET_ITEM(mc->args, i));
1576 if (onerepr == NULL)
1577 goto done;
1578 PyTuple_SET_ITEM(argreprs, i, onerepr);
1579 }
1580
1581 if (numkwdargs != 0) {
1582 PyObject *key, *value;
1583 Py_ssize_t pos = 0;
1584 while (PyDict_Next(mc->kwds, &pos, &key, &value)) {
1585 PyObject *onerepr = PyUnicode_FromFormat("%U=%R", key, value);
1586 if (onerepr == NULL)
1587 goto done;
1588 if (i >= numtotalargs) {
1589 i = -1;
Zackery Spytz5b83ef72018-11-23 12:26:46 -07001590 Py_DECREF(onerepr);
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001591 break;
1592 }
1593 PyTuple_SET_ITEM(argreprs, i, onerepr);
1594 ++i;
1595 }
1596 if (i != numtotalargs) {
1597 PyErr_SetString(PyExc_RuntimeError,
1598 "keywords dict changed size during iteration");
1599 goto done;
1600 }
1601 }
1602
1603 sep = PyUnicode_FromString(", ");
1604 if (sep == NULL)
1605 goto done;
1606
1607 joinedargreprs = PyUnicode_Join(sep, argreprs);
1608 Py_DECREF(sep);
1609 if (joinedargreprs == NULL)
1610 goto done;
1611
1612 repr = PyUnicode_FromFormat("%s(%R, %U)", Py_TYPE(mc)->tp_name,
1613 mc->name, joinedargreprs);
1614 Py_DECREF(joinedargreprs);
1615
1616done:
1617 Py_DECREF(argreprs);
1618 Py_ReprLeave((PyObject *)mc);
1619 return repr;
1620}
1621
1622static PyObject *
Siddhesh Poyarekar55edd0c2018-04-30 00:29:33 +05301623methodcaller_reduce(methodcallerobject *mc, PyObject *Py_UNUSED(ignored))
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001624{
1625 PyObject *newargs;
Serhiy Storchaka5ab81d72016-12-16 16:18:57 +02001626 if (!mc->kwds || PyDict_GET_SIZE(mc->kwds) == 0) {
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001627 Py_ssize_t i;
1628 Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args);
1629 newargs = PyTuple_New(1 + callargcount);
1630 if (newargs == NULL)
1631 return NULL;
1632 Py_INCREF(mc->name);
1633 PyTuple_SET_ITEM(newargs, 0, mc->name);
1634 for (i = 0; i < callargcount; ++i) {
1635 PyObject *arg = PyTuple_GET_ITEM(mc->args, i);
1636 Py_INCREF(arg);
1637 PyTuple_SET_ITEM(newargs, i + 1, arg);
1638 }
1639 return Py_BuildValue("ON", Py_TYPE(mc), newargs);
1640 }
1641 else {
1642 PyObject *functools;
1643 PyObject *partial;
1644 PyObject *constructor;
Victor Stinner7e7823a2016-08-23 00:23:23 +02001645 PyObject *newargs[2];
1646
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001647 _Py_IDENTIFIER(partial);
1648 functools = PyImport_ImportModule("functools");
1649 if (!functools)
1650 return NULL;
1651 partial = _PyObject_GetAttrId(functools, &PyId_partial);
1652 Py_DECREF(functools);
1653 if (!partial)
1654 return NULL;
Victor Stinner7e7823a2016-08-23 00:23:23 +02001655
1656 newargs[0] = (PyObject *)Py_TYPE(mc);
1657 newargs[1] = mc->name;
Petr Viktorinffd97532020-02-11 17:46:57 +01001658 constructor = PyObject_VectorcallDict(partial, newargs, 2, mc->kwds);
Victor Stinner7e7823a2016-08-23 00:23:23 +02001659
Serhiy Storchaka35ac5f82015-05-20 18:29:18 +03001660 Py_DECREF(partial);
1661 return Py_BuildValue("NO", constructor, mc->args);
1662 }
1663}
1664
1665static PyMethodDef methodcaller_methods[] = {
1666 {"__reduce__", (PyCFunction)methodcaller_reduce, METH_NOARGS,
1667 reduce_doc},
1668 {NULL}
1669};
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001670PyDoc_STRVAR(methodcaller_doc,
1671"methodcaller(name, ...) --> methodcaller object\n\
1672\n\
1673Return a callable object that calls the given method on its operand.\n\
Antoine Pitroua85017f2013-04-20 19:21:44 +02001674After f = methodcaller('name'), the call f(r) returns r.name().\n\
1675After g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001676r.name('date', foo=1).");
1677
Dong-hee Na31967fd2020-08-26 17:22:27 +00001678static PyType_Slot methodcaller_type_slots[] = {
1679 {Py_tp_doc, (void *)methodcaller_doc},
1680 {Py_tp_dealloc, methodcaller_dealloc},
1681 {Py_tp_call, methodcaller_call},
1682 {Py_tp_traverse, methodcaller_traverse},
1683 {Py_tp_methods, methodcaller_methods},
1684 {Py_tp_new, methodcaller_new},
1685 {Py_tp_getattro, PyObject_GenericGetAttr},
1686 {Py_tp_repr, methodcaller_repr},
1687 {0, 0}
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001688};
1689
Dong-hee Na31967fd2020-08-26 17:22:27 +00001690static PyType_Spec methodcaller_type_spec = {
1691 .name = "operator.methodcaller",
1692 .basicsize = sizeof(methodcallerobject),
1693 .itemsize = 0,
1694 .flags = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,
1695 .slots = methodcaller_type_slots,
1696};
Christian Heimesd3eb5a152008-02-24 00:38:49 +00001697
Paulo Henrique Silvaf3d5ac42020-03-24 23:18:47 -03001698static int
1699operator_exec(PyObject *module)
1700{
Dong-hee Na31967fd2020-08-26 17:22:27 +00001701 _operator_state *state = get_operator_state(module);
1702 state->attrgetter_type = PyType_FromModuleAndSpec(module, &attrgetter_type_spec, NULL);
1703 if (state->attrgetter_type == NULL) {
1704 return -1;
1705 }
1706 if (PyModule_AddType(module, (PyTypeObject *)state->attrgetter_type) < 0) {
1707 return -1;
1708 }
Paulo Henrique Silvaf3d5ac42020-03-24 23:18:47 -03001709
Dong-hee Na31967fd2020-08-26 17:22:27 +00001710 state->itemgetter_type = PyType_FromModuleAndSpec(module, &itemgetter_type_spec, NULL);
1711 if (state->itemgetter_type == NULL) {
1712 return -1;
1713 }
1714 if (PyModule_AddType(module, (PyTypeObject *)state->itemgetter_type) < 0) {
1715 return -1;
1716 }
1717
1718 state->methodcaller_type = PyType_FromModuleAndSpec(module, &methodcaller_type_spec, NULL);
1719 if (state->methodcaller_type == NULL) {
1720 return -1;
1721 }
1722 if (PyModule_AddType(module, (PyTypeObject *)state->methodcaller_type) < 0) {
1723 return -1;
Paulo Henrique Silvaf3d5ac42020-03-24 23:18:47 -03001724 }
1725
1726 return 0;
1727}
1728
1729
1730static struct PyModuleDef_Slot operator_slots[] = {
1731 {Py_mod_exec, operator_exec},
1732 {0, NULL}
1733};
Martin v. Löwis1a214512008-06-11 05:26:20 +00001734
Dong-hee Na31967fd2020-08-26 17:22:27 +00001735static int
1736operator_traverse(PyObject *module, visitproc visit, void *arg)
1737{
1738 _operator_state *state = get_operator_state(module);
1739 Py_VISIT(state->attrgetter_type);
1740 Py_VISIT(state->itemgetter_type);
1741 Py_VISIT(state->methodcaller_type);
1742 return 0;
1743}
1744
1745static int
1746operator_clear(PyObject *module)
1747{
1748 _operator_state *state = get_operator_state(module);
1749 Py_CLEAR(state->attrgetter_type);
1750 Py_CLEAR(state->itemgetter_type);
1751 Py_CLEAR(state->methodcaller_type);
1752 return 0;
1753}
1754
1755static void
1756operator_free(void *module)
1757{
1758 operator_clear((PyObject *)module);
1759}
Martin v. Löwis1a214512008-06-11 05:26:20 +00001760
1761static struct PyModuleDef operatormodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001762 PyModuleDef_HEAD_INIT,
Dong-hee Na31967fd2020-08-26 17:22:27 +00001763 .m_name = "_operator",
1764 .m_doc = operator_doc,
1765 .m_size = sizeof(_operator_state),
1766 .m_methods = operator_methods,
1767 .m_slots = operator_slots,
1768 .m_traverse = operator_traverse,
1769 .m_clear = operator_clear,
1770 .m_free = operator_free,
Martin v. Löwis1a214512008-06-11 05:26:20 +00001771};
Guido van Rossum037b9401996-07-30 16:55:54 +00001772
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001773PyMODINIT_FUNC
Antoine Pitroua85017f2013-04-20 19:21:44 +02001774PyInit__operator(void)
Guido van Rossum037b9401996-07-30 16:55:54 +00001775{
Paulo Henrique Silvaf3d5ac42020-03-24 23:18:47 -03001776 return PyModuleDef_Init(&operatormodule);
Guido van Rossum037b9401996-07-30 16:55:54 +00001777}