Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 428e75f

Browse files
committed
Add wrappers around the rich-comparison operations.
This closes SF patch #428320. Add wrappers to expose "floor" and "true" division. This closes SF feature request #449093.
1 parent 3720261 commit 428e75f

1 file changed

Lines changed: 22 additions & 1 deletion

File tree

Modules/operator.c

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,13 +110,20 @@ used for special class methods; variants without leading and trailing\n\
110110
if(-1 == (r=AOP(a1,a2))) return NULL; \
111111
return PyInt_FromLong(r); }
112112

113+
#define spamrc(OP,A) static PyObject *OP(PyObject *s, PyObject *a) { \
114+
PyObject *a1, *a2; \
115+
if(! PyArg_ParseTuple(a,"OO:" #OP,&a1,&a2)) return NULL; \
116+
return PyObject_RichCompare(a1,a2,A); }
117+
113118
spami(isCallable , PyCallable_Check)
114119
spami(isNumberType , PyNumber_Check)
115120
spami(truth , PyObject_IsTrue)
116121
spam2(op_add , PyNumber_Add)
117122
spam2(op_sub , PyNumber_Subtract)
118123
spam2(op_mul , PyNumber_Multiply)
119124
spam2(op_div , PyNumber_Divide)
125+
spam2(op_floordiv , PyNumber_FloorDivide)
126+
spam2(op_truediv , PyNumber_TrueDivide)
120127
spam2(op_mod , PyNumber_Remainder)
121128
spam1(op_neg , PyNumber_Negative)
122129
spam1(op_pos , PyNumber_Positive)
@@ -140,6 +147,12 @@ spami(isMappingType , PyMapping_Check)
140147
spam2(op_getitem , PyObject_GetItem)
141148
spam2n(op_delitem , PyObject_DelItem)
142149
spam3n(op_setitem , PyObject_SetItem)
150+
spamrc(op_lt , Py_LT)
151+
spamrc(op_le , Py_LE)
152+
spamrc(op_eq , Py_EQ)
153+
spamrc(op_ne , Py_NE)
154+
spamrc(op_gt , Py_GT)
155+
spamrc(op_ge , Py_GE)
143156

144157
static PyObject*
145158
op_getslice(PyObject *s, PyObject *a)
@@ -214,7 +227,9 @@ spam1(isMappingType,
214227
spam2(add,__add__, "add(a, b) -- Same as a + b.")
215228
spam2(sub,__sub__, "sub(a, b) -- Same as a - b.")
216229
spam2(mul,__mul__, "mul(a, b) -- Same as a * b.")
217-
spam2(div,__div__, "div(a, b) -- Same as a / b.")
230+
spam2(div,__div__, "div(a, b) -- Same as a / b when __future__.division is not in effect.")
231+
spam2(floordiv,__floordiv__, "floordiv(a, b) -- Same as a // b.")
232+
spam2(truediv,__truediv__, "truediv(a, b) -- Same as a / b when __future__.division is in effect.")
218233
spam2(mod,__mod__, "mod(a, b) -- Same as a % b.")
219234
spam2(neg,__neg__, "neg(a) -- Same as -a.")
220235
spam2(pos,__pos__, "pos(a) -- Same as +a.")
@@ -243,6 +258,12 @@ spam2(setslice,__setslice__,
243258
"setslice(a, b, c, d) -- Same as a[b:c] = d.")
244259
spam2(delslice,__delslice__,
245260
"delslice(a, b, c) -- Same as del a[b:c].")
261+
spam2(lt,__lt__, "lt(a, b) -- Same as a<b.")
262+
spam2(le,__le__, "le(a, b) -- Same as a<=b.")
263+
spam2(eq,__eq__, "eq(a, b) -- Same as a==b.")
264+
spam2(ne,__ne__, "ne(a, b) -- Same as a!=b.")
265+
spam2(gt,__gt__, "gt(a, b) -- Same as a>b.")
266+
spam2(ge,__ge__, "ge(a, b) -- Same as a>=b.")
246267

247268
{NULL, NULL} /* sentinel */
248269

0 commit comments

Comments
 (0)