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

Skip to content

Commit 5f284ce

Browse files
committed
Introduction to rich comparisons:
- Removed the nb_add slot from the PyNumberMethods struct. - Renamed Py_TPFLAGS_NEWSTYLENUMBER to Py_TPFLAGS_CHECKTYPES. - Added typedef richcmpfunc. - Added tp_richcompare slot to PyTypeObject (replacing spare tp_xxx7). - Added APIs PyObject_RichCompare() and PyObject_RichCompareBool(). - Added rich comparison operators Py_LT through Py_GE.
1 parent c14fa30 commit 5f284ce

1 file changed

Lines changed: 22 additions & 12 deletions

File tree

Include/object.h

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,13 @@ typedef int (*visitproc)(PyObject *, void *);
119119
typedef int (*traverseproc)(PyObject *, visitproc, void *);
120120

121121
typedef struct {
122-
/* For old style numbers all arguments are guaranteed to be of the
123-
object's type (modulo coercion hacks that is); new style numbers
124-
should check both arguments for proper type and implement the
125-
necessary conversions in the slots themselves. */
122+
/* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all
123+
arguments are guaranteed to be of the object's type (modulo
124+
coercion hacks that is -- i.e. if the type's coercion function
125+
returns other types, then these are allowed as well). Numbers that
126+
have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both*
127+
arguments for proper type and implement the necessary conversions
128+
in the slot functions themselves. */
126129

127130
binaryfunc nb_add;
128131
binaryfunc nb_subtract;
@@ -158,12 +161,6 @@ typedef struct {
158161
binaryfunc nb_inplace_and;
159162
binaryfunc nb_inplace_xor;
160163
binaryfunc nb_inplace_or;
161-
162-
/* New style number slots; these are only used the
163-
Py_TPFLAGS_NEWSTYLENUMBER flag is set */
164-
165-
binaryfunc nb_cmp; /* XXX this should be richcmpfunc */
166-
167164
} PyNumberMethods;
168165

169166
typedef struct {
@@ -202,6 +199,7 @@ typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *);
202199
typedef int (*cmpfunc)(PyObject *, PyObject *);
203200
typedef PyObject *(*reprfunc)(PyObject *);
204201
typedef long (*hashfunc)(PyObject *);
202+
typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int);
205203

206204
typedef struct _typeobject {
207205
PyObject_VAR_HEAD
@@ -245,8 +243,10 @@ typedef struct _typeobject {
245243
/* delete references to contained objects */
246244
inquiry tp_clear;
247245

246+
/* rich comparisons */
247+
richcmpfunc tp_richcompare;
248+
248249
/* More spares */
249-
long tp_xxx7;
250250
long tp_xxx8;
251251

252252
#ifdef COUNT_ALLOCS
@@ -267,6 +267,8 @@ extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int);
267267
extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *);
268268
extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *);
269269
extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *);
270+
extern DL_IMPORT(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int);
271+
extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject *, PyObject *, int);
270272
extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *);
271273
extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *);
272274
extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *);
@@ -334,7 +336,7 @@ given type object has a specified feature.
334336
#define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3)
335337

336338
/* PyNumberMethods do their own coercion */
337-
#define Py_TPFLAGS_NEWSTYLENUMBER (1L<<4)
339+
#define Py_TPFLAGS_CHECKTYPES (1L<<4)
338340

339341
#define Py_TPFLAGS_DEFAULT (Py_TPFLAGS_HAVE_GETCHARBUFFER | \
340342
Py_TPFLAGS_HAVE_SEQUENCE_IN | \
@@ -457,6 +459,14 @@ extern DL_IMPORT(PyObject) _Py_NotImplementedStruct; /* Don't use this directly
457459

458460
#define Py_NotImplemented (&_Py_NotImplementedStruct)
459461

462+
/* Rich comparison opcodes */
463+
#define Py_LT 0
464+
#define Py_LE 1
465+
#define Py_EQ 2
466+
#define Py_NE 3
467+
#define Py_GT 4
468+
#define Py_GE 5
469+
460470
/*
461471
A common programming style in Python requires the forward declaration
462472
of static, initialized structures, e.g. for a type object that is used

0 commit comments

Comments
 (0)