Mercurial > cpython-lru_cache-weakref
annotate Modules/operator.c @ 51271:87fbbf22adf3 3.0
Merged revisions 69214 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/branches/py3k
........
r69214 | mark.dickinson | 2009-02-02 20:36:42 +0000 (Mon, 02 Feb 2009) | 4 lines
Issue #1717: rename tp_compare to tp_reserved. I'll change the
type of tp_compare in a separate commit, for ease of reversion
should things go wrong.
........
| author | Mark Dickinson <dickinsm@gmail.com> |
|---|---|
| date | Mon, 02 Feb 2009 20:41:29 +0000 |
| parents | 1c50a17c0f21 |
| children | a0951f11be3d |
| 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 | 6 \n\ |
| 7 This module exports a set of functions implemented in C corresponding\n\ | |
| 8 to the intrinsic operators of Python. For example, operator.add(x, y)\n\ | |
| 9 is equivalent to the expression x+y. The function names are those\n\ | |
| 10 used for special class 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 | 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 | 14 return AOP(a1); } |
| 15 | |
| 13422 | 16 #define spam2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
| 3844 | 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 | 19 return AOP(a1,a2); } |
| 20 | |
| 13422 | 21 #define spamoi(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
| 3844 | 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 | 24 return AOP(a1,a2); } |
| 25 | |
| 13422 | 26 #define spam2n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
| 3844 | 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 | 29 if(-1 == AOP(a1,a2)) return NULL; \ |
| 30 Py_INCREF(Py_None); \ | |
| 31 return Py_None; } | |
| 32 | |
| 13422 | 33 #define spam3n(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
| 3844 | 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 | 36 if(-1 == AOP(a1,a2,a3)) return NULL; \ |
| 37 Py_INCREF(Py_None); \ | |
| 38 return Py_None; } | |
| 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 | 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 | 44 |
| 13422 | 45 #define spami2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
| 3844 | 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 | 48 if(-1 == (r=AOP(a1,a2))) return NULL; \ |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
42334
diff
changeset
|
49 return PyLong_FromLong(r); } |
| 3844 | 50 |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
51 #define spamn2(OP,AOP) static PyObject *OP(PyObject *s, PyObject *a) { \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
52 PyObject *a1, *a2; Py_ssize_t r; \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
53 if(! PyArg_UnpackTuple(a,#OP,2,2,&a1,&a2)) return NULL; \ |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
54 if(-1 == (r=AOP(a1,a2))) return NULL; \ |
|
44099
f324631462a2
Cleanup: Replaced most PyInt_ aliases with PyLong_ and disabled the aliases in intobject.h
Christian Heimes <christian@cheimes.de>
parents:
42334
diff
changeset
|
55 return PyLong_FromSsize_t(r); } |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
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 |
| 3844 | 68 spami(truth , PyObject_IsTrue) |
|
4089
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
69 spam2(op_add , PyNumber_Add) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
70 spam2(op_sub , PyNumber_Subtract) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
71 spam2(op_mul , PyNumber_Multiply) |
|
19111
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
72 spam2(op_floordiv , PyNumber_FloorDivide) |
|
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
73 spam2(op_truediv , PyNumber_TrueDivide) |
|
4089
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
74 spam2(op_mod , PyNumber_Remainder) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
75 spam1(op_neg , PyNumber_Negative) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
76 spam1(op_pos , PyNumber_Positive) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
77 spam1(op_abs , PyNumber_Absolute) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
78 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
|
79 spam1(op_invert , PyNumber_Invert) |
|
4089
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
80 spam2(op_lshift , PyNumber_Lshift) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
81 spam2(op_rshift , PyNumber_Rshift) |
| 7936 | 82 spami(op_not_ , PyObject_Not) |
|
4089
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
83 spam2(op_and_ , PyNumber_And) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
84 spam2(op_xor , PyNumber_Xor) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
85 spam2(op_or_ , PyNumber_Or) |
|
36152
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
86 spam2(op_iadd , PyNumber_InPlaceAdd) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
87 spam2(op_isub , PyNumber_InPlaceSubtract) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
88 spam2(op_imul , PyNumber_InPlaceMultiply) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
89 spam2(op_ifloordiv , PyNumber_InPlaceFloorDivide) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
90 spam2(op_itruediv , PyNumber_InPlaceTrueDivide) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
91 spam2(op_imod , PyNumber_InPlaceRemainder) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
92 spam2(op_ilshift , PyNumber_InPlaceLshift) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
93 spam2(op_irshift , PyNumber_InPlaceRshift) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
94 spam2(op_iand , PyNumber_InPlaceAnd) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
95 spam2(op_ixor , PyNumber_InPlaceXor) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
96 spam2(op_ior , PyNumber_InPlaceOr) |
|
4089
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
97 spam2(op_concat , PySequence_Concat) |
|
36152
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
98 spam2(op_iconcat , PySequence_InPlaceConcat) |
|
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
|
99 spami2b(op_contains , PySequence_Contains) |
|
38453
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
100 spamn2(indexOf , PySequence_Index) |
|
29ae5e0fc348
Much-needed merge (using svnmerge.py this time) of trunk changes into p3yk.
Thomas Wouters <thomas@python.org>
parents:
37879
diff
changeset
|
101 spamn2(countOf , PySequence_Count) |
|
4089
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
102 spam2(op_getitem , PyObject_GetItem) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
103 spam2n(op_delitem , PyObject_DelItem) |
|
9f70d995d027
Added delslice and delitem.
Guido van Rossum <guido@python.org>
parents:
4033
diff
changeset
|
104 spam3n(op_setitem , PyObject_SetItem) |
|
19111
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
105 spamrc(op_lt , Py_LT) |
|
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
106 spamrc(op_le , Py_LE) |
|
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
107 spamrc(op_eq , Py_EQ) |
|
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
108 spamrc(op_ne , Py_NE) |
|
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
109 spamrc(op_gt , Py_GT) |
|
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
110 spamrc(op_ge , Py_GE) |
| 3844 | 111 |
| 112 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
|
113 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
|
114 { |
|
d797cfa8ca0d
Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents:
24796
diff
changeset
|
115 PyObject *a1, *a2; |
|
26818
e094a03a80ff
SF patch #659536: Use PyArg_UnpackTuple where possible.
Raymond Hettinger <python@rcn.com>
parents:
24887
diff
changeset
|
116 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
|
117 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
|
118 return NULL; |
|
d797cfa8ca0d
Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents:
24796
diff
changeset
|
119 } |
|
d797cfa8ca0d
Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents:
24796
diff
changeset
|
120 |
|
d797cfa8ca0d
Added __pow__(a,b) to the operator module. Completes the pattern of
Raymond Hettinger <python@rcn.com>
parents:
24796
diff
changeset
|
121 static PyObject* |
|
36152
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
122 op_ipow(PyObject *s, PyObject *a) |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
123 { |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
124 PyObject *a1, *a2; |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
125 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
|
126 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
|
127 return NULL; |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
128 } |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
129 |
|
36882
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36518
diff
changeset
|
130 static PyObject * |
|
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36518
diff
changeset
|
131 op_index(PyObject *s, PyObject *a) |
|
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36518
diff
changeset
|
132 { |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
133 return PyNumber_Index(a); |
|
36882
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36518
diff
changeset
|
134 } |
|
6a9c7165b17b
Checking in the code for PEP 357.
Guido van Rossum <guido@python.org>
parents:
36518
diff
changeset
|
135 |
|
36152
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
136 static PyObject* |
|
27290
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
137 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
|
138 { |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
139 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
|
140 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
|
141 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
|
142 Py_INCREF(result); |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
143 } |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
144 return result; |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
145 } |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
146 |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
147 static PyObject* |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
148 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
|
149 { |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
150 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
|
151 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
|
152 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
|
153 Py_INCREF(result); |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
154 } |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
155 return result; |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
156 } |
|
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
157 |
| 3844 | 158 #undef spam1 |
| 159 #undef spam2 | |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
160 #undef spam1o |
|
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
161 #undef spam1o |
|
24796
de71e6fe64b0
Allow more docstrings to be removed during compilation in some modules
Neal Norwitz <nnorwitz@gmail.com>
parents:
24471
diff
changeset
|
162 #define spam1(OP,DOC) {#OP, OP, METH_VARARGS, PyDoc_STR(DOC)}, |
| 36149 | 163 #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
|
164 {#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
|
165 #define spam1o(OP,DOC) {#OP, OP, METH_O, PyDoc_STR(DOC)}, |
| 36149 | 166 #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
|
167 {#ALTOP, op_##OP, METH_O, PyDoc_STR(DOC)}, |
| 3844 | 168 |
| 169 static struct PyMethodDef operator_methods[] = { | |
| 170 | |
|
27044
23280a64288e
SF patch 660559: Use METH_O and METH_NOARGS where possible
Raymond Hettinger <python@rcn.com>
parents:
26818
diff
changeset
|
171 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
|
172 "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
|
173 spam2(contains,__contains__, |
|
8414
4064b6729537
Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents:
8406
diff
changeset
|
174 "contains(a, b) -- Same as b in a (note reversed operands).") |
|
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
|
175 spam1(indexOf, |
|
8414
4064b6729537
Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents:
8406
diff
changeset
|
176 "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
|
177 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
|
178 "countOf(a, b) -- Return the number of times b occurs in a.") |
| 3844 | 179 |
|
27290
e83a42a90df1
SF patch #670423: Add missing identity tests to operator.c
Raymond Hettinger <python@rcn.com>
parents:
27044
diff
changeset
|
180 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
|
181 spam1(is_not, "is_not(a, b) -- Same as a is not b.") |
|
39610
3674f20ac781
Merge current trunk into p3yk. This includes the PyNumber_Index API change,
Thomas Wouters <thomas@python.org>
parents:
38453
diff
changeset
|
182 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
|
183 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
|
184 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
|
185 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
|
186 spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.") |
|
37191
4bfe8afe8f65
Get rid of remnants of integer division
Neal Norwitz <nnorwitz@gmail.com>
parents:
36882
diff
changeset
|
187 spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b.") |
|
8414
4064b6729537
Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents:
8406
diff
changeset
|
188 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
|
189 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
|
190 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
|
191 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
|
192 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
|
193 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
|
194 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
|
195 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
|
196 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
|
197 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
|
198 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
|
199 spam2(or_,__or__, "or_(a, b) -- Same as a | b.") |
|
36152
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
200 spam2(iadd,__iadd__, "iadd(a, b) -- Same as a += b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
201 spam2(isub,__isub__, "isub(a, b) -- Same as a -= b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
202 spam2(imul,__imul__, "imul(a, b) -- Same as a *= b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
203 spam2(ifloordiv,__ifloordiv__, "ifloordiv(a, b) -- Same as a //= b.") |
|
37191
4bfe8afe8f65
Get rid of remnants of integer division
Neal Norwitz <nnorwitz@gmail.com>
parents:
36882
diff
changeset
|
204 spam2(itruediv,__itruediv__, "itruediv(a, b) -- Same as a /= b.") |
|
36152
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
205 spam2(imod,__imod__, "imod(a, b) -- Same as a %= b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
206 spam2(ilshift,__ilshift__, "ilshift(a, b) -- Same as a <<= b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
207 spam2(irshift,__irshift__, "irshift(a, b) -- Same as a >>= b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
208 spam2(iand,__iand__, "iand(a, b) -- Same as a &= b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
209 spam2(ixor,__ixor__, "ixor(a, b) -- Same as a ^= b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
210 spam2(ior,__ior__, "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
|
211 spam2(concat,__concat__, |
|
8414
4064b6729537
Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents:
8406
diff
changeset
|
212 "concat(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
|
213 spam2(iconcat,__iconcat__, |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
214 "iconcat(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
|
215 spam2(getitem,__getitem__, |
|
8414
4064b6729537
Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents:
8406
diff
changeset
|
216 "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
|
217 spam2(setitem,__setitem__, |
|
8414
4064b6729537
Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents:
8406
diff
changeset
|
218 "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
|
219 spam2(delitem,__delitem__, |
|
8414
4064b6729537
Add an alias (and preferred name) "contains" for "sequenceIncludes".
Guido van Rossum <guido@python.org>
parents:
8406
diff
changeset
|
220 "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
|
221 spam2(pow,__pow__, "pow(a, b) -- Same as a ** b.") |
|
f9281283b109
adding in-place operators to the operator module.
Armin Rigo <arigo@tunes.org>
parents:
36149
diff
changeset
|
222 spam2(ipow,__ipow__, "ipow(a, b) -- Same as a **= b.") |
|
19111
6ba441507220
Add wrappers around the rich-comparison operations.
Fred Drake <fdrake@acm.org>
parents:
14654
diff
changeset
|
223 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
|
224 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
|
225 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
|
226 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
|
227 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
|
228 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
|
229 |
| 3844 | 230 {NULL, NULL} /* sentinel */ |
| 231 | |
| 232 }; | |
| 233 | |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
234 /* itemgetter object **********************************************************/ |
| 3844 | 235 |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
236 typedef struct { |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
237 PyObject_HEAD |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
238 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
|
239 PyObject *item; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
240 } itemgetterobject; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
241 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
242 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
|
243 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
244 static PyObject * |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
245 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
|
246 { |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
247 itemgetterobject *ig; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
248 PyObject *item; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
249 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
|
250 |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34716
diff
changeset
|
251 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
|
252 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34716
diff
changeset
|
253 |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
254 nitems = PyTuple_GET_SIZE(args); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
255 if (nitems <= 1) { |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
256 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
|
257 return NULL; |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
258 } else |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
259 item = args; |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
260 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
261 /* create itemgetterobject structure */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
262 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
|
263 if (ig == NULL) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
264 return NULL; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
265 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
266 Py_INCREF(item); |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
267 ig->item = item; |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
268 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
|
269 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
270 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
|
271 return (PyObject *)ig; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
272 } |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
273 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
274 static void |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
275 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
|
276 { |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
277 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
|
278 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
|
279 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
|
280 } |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
281 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
282 static int |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
283 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
|
284 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
285 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
|
286 return 0; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
287 } |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
288 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
289 static PyObject * |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
290 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
|
291 { |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
292 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
|
293 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
|
294 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
295 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
|
296 return NULL; |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
297 if (nitems == 1) |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
298 return PyObject_GetItem(obj, ig->item); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
299 |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
300 assert(PyTuple_Check(ig->item)); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
301 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
|
302 |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
303 result = PyTuple_New(nitems); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
304 if (result == NULL) |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
305 return NULL; |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
306 |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
307 for (i=0 ; i < nitems ; i++) { |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
308 PyObject *item, *val; |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
309 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
|
310 val = PyObject_GetItem(obj, item); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
311 if (val == NULL) { |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
312 Py_DECREF(result); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
313 return NULL; |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
314 } |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
315 PyTuple_SET_ITEM(result, i, val); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
316 } |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
317 return result; |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
318 } |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
319 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
320 PyDoc_STRVAR(itemgetter_doc, |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
321 "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
|
322 \n\ |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
323 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
|
324 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
|
325 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
|
326 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
327 static PyTypeObject itemgetter_type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
41554
diff
changeset
|
328 PyVarObject_HEAD_INIT(NULL, 0) |
| 31110 | 329 "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
|
330 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
|
331 0, /* tp_itemsize */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
332 /* methods */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
333 (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
|
334 0, /* tp_print */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
335 0, /* tp_getattr */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
336 0, /* tp_setattr */ |
|
51271
87fbbf22adf3
Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
51064
diff
changeset
|
337 0, /* tp_reserved */ |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
338 0, /* tp_repr */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
339 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
|
340 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
|
341 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
|
342 0, /* tp_hash */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
343 (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
|
344 0, /* tp_str */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
345 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
|
346 0, /* tp_setattro */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
347 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
|
348 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
|
349 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
|
350 (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
|
351 0, /* tp_clear */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
352 0, /* tp_richcompare */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
353 0, /* tp_weaklistoffset */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
354 0, /* tp_iter */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
355 0, /* tp_iternext */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
356 0, /* tp_methods */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
357 0, /* tp_members */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
358 0, /* tp_getset */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
359 0, /* tp_base */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
360 0, /* tp_dict */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
361 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
|
362 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
|
363 0, /* tp_dictoffset */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
364 0, /* tp_init */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
365 0, /* tp_alloc */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
366 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
|
367 0, /* tp_free */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
368 }; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
369 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
370 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
371 /* attrgetter object **********************************************************/ |
|
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 typedef struct { |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
374 PyObject_HEAD |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
375 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
|
376 PyObject *attr; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
377 } attrgetterobject; |
|
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 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
|
380 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
381 static PyObject * |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
382 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
|
383 { |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
384 attrgetterobject *ag; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
385 PyObject *attr; |
|
36518
9f3f87e920a6
Use Py_ssize_t for counts and sizes.
Martin v. Löwis <martin@v.loewis.de>
parents:
36324
diff
changeset
|
386 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
|
387 |
|
35449
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34716
diff
changeset
|
388 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
|
389 return NULL; |
|
1fdfd0130e28
Disallow keyword arguments for type constructors that don't use them.
Georg Brandl <georg@python.org>
parents:
34716
diff
changeset
|
390 |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
391 nattrs = PyTuple_GET_SIZE(args); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
392 if (nattrs <= 1) { |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
393 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
|
394 return NULL; |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
395 } else |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
396 attr = args; |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
397 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
398 /* create attrgetterobject structure */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
399 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
|
400 if (ag == NULL) |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
401 return NULL; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
402 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
403 Py_INCREF(attr); |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
404 ag->attr = attr; |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
405 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
|
406 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
407 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
|
408 return (PyObject *)ag; |
|
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 static void |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
412 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
|
413 { |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
414 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
|
415 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
|
416 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
|
417 } |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
418 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
419 static int |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
420 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
|
421 { |
|
37879
f731859e9b3b
Merge p3yk branch with the trunk up to revision 45595. This breaks a fair
Thomas Wouters <thomas@python.org>
parents:
37191
diff
changeset
|
422 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
|
423 return 0; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
424 } |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
425 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
426 static PyObject * |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
427 dotted_getattr(PyObject *obj, PyObject *attr) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
428 { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
429 char *s, *p; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
430 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
431 if (!PyUnicode_Check(attr)) { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
432 PyErr_SetString(PyExc_TypeError, |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
433 "attribute name must be a string"); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
434 return NULL; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
435 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
436 |
|
48660
85b67fb5672b
Rename PyUnicode_AsString -> _PyUnicode_AsString and
Marc-André Lemburg <mal@egenix.com>
parents:
47580
diff
changeset
|
437 s = _PyUnicode_AsString(attr); |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
438 Py_INCREF(obj); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
439 for (;;) { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
440 PyObject *newobj, *str; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
441 p = strchr(s, '.'); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
442 str = p ? PyUnicode_FromStringAndSize(s, (p-s)) : |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
443 PyUnicode_FromString(s); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
444 if (str == NULL) { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
445 Py_DECREF(obj); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
446 return NULL; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
447 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
448 newobj = PyObject_GetAttr(obj, str); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
449 Py_DECREF(str); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
450 Py_DECREF(obj); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
451 if (newobj == NULL) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
452 return NULL; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
453 obj = newobj; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
454 if (p == NULL) break; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
455 s = p+1; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
456 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
457 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
458 return obj; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
459 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
460 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
461 static PyObject * |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
462 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
|
463 { |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
464 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
|
465 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
|
466 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
467 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
|
468 return NULL; |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
469 if (ag->nattrs == 1) |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
470 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
|
471 |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
472 assert(PyTuple_Check(ag->attr)); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
473 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
|
474 |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
475 result = PyTuple_New(nattrs); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
476 if (result == NULL) |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
477 return NULL; |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
478 |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
479 for (i=0 ; i < nattrs ; i++) { |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
480 PyObject *attr, *val; |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
481 attr = PyTuple_GET_ITEM(ag->attr, i); |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
482 val = dotted_getattr(obj, attr); |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
483 if (val == NULL) { |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
484 Py_DECREF(result); |
|
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 } |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
487 PyTuple_SET_ITEM(result, i, val); |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
488 } |
|
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
489 return result; |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
490 } |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
491 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
492 PyDoc_STRVAR(attrgetter_doc, |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
493 "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
|
494 \n\ |
|
34716
ddc2276c247e
operator.itemgetter() and operator.attrgetter() now support extraction
Raymond Hettinger <python@rcn.com>
parents:
31110
diff
changeset
|
495 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
|
496 After, f=attrgetter('name'), the call f(r) returns r.name.\n\ |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
497 After, g=attrgetter('name', 'date'), the call g(r) returns (r.name, r.date).\n\ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
498 After, h=attrgetter('name.first', 'name.last'), the call h(r) returns\n\ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
499 (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
|
500 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
501 static PyTypeObject attrgetter_type = { |
|
42334
e70cdce0e8b2
Merged revisions 56467-56482 via svnmerge from
Martin v. Löwis <martin@v.loewis.de>
parents:
41554
diff
changeset
|
502 PyVarObject_HEAD_INIT(NULL, 0) |
| 31110 | 503 "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
|
504 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
|
505 0, /* tp_itemsize */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
506 /* methods */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
507 (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
|
508 0, /* tp_print */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
509 0, /* tp_getattr */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
510 0, /* tp_setattr */ |
|
51271
87fbbf22adf3
Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
51064
diff
changeset
|
511 0, /* tp_reserved */ |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
512 0, /* tp_repr */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
513 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
|
514 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
|
515 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
|
516 0, /* tp_hash */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
517 (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
|
518 0, /* tp_str */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
519 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
|
520 0, /* tp_setattro */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
521 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
|
522 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
|
523 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
|
524 (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
|
525 0, /* tp_clear */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
526 0, /* tp_richcompare */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
527 0, /* tp_weaklistoffset */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
528 0, /* tp_iter */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
529 0, /* tp_iternext */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
530 0, /* tp_methods */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
531 0, /* tp_members */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
532 0, /* tp_getset */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
533 0, /* tp_base */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
534 0, /* tp_dict */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
535 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
|
536 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
|
537 0, /* tp_dictoffset */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
538 0, /* tp_init */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
539 0, /* tp_alloc */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
540 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
|
541 0, /* tp_free */ |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
542 }; |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
543 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
544 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
545 /* methodcaller object **********************************************************/ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
546 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
547 typedef struct { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
548 PyObject_HEAD |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
549 PyObject *name; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
550 PyObject *args; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
551 PyObject *kwds; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
552 } methodcallerobject; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
553 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
554 static PyTypeObject methodcaller_type; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
555 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
556 static PyObject * |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
557 methodcaller_new(PyTypeObject *type, PyObject *args, PyObject *kwds) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
558 { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
559 methodcallerobject *mc; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
560 PyObject *name, *newargs; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
561 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
562 if (PyTuple_GET_SIZE(args) < 1) { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
563 PyErr_SetString(PyExc_TypeError, "methodcaller needs at least " |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
564 "one argument, the method name"); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
565 return NULL; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
566 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
567 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
568 /* create methodcallerobject structure */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
569 mc = PyObject_GC_New(methodcallerobject, &methodcaller_type); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
570 if (mc == NULL) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
571 return NULL; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
572 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
573 newargs = PyTuple_GetSlice(args, 1, PyTuple_GET_SIZE(args)); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
574 if (newargs == NULL) { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
575 Py_DECREF(mc); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
576 return NULL; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
577 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
578 mc->args = newargs; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
579 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
580 name = PyTuple_GET_ITEM(args, 0); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
581 Py_INCREF(name); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
582 mc->name = name; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
583 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
584 Py_XINCREF(kwds); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
585 mc->kwds = kwds; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
586 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
587 PyObject_GC_Track(mc); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
588 return (PyObject *)mc; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
589 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
590 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
591 static void |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
592 methodcaller_dealloc(methodcallerobject *mc) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
593 { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
594 PyObject_GC_UnTrack(mc); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
595 Py_XDECREF(mc->name); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
596 Py_XDECREF(mc->args); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
597 Py_XDECREF(mc->kwds); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
598 PyObject_GC_Del(mc); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
599 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
600 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
601 static int |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
602 methodcaller_traverse(methodcallerobject *mc, visitproc visit, void *arg) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
603 { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
604 Py_VISIT(mc->args); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
605 Py_VISIT(mc->kwds); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
606 return 0; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
607 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
608 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
609 static PyObject * |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
610 methodcaller_call(methodcallerobject *mc, PyObject *args, PyObject *kw) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
611 { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
612 PyObject *method, *obj, *result; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
613 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
614 if (!PyArg_UnpackTuple(args, "methodcaller", 1, 1, &obj)) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
615 return NULL; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
616 method = PyObject_GetAttr(obj, mc->name); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
617 if (method == NULL) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
618 return NULL; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
619 result = PyObject_Call(method, mc->args, mc->kwds); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
620 Py_DECREF(method); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
621 return result; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
622 } |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
623 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
624 PyDoc_STRVAR(methodcaller_doc, |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
625 "methodcaller(name, ...) --> methodcaller object\n\ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
626 \n\ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
627 Return a callable object that calls the given method on its operand.\n\ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
628 After, f = methodcaller('name'), the call f(r) returns r.name().\n\ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
629 After, g = methodcaller('name', 'date', foo=1), the call g(r) returns\n\ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
630 r.name('date', foo=1)."); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
631 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
632 static PyTypeObject methodcaller_type = { |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
633 PyVarObject_HEAD_INIT(NULL, 0) |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
634 "operator.methodcaller", /* tp_name */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
635 sizeof(methodcallerobject), /* tp_basicsize */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
636 0, /* tp_itemsize */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
637 /* methods */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
638 (destructor)methodcaller_dealloc, /* tp_dealloc */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
639 0, /* tp_print */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
640 0, /* tp_getattr */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
641 0, /* tp_setattr */ |
|
51271
87fbbf22adf3
Merged revisions 69214 via svnmerge from
Mark Dickinson <dickinsm@gmail.com>
parents:
51064
diff
changeset
|
642 0, /* tp_reserved */ |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
643 0, /* tp_repr */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
644 0, /* tp_as_number */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
645 0, /* tp_as_sequence */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
646 0, /* tp_as_mapping */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
647 0, /* tp_hash */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
648 (ternaryfunc)methodcaller_call, /* tp_call */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
649 0, /* tp_str */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
650 PyObject_GenericGetAttr, /* tp_getattro */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
651 0, /* tp_setattro */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
652 0, /* tp_as_buffer */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
653 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC,/* tp_flags */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
654 methodcaller_doc, /* tp_doc */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
655 (traverseproc)methodcaller_traverse, /* tp_traverse */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
656 0, /* tp_clear */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
657 0, /* tp_richcompare */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
658 0, /* tp_weaklistoffset */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
659 0, /* tp_iter */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
660 0, /* tp_iternext */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
661 0, /* tp_methods */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
662 0, /* tp_members */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
663 0, /* tp_getset */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
664 0, /* tp_base */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
665 0, /* tp_dict */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
666 0, /* tp_descr_get */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
667 0, /* tp_descr_set */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
668 0, /* tp_dictoffset */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
669 0, /* tp_init */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
670 0, /* tp_alloc */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
671 methodcaller_new, /* tp_new */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
672 0, /* tp_free */ |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
673 }; |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
674 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
675 |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
676 /* Initialization function for the module (*must* be called PyInit_operator) */ |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
677 |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
678 |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
679 static struct PyModuleDef operatormodule = { |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
680 PyModuleDef_HEAD_INIT, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
681 "operator", |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
682 operator_doc, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
683 -1, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
684 operator_methods, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
685 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
686 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
687 NULL, |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
688 NULL |
|
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
689 }; |
| 3844 | 690 |
|
24471
f66b3a3509c7
Excise DL_EXPORT/DL_IMPORT from Modules/*. Required adding a prototype
Mark Hammond <mhammond@skippinet.com.au>
parents:
23832
diff
changeset
|
691 PyMODINIT_FUNC |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
692 PyInit_operator(void) |
| 3844 | 693 { |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
694 PyObject *m; |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
695 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
696 /* Create the module and add the functions */ |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
697 m = PyModule_Create(&operatormodule); |
|
36324
1af7f719ed28
Check return result from Py_InitModule*(). This API can fail.
Neal Norwitz <nnorwitz@gmail.com>
parents:
36152
diff
changeset
|
698 if (m == NULL) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
699 return NULL; |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
700 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
701 if (PyType_Ready(&itemgetter_type) < 0) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
702 return NULL; |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
703 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
|
704 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
|
705 |
|
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
706 if (PyType_Ready(&attrgetter_type) < 0) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
707 return NULL; |
|
31052
456e274f8602
As discussed on python-dev, added two extractor functions to the
Raymond Hettinger <python@rcn.com>
parents:
27290
diff
changeset
|
708 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
|
709 PyModule_AddObject(m, "attrgetter", (PyObject *)&attrgetter_type); |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
710 |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
711 if (PyType_Ready(&methodcaller_type) < 0) |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
712 return NULL; |
|
45495
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
713 Py_INCREF(&methodcaller_type); |
|
48488fa50729
Merged revisions 61003-61033 via svnmerge from
Christian Heimes <christian@cheimes.de>
parents:
44099
diff
changeset
|
714 PyModule_AddObject(m, "methodcaller", (PyObject *)&methodcaller_type); |
|
47580
94f64f11fdd0
Implement PEP 3121: new module initialization and finalization API.
Martin v. Löwis <martin@v.loewis.de>
parents:
45795
diff
changeset
|
715 return m; |
| 3844 | 716 } |
