ยปCore Development>Code coverage>Modules/_operator.c

Python code coverage for Modules/_operator.c

#countcontent
1n/a
2n/a#include "Python.h"
3n/a
4n/a#include "clinic/_operator.c.h"
5n/a
6n/a/*[clinic input]
7n/amodule _operator
8n/a[clinic start generated code]*/
9n/a/*[clinic end generated code: output=da39a3ee5e6b4b0d input=672ecf48487521e7]*/
10n/a
11n/aPyDoc_STRVAR(operator_doc,
12n/a"Operator interface.\n\
13n/a\n\
14n/aThis module exports a set of functions implemented in C corresponding\n\
15n/ato the intrinsic operators of Python. For example, operator.add(x, y)\n\
16n/ais equivalent to the expression x+y. The function names are those\n\
17n/aused for special methods; variants without leading and trailing\n\
18n/a'__' are also provided for convenience.");
19n/a
20n/a
21n/a/*[clinic input]
22n/a_operator.truth -> bool
23n/a
24n/a a: object
25n/a /
26n/a
27n/aReturn True if a is true, False otherwise.
28n/a[clinic start generated code]*/
29n/a
30n/astatic int
31n/a_operator_truth_impl(PyObject *module, PyObject *a)
32n/a/*[clinic end generated code: output=eaf87767234fa5d7 input=bc74a4cd90235875]*/
33n/a{
34n/a return PyObject_IsTrue(a);
35n/a}
36n/a
37n/a/*[clinic input]
38n/a_operator.add
39n/a
40n/a a: object
41n/a b: object
42n/a /
43n/a
44n/aSame as a + b.
45n/a[clinic start generated code]*/
46n/a
47n/astatic PyObject *
48n/a_operator_add_impl(PyObject *module, PyObject *a, PyObject *b)
49n/a/*[clinic end generated code: output=8292984204f45164 input=5efe3bff856ac215]*/
50n/a{
51n/a return PyNumber_Add(a, b);
52n/a}
53n/a
54n/a/*[clinic input]
55n/a_operator.sub = _operator.add
56n/a
57n/aSame as a - b.
58n/a[clinic start generated code]*/
59n/a
60n/astatic PyObject *
61n/a_operator_sub_impl(PyObject *module, PyObject *a, PyObject *b)
62n/a/*[clinic end generated code: output=4adfc3b888c1ee2e input=6494c6b100b8e795]*/
63n/a{
64n/a return PyNumber_Subtract(a, b);
65n/a}
66n/a
67n/a/*[clinic input]
68n/a_operator.mul = _operator.add
69n/a
70n/aSame as a * b.
71n/a[clinic start generated code]*/
72n/a
73n/astatic PyObject *
74n/a_operator_mul_impl(PyObject *module, PyObject *a, PyObject *b)
75n/a/*[clinic end generated code: output=d24d66f55a01944c input=2368615b4358b70d]*/
76n/a{
77n/a return PyNumber_Multiply(a, b);
78n/a}
79n/a
80n/a/*[clinic input]
81n/a_operator.matmul = _operator.add
82n/a
83n/aSame as a @ b.
84n/a[clinic start generated code]*/
85n/a
86n/astatic PyObject *
87n/a_operator_matmul_impl(PyObject *module, PyObject *a, PyObject *b)
88n/a/*[clinic end generated code: output=a20d917eb35d0101 input=9ab304e37fb42dd4]*/
89n/a{
90n/a return PyNumber_MatrixMultiply(a, b);
91n/a}
92n/a
93n/a/*[clinic input]
94n/a_operator.floordiv = _operator.add
95n/a
96n/aSame as a // b.
97n/a[clinic start generated code]*/
98n/a
99n/astatic PyObject *
100n/a_operator_floordiv_impl(PyObject *module, PyObject *a, PyObject *b)
101n/a/*[clinic end generated code: output=df26b71a60589f99 input=bb2e88ba446c612c]*/
102n/a{
103n/a return PyNumber_FloorDivide(a, b);
104n/a}
105n/a
106n/a/*[clinic input]
107n/a_operator.truediv = _operator.add
108n/a
109n/aSame as a / b.
110n/a[clinic start generated code]*/
111n/a
112n/astatic PyObject *
113n/a_operator_truediv_impl(PyObject *module, PyObject *a, PyObject *b)
114n/a/*[clinic end generated code: output=0e6a959944d77719 input=ecbb947673f4eb1f]*/
115n/a{
116n/a return PyNumber_TrueDivide(a, b);
117n/a}
118n/a
119n/a/*[clinic input]
120n/a_operator.mod = _operator.add
121n/a
122n/aSame as a % b.
123n/a[clinic start generated code]*/
124n/a
125n/astatic PyObject *
126n/a_operator_mod_impl(PyObject *module, PyObject *a, PyObject *b)
127n/a/*[clinic end generated code: output=9519822f0bbec166 input=102e19b422342ac1]*/
128n/a{
129n/a return PyNumber_Remainder(a, b);
130n/a}
131n/a
132n/a/*[clinic input]
133n/a_operator.neg
134n/a
135n/a a: object
136n/a /
137n/a
138n/aSame as -a.
139n/a[clinic start generated code]*/
140n/a
141n/astatic PyObject *
142n/a_operator_neg(PyObject *module, PyObject *a)
143n/a/*[clinic end generated code: output=36e08ecfc6a1c08c input=84f09bdcf27c96ec]*/
144n/a{
145n/a return PyNumber_Negative(a);
146n/a}
147n/a
148n/a/*[clinic input]
149n/a_operator.pos = _operator.neg
150n/a
151n/aSame as +a.
152n/a[clinic start generated code]*/
153n/a
154n/astatic PyObject *
155n/a_operator_pos(PyObject *module, PyObject *a)
156n/a/*[clinic end generated code: output=dad7a126221dd091 input=b6445b63fddb8772]*/
157n/a{
158n/a return PyNumber_Positive(a);
159n/a}
160n/a
161n/a/*[clinic input]
162n/a_operator.abs = _operator.neg
163n/a
164n/aSame as abs(a).
165n/a[clinic start generated code]*/
166n/a
167n/astatic PyObject *
168n/a_operator_abs(PyObject *module, PyObject *a)
169n/a/*[clinic end generated code: output=1389a93ba053ea3e input=341d07ba86f58039]*/
170n/a{
171n/a return PyNumber_Absolute(a);
172n/a}
173n/a
174n/a/*[clinic input]
175n/a_operator.inv = _operator.neg
176n/a
177n/aSame as ~a.
178n/a[clinic start generated code]*/
179n/a
180n/astatic PyObject *
181n/a_operator_inv(PyObject *module, PyObject *a)
182n/a/*[clinic end generated code: output=a56875ba075ee06d input=b01a4677739f6eb2]*/
183n/a{
184n/a return PyNumber_Invert(a);
185n/a}
186n/a
187n/a/*[clinic input]
188n/a_operator.invert = _operator.neg
189n/a
190n/aSame as ~a.
191n/a[clinic start generated code]*/
192n/a
193n/astatic PyObject *
194n/a_operator_invert(PyObject *module, PyObject *a)
195n/a/*[clinic end generated code: output=406b5aa030545fcc input=7f2d607176672e55]*/
196n/a{
197n/a return PyNumber_Invert(a);
198n/a}
199n/a
200n/a/*[clinic input]
201n/a_operator.lshift = _operator.add
202n/a
203n/aSame as a << b.
204n/a[clinic start generated code]*/
205n/a
206n/astatic PyObject *
207n/a_operator_lshift_impl(PyObject *module, PyObject *a, PyObject *b)
208n/a/*[clinic end generated code: output=37f7e52c41435bd8 input=746e8a160cbbc9eb]*/
209n/a{
210n/a return PyNumber_Lshift(a, b);
211n/a}
212n/a
213n/a/*[clinic input]
214n/a_operator.rshift = _operator.add
215n/a
216n/aSame as a >> b.
217n/a[clinic start generated code]*/
218n/a
219n/astatic PyObject *
220n/a_operator_rshift_impl(PyObject *module, PyObject *a, PyObject *b)
221n/a/*[clinic end generated code: output=4593c7ef30ec2ee3 input=d2c85bb5a64504c2]*/
222n/a{
223n/a return PyNumber_Rshift(a, b);
224n/a}
225n/a
226n/a/*[clinic input]
227n/a_operator.not_ = _operator.truth
228n/a
229n/aSame as not a.
230n/a[clinic start generated code]*/
231n/a
232n/astatic int
233n/a_operator_not__impl(PyObject *module, PyObject *a)
234n/a/*[clinic end generated code: output=743f9c24a09759ef input=854156d50804d9b8]*/
235n/a{
236n/a return PyObject_Not(a);
237n/a}
238n/a
239n/a/*[clinic input]
240n/a_operator.and_ = _operator.add
241n/a
242n/aSame as a & b.
243n/a[clinic start generated code]*/
244n/a
245n/astatic PyObject *
246n/a_operator_and__impl(PyObject *module, PyObject *a, PyObject *b)
247n/a/*[clinic end generated code: output=93c4fe88f7b76d9e input=4f3057c90ec4c99f]*/
248n/a{
249n/a return PyNumber_And(a, b);
250n/a}
251n/a
252n/a/*[clinic input]
253n/a_operator.xor = _operator.add
254n/a
255n/aSame as a ^ b.
256n/a[clinic start generated code]*/
257n/a
258n/astatic PyObject *
259n/a_operator_xor_impl(PyObject *module, PyObject *a, PyObject *b)
260n/a/*[clinic end generated code: output=b24cd8b79fde0004 input=3c5cfa7253d808dd]*/
261n/a{
262n/a return PyNumber_Xor(a, b);
263n/a}
264n/a
265n/a/*[clinic input]
266n/a_operator.or_ = _operator.add
267n/a
268n/aSame as a | b.
269n/a[clinic start generated code]*/
270n/a
271n/astatic PyObject *
272n/a_operator_or__impl(PyObject *module, PyObject *a, PyObject *b)
273n/a/*[clinic end generated code: output=58024867b8d90461 input=b40c6c44f7c79c09]*/
274n/a{
275n/a return PyNumber_Or(a, b);
276n/a}
277n/a
278n/a/*[clinic input]
279n/a_operator.iadd = _operator.add
280n/a
281n/aSame as a += b.
282n/a[clinic start generated code]*/
283n/a
284n/astatic PyObject *
285n/a_operator_iadd_impl(PyObject *module, PyObject *a, PyObject *b)
286n/a/*[clinic end generated code: output=07dc627832526eb5 input=d22a91c07ac69227]*/
287n/a{
288n/a return PyNumber_InPlaceAdd(a, b);
289n/a}
290n/a
291n/a/*[clinic input]
292n/a_operator.isub = _operator.add
293n/a
294n/aSame as a -= b.
295n/a[clinic start generated code]*/
296n/a
297n/astatic PyObject *
298n/a_operator_isub_impl(PyObject *module, PyObject *a, PyObject *b)
299n/a/*[clinic end generated code: output=4513467d23b5e0b1 input=4591b00d0a0ccafd]*/
300n/a{
301n/a return PyNumber_InPlaceSubtract(a, b);
302n/a}
303n/a
304n/a/*[clinic input]
305n/a_operator.imul = _operator.add
306n/a
307n/aSame as a *= b.
308n/a[clinic start generated code]*/
309n/a
310n/astatic PyObject *
311n/a_operator_imul_impl(PyObject *module, PyObject *a, PyObject *b)
312n/a/*[clinic end generated code: output=5e87dacd19a71eab input=0e01fb8631e1b76f]*/
313n/a{
314n/a return PyNumber_InPlaceMultiply(a, b);
315n/a}
316n/a
317n/a/*[clinic input]
318n/a_operator.imatmul = _operator.add
319n/a
320n/aSame as a @= b.
321n/a[clinic start generated code]*/
322n/a
323n/astatic PyObject *
324n/a_operator_imatmul_impl(PyObject *module, PyObject *a, PyObject *b)
325n/a/*[clinic end generated code: output=d603cbdf716ce519 input=bb614026372cd542]*/
326n/a{
327n/a return PyNumber_InPlaceMatrixMultiply(a, b);
328n/a}
329n/a
330n/a/*[clinic input]
331n/a_operator.ifloordiv = _operator.add
332n/a
333n/aSame as a //= b.
334n/a[clinic start generated code]*/
335n/a
336n/astatic PyObject *
337n/a_operator_ifloordiv_impl(PyObject *module, PyObject *a, PyObject *b)
338n/a/*[clinic end generated code: output=535336048c681794 input=9df3b5021cff4ca1]*/
339n/a{
340n/a return PyNumber_InPlaceFloorDivide(a, b);
341n/a}
342n/a
343n/a/*[clinic input]
344n/a_operator.itruediv = _operator.add
345n/a
346n/aSame as a /= b.
347n/a[clinic start generated code]*/
348n/a
349n/astatic PyObject *
350n/a_operator_itruediv_impl(PyObject *module, PyObject *a, PyObject *b)
351n/a/*[clinic end generated code: output=28017fbd3563952f input=9a1ee01608f5f590]*/
352n/a{
353n/a return PyNumber_InPlaceTrueDivide(a, b);
354n/a}
355n/a
356n/a/*[clinic input]
357n/a_operator.imod = _operator.add
358n/a
359n/aSame as a %= b.
360n/a[clinic start generated code]*/
361n/a
362n/astatic PyObject *
363n/a_operator_imod_impl(PyObject *module, PyObject *a, PyObject *b)
364n/a/*[clinic end generated code: output=f7c540ae0fc70904 input=d0c384a3ce38e1dd]*/
365n/a{
366n/a return PyNumber_InPlaceRemainder(a, b);
367n/a}
368n/a
369n/a/*[clinic input]
370n/a_operator.ilshift = _operator.add
371n/a
372n/aSame as a <<= b.
373n/a[clinic start generated code]*/
374n/a
375n/astatic PyObject *
376n/a_operator_ilshift_impl(PyObject *module, PyObject *a, PyObject *b)
377n/a/*[clinic end generated code: output=e73a8fee1ac18749 input=e21b6b310f54572e]*/
378n/a{
379n/a return PyNumber_InPlaceLshift(a, b);
380n/a}
381n/a
382n/a/*[clinic input]
383n/a_operator.irshift = _operator.add
384n/a
385n/aSame as a >>= b.
386n/a[clinic start generated code]*/
387n/a
388n/astatic PyObject *
389n/a_operator_irshift_impl(PyObject *module, PyObject *a, PyObject *b)
390n/a/*[clinic end generated code: output=97f2af6b5ff2ed81 input=6778dbd0f6e1ec16]*/
391n/a{
392n/a return PyNumber_InPlaceRshift(a, b);
393n/a}
394n/a
395n/a/*[clinic input]
396n/a_operator.iand = _operator.add
397n/a
398n/aSame as a &= b.
399n/a[clinic start generated code]*/
400n/a
401n/astatic PyObject *
402n/a_operator_iand_impl(PyObject *module, PyObject *a, PyObject *b)
403n/a/*[clinic end generated code: output=4599e9d40cbf7d00 input=71dfd8e70c156a7b]*/
404n/a{
405n/a return PyNumber_InPlaceAnd(a, b);
406n/a}
407n/a
408n/a/*[clinic input]
409n/a_operator.ixor = _operator.add
410n/a
411n/aSame as a ^= b.
412n/a[clinic start generated code]*/
413n/a
414n/astatic PyObject *
415n/a_operator_ixor_impl(PyObject *module, PyObject *a, PyObject *b)
416n/a/*[clinic end generated code: output=5ff881766872be03 input=695c32bec0604d86]*/
417n/a{
418n/a return PyNumber_InPlaceXor(a, b);
419n/a}
420n/a
421n/a/*[clinic input]
422n/a_operator.ior = _operator.add
423n/a
424n/aSame as a |= b.
425n/a[clinic start generated code]*/
426n/a
427n/astatic PyObject *
428n/a_operator_ior_impl(PyObject *module, PyObject *a, PyObject *b)
429n/a/*[clinic end generated code: output=48aac319445bf759 input=8f01d03eda9920cf]*/
430n/a{
431n/a return PyNumber_InPlaceOr(a, b);
432n/a}
433n/a
434n/a/*[clinic input]
435n/a_operator.concat = _operator.add
436n/a
437n/aSame as a + b, for a and b sequences.
438n/a[clinic start generated code]*/
439n/a
440n/astatic PyObject *
441n/a_operator_concat_impl(PyObject *module, PyObject *a, PyObject *b)
442n/a/*[clinic end generated code: output=80028390942c5f11 input=8544ccd5341a3658]*/
443n/a{
444n/a return PySequence_Concat(a, b);
445n/a}
446n/a
447n/a/*[clinic input]
448n/a_operator.iconcat = _operator.add
449n/a
450n/aSame as a += b, for a and b sequences.
451n/a[clinic start generated code]*/
452n/a
453n/astatic PyObject *
454n/a_operator_iconcat_impl(PyObject *module, PyObject *a, PyObject *b)
455n/a/*[clinic end generated code: output=3ea0a162ebb2e26d input=8f5fe5722fcd837e]*/
456n/a{
457n/a return PySequence_InPlaceConcat(a, b);
458n/a}
459n/a
460n/a/*[clinic input]
461n/a_operator.contains -> bool
462n/a
463n/a a: object
464n/a b: object
465n/a /
466n/a
467n/aSame as b in a (note reversed operands).
468n/a[clinic start generated code]*/
469n/a
470n/astatic int
471n/a_operator_contains_impl(PyObject *module, PyObject *a, PyObject *b)
472n/a/*[clinic end generated code: output=413b4dbe82b6ffc1 input=9122a69b505fde13]*/
473n/a{
474n/a return PySequence_Contains(a, b);
475n/a}
476n/a
477n/a/*[clinic input]
478n/a_operator.indexOf -> Py_ssize_t
479n/a
480n/a a: object
481n/a b: object
482n/a /
483n/a
484n/aReturn the first index of b in a.
485n/a[clinic start generated code]*/
486n/a
487n/astatic Py_ssize_t
488n/a_operator_indexOf_impl(PyObject *module, PyObject *a, PyObject *b)
489n/a/*[clinic end generated code: output=c6226d8e0fb60fa6 input=8be2e43b6a6fffe3]*/
490n/a{
491n/a return PySequence_Index(a, b);
492n/a}
493n/a
494n/a/*[clinic input]
495n/a_operator.countOf = _operator.indexOf
496n/a
497n/aReturn the number of times b occurs in a.
498n/a[clinic start generated code]*/
499n/a
500n/astatic Py_ssize_t
501n/a_operator_countOf_impl(PyObject *module, PyObject *a, PyObject *b)
502n/a/*[clinic end generated code: output=9e1623197daf3382 input=0c3a2656add252db]*/
503n/a{
504n/a return PySequence_Count(a, b);
505n/a}
506n/a
507n/a/*[clinic input]
508n/a_operator.getitem
509n/a
510n/a a: object
511n/a b: object
512n/a /
513n/a
514n/aSame as a[b].
515n/a[clinic start generated code]*/
516n/a
517n/astatic PyObject *
518n/a_operator_getitem_impl(PyObject *module, PyObject *a, PyObject *b)
519n/a/*[clinic end generated code: output=6c8d8101a676e594 input=6682797320e48845]*/
520n/a{
521n/a return PyObject_GetItem(a, b);
522n/a}
523n/a
524n/a/*[clinic input]
525n/a_operator.setitem
526n/a
527n/a a: object
528n/a b: object
529n/a c: object
530n/a /
531n/a
532n/aSame as a[b] = c.
533n/a[clinic start generated code]*/
534n/a
535n/astatic PyObject *
536n/a_operator_setitem_impl(PyObject *module, PyObject *a, PyObject *b,
537n/a PyObject *c)
538n/a/*[clinic end generated code: output=1324f9061ae99e25 input=ceaf453c4d3a58df]*/
539n/a{
540n/a if (-1 == PyObject_SetItem(a, b, c))
541n/a return NULL;
542n/a Py_RETURN_NONE;
543n/a}
544n/a
545n/a/*[clinic input]
546n/a_operator.delitem = _operator.getitem
547n/a
548n/aSame as del a[b].
549n/a[clinic start generated code]*/
550n/a
551n/astatic PyObject *
552n/a_operator_delitem_impl(PyObject *module, PyObject *a, PyObject *b)
553n/a/*[clinic end generated code: output=db18f61506295799 input=991bec56a0d3ec7f]*/
554n/a{
555n/a if (-1 == PyObject_DelItem(a, b))
556n/a return NULL;
557n/a Py_RETURN_NONE;
558n/a}
559n/a
560n/a/*[clinic input]
561n/a_operator.eq
562n/a
563n/a a: object
564n/a b: object
565n/a /
566n/a
567n/aSame as a == b.
568n/a[clinic start generated code]*/
569n/a
570n/astatic PyObject *
571n/a_operator_eq_impl(PyObject *module, PyObject *a, PyObject *b)
572n/a/*[clinic end generated code: output=8d7d46ed4135677c input=586fca687a95a83f]*/
573n/a{
574n/a return PyObject_RichCompare(a, b, Py_EQ);
575n/a}
576n/a
577n/a/*[clinic input]
578n/a_operator.ne = _operator.eq
579n/a
580n/aSame as a != b.
581n/a[clinic start generated code]*/
582n/a
583n/astatic PyObject *
584n/a_operator_ne_impl(PyObject *module, PyObject *a, PyObject *b)
585n/a/*[clinic end generated code: output=c99bd0c3a4c01297 input=5d88f23d35e9abac]*/
586n/a{
587n/a return PyObject_RichCompare(a, b, Py_NE);
588n/a}
589n/a
590n/a/*[clinic input]
591n/a_operator.lt = _operator.eq
592n/a
593n/aSame as a < b.
594n/a[clinic start generated code]*/
595n/a
596n/astatic PyObject *
597n/a_operator_lt_impl(PyObject *module, PyObject *a, PyObject *b)
598n/a/*[clinic end generated code: output=082d7c45c440e535 input=34a59ad6d39d3a2b]*/
599n/a{
600n/a return PyObject_RichCompare(a, b, Py_LT);
601n/a}
602n/a
603n/a/*[clinic input]
604n/a_operator.le = _operator.eq
605n/a
606n/aSame as a <= b.
607n/a[clinic start generated code]*/
608n/a
609n/astatic PyObject *
610n/a_operator_le_impl(PyObject *module, PyObject *a, PyObject *b)
611n/a/*[clinic end generated code: output=00970a2923d0ae17 input=b812a7860a0bef44]*/
612n/a{
613n/a return PyObject_RichCompare(a, b, Py_LE);
614n/a}
615n/a
616n/a/*[clinic input]
617n/a_operator.gt = _operator.eq
618n/a
619n/aSame as a > b.
620n/a[clinic start generated code]*/
621n/a
622n/astatic PyObject *
623n/a_operator_gt_impl(PyObject *module, PyObject *a, PyObject *b)
624n/a/*[clinic end generated code: output=8d373349ecf25641 input=9bdb45b995ada35b]*/
625n/a{
626n/a return PyObject_RichCompare(a, b, Py_GT);
627n/a}
628n/a
629n/a/*[clinic input]
630n/a_operator.ge = _operator.eq
631n/a
632n/aSame as a >= b.
633n/a[clinic start generated code]*/
634n/a
635n/astatic PyObject *
636n/a_operator_ge_impl(PyObject *module, PyObject *a, PyObject *b)
637n/a/*[clinic end generated code: output=7ce3882256d4b137 input=cf1dc4a5ca9c35f5]*/
638n/a{
639n/a return PyObject_RichCompare(a, b, Py_GE);
640n/a}
641n/a
642n/a/*[clinic input]
643n/a_operator.pow = _operator.add
644n/a
645n/aSame as a ** b.
646n/a[clinic start generated code]*/
647n/a
648n/astatic PyObject *
649n/a_operator_pow_impl(PyObject *module, PyObject *a, PyObject *b)
650n/a/*[clinic end generated code: output=09e668ad50036120 input=690b40f097ab1637]*/
651n/a{
652n/a return PyNumber_Power(a, b, Py_None);
653n/a}
654n/a
655n/a/*[clinic input]
656n/a_operator.ipow = _operator.add
657n/a
658n/aSame as a **= b.
659n/a[clinic start generated code]*/
660n/a
661n/astatic PyObject *
662n/a_operator_ipow_impl(PyObject *module, PyObject *a, PyObject *b)
663n/a/*[clinic end generated code: output=7189ff4d4367c808 input=f00623899d07499a]*/
664n/a{
665n/a return PyNumber_InPlacePower(a, b, Py_None);
666n/a}
667n/a
668n/a/*[clinic input]
669n/a_operator.index
670n/a
671n/a a: object
672n/a /
673n/a
674n/aSame as a.__index__()
675n/a[clinic start generated code]*/
676n/a
677n/astatic PyObject *
678n/a_operator_index(PyObject *module, PyObject *a)
679n/a/*[clinic end generated code: output=d972b0764ac305fc input=6f54d50ea64a579c]*/
680n/a{
681n/a return PyNumber_Index(a);
682n/a}
683n/a
684n/a/*[clinic input]
685n/a_operator.is_ = _operator.add
686n/a
687n/aSame as a is b.
688n/a[clinic start generated code]*/
689n/a
690n/astatic PyObject *
691n/a_operator_is__impl(PyObject *module, PyObject *a, PyObject *b)
692n/a/*[clinic end generated code: output=bcd47a402e482e1d input=5fa9b97df03c427f]*/
693n/a{
694n/a PyObject *result;
695n/a result = (a == b) ? Py_True : Py_False;
696n/a Py_INCREF(result);
697n/a return result;
698n/a}
699n/a
700n/a/*[clinic input]
701n/a_operator.is_not = _operator.add
702n/a
703n/aSame as a is not b.
704n/a[clinic start generated code]*/
705n/a
706n/astatic PyObject *
707n/a_operator_is_not_impl(PyObject *module, PyObject *a, PyObject *b)
708n/a/*[clinic end generated code: output=491a1f2f81f6c7f9 input=5a93f7e1a93535f1]*/
709n/a{
710n/a PyObject *result;
711n/a result = (a != b) ? Py_True : Py_False;
712n/a Py_INCREF(result);
713n/a return result;
714n/a}
715n/a
716n/a/* compare_digest **********************************************************/
717n/a
718n/a/*
719n/a * timing safe compare
720n/a *
721n/a * Returns 1 of the strings are equal.
722n/a * In case of len(a) != len(b) the function tries to keep the timing
723n/a * dependent on the length of b. CPU cache locally may still alter timing
724n/a * a bit.
725n/a */
726n/astatic int
727n/a_tscmp(const unsigned char *a, const unsigned char *b,
728n/a Py_ssize_t len_a, Py_ssize_t len_b)
729n/a{
730n/a /* The volatile type declarations make sure that the compiler has no
731n/a * chance to optimize and fold the code in any way that may change
732n/a * the timing.
733n/a */
734n/a volatile Py_ssize_t length;
735n/a volatile const unsigned char *left;
736n/a volatile const unsigned char *right;
737n/a Py_ssize_t i;
738n/a unsigned char result;
739n/a
740n/a /* loop count depends on length of b */
741n/a length = len_b;
742n/a left = NULL;
743n/a right = b;
744n/a
745n/a /* don't use else here to keep the amount of CPU instructions constant,
746n/a * volatile forces re-evaluation
747n/a * */
748n/a if (len_a == length) {
749n/a left = *((volatile const unsigned char**)&a);
750n/a result = 0;
751n/a }
752n/a if (len_a != length) {
753n/a left = b;
754n/a result = 1;
755n/a }
756n/a
757n/a for (i=0; i < length; i++) {
758n/a result |= *left++ ^ *right++;
759n/a }
760n/a
761n/a return (result == 0);
762n/a}
763n/a
764n/a/*[clinic input]
765n/a_operator.length_hint -> Py_ssize_t
766n/a
767n/a obj: object
768n/a default: Py_ssize_t = 0
769n/a /
770n/a
771n/aReturn an estimate of the number of items in obj.
772n/a
773n/aThis is useful for presizing containers when building from an iterable.
774n/a
775n/aIf the object supports len(), the result will be exact.
776n/aOtherwise, it may over- or under-estimate by an arbitrary amount.
777n/aThe result will be an integer >= 0.
778n/a[clinic start generated code]*/
779n/a
780n/astatic Py_ssize_t
781n/a_operator_length_hint_impl(PyObject *module, PyObject *obj,
782n/a Py_ssize_t default_value)
783n/a/*[clinic end generated code: output=01d469edc1d612ad input=65ed29f04401e96a]*/
784n/a{
785n/a return PyObject_LengthHint(obj, default_value);
786n/a}
787n/a
788n/a/*[clinic input]
789n/a_operator._compare_digest = _operator.eq
790n/a
791n/aReturn 'a == b'.
792n/a
793n/aThis function uses an approach designed to prevent
794n/atiming analysis, making it appropriate for cryptography.
795n/a
796n/aa and b must both be of the same type: either str (ASCII only),
797n/aor any bytes-like object.
798n/a
799n/aNote: If a and b are of different lengths, or if an error occurs,
800n/aa timing attack could theoretically reveal information about the
801n/atypes and lengths of a and b--but not their values.
802n/a[clinic start generated code]*/
803n/a
804n/astatic PyObject *
805n/a_operator__compare_digest_impl(PyObject *module, PyObject *a, PyObject *b)
806n/a/*[clinic end generated code: output=11d452bdd3a23cbc input=9ac7e2c4e30bc356]*/
807n/a{
808n/a int rc;
809n/a
810n/a /* ASCII unicode string */
811n/a if(PyUnicode_Check(a) && PyUnicode_Check(b)) {
812n/a if (PyUnicode_READY(a) == -1 || PyUnicode_READY(b) == -1) {
813n/a return NULL;
814n/a }
815n/a if (!PyUnicode_IS_ASCII(a) || !PyUnicode_IS_ASCII(b)) {
816n/a PyErr_SetString(PyExc_TypeError,
817n/a "comparing strings with non-ASCII characters is "
818n/a "not supported");
819n/a return NULL;
820n/a }
821n/a
822n/a rc = _tscmp(PyUnicode_DATA(a),
823n/a PyUnicode_DATA(b),
824n/a PyUnicode_GET_LENGTH(a),
825n/a PyUnicode_GET_LENGTH(b));
826n/a }
827n/a /* fallback to buffer interface for bytes, bytesarray and other */
828n/a else {
829n/a Py_buffer view_a;
830n/a Py_buffer view_b;
831n/a
832n/a if (PyObject_CheckBuffer(a) == 0 && PyObject_CheckBuffer(b) == 0) {
833n/a PyErr_Format(PyExc_TypeError,
834n/a "unsupported operand types(s) or combination of types: "
835n/a "'%.100s' and '%.100s'",
836n/a Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
837n/a return NULL;
838n/a }
839n/a
840n/a if (PyObject_GetBuffer(a, &view_a, PyBUF_SIMPLE) == -1) {
841n/a return NULL;
842n/a }
843n/a if (view_a.ndim > 1) {
844n/a PyErr_SetString(PyExc_BufferError,
845n/a "Buffer must be single dimension");
846n/a PyBuffer_Release(&view_a);
847n/a return NULL;
848n/a }
849n/a
850n/a if (PyObject_GetBuffer(b, &view_b, PyBUF_SIMPLE) == -1) {
851n/a PyBuffer_Release(&view_a);
852n/a return NULL;
853n/a }
854n/a if (view_b.ndim > 1) {
855n/a PyErr_SetString(PyExc_BufferError,
856n/a "Buffer must be single dimension");
857n/a PyBuffer_Release(&view_a);
858n/a PyBuffer_Release(&view_b);
859n/a return NULL;
860n/a }
861n/a
862n/a rc = _tscmp((const unsigned char*)view_a.buf,
863n/a (const unsigned char*)view_b.buf,
864n/a view_a.len,
865n/a view_b.len);
866n/a
867n/a PyBuffer_Release(&view_a);
868n/a PyBuffer_Release(&view_b);
869n/a }
870n/a
871n/a return PyBool_FromLong(rc);
872n/a}
873n/a
874n/a/* operator methods **********************************************************/
875n/a
876n/astatic struct PyMethodDef operator_methods[] = {
877n/a
878n/a _OPERATOR_TRUTH_METHODDEF
879n/a _OPERATOR_CONTAINS_METHODDEF
880n/a _OPERATOR_INDEXOF_METHODDEF
881n/a _OPERATOR_COUNTOF_METHODDEF
882n/a _OPERATOR_IS__METHODDEF
883n/a _OPERATOR_IS_NOT_METHODDEF
884n/a _OPERATOR_INDEX_METHODDEF
885n/a _OPERATOR_ADD_METHODDEF
886n/a _OPERATOR_SUB_METHODDEF
887n/a _OPERATOR_MUL_METHODDEF
888n/a _OPERATOR_MATMUL_METHODDEF
889n/a _OPERATOR_FLOORDIV_METHODDEF
890n/a _OPERATOR_TRUEDIV_METHODDEF
891n/a _OPERATOR_MOD_METHODDEF
892n/a _OPERATOR_NEG_METHODDEF
893n/a _OPERATOR_POS_METHODDEF
894n/a _OPERATOR_ABS_METHODDEF
895n/a _OPERATOR_INV_METHODDEF
896n/a _OPERATOR_INVERT_METHODDEF
897n/a _OPERATOR_LSHIFT_METHODDEF
898n/a _OPERATOR_RSHIFT_METHODDEF
899n/a _OPERATOR_NOT__METHODDEF
900n/a _OPERATOR_AND__METHODDEF
901n/a _OPERATOR_XOR_METHODDEF
902n/a _OPERATOR_OR__METHODDEF
903n/a _OPERATOR_IADD_METHODDEF
904n/a _OPERATOR_ISUB_METHODDEF
905n/a _OPERATOR_IMUL_METHODDEF
906n/a _OPERATOR_IMATMUL_METHODDEF
907n/a _OPERATOR_IFLOORDIV_METHODDEF
908n/a _OPERATOR_ITRUEDIV_METHODDEF
909n/a _OPERATOR_IMOD_METHODDEF
910n/a _OPERATOR_ILSHIFT_METHODDEF
911n/a _OPERATOR_IRSHIFT_METHODDEF
912n/a _OPERATOR_IAND_METHODDEF
913n/a _OPERATOR_IXOR_METHODDEF
914n/a _OPERATOR_IOR_METHODDEF
915n/a _OPERATOR_CONCAT_METHODDEF
916n/a _OPERATOR_ICONCAT_METHODDEF
917n/a _OPERATOR_GETITEM_METHODDEF
918n/a _OPERATOR_SETITEM_METHODDEF
919n/a _OPERATOR_DELITEM_METHODDEF
920n/a _OPERATOR_POW_METHODDEF
921n/a _OPERATOR_IPOW_METHODDEF
922n/a _OPERATOR_EQ_METHODDEF
923n/a _OPERATOR_NE_METHODDEF
924n/a _OPERATOR_LT_METHODDEF
925n/a _OPERATOR_LE_METHODDEF
926n/a _OPERATOR_GT_METHODDEF
927n/a _OPERATOR_GE_METHODDEF
928n/a _OPERATOR__COMPARE_DIGEST_METHODDEF
929n/a _OPERATOR_LENGTH_HINT_METHODDEF
930n/a {NULL, NULL} /* sentinel */
931n/a
932n/a};
933n/a
934n/a/* itemgetter object **********************************************************/
935n/a
936n/atypedef struct {
937n/a PyObject_HEAD
938n/a Py_ssize_t nitems;
939n/a PyObject *item;
940n/a} itemgetterobject;
941n/a
942n/astatic PyTypeObject itemgetter_type;
943n/a
944n/a/* AC 3.5: treats first argument as an iterable, otherwise uses *args */
945n/astatic PyObject *
946n/aitemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
947n/a{
948n/a itemgetterobject *ig;
949n/a PyObject *item;
950n/a Py_ssize_t nitems;
951n/a
952n/a if (!_PyArg_NoKeywords("itemgetter()", kwds))
953n/a return NULL;
954n/a
955n/a nitems = PyTuple_GET_SIZE(args);
956n/a if (nitems <= 1) {
957n/a if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
958n/a return NULL;
959n/a } else
960n/a item = args;
961n/a
962n/a /* create itemgetterobject structure */
963n/a ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
964n/a if (ig == NULL)
965n/a return NULL;
966n/a
967n/a Py_INCREF(item);
968n/a ig->item = item;
969n/a ig->nitems = nitems;
970n/a
971n/a PyObject_GC_Track(ig);
972n/a return (PyObject *)ig;
973n/a}
974n/a
975n/astatic void
976n/aitemgetter_dealloc(itemgetterobject *ig)
977n/a{
978n/a PyObject_GC_UnTrack(ig);
979n/a Py_XDECREF(ig->item);
980n/a PyObject_GC_Del(ig);
981n/a}
982n/a
983n/astatic int
984n/aitemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
985n/a{
986n/a Py_VISIT(ig->item);
987n/a return 0;
988n/a}
989n/a
990n/astatic PyObject *
991n/aitemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
992n/a{
993n/a PyObject *obj, *result;
994n/a Py_ssize_t i, nitems=ig->nitems;
995n/a
996n/a if (!_PyArg_NoKeywords("itemgetter", kw))
997n/a return NULL;
998n/a if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
999n/a return NULL;
1000n/a if (nitems == 1)
1001n/a return PyObject_GetItem(obj, ig->item);
1002n/a
1003n/a assert(PyTuple_Check(ig->item));
1004n/a assert(PyTuple_GET_SIZE(ig->item) == nitems);
1005n/a
1006n/a result = PyTuple_New(nitems);
1007n/a if (result == NULL)
1008n/a return NULL;
1009n/a
1010n/a for (i=0 ; i < nitems ; i++) {
1011n/a PyObject *item, *val;
1012n/a item = PyTuple_GET_ITEM(ig->item, i);
1013n/a val = PyObject_GetItem(obj, item);
1014n/a if (val == NULL) {
1015n/a Py_DECREF(result);
1016n/a return NULL;
1017n/a }
1018n/a PyTuple_SET_ITEM(result, i, val);
1019n/a }
1020n/a return result;
1021n/a}
1022n/a
1023n/astatic PyObject *
1024n/aitemgetter_repr(itemgetterobject *ig)
1025n/a{
1026n/a PyObject *repr;
1027n/a const char *reprfmt;
1028n/a
1029n/a int status = Py_ReprEnter((PyObject *)ig);
1030n/a if (status != 0) {
1031n/a if (status < 0)
1032n/a return NULL;
1033n/a return PyUnicode_FromFormat("%s(...)", Py_TYPE(ig)->tp_name);
1034n/a }
1035n/a
1036n/a reprfmt = ig->nitems == 1 ? "%s(%R)" : "%s%R";
1037n/a repr = PyUnicode_FromFormat(reprfmt, Py_TYPE(ig)->tp_name, ig->item);
1038n/a Py_ReprLeave((PyObject *)ig);
1039n/a return repr;
1040n/a}
1041n/a
1042n/astatic PyObject *
1043n/aitemgetter_reduce(itemgetterobject *ig)
1044n/a{
1045n/a if (ig->nitems == 1)
1046n/a return Py_BuildValue("O(O)", Py_TYPE(ig), ig->item);
1047n/a return PyTuple_Pack(2, Py_TYPE(ig), ig->item);
1048n/a}
1049n/a
1050n/aPyDoc_STRVAR(reduce_doc, "Return state information for pickling");
1051n/a
1052n/astatic PyMethodDef itemgetter_methods[] = {
1053n/a {"__reduce__", (PyCFunction)itemgetter_reduce, METH_NOARGS,
1054n/a reduce_doc},
1055n/a {NULL}
1056n/a};
1057n/a
1058n/aPyDoc_STRVAR(itemgetter_doc,
1059n/a"itemgetter(item, ...) --> itemgetter object\n\
1060n/a\n\
1061n/aReturn a callable object that fetches the given item(s) from its operand.\n\
1062n/aAfter f = itemgetter(2), the call f(r) returns r[2].\n\
1063n/aAfter g = itemgetter(2, 5, 3), the call g(r) returns (r[2], r[5], r[3])");
1064n/a
1065n/astatic PyTypeObject itemgetter_type = {
1066n/a PyVarObject_HEAD_INIT(NULL, 0)
1067n/a "operator.itemgetter", /* tp_name */
1068n/a sizeof(itemgetterobject), /* tp_basicsize */
1069n/a 0, /* tp_itemsize */
1070n/a /* methods */
1071n/a (destructor)itemgetter_dealloc, /* tp_dealloc */
1072n/a 0, /* tp_print */
1073n/a 0, /* tp_getattr */
1074n/a 0, /* tp_setattr */
1075n/a 0, /* tp_reserved */
1076n/a (reprfunc)itemgetter_repr, /* tp_repr */
1077n/a 0, /* tp_as_number */
1078n/a 0, /* tp_as_sequence */
1079n/a 0, /* tp_as_mapping */
1080n/a 0, /* tp_hash */
1081n/a (ternaryfunc)itemgetter_call, /* tp_call */
1082n/a 0, /* tp_str */
1083n/a PyObject_GenericGetAttr, /* tp_getattro */
1084n/a 0, /* tp_setattro */
1085n/a 0, /* tp_as_buffer */
1086n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1087n/a itemgetter_doc, /* tp_doc */
1088n/a (traverseproc)itemgetter_traverse, /* tp_traverse */
1089n/a 0, /* tp_clear */
1090n/a 0, /* tp_richcompare */
1091n/a 0, /* tp_weaklistoffset */
1092n/a 0, /* tp_iter */
1093n/a 0, /* tp_iternext */
1094n/a itemgetter_methods, /* tp_methods */
1095n/a 0, /* tp_members */
1096n/a 0, /* tp_getset */
1097n/a 0, /* tp_base */
1098n/a 0, /* tp_dict */
1099n/a 0, /* tp_descr_get */
1100n/a 0, /* tp_descr_set */
1101n/a 0, /* tp_dictoffset */
1102n/a 0, /* tp_init */
1103n/a 0, /* tp_alloc */
1104n/a itemgetter_new, /* tp_new */
1105n/a 0, /* tp_free */
1106n/a};
1107n/a
1108n/a
1109n/a/* attrgetter object **********************************************************/
1110n/a
1111n/atypedef struct {
1112n/a PyObject_HEAD
1113n/a Py_ssize_t nattrs;
1114n/a PyObject *attr;
1115n/a} attrgetterobject;
1116n/a
1117n/astatic PyTypeObject attrgetter_type;
1118n/a
1119n/a/* AC 3.5: treats first argument as an iterable, otherwise uses *args */
1120n/astatic PyObject *
1121n/aattrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1122n/a{
1123n/a attrgetterobject *ag;
1124n/a PyObject *attr;
1125n/a Py_ssize_t nattrs, idx, char_idx;
1126n/a
1127n/a if (!_PyArg_NoKeywords("attrgetter()", kwds))
1128n/a return NULL;
1129n/a
1130n/a nattrs = PyTuple_GET_SIZE(args);
1131n/a if (nattrs <= 1) {
1132n/a if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
1133n/a return NULL;
1134n/a }
1135n/a
1136n/a attr = PyTuple_New(nattrs);
1137n/a if (attr == NULL)
1138n/a return NULL;
1139n/a
1140n/a /* prepare attr while checking args */
1141n/a for (idx = 0; idx < nattrs; ++idx) {
1142n/a PyObject *item = PyTuple_GET_ITEM(args, idx);
1143n/a Py_ssize_t item_len;
1144n/a void *data;
1145n/a unsigned int kind;
1146n/a int dot_count;
1147n/a
1148n/a if (!PyUnicode_Check(item)) {
1149n/a PyErr_SetString(PyExc_TypeError,
1150n/a "attribute name must be a string");
1151n/a Py_DECREF(attr);
1152n/a return NULL;
1153n/a }
1154n/a if (PyUnicode_READY(item)) {
1155n/a Py_DECREF(attr);
1156n/a return NULL;
1157n/a }
1158n/a item_len = PyUnicode_GET_LENGTH(item);
1159n/a kind = PyUnicode_KIND(item);
1160n/a data = PyUnicode_DATA(item);
1161n/a
1162n/a /* check whethere the string is dotted */
1163n/a dot_count = 0;
1164n/a for (char_idx = 0; char_idx < item_len; ++char_idx) {
1165n/a if (PyUnicode_READ(kind, data, char_idx) == '.')
1166n/a ++dot_count;
1167n/a }
1168n/a
1169n/a if (dot_count == 0) {
1170n/a Py_INCREF(item);
1171n/a PyUnicode_InternInPlace(&item);
1172n/a PyTuple_SET_ITEM(attr, idx, item);
1173n/a } else { /* make it a tuple of non-dotted attrnames */
1174n/a PyObject *attr_chain = PyTuple_New(dot_count + 1);
1175n/a PyObject *attr_chain_item;
1176n/a Py_ssize_t unibuff_from = 0;
1177n/a Py_ssize_t unibuff_till = 0;
1178n/a Py_ssize_t attr_chain_idx = 0;
1179n/a
1180n/a if (attr_chain == NULL) {
1181n/a Py_DECREF(attr);
1182n/a return NULL;
1183n/a }
1184n/a
1185n/a for (; dot_count > 0; --dot_count) {
1186n/a while (PyUnicode_READ(kind, data, unibuff_till) != '.') {
1187n/a ++unibuff_till;
1188n/a }
1189n/a attr_chain_item = PyUnicode_Substring(item,
1190n/a unibuff_from,
1191n/a unibuff_till);
1192n/a if (attr_chain_item == NULL) {
1193n/a Py_DECREF(attr_chain);
1194n/a Py_DECREF(attr);
1195n/a return NULL;
1196n/a }
1197n/a PyUnicode_InternInPlace(&attr_chain_item);
1198n/a PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
1199n/a ++attr_chain_idx;
1200n/a unibuff_till = unibuff_from = unibuff_till + 1;
1201n/a }
1202n/a
1203n/a /* now add the last dotless name */
1204n/a attr_chain_item = PyUnicode_Substring(item,
1205n/a unibuff_from, item_len);
1206n/a if (attr_chain_item == NULL) {
1207n/a Py_DECREF(attr_chain);
1208n/a Py_DECREF(attr);
1209n/a return NULL;
1210n/a }
1211n/a PyUnicode_InternInPlace(&attr_chain_item);
1212n/a PyTuple_SET_ITEM(attr_chain, attr_chain_idx, attr_chain_item);
1213n/a
1214n/a PyTuple_SET_ITEM(attr, idx, attr_chain);
1215n/a }
1216n/a }
1217n/a
1218n/a /* create attrgetterobject structure */
1219n/a ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
1220n/a if (ag == NULL) {
1221n/a Py_DECREF(attr);
1222n/a return NULL;
1223n/a }
1224n/a
1225n/a ag->attr = attr;
1226n/a ag->nattrs = nattrs;
1227n/a
1228n/a PyObject_GC_Track(ag);
1229n/a return (PyObject *)ag;
1230n/a}
1231n/a
1232n/astatic void
1233n/aattrgetter_dealloc(attrgetterobject *ag)
1234n/a{
1235n/a PyObject_GC_UnTrack(ag);
1236n/a Py_XDECREF(ag->attr);
1237n/a PyObject_GC_Del(ag);
1238n/a}
1239n/a
1240n/astatic int
1241n/aattrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
1242n/a{
1243n/a Py_VISIT(ag->attr);
1244n/a return 0;
1245n/a}
1246n/a
1247n/astatic PyObject *
1248n/adotted_getattr(PyObject *obj, PyObject *attr)
1249n/a{
1250n/a PyObject *newobj;
1251n/a
1252n/a /* attr is either a tuple or instance of str.
1253n/a Ensured by the setup code of attrgetter_new */
1254n/a if (PyTuple_CheckExact(attr)) { /* chained getattr */
1255n/a Py_ssize_t name_idx = 0, name_count;
1256n/a PyObject *attr_name;
1257n/a
1258n/a name_count = PyTuple_GET_SIZE(attr);
1259n/a Py_INCREF(obj);
1260n/a for (name_idx = 0; name_idx < name_count; ++name_idx) {
1261n/a attr_name = PyTuple_GET_ITEM(attr, name_idx);
1262n/a newobj = PyObject_GetAttr(obj, attr_name);
1263n/a Py_DECREF(obj);
1264n/a if (newobj == NULL) {
1265n/a return NULL;
1266n/a }
1267n/a /* here */
1268n/a obj = newobj;
1269n/a }
1270n/a } else { /* single getattr */
1271n/a newobj = PyObject_GetAttr(obj, attr);
1272n/a if (newobj == NULL)
1273n/a return NULL;
1274n/a obj = newobj;
1275n/a }
1276n/a
1277n/a return obj;
1278n/a}
1279n/a
1280n/astatic PyObject *
1281n/aattrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
1282n/a{
1283n/a PyObject *obj, *result;
1284n/a Py_ssize_t i, nattrs=ag->nattrs;
1285n/a
1286n/a if (!_PyArg_NoKeywords("attrgetter", kw))
1287n/a return NULL;
1288n/a if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
1289n/a return NULL;
1290n/a if (ag->nattrs == 1) /* ag->attr is always a tuple */
1291n/a return dotted_getattr(obj, PyTuple_GET_ITEM(ag->attr, 0));
1292n/a
1293n/a assert(PyTuple_Check(ag->attr));
1294n/a assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
1295n/a
1296n/a result = PyTuple_New(nattrs);
1297n/a if (result == NULL)
1298n/a return NULL;
1299n/a
1300n/a for (i=0 ; i < nattrs ; i++) {
1301n/a PyObject *attr, *val;
1302n/a attr = PyTuple_GET_ITEM(ag->attr, i);
1303n/a val = dotted_getattr(obj, attr);
1304n/a if (val == NULL) {
1305n/a Py_DECREF(result);
1306n/a return NULL;
1307n/a }
1308n/a PyTuple_SET_ITEM(result, i, val);
1309n/a }
1310n/a return result;
1311n/a}
1312n/a
1313n/astatic PyObject *
1314n/adotjoinattr(PyObject *attr, PyObject **attrsep)
1315n/a{
1316n/a if (PyTuple_CheckExact(attr)) {
1317n/a if (*attrsep == NULL) {
1318n/a *attrsep = PyUnicode_FromString(".");
1319n/a if (*attrsep == NULL)
1320n/a return NULL;
1321n/a }
1322n/a return PyUnicode_Join(*attrsep, attr);
1323n/a } else {
1324n/a Py_INCREF(attr);
1325n/a return attr;
1326n/a }
1327n/a}
1328n/a
1329n/astatic PyObject *
1330n/aattrgetter_args(attrgetterobject *ag)
1331n/a{
1332n/a Py_ssize_t i;
1333n/a PyObject *attrsep = NULL;
1334n/a PyObject *attrstrings = PyTuple_New(ag->nattrs);
1335n/a if (attrstrings == NULL)
1336n/a return NULL;
1337n/a
1338n/a for (i = 0; i < ag->nattrs; ++i) {
1339n/a PyObject *attr = PyTuple_GET_ITEM(ag->attr, i);
1340n/a PyObject *attrstr = dotjoinattr(attr, &attrsep);
1341n/a if (attrstr == NULL) {
1342n/a Py_XDECREF(attrsep);
1343n/a Py_DECREF(attrstrings);
1344n/a return NULL;
1345n/a }
1346n/a PyTuple_SET_ITEM(attrstrings, i, attrstr);
1347n/a }
1348n/a Py_XDECREF(attrsep);
1349n/a return attrstrings;
1350n/a}
1351n/a
1352n/astatic PyObject *
1353n/aattrgetter_repr(attrgetterobject *ag)
1354n/a{
1355n/a PyObject *repr = NULL;
1356n/a int status = Py_ReprEnter((PyObject *)ag);
1357n/a if (status != 0) {
1358n/a if (status < 0)
1359n/a return NULL;
1360n/a return PyUnicode_FromFormat("%s(...)", Py_TYPE(ag)->tp_name);
1361n/a }
1362n/a
1363n/a if (ag->nattrs == 1) {
1364n/a PyObject *attrsep = NULL;
1365n/a PyObject *attr = dotjoinattr(PyTuple_GET_ITEM(ag->attr, 0), &attrsep);
1366n/a if (attr != NULL) {
1367n/a repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(ag)->tp_name, attr);
1368n/a Py_DECREF(attr);
1369n/a }
1370n/a Py_XDECREF(attrsep);
1371n/a }
1372n/a else {
1373n/a PyObject *attrstrings = attrgetter_args(ag);
1374n/a if (attrstrings != NULL) {
1375n/a repr = PyUnicode_FromFormat("%s%R",
1376n/a Py_TYPE(ag)->tp_name, attrstrings);
1377n/a Py_DECREF(attrstrings);
1378n/a }
1379n/a }
1380n/a Py_ReprLeave((PyObject *)ag);
1381n/a return repr;
1382n/a}
1383n/a
1384n/astatic PyObject *
1385n/aattrgetter_reduce(attrgetterobject *ag)
1386n/a{
1387n/a PyObject *attrstrings = attrgetter_args(ag);
1388n/a if (attrstrings == NULL)
1389n/a return NULL;
1390n/a
1391n/a return Py_BuildValue("ON", Py_TYPE(ag), attrstrings);
1392n/a}
1393n/a
1394n/astatic PyMethodDef attrgetter_methods[] = {
1395n/a {"__reduce__", (PyCFunction)attrgetter_reduce, METH_NOARGS,
1396n/a reduce_doc},
1397n/a {NULL}
1398n/a};
1399n/a
1400n/aPyDoc_STRVAR(attrgetter_doc,
1401n/a"attrgetter(attr, ...) --> attrgetter object\n\
1402n/a\n\
1403n/aReturn a callable object that fetches the given attribute(s) from its operand.\n\
1404n/aAfter f = attrgetter('name'), the call f(r) returns r.name.\n\
1405n/aAfter g = attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
1406n/aAfter h = attrgetter('name.first', 'name.last'), the call h(r) returns\n\
1407n/a(r.name.first, r.name.last).");
1408n/a
1409n/astatic PyTypeObject attrgetter_type = {
1410n/a PyVarObject_HEAD_INIT(NULL, 0)
1411n/a "operator.attrgetter", /* tp_name */
1412n/a sizeof(attrgetterobject), /* tp_basicsize */
1413n/a 0, /* tp_itemsize */
1414n/a /* methods */
1415n/a (destructor)attrgetter_dealloc, /* tp_dealloc */
1416n/a 0, /* tp_print */
1417n/a 0, /* tp_getattr */
1418n/a 0, /* tp_setattr */
1419n/a 0, /* tp_reserved */
1420n/a (reprfunc)attrgetter_repr, /* tp_repr */
1421n/a 0, /* tp_as_number */
1422n/a 0, /* tp_as_sequence */
1423n/a 0, /* tp_as_mapping */
1424n/a 0, /* tp_hash */
1425n/a (ternaryfunc)attrgetter_call, /* tp_call */
1426n/a 0, /* tp_str */
1427n/a PyObject_GenericGetAttr, /* tp_getattro */
1428n/a 0, /* tp_setattro */
1429n/a 0, /* tp_as_buffer */
1430n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
1431n/a attrgetter_doc, /* tp_doc */
1432n/a (traverseproc)attrgetter_traverse, /* tp_traverse */
1433n/a 0, /* tp_clear */
1434n/a 0, /* tp_richcompare */
1435n/a 0, /* tp_weaklistoffset */
1436n/a 0, /* tp_iter */
1437n/a 0, /* tp_iternext */
1438n/a attrgetter_methods, /* tp_methods */
1439n/a 0, /* tp_members */
1440n/a 0, /* tp_getset */
1441n/a 0, /* tp_base */
1442n/a 0, /* tp_dict */
1443n/a 0, /* tp_descr_get */
1444n/a 0, /* tp_descr_set */
1445n/a 0, /* tp_dictoffset */
1446n/a 0, /* tp_init */
1447n/a 0, /* tp_alloc */
1448n/a attrgetter_new, /* tp_new */
1449n/a 0, /* tp_free */
1450n/a};
1451n/a
1452n/a
1453n/a/* methodcaller object **********************************************************/
1454n/a
1455n/atypedef struct {
1456n/a PyObject_HEAD
1457n/a PyObject *name;
1458n/a PyObject *args;
1459n/a PyObject *kwds;
1460n/a} methodcallerobject;
1461n/a
1462n/astatic PyTypeObject methodcaller_type;
1463n/a
1464n/a/* AC 3.5: variable number of arguments, not currently support by AC */
1465n/astatic PyObject *
1466n/amethodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
1467n/a{
1468n/a methodcallerobject *mc;
1469n/a PyObject *name;
1470n/a
1471n/a if (PyTuple_GET_SIZE(args) < 1) {
1472n/a PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
1473n/a "one argument, the method name");
1474n/a return NULL;
1475n/a }
1476n/a
1477n/a name = PyTuple_GET_ITEM(args, 0);
1478n/a if (!PyUnicode_Check(name)) {
1479n/a PyErr_SetString(PyExc_TypeError,
1480n/a "method name must be a string");
1481n/a return NULL;
1482n/a }
1483n/a
1484n/a /* create methodcallerobject structure */
1485n/a mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
1486n/a if (mc == NULL)
1487n/a return NULL;
1488n/a
1489n/a name = PyTuple_GET_ITEM(args, 0);
1490n/a Py_INCREF(name);
1491n/a PyUnicode_InternInPlace(&name);
1492n/a mc->name = name;
1493n/a
1494n/a Py_XINCREF(kwds);
1495n/a mc->kwds = kwds;
1496n/a
1497n/a mc->args = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
1498n/a if (mc->args == NULL) {
1499n/a Py_DECREF(mc);
1500n/a return NULL;
1501n/a }
1502n/a
1503n/a PyObject_GC_Track(mc);
1504n/a return (PyObject *)mc;
1505n/a}
1506n/a
1507n/astatic void
1508n/amethodcaller_dealloc(methodcallerobject *mc)
1509n/a{
1510n/a PyObject_GC_UnTrack(mc);
1511n/a Py_XDECREF(mc->name);
1512n/a Py_XDECREF(mc->args);
1513n/a Py_XDECREF(mc->kwds);
1514n/a PyObject_GC_Del(mc);
1515n/a}
1516n/a
1517n/astatic int
1518n/amethodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
1519n/a{
1520n/a Py_VISIT(mc->args);
1521n/a Py_VISIT(mc->kwds);
1522n/a return 0;
1523n/a}
1524n/a
1525n/astatic PyObject *
1526n/amethodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
1527n/a{
1528n/a PyObject *method, *obj, *result;
1529n/a
1530n/a if (!_PyArg_NoKeywords("methodcaller", kw))
1531n/a return NULL;
1532n/a if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
1533n/a return NULL;
1534n/a method = PyObject_GetAttr(obj, mc->name);
1535n/a if (method == NULL)
1536n/a return NULL;
1537n/a result = PyObject_Call(method, mc->args, mc->kwds);
1538n/a Py_DECREF(method);
1539n/a return result;
1540n/a}
1541n/a
1542n/astatic PyObject *
1543n/amethodcaller_repr(methodcallerobject *mc)
1544n/a{
1545n/a PyObject *argreprs, *repr = NULL, *sep, *joinedargreprs;
1546n/a Py_ssize_t numtotalargs, numposargs, numkwdargs, i;
1547n/a int status = Py_ReprEnter((PyObject *)mc);
1548n/a if (status != 0) {
1549n/a if (status < 0)
1550n/a return NULL;
1551n/a return PyUnicode_FromFormat("%s(...)", Py_TYPE(mc)->tp_name);
1552n/a }
1553n/a
1554n/a numkwdargs = mc->kwds != NULL ? PyDict_GET_SIZE(mc->kwds) : 0;
1555n/a numposargs = PyTuple_GET_SIZE(mc->args);
1556n/a numtotalargs = numposargs + numkwdargs;
1557n/a
1558n/a if (numtotalargs == 0) {
1559n/a repr = PyUnicode_FromFormat("%s(%R)", Py_TYPE(mc)->tp_name, mc->name);
1560n/a Py_ReprLeave((PyObject *)mc);
1561n/a return repr;
1562n/a }
1563n/a
1564n/a argreprs = PyTuple_New(numtotalargs);
1565n/a if (argreprs == NULL) {
1566n/a Py_ReprLeave((PyObject *)mc);
1567n/a return NULL;
1568n/a }
1569n/a
1570n/a for (i = 0; i < numposargs; ++i) {
1571n/a PyObject *onerepr = PyObject_Repr(PyTuple_GET_ITEM(mc->args, i));
1572n/a if (onerepr == NULL)
1573n/a goto done;
1574n/a PyTuple_SET_ITEM(argreprs, i, onerepr);
1575n/a }
1576n/a
1577n/a if (numkwdargs != 0) {
1578n/a PyObject *key, *value;
1579n/a Py_ssize_t pos = 0;
1580n/a while (PyDict_Next(mc->kwds, &pos, &key, &value)) {
1581n/a PyObject *onerepr = PyUnicode_FromFormat("%U=%R", key, value);
1582n/a if (onerepr == NULL)
1583n/a goto done;
1584n/a if (i >= numtotalargs) {
1585n/a i = -1;
1586n/a break;
1587n/a }
1588n/a PyTuple_SET_ITEM(argreprs, i, onerepr);
1589n/a ++i;
1590n/a }
1591n/a if (i != numtotalargs) {
1592n/a PyErr_SetString(PyExc_RuntimeError,
1593n/a "keywords dict changed size during iteration");
1594n/a goto done;
1595n/a }
1596n/a }
1597n/a
1598n/a sep = PyUnicode_FromString(", ");
1599n/a if (sep == NULL)
1600n/a goto done;
1601n/a
1602n/a joinedargreprs = PyUnicode_Join(sep, argreprs);
1603n/a Py_DECREF(sep);
1604n/a if (joinedargreprs == NULL)
1605n/a goto done;
1606n/a
1607n/a repr = PyUnicode_FromFormat("%s(%R, %U)", Py_TYPE(mc)->tp_name,
1608n/a mc->name, joinedargreprs);
1609n/a Py_DECREF(joinedargreprs);
1610n/a
1611n/adone:
1612n/a Py_DECREF(argreprs);
1613n/a Py_ReprLeave((PyObject *)mc);
1614n/a return repr;
1615n/a}
1616n/a
1617n/astatic PyObject *
1618n/amethodcaller_reduce(methodcallerobject *mc)
1619n/a{
1620n/a PyObject *newargs;
1621n/a if (!mc->kwds || PyDict_GET_SIZE(mc->kwds) == 0) {
1622n/a Py_ssize_t i;
1623n/a Py_ssize_t callargcount = PyTuple_GET_SIZE(mc->args);
1624n/a newargs = PyTuple_New(1 + callargcount);
1625n/a if (newargs == NULL)
1626n/a return NULL;
1627n/a Py_INCREF(mc->name);
1628n/a PyTuple_SET_ITEM(newargs, 0, mc->name);
1629n/a for (i = 0; i < callargcount; ++i) {
1630n/a PyObject *arg = PyTuple_GET_ITEM(mc->args, i);
1631n/a Py_INCREF(arg);
1632n/a PyTuple_SET_ITEM(newargs, i + 1, arg);
1633n/a }
1634n/a return Py_BuildValue("ON", Py_TYPE(mc), newargs);
1635n/a }
1636n/a else {
1637n/a PyObject *functools;
1638n/a PyObject *partial;
1639n/a PyObject *constructor;
1640n/a PyObject *newargs[2];
1641n/a
1642n/a _Py_IDENTIFIER(partial);
1643n/a functools = PyImport_ImportModule("functools");
1644n/a if (!functools)
1645n/a return NULL;
1646n/a partial = _PyObject_GetAttrId(functools, &PyId_partial);
1647n/a Py_DECREF(functools);
1648n/a if (!partial)
1649n/a return NULL;
1650n/a
1651n/a newargs[0] = (PyObject *)Py_TYPE(mc);
1652n/a newargs[1] = mc->name;
1653n/a constructor = _PyObject_FastCallDict(partial, newargs, 2, mc->kwds);
1654n/a
1655n/a Py_DECREF(partial);
1656n/a return Py_BuildValue("NO", constructor, mc->args);
1657n/a }
1658n/a}
1659n/a
1660n/astatic PyMethodDef methodcaller_methods[] = {
1661n/a {"__reduce__", (PyCFunction)methodcaller_reduce, METH_NOARGS,
1662n/a reduce_doc},
1663n/a {NULL}
1664n/a};
1665n/aPyDoc_STRVAR(methodcaller_doc,
1666n/a"methodcaller(name, ...) --> methodcaller object\n\
1667n/a\n\
1668n/aReturn a callable object that calls the given method on its operand.\n\
1669n/aAfter f = methodcaller('name'), the call f(r) returns r.name().\n\
1670n/aAfter g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
1671n/ar.name('date', foo=1).");
1672n/a
1673n/astatic PyTypeObject methodcaller_type = {
1674n/a PyVarObject_HEAD_INIT(NULL, 0)
1675n/a "operator.methodcaller", /* tp_name */
1676n/a sizeof(methodcallerobject), /* tp_basicsize */
1677n/a 0, /* tp_itemsize */
1678n/a /* methods */
1679n/a (destructor)methodcaller_dealloc, /* tp_dealloc */
1680n/a 0, /* tp_print */
1681n/a 0, /* tp_getattr */
1682n/a 0, /* tp_setattr */
1683n/a 0, /* tp_reserved */
1684n/a (reprfunc)methodcaller_repr, /* tp_repr */
1685n/a 0, /* tp_as_number */
1686n/a 0, /* tp_as_sequence */
1687n/a 0, /* tp_as_mapping */
1688n/a 0, /* tp_hash */
1689n/a (ternaryfunc)methodcaller_call, /* tp_call */
1690n/a 0, /* tp_str */
1691n/a PyObject_GenericGetAttr, /* tp_getattro */
1692n/a 0, /* tp_setattro */
1693n/a 0, /* tp_as_buffer */
1694n/a Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
1695n/a methodcaller_doc, /* tp_doc */
1696n/a (traverseproc)methodcaller_traverse, /* tp_traverse */
1697n/a 0, /* tp_clear */
1698n/a 0, /* tp_richcompare */
1699n/a 0, /* tp_weaklistoffset */
1700n/a 0, /* tp_iter */
1701n/a 0, /* tp_iternext */
1702n/a methodcaller_methods, /* tp_methods */
1703n/a 0, /* tp_members */
1704n/a 0, /* tp_getset */
1705n/a 0, /* tp_base */
1706n/a 0, /* tp_dict */
1707n/a 0, /* tp_descr_get */
1708n/a 0, /* tp_descr_set */
1709n/a 0, /* tp_dictoffset */
1710n/a 0, /* tp_init */
1711n/a 0, /* tp_alloc */
1712n/a methodcaller_new, /* tp_new */
1713n/a 0, /* tp_free */
1714n/a};
1715n/a
1716n/a
1717n/a/* Initialization function for the module (*must* be called PyInit__operator) */
1718n/a
1719n/a
1720n/astatic struct PyModuleDef operatormodule = {
1721n/a PyModuleDef_HEAD_INIT,
1722n/a "_operator",
1723n/a operator_doc,
1724n/a -1,
1725n/a operator_methods,
1726n/a NULL,
1727n/a NULL,
1728n/a NULL,
1729n/a NULL
1730n/a};
1731n/a
1732n/aPyMODINIT_FUNC
1733n/aPyInit__operator(void)
1734n/a{
1735n/a PyObject *m;
1736n/a
1737n/a /* Create the module and add the functions */
1738n/a m = PyModule_Create(&operatormodule);
1739n/a if (m == NULL)
1740n/a return NULL;
1741n/a
1742n/a if (PyType_Ready(&itemgetter_type) < 0)
1743n/a return NULL;
1744n/a Py_INCREF(&itemgetter_type);
1745n/a PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
1746n/a
1747n/a if (PyType_Ready(&attrgetter_type) < 0)
1748n/a return NULL;
1749n/a Py_INCREF(&attrgetter_type);
1750n/a PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
1751n/a
1752n/a if (PyType_Ready(&methodcaller_type) < 0)
1753n/a return NULL;
1754n/a Py_INCREF(&methodcaller_type);
1755n/a PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
1756n/a return m;
1757n/a}