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

Skip to content

Commit 890a49a

Browse files
committed
#1717: fix-up docs for comparison in newtypes document.
1 parent 32d68c2 commit 890a49a

1 file changed

Lines changed: 47 additions & 44 deletions

File tree

Doc/extending/newtypes.rst

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,50 +1231,53 @@ example that simply raises an exception; if this were really all you wanted, the
12311231
return -1;
12321232
}
12331233

1234-
.. XXX tp_compare is dead; need to rewrite for tp_richcompare!
1235-
1236-
Object Comparison
1237-
-----------------
1238-
1239-
::
1240-
1241-
cmpfunc tp_compare;
1242-
1243-
The :attr:`tp_compare` handler is called when comparisons are needed and the
1244-
object does not implement the specific rich comparison method which matches the
1245-
requested comparison. (It is always used if defined and the
1246-
:cfunc:`PyObject_Compare` or :cfunc:`PyObject_Cmp` functions are used, or if
1247-
:func:`cmp` is used from Python.) It is analogous to the :meth:`__cmp__` method.
1248-
This function should return ``-1`` if *obj1* is less than *obj2*, ``0`` if they
1249-
are equal, and ``1`` if *obj1* is greater than *obj2*. (It was previously
1250-
allowed to return arbitrary negative or positive integers for less than and
1251-
greater than, respectively; as of Python 2.2, this is no longer allowed. In the
1252-
future, other return values may be assigned a different meaning.)
1253-
1254-
A :attr:`tp_compare` handler may raise an exception. In this case it should
1255-
return a negative value. The caller has to test for the exception using
1256-
:cfunc:`PyErr_Occurred`.
1257-
1258-
Here is a sample implementation::
1259-
1260-
static int
1261-
newdatatype_compare(newdatatypeobject * obj1, newdatatypeobject * obj2)
1262-
{
1263-
long result;
1264-
1265-
if (obj1->obj_UnderlyingDatatypePtr->size <
1266-
obj2->obj_UnderlyingDatatypePtr->size) {
1267-
result = -1;
1268-
}
1269-
else if (obj1->obj_UnderlyingDatatypePtr->size >
1270-
obj2->obj_UnderlyingDatatypePtr->size) {
1271-
result = 1;
1272-
}
1273-
else {
1274-
result = 0;
1275-
}
1276-
return result;
1277-
}
1234+
Object Comparison
1235+
-----------------
1236+
1237+
::
1238+
1239+
richcmpfunc tp_richcompare;
1240+
1241+
The :attr:`tp_richcompare` handler is called when comparisons are needed. It is
1242+
analogous to the :ref:`rich comparison methods <richcmpfuncs>`, like
1243+
:meth:`__lt__`, and also called by :cfunc:`PyObject_RichCompare` and
1244+
:cfunc:`PyObject_RichCompareBool`.
1245+
1246+
This function is called with two Python objects and the operator as arguments,
1247+
where the operator is one of ``Py_EQ``, ``Py_NE``, ``Py_LE``, ``Py_GT``,
1248+
``Py_LT`` or ``Py_GT``. It should compare the two objects with respect to the
1249+
specified operator and return ``Py_True`` or ``Py_False`` if the comparison is
1250+
successfull, ``Py_NotImplemented`` to indicate that comparison is not
1251+
implemented and the other object's comparison method should be tried, or *NULL*
1252+
if an exception was set.
1253+
1254+
Here is a sample implementation, for a datatype that is considered equal if the
1255+
size of an internal pointer is equal::
1256+
1257+
static int
1258+
newdatatype_richcmp(PyObject *obj1, PyObject *obj2, int op)
1259+
{
1260+
PyObject *result;
1261+
int c, size1, size2;
1262+
1263+
/* code to make sure that both arguments are of type
1264+
newdatatype omitted */
1265+
1266+
size1 = obj1->obj_UnderlyingDatatypePtr->size;
1267+
size2 = obj2->obj_UnderlyingDatatypePtr->size;
1268+
1269+
switch (op) {
1270+
case Py_LT: c = size1 < size2; break;
1271+
case Py_LE: c = size1 <= size2; break;
1272+
case Py_EQ: c = size1 == size2; break;
1273+
case Py_NE: c = size1 != size2; break;
1274+
case Py_GT: c = size1 > size2; break;
1275+
case Py_GE: c = size1 >= size2; break;
1276+
}
1277+
result = c ? Py_True : Py_False;
1278+
Py_INCREF(result);
1279+
return result;
1280+
}
12781281

12791282

12801283
Abstract Protocol Support

0 commit comments

Comments
 (0)