annotate Modules/operator.c @ 58765:2ba7677bfa95 legacy-trunk

testCopysign was defined twice in test_math; combine the definitions
author Mark Dickinson <dickinsm@gmail.com>
date Sat, 06 Feb 2010 23:11:25 +0000
parents f5d68604c6b9
children fd25030223b0
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22805
diff changeset
1
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22805
diff changeset
2 #include "Python.h"
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22805
diff changeset
3
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22805
diff changeset
4 PyDoc_STRVAR(operator_doc,
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22805
diff changeset
5 "Operator interface.\n\
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
6 \n\
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
7 This module exports a set of functions implemented in C corresponding\n\
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
8 to the intrinsic operators of Python. For example, operator.add(x, y)\n\
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
9 is equivalent to the expression x+y. The function names are those\n\
57111
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
10 used for special methods; variants without leading and trailing\n\
23832
ed1ba37a46b7 Patch #568124: Add doc string macros.
Martin v. Löwis <martin@v.loewis.de>
parents: 22805
diff changeset
11 '__' are also provided for convenience.");
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
12
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
13 #define spam1(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
14 return AOP(a1); }
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
15
13422
aa3d3bece774 ANSI-fy the sources.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
16 #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
17 PyObject *a1, *a2; \
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
18 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
19 return AOP(a1,a2); }
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
20
13422
aa3d3bece774 ANSI-fy the sources.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
21 #define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
22 PyObject *a1; int a2; \
14654
0bd746bc7627 Add invert() and __invert__() as aliases for inv()/__inv__().
Fred Drake <fdrake@acm.org>
parents: 13651
diff changeset
23 if(! PyArg_ParseTuple(a,"Oi:" #OP,&a1,&a2)) return NULL; \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
24 return AOP(a1,a2); }
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
25
13422
aa3d3bece774 ANSI-fy the sources.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
26 #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
27 PyObject *a1, *a2; \
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
28 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
29 if(-1 == AOP(a1,a2)) return NULL; \
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
30 Py_INCREF(Py_None); \
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
31 return Py_None; }
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
32
13422
aa3d3bece774 ANSI-fy the sources.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
33 #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
34 PyObject *a1, *a2, *a3; \
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
35 if(! PyArg_UnpackTuple(a,#OP,3,3,&a1,&a2,&a3)) return NULL; \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
36 if(-1 == AOP(a1,a2,a3)) return NULL; \
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
37 Py_INCREF(Py_None); \
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
38 return Py_None; }
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
39
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
40 #define spami(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a1) { \
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
41 long r; \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
42 if(-1 == (r=AOP(a1))) return NULL; \
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
43 return PyBool_FromLong(r); }
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
44
13422
aa3d3bece774 ANSI-fy the sources.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
45 #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
46 PyObject *a1, *a2; long r; \
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
47 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
48 if(-1 == (r=AOP(a1,a2))) return NULL; \
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
49 return PyInt_FromLong(r); }
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
50
37919
ab4e65c809e6 Fix more ssize_t problems.
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
51 #define spamn2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
ab4e65c809e6 Fix more ssize_t problems.
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
52 PyObject *a1, *a2; Py_ssize_t r; \
ab4e65c809e6 Fix more ssize_t problems.
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
53 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
ab4e65c809e6 Fix more ssize_t problems.
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
54 if(-1 == (r=AOP(a1,a2))) return NULL; \
ab4e65c809e6 Fix more ssize_t problems.
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
55 return PyInt_FromSsize_t(r); }
ab4e65c809e6 Fix more ssize_t problems.
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
56
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
57 #define spami2b(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
58 PyObject *a1, *a2; long r; \
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
59 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
60 if(-1 == (r=AOP(a1,a2))) return NULL; \
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
61 return PyBool_FromLong(r); }
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
62
19111
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
63 #define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
64 PyObject *a1, *a2; \
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
65 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \
19111
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
66 return PyObject_RichCompare(a1,a2,A); }
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
67
55111
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
68 /* Deprecated operators that need warnings. */
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
69 static int
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
70 op_isCallable(PyObject *x)
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
71 {
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
72 if (PyErr_WarnPy3k("operator.isCallable() is not supported in 3.x. "
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
73 "Use hasattr(obj, '__call__').", 1) < 0)
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
74 return -1;
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
75 return PyCallable_Check(x);
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
76 }
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
77
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
78 static int
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
79 op_sequenceIncludes(PyObject *seq, PyObject* ob)
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
80 {
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
81 if (PyErr_WarnPy3k("operator.sequenceIncludes() is not supported "
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
82 "in 3.x. Use operator.contains().", 1) < 0)
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
83 return -1;
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
84 return PySequence_Contains(seq, ob);
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
85 }
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
86
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
87 spami(isCallable , op_isCallable)
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
88 spami(isNumberType , PyNumber_Check)
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
89 spami(truth , PyObject_IsTrue)
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
90 spam2(op_add , PyNumber_Add)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
91 spam2(op_sub , PyNumber_Subtract)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
92 spam2(op_mul , PyNumber_Multiply)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
93 spam2(op_div , PyNumber_Divide)
19111
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
94 spam2(op_floordiv , PyNumber_FloorDivide)
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
95 spam2(op_truediv , PyNumber_TrueDivide)
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
96 spam2(op_mod , PyNumber_Remainder)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
97 spam1(op_neg , PyNumber_Negative)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
98 spam1(op_pos , PyNumber_Positive)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
99 spam1(op_abs , PyNumber_Absolute)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
100 spam1(op_inv , PyNumber_Invert)
14654
0bd746bc7627 Add invert() and __invert__() as aliases for inv()/__inv__().
Fred Drake <fdrake@acm.org>
parents: 13651
diff changeset
101 spam1(op_invert , PyNumber_Invert)
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
102 spam2(op_lshift , PyNumber_Lshift)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
103 spam2(op_rshift , PyNumber_Rshift)
7936
1f38a2b548bf Add not_ and __not__.
Guido van Rossum <guido@python.org>
parents: 6227
diff changeset
104 spami(op_not_ , PyObject_Not)
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
105 spam2(op_and_ , PyNumber_And)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
106 spam2(op_xor , PyNumber_Xor)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
107 spam2(op_or_ , PyNumber_Or)
36152
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
108 spam2(op_iadd , PyNumber_InPlaceAdd)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
109 spam2(op_isub , PyNumber_InPlaceSubtract)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
110 spam2(op_imul , PyNumber_InPlaceMultiply)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
111 spam2(op_idiv , PyNumber_InPlaceDivide)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
112 spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
113 spam2(op_itruediv , PyNumber_InPlaceTrueDivide)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
114 spam2(op_imod , PyNumber_InPlaceRemainder)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
115 spam2(op_ilshift , PyNumber_InPlaceLshift)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
116 spam2(op_irshift , PyNumber_InPlaceRshift)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
117 spam2(op_iand , PyNumber_InPlaceAnd)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
118 spam2(op_ixor , PyNumber_InPlaceXor)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
119 spam2(op_ior , PyNumber_InPlaceOr)
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
120 spami(isSequenceType , PySequence_Check)
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
121 spam2(op_concat , PySequence_Concat)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
122 spamoi(op_repeat , PySequence_Repeat)
36152
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
123 spam2(op_iconcat , PySequence_InPlaceConcat)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
124 spamoi(op_irepeat , PySequence_InPlaceRepeat)
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
125 spami2b(op_contains , PySequence_Contains)
55111
466ca96be182 Issue 2370: Add Python 3 warnings for the removal of operator.isCallable and
Alexandre Vassalotti <alexandre@peadrop.com>
parents: 47527
diff changeset
126 spami2b(sequenceIncludes, op_sequenceIncludes)
37919
ab4e65c809e6 Fix more ssize_t problems.
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
127 spamn2(indexOf , PySequence_Index)
ab4e65c809e6 Fix more ssize_t problems.
Martin v. Löwis <martin@v.loewis.de>
parents: 37756
diff changeset
128 spamn2(countOf , PySequence_Count)
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
129 spami(isMappingType , PyMapping_Check)
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
130 spam2(op_getitem , PyObject_GetItem)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
131 spam2n(op_delitem , PyObject_DelItem)
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
132 spam3n(op_setitem , PyObject_SetItem)
19111
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
133 spamrc(op_lt , Py_LT)
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
134 spamrc(op_le , Py_LE)
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
135 spamrc(op_eq , Py_EQ)
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
136 spamrc(op_ne , Py_NE)
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
137 spamrc(op_gt , Py_GT)
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
138 spamrc(op_ge , Py_GE)
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
139
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
140 static PyObject*
24887
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24796
diff changeset
141 op_pow(PyObject *s, PyObject *a)
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24796
diff changeset
142 {
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24796
diff changeset
143 PyObject *a1, *a2;
26818
e094a03a80ff SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents: 24887
diff changeset
144 if (PyArg_UnpackTuple(a,"pow", 2, 2, &a1, &a2))
24887
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24796
diff changeset
145 return PyNumber_Power(a1, a2, Py_None);
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24796
diff changeset
146 return NULL;
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24796
diff changeset
147 }
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24796
diff changeset
148
d797cfa8ca0d Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents: 24796
diff changeset
149 static PyObject*
36152
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
150 op_ipow(PyObject *s, PyObject *a)
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
151 {
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
152 PyObject *a1, *a2;
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
153 if (PyArg_UnpackTuple(a,"ipow", 2, 2, &a1, &a2))
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
154 return PyNumber_InPlacePower(a1, a2, Py_None);
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
155 return NULL;
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
156 }
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
157
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
158 static PyObject *
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
159 op_index(PyObject *s, PyObject *a)
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
160 {
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37919
diff changeset
161 return PyNumber_Index(a);
36882
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
162 }
6a9c7165b17b Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents: 36518
diff changeset
163
36152
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
164 static PyObject*
27290
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
165 is_(PyObject *s, PyObject *a)
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
166 {
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
167 PyObject *a1, *a2, *result = NULL;
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
168 if (PyArg_UnpackTuple(a,"is_", 2, 2, &a1, &a2)) {
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
169 result = (a1 == a2) ? Py_True : Py_False;
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
170 Py_INCREF(result);
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
171 }
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
172 return result;
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
173 }
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
174
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
175 static PyObject*
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
176 is_not(PyObject *s, PyObject *a)
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
177 {
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
178 PyObject *a1, *a2, *result = NULL;
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
179 if (PyArg_UnpackTuple(a,"is_not", 2, 2, &a1, &a2)) {
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
180 result = (a1 != a2) ? Py_True : Py_False;
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
181 Py_INCREF(result);
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
182 }
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
183 return result;
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
184 }
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
185
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
186 static PyObject*
13422
aa3d3bece774 ANSI-fy the sources.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
187 op_getslice(PyObject *s, PyObject *a)
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
188 {
4775
0d0c413b6f25 Tabification changes only; the module was already newly named.
Barry Warsaw <barry@python.org>
parents: 4580
diff changeset
189 PyObject *a1;
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
190 Py_ssize_t a2, a3;
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
191
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
192 if (!PyArg_ParseTuple(a, "Onn:getslice", &a1, &a2, &a3))
4775
0d0c413b6f25 Tabification changes only; the module was already newly named.
Barry Warsaw <barry@python.org>
parents: 4580
diff changeset
193 return NULL;
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
194 return PySequence_GetSlice(a1, a2, a3);
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
195 }
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
196
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
197 static PyObject*
13422
aa3d3bece774 ANSI-fy the sources.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
198 op_setslice(PyObject *s, PyObject *a)
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
199 {
4775
0d0c413b6f25 Tabification changes only; the module was already newly named.
Barry Warsaw <barry@python.org>
parents: 4580
diff changeset
200 PyObject *a1, *a4;
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
201 Py_ssize_t a2, a3;
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
202
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
203 if (!PyArg_ParseTuple(a, "OnnO:setslice", &a1, &a2, &a3, &a4))
4775
0d0c413b6f25 Tabification changes only; the module was already newly named.
Barry Warsaw <barry@python.org>
parents: 4580
diff changeset
204 return NULL;
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
205
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
206 if (-1 == PySequence_SetSlice(a1, a2, a3, a4))
4775
0d0c413b6f25 Tabification changes only; the module was already newly named.
Barry Warsaw <barry@python.org>
parents: 4580
diff changeset
207 return NULL;
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
208
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
209 Py_RETURN_NONE;
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
210 }
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
211
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
212 static PyObject*
13422
aa3d3bece774 ANSI-fy the sources.
Fred Drake <fdrake@acm.org>
parents: 9588
diff changeset
213 op_delslice(PyObject *s, PyObject *a)
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
214 {
4775
0d0c413b6f25 Tabification changes only; the module was already newly named.
Barry Warsaw <barry@python.org>
parents: 4580
diff changeset
215 PyObject *a1;
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
216 Py_ssize_t a2, a3;
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
217
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
218 if (!PyArg_ParseTuple(a, "Onn:delslice", &a1, &a2, &a3))
4775
0d0c413b6f25 Tabification changes only; the module was already newly named.
Barry Warsaw <barry@python.org>
parents: 4580
diff changeset
219 return NULL;
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
220
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
221 if (-1 == PySequence_DelSlice(a1, a2, a3))
4775
0d0c413b6f25 Tabification changes only; the module was already newly named.
Barry Warsaw <barry@python.org>
parents: 4580
diff changeset
222 return NULL;
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
223
41036
6d8dcad12fd9 Patch #1654417: make operator.{get,set,del}slice use the full range
Georg Brandl <georg@python.org>
parents: 39463
diff changeset
224 Py_RETURN_NONE;
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
225 }
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
226
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
227 #undef spam1
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
228 #undef spam2
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
229 #undef spam1o
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
230 #undef spam1o
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24471
diff changeset
231 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)},
36149
e44a833eca43 two missing PyDoc_STR().
Armin Rigo <arigo@tunes.org>
parents: 35449
diff changeset
232 #define spam2(OP,ALTOP,DOC) {#OP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)}, \
24796
de71e6fe64b0 Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents: 24471
diff changeset
233 {#ALTOP, op_##OP, METH_VARARGS, PyDoc_STR(DOC)},
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
234 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)},
36149
e44a833eca43 two missing PyDoc_STR().
Armin Rigo <arigo@tunes.org>
parents: 35449
diff changeset
235 #define spam2o(OP,ALTOP,DOC) {#OP, op_##OP, METH_O, PyDoc_STR(DOC)}, \
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
236 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)},
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
237
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
238 static struct PyMethodDef operator_methods[] = {
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
239
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
240 spam1o(isCallable,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
241 "isCallable(a) -- Same as callable(a).")
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
242 spam1o(isNumberType,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
243 "isNumberType(a) -- Return True if a has a numeric type, False otherwise.")
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
244 spam1o(isSequenceType,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
245 "isSequenceType(a) -- Return True if a has a sequence type, False otherwise.")
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
246 spam1o(truth,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
247 "truth(a) -- Return True if a is true, False otherwise.")
14654
0bd746bc7627 Add invert() and __invert__() as aliases for inv()/__inv__().
Fred Drake <fdrake@acm.org>
parents: 13651
diff changeset
248 spam2(contains,__contains__,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
249 "contains(a, b) -- Same as b in a (note reversed operands).")
14654
0bd746bc7627 Add invert() and __invert__() as aliases for inv()/__inv__().
Fred Drake <fdrake@acm.org>
parents: 13651
diff changeset
250 spam1(sequenceIncludes,
0bd746bc7627 Add invert() and __invert__() as aliases for inv()/__inv__().
Fred Drake <fdrake@acm.org>
parents: 13651
diff changeset
251 "sequenceIncludes(a, b) -- Same as b in a (note reversed operands; deprecated).")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
252 spam1(indexOf,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
253 "indexOf(a, b) -- Return the first index of b in a.")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
254 spam1(countOf,
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
255 "countOf(a, b) -- Return the number of times b occurs in a.")
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
256 spam1o(isMappingType,
22785
aa8d59823add Add the 'bool' type and its values 'False' and 'True', as described in
Guido van Rossum <guido@python.org>
parents: 19111
diff changeset
257 "isMappingType(a) -- Return True if a has a mapping type, False otherwise.")
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
258
27290
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
259 spam1(is_, "is_(a, b) -- Same as a is b.")
e83a42a90df1 SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents: 27044
diff changeset
260 spam1(is_not, "is_not(a, b) -- Same as a is not b.")
39463
6cafdc81cfde Patch #1538606, Patch to fix __index__() clipping.
Neal Norwitz <nnorwitz@gmail.com>
parents: 37919
diff changeset
261 spam2o(index, __index__, "index(a) -- Same as a.__index__()")
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
262 spam2(add,__add__, "add(a, b) -- Same as a + b.")
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
263 spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
264 spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
19111
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
265 spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
266 spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
267 spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
268 spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
269 spam2o(neg,__neg__, "neg(a) -- Same as -a.")
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
270 spam2o(pos,__pos__, "pos(a) -- Same as +a.")
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
271 spam2o(abs,__abs__, "abs(a) -- Same as abs(a).")
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
272 spam2o(inv,__inv__, "inv(a) -- Same as ~a.")
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
273 spam2o(invert,__invert__, "invert(a) -- Same as ~a.")
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
274 spam2(lshift,__lshift__, "lshift(a, b) -- Same as a << b.")
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
275 spam2(rshift,__rshift__, "rshift(a, b) -- Same as a >> b.")
27044
23280a64288e SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents: 26818
diff changeset
276 spam2o(not_,__not__, "not_(a) -- Same as not a.")
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
277 spam2(and_,__and__, "and_(a, b) -- Same as a & b.")
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
278 spam2(xor,__xor__, "xor(a, b) -- Same as a ^ b.")
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
279 spam2(or_,__or__, "or_(a, b) -- Same as a | b.")
57111
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
280 spam2(iadd,__iadd__, "a = iadd(a, b) -- Same as a += b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
281 spam2(isub,__isub__, "a = isub(a, b) -- Same as a -= b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
282 spam2(imul,__imul__, "a = imul(a, b) -- Same as a *= b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
283 spam2(idiv,__idiv__, "a = idiv(a, b) -- Same as a /= b when __future__.division is not in effect.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
284 spam2(ifloordiv,__ifloordiv__, "a = ifloordiv(a, b) -- Same as a //= b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
285 spam2(itruediv,__itruediv__, "a = itruediv(a, b) -- Same as a /= b when __future__.division is in effect.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
286 spam2(imod,__imod__, "a = imod(a, b) -- Same as a %= b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
287 spam2(ilshift,__ilshift__, "a = ilshift(a, b) -- Same as a <<= b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
288 spam2(irshift,__irshift__, "a = irshift(a, b) -- Same as a >>= b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
289 spam2(iand,__iand__, "a = iand(a, b) -- Same as a &= b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
290 spam2(ixor,__ixor__, "a = ixor(a, b) -- Same as a ^= b.")
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
291 spam2(ior,__ior__, "a = ior(a, b) -- Same as a |= b.")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
292 spam2(concat,__concat__,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
293 "concat(a, b) -- Same as a + b, for a and b sequences.")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
294 spam2(repeat,__repeat__,
4580
11bd4f497742 Correct typo -- repeat implements *, not +.
Guido van Rossum <guido@python.org>
parents: 4104
diff changeset
295 "repeat(a, b) -- Return a * b, where a is a sequence, and b is an integer.")
36152
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
296 spam2(iconcat,__iconcat__,
57111
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
297 "a = iconcat(a, b) -- Same as a += b, for a and b sequences.")
36152
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
298 spam2(irepeat,__irepeat__,
57111
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
299 "a = irepeat(a, b) -- Same as a *= b, where a is a sequence, and b is an integer.")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
300 spam2(getitem,__getitem__,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
301 "getitem(a, b) -- Same as a[b].")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
302 spam2(setitem,__setitem__,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
303 "setitem(a, b, c) -- Same as a[b] = c.")
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
304 spam2(delitem,__delitem__,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
305 "delitem(a, b) -- Same as del a[b].")
36152
f9281283b109 adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents: 36149
diff changeset
306 spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.")
57111
f5d68604c6b9 #7259: show correct equivalent for operator.i* operations in docstring; fix minor issues in operator docs.
Georg Brandl <georg@python.org>
parents: 55111
diff changeset
307 spam2(ipow,__ipow__, "a = ipow(a, b) -- Same as a **= b.")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
308 spam2(getslice,__getslice__,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
309 "getslice(a, b, c) -- Same as a[b:c].")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
310 spam2(setslice,__setslice__,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
311 "setslice(a, b, c, d) -- Same as a[b:c] = d.")
4089
9f70d995d027 Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents: 4033
diff changeset
312 spam2(delslice,__delslice__,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
313 "delslice(a, b, c) -- Same as del a[b:c].")
19111
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
314 spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
315 spam2(le,__le__, "le(a, b) -- Same as a<=b.")
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
316 spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
317 spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
318 spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
6ba441507220 Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents: 14654
diff changeset
319 spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
4033
5592de9656de Made it palatable for K&R preprocessors. The spam macros don't have
Guido van Rossum <guido@python.org>
parents: 3976
diff changeset
320
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
321 {NULL, NULL} /* sentinel */
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
322
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
323 };
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
324
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
325 /* itemgetter object **********************************************************/
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
326
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
327 typedef struct {
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
328 PyObject_HEAD
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
329 Py_ssize_t nitems;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
330 PyObject *item;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
331 } itemgetterobject;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
332
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
333 static PyTypeObject itemgetter_type;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
334
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
335 static PyObject *
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
336 itemgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
337 {
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
338 itemgetterobject *ig;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
339 PyObject *item;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
340 Py_ssize_t nitems;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
341
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34716
diff changeset
342 if (!_PyArg_NoKeywords("itemgetter()", kwds))
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34716
diff changeset
343 return NULL;
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34716
diff changeset
344
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
345 nitems = PyTuple_GET_SIZE(args);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
346 if (nitems <= 1) {
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
347 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &item))
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
348 return NULL;
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
349 } else
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
350 item = args;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
351
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
352 /* create itemgetterobject structure */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
353 ig = PyObject_GC_New(itemgetterobject, &itemgetter_type);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
354 if (ig == NULL)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
355 return NULL;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
356
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
357 Py_INCREF(item);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
358 ig->item = item;
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
359 ig->nitems = nitems;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
360
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
361 PyObject_GC_Track(ig);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
362 return (PyObject *)ig;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
363 }
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
364
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
365 static void
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
366 itemgetter_dealloc(itemgetterobject *ig)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
367 {
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
368 PyObject_GC_UnTrack(ig);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
369 Py_XDECREF(ig->item);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
370 PyObject_GC_Del(ig);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
371 }
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
372
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
373 static int
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
374 itemgetter_traverse(itemgetterobject *ig, visitproc visit, void *arg)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
375 {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
376 Py_VISIT(ig->item);
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
377 return 0;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
378 }
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
379
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
380 static PyObject *
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
381 itemgetter_call(itemgetterobject *ig, PyObject *args, PyObject *kw)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
382 {
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
383 PyObject *obj, *result;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
384 Py_ssize_t i, nitems=ig->nitems;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
385
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
386 if (!PyArg_UnpackTuple(args, "itemgetter", 1, 1, &obj))
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
387 return NULL;
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
388 if (nitems == 1)
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
389 return PyObject_GetItem(obj, ig->item);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
390
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
391 assert(PyTuple_Check(ig->item));
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
392 assert(PyTuple_GET_SIZE(ig->item) == nitems);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
393
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
394 result = PyTuple_New(nitems);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
395 if (result == NULL)
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
396 return NULL;
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
397
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
398 for (i=0 ; i < nitems ; i++) {
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
399 PyObject *item, *val;
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
400 item = PyTuple_GET_ITEM(ig->item, i);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
401 val = PyObject_GetItem(obj, item);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
402 if (val == NULL) {
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
403 Py_DECREF(result);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
404 return NULL;
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
405 }
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
406 PyTuple_SET_ITEM(result, i, val);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
407 }
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
408 return result;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
409 }
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
410
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
411 PyDoc_STRVAR(itemgetter_doc,
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
412 "itemgetter(item, ...) --> itemgetter object\n\
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
413 \n\
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
414 Return a callable object that fetches the given item(s) from its operand.\n\
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
415 After, f=itemgetter(2), the call f(r) returns r[2].\n\
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
416 After, g=itemgetter(2,5,3), the call g(r) returns (r[2], r[5], r[3])");
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
417
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
418 static PyTypeObject itemgetter_type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41036
diff changeset
419 PyVarObject_HEAD_INIT(NULL, 0)
31110
d7630ce7aa9f Fix typos.
Raymond Hettinger <python@rcn.com>
parents: 31052
diff changeset
420 "operator.itemgetter", /* tp_name */
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
421 sizeof(itemgetterobject), /* tp_basicsize */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
422 0, /* tp_itemsize */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
423 /* methods */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
424 (destructor)itemgetter_dealloc, /* tp_dealloc */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
425 0, /* tp_print */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
426 0, /* tp_getattr */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
427 0, /* tp_setattr */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
428 0, /* tp_compare */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
429 0, /* tp_repr */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
430 0, /* tp_as_number */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
431 0, /* tp_as_sequence */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
432 0, /* tp_as_mapping */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
433 0, /* tp_hash */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
434 (ternaryfunc)itemgetter_call, /* tp_call */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
435 0, /* tp_str */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
436 PyObject_GenericGetAttr, /* tp_getattro */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
437 0, /* tp_setattro */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
438 0, /* tp_as_buffer */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
439 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
440 itemgetter_doc, /* tp_doc */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
441 (traverseproc)itemgetter_traverse, /* tp_traverse */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
442 0, /* tp_clear */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
443 0, /* tp_richcompare */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
444 0, /* tp_weaklistoffset */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
445 0, /* tp_iter */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
446 0, /* tp_iternext */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
447 0, /* tp_methods */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
448 0, /* tp_members */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
449 0, /* tp_getset */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
450 0, /* tp_base */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
451 0, /* tp_dict */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
452 0, /* tp_descr_get */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
453 0, /* tp_descr_set */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
454 0, /* tp_dictoffset */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
455 0, /* tp_init */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
456 0, /* tp_alloc */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
457 itemgetter_new, /* tp_new */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
458 0, /* tp_free */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
459 };
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
460
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
461
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
462 /* attrgetter object **********************************************************/
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
463
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
464 typedef struct {
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
465 PyObject_HEAD
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
466 Py_ssize_t nattrs;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
467 PyObject *attr;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
468 } attrgetterobject;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
469
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
470 static PyTypeObject attrgetter_type;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
471
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
472 static PyObject *
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
473 attrgetter_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
474 {
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
475 attrgetterobject *ag;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
476 PyObject *attr;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
477 Py_ssize_t nattrs;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
478
35449
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34716
diff changeset
479 if (!_PyArg_NoKeywords("attrgetter()", kwds))
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34716
diff changeset
480 return NULL;
1fdfd0130e28 Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents: 34716
diff changeset
481
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
482 nattrs = PyTuple_GET_SIZE(args);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
483 if (nattrs <= 1) {
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
484 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &attr))
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
485 return NULL;
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
486 } else
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
487 attr = args;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
488
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
489 /* create attrgetterobject structure */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
490 ag = PyObject_GC_New(attrgetterobject, &attrgetter_type);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
491 if (ag == NULL)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
492 return NULL;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
493
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
494 Py_INCREF(attr);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
495 ag->attr = attr;
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
496 ag->nattrs = nattrs;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
497
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
498 PyObject_GC_Track(ag);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
499 return (PyObject *)ag;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
500 }
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
501
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
502 static void
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
503 attrgetter_dealloc(attrgetterobject *ag)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
504 {
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
505 PyObject_GC_UnTrack(ag);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
506 Py_XDECREF(ag->attr);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
507 PyObject_GC_Del(ag);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
508 }
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
509
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
510 static int
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
511 attrgetter_traverse(attrgetterobject *ag, visitproc visit, void *arg)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
512 {
37756
7b2e436a4a21 Use Py_VISIT in all tp_traverse methods, instead of traversing manually or
Thomas Wouters <thomas@python.org>
parents: 36882
diff changeset
513 Py_VISIT(ag->attr);
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
514 return 0;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
515 }
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
516
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
517 static PyObject *
45487
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
518 dotted_getattr(PyObject *obj, PyObject *attr)
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
519 {
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
520 char *s, *p;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
521
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
522 #ifdef Py_USING_UNICODE
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
523 if (PyUnicode_Check(attr)) {
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
524 attr = _PyUnicode_AsDefaultEncodedString(attr, NULL);
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
525 if (attr == NULL)
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
526 return NULL;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
527 }
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
528 #endif
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
529
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
530 if (!PyString_Check(attr)) {
45487
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
531 PyErr_SetString(PyExc_TypeError,
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
532 "attribute name must be a string");
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
533 return NULL;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
534 }
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
535
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
536 s = PyString_AS_STRING(attr);
45487
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
537 Py_INCREF(obj);
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
538 for (;;) {
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
539 PyObject *newobj, *str;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
540 p = strchr(s, '.');
47527
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
541 str = p ? PyString_FromStringAndSize(s, (p-s)) :
52294af4b663 This reverts r63675 based on the discussion in this thread:
Gregory P. Smith <greg@mad-scientist.com>
parents: 47294
diff changeset
542 PyString_FromString(s);
45487
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
543 if (str == NULL) {
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
544 Py_DECREF(obj);
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
545 return NULL;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
546 }
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
547 newobj = PyObject_GetAttr(obj, str);
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
548 Py_DECREF(str);
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
549 Py_DECREF(obj);
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
550 if (newobj == NULL)
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
551 return NULL;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
552 obj = newobj;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
553 if (p == NULL) break;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
554 s = p+1;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
555 }
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
556
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
557 return obj;
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
558 }
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
559
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
560 static PyObject *
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
561 attrgetter_call(attrgetterobject *ag, PyObject *args, PyObject *kw)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
562 {
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
563 PyObject *obj, *result;
36518
9f3f87e920a6 Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents: 36324
diff changeset
564 Py_ssize_t i, nattrs=ag->nattrs;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
565
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
566 if (!PyArg_UnpackTuple(args, "attrgetter", 1, 1, &obj))
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
567 return NULL;
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
568 if (ag->nattrs == 1)
45487
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
569 return dotted_getattr(obj, ag->attr);
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
570
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
571 assert(PyTuple_Check(ag->attr));
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
572 assert(PyTuple_GET_SIZE(ag->attr) == nattrs);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
573
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
574 result = PyTuple_New(nattrs);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
575 if (result == NULL)
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
576 return NULL;
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
577
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
578 for (i=0 ; i < nattrs ; i++) {
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
579 PyObject *attr, *val;
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
580 attr = PyTuple_GET_ITEM(ag->attr, i);
45487
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
581 val = dotted_getattr(obj, attr);
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
582 if (val == NULL) {
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
583 Py_DECREF(result);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
584 return NULL;
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
585 }
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
586 PyTuple_SET_ITEM(result, i, val);
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
587 }
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
588 return result;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
589 }
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
590
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
591 PyDoc_STRVAR(attrgetter_doc,
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
592 "attrgetter(attr, ...) --> attrgetter object\n\
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
593 \n\
34716
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
594 Return a callable object that fetches the given attribute(s) from its operand.\n\
ddc2276c247e operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents: 31110
diff changeset
595 After, f=attrgetter('name'), the call f(r) returns r.name.\n\
45487
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
596 After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
597 After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\
813c146858f7 #1826: allow dotted attribute paths in operator.attrgetter.
Georg Brandl <georg@python.org>
parents: 42333
diff changeset
598 (r.name.first, r.name.last).");
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
599
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
600 static PyTypeObject attrgetter_type = {
42333
6620a510e9a9 PEP 3123: Provide forward compatibility with Python 3.0, while keeping
Martin v. Löwis <martin@v.loewis.de>
parents: 41036
diff changeset
601 PyVarObject_HEAD_INIT(NULL, 0)
31110
d7630ce7aa9f Fix typos.
Raymond Hettinger <python@rcn.com>
parents: 31052
diff changeset
602 "operator.attrgetter", /* tp_name */
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
603 sizeof(attrgetterobject), /* tp_basicsize */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
604 0, /* tp_itemsize */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
605 /* methods */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
606 (destructor)attrgetter_dealloc, /* tp_dealloc */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
607 0, /* tp_print */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
608 0, /* tp_getattr */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
609 0, /* tp_setattr */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
610 0, /* tp_compare */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
611 0, /* tp_repr */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
612 0, /* tp_as_number */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
613 0, /* tp_as_sequence */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
614 0, /* tp_as_mapping */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
615 0, /* tp_hash */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
616 (ternaryfunc)attrgetter_call, /* tp_call */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
617 0, /* tp_str */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
618 PyObject_GenericGetAttr, /* tp_getattro */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
619 0, /* tp_setattro */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
620 0, /* tp_as_buffer */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
621 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
622 attrgetter_doc, /* tp_doc */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
623 (traverseproc)attrgetter_traverse, /* tp_traverse */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
624 0, /* tp_clear */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
625 0, /* tp_richcompare */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
626 0, /* tp_weaklistoffset */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
627 0, /* tp_iter */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
628 0, /* tp_iternext */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
629 0, /* tp_methods */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
630 0, /* tp_members */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
631 0, /* tp_getset */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
632 0, /* tp_base */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
633 0, /* tp_dict */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
634 0, /* tp_descr_get */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
635 0, /* tp_descr_set */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
636 0, /* tp_dictoffset */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
637 0, /* tp_init */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
638 0, /* tp_alloc */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
639 attrgetter_new, /* tp_new */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
640 0, /* tp_free */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
641 };
45488
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
642
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
643
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
644 /* methodcaller object **********************************************************/
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
645
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
646 typedef struct {
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
647 PyObject_HEAD
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
648 PyObject *name;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
649 PyObject *args;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
650 PyObject *kwds;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
651 } methodcallerobject;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
652
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
653 static PyTypeObject methodcaller_type;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
654
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
655 static PyObject *
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
656 methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
657 {
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
658 methodcallerobject *mc;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
659 PyObject *name, *newargs;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
660
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
661 if (PyTuple_GET_SIZE(args) < 1) {
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
662 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least "
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
663 "one argument, the method name");
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
664 return NULL;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
665 }
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
666
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
667 /* create methodcallerobject structure */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
668 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
669 if (mc == NULL)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
670 return NULL;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
671
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
672 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args));
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
673 if (newargs == NULL) {
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
674 Py_DECREF(mc);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
675 return NULL;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
676 }
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
677 mc->args = newargs;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
678
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
679 name = PyTuple_GET_ITEM(args, 0);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
680 Py_INCREF(name);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
681 mc->name = name;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
682
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
683 Py_XINCREF(kwds);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
684 mc->kwds = kwds;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
685
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
686 PyObject_GC_Track(mc);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
687 return (PyObject *)mc;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
688 }
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
689
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
690 static void
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
691 methodcaller_dealloc(methodcallerobject *mc)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
692 {
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
693 PyObject_GC_UnTrack(mc);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
694 Py_XDECREF(mc->name);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
695 Py_XDECREF(mc->args);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
696 Py_XDECREF(mc->kwds);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
697 PyObject_GC_Del(mc);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
698 }
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
699
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
700 static int
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
701 methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
702 {
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
703 Py_VISIT(mc->args);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
704 Py_VISIT(mc->kwds);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
705 return 0;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
706 }
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
707
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
708 static PyObject *
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
709 methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
710 {
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
711 PyObject *method, *obj, *result;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
712
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
713 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj))
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
714 return NULL;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
715 method = PyObject_GetAttr(obj, mc->name);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
716 if (method == NULL)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
717 return NULL;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
718 result = PyObject_Call(method, mc->args, mc->kwds);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
719 Py_DECREF(method);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
720 return result;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
721 }
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
722
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
723 PyDoc_STRVAR(methodcaller_doc,
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
724 "methodcaller(name, ...) --> methodcaller object\n\
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
725 \n\
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
726 Return a callable object that calls the given method on its operand.\n\
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
727 After, f = methodcaller('name'), the call f(r) returns r.name().\n\
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
728 After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
729 r.name('date', foo=1).");
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
730
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
731 static PyTypeObject methodcaller_type = {
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
732 PyVarObject_HEAD_INIT(NULL, 0)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
733 "operator.methodcaller", /* tp_name */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
734 sizeof(methodcallerobject), /* tp_basicsize */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
735 0, /* tp_itemsize */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
736 /* methods */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
737 (destructor)methodcaller_dealloc, /* tp_dealloc */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
738 0, /* tp_print */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
739 0, /* tp_getattr */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
740 0, /* tp_setattr */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
741 0, /* tp_compare */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
742 0, /* tp_repr */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
743 0, /* tp_as_number */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
744 0, /* tp_as_sequence */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
745 0, /* tp_as_mapping */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
746 0, /* tp_hash */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
747 (ternaryfunc)methodcaller_call, /* tp_call */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
748 0, /* tp_str */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
749 PyObject_GenericGetAttr, /* tp_getattro */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
750 0, /* tp_setattro */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
751 0, /* tp_as_buffer */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
752 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
753 methodcaller_doc, /* tp_doc */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
754 (traverseproc)methodcaller_traverse, /* tp_traverse */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
755 0, /* tp_clear */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
756 0, /* tp_richcompare */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
757 0, /* tp_weaklistoffset */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
758 0, /* tp_iter */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
759 0, /* tp_iternext */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
760 0, /* tp_methods */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
761 0, /* tp_members */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
762 0, /* tp_getset */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
763 0, /* tp_base */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
764 0, /* tp_dict */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
765 0, /* tp_descr_get */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
766 0, /* tp_descr_set */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
767 0, /* tp_dictoffset */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
768 0, /* tp_init */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
769 0, /* tp_alloc */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
770 methodcaller_new, /* tp_new */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
771 0, /* tp_free */
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
772 };
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
773
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
774
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
775 /* Initialization function for the module (*must* be called initoperator) */
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
776
24471
f66b3a3509c7 Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents: 23832
diff changeset
777 PyMODINIT_FUNC
13651
8a063423e0b7 Bunch of minor ANSIfications: 'void initfunc()' -> 'void initfunc(void)',
Thomas Wouters <thomas@python.org>
parents: 13424
diff changeset
778 initoperator(void)
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
779 {
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
780 PyObject *m;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
781
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
782 /* Create the module and add the functions */
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
783 m = Py_InitModule4("operator", operator_methods, operator_doc,
8414
4064b6729537 Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents: 8406
diff changeset
784 (PyObject*)NULL, PYTHON_API_VERSION);
36324
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36152
diff changeset
785 if (m == NULL)
1af7f719ed28 Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents: 36152
diff changeset
786 return;
31052
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
787
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
788 if (PyType_Ready(&itemgetter_type) < 0)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
789 return;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
790 Py_INCREF(&itemgetter_type);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
791 PyModule_AddObject(m, "itemgetter", (PyObject *)&itemgetter_type);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
792
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
793 if (PyType_Ready(&attrgetter_type) < 0)
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
794 return;
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
795 Py_INCREF(&attrgetter_type);
456e274f8602 As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents: 27290
diff changeset
796 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type);
45488
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
797
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
798 if (PyType_Ready(&methodcaller_type) < 0)
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
799 return;
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
800 Py_INCREF(&methodcaller_type);
c126bd9b505b #1506171: added operator.methodcaller().
Georg Brandl <georg@python.org>
parents: 45487
diff changeset
801 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type);
3844
0d54a14ef0b8 Operators like __add__
Guido van Rossum <guido@python.org>
parents:
diff changeset
802 }