diff options
author | Guido van Rossum <guido@python.org> | 2001-01-17 15:20:39 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2001-01-17 15:20:39 (GMT) |
commit | 5f284ce68e95e679b007298320d24e3e3905c43c (patch) | |
tree | 216f12ad2f4bcb4fe04904a66c8bdba7ca9dd314 /Include/object.h | |
parent | c14fa303e109bd8dee7ef9c6a127fbd1fa1c5113 (diff) | |
download | cpython-5f284ce68e95e679b007298320d24e3e3905c43c.zip cpython-5f284ce68e95e679b007298320d24e3e3905c43c.tar.gz cpython-5f284ce68e95e679b007298320d24e3e3905c43c.tar.bz2 |
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.
Diffstat (limited to 'Include/object.h')
-rw-r--r-- | Include/object.h | 34 |
1 files changed, 22 insertions, 12 deletions
diff --git a/Include/object.h b/Include/object.h index e5ef710..0ebe805 100644 --- a/Include/object.h +++ b/Include/object.h @@ -119,10 +119,13 @@ typedef int (*visitproc)(PyObject *, void *); typedef int (*traverseproc)(PyObject *, visitproc, void *); typedef struct { - /* For old style numbers all arguments are guaranteed to be of the - object's type (modulo coercion hacks that is); new style numbers - should check both arguments for proper type and implement the - necessary conversions in the slots themselves. */ + /* For numbers without flag bit Py_TPFLAGS_CHECKTYPES set, all + arguments are guaranteed to be of the object's type (modulo + coercion hacks that is -- i.e. if the type's coercion function + returns other types, then these are allowed as well). Numbers that + have the Py_TPFLAGS_CHECKTYPES flag bit set should check *both* + arguments for proper type and implement the necessary conversions + in the slot functions themselves. */ binaryfunc nb_add; binaryfunc nb_subtract; @@ -158,12 +161,6 @@ typedef struct { binaryfunc nb_inplace_and; binaryfunc nb_inplace_xor; binaryfunc nb_inplace_or; - - /* New style number slots; these are only used the - Py_TPFLAGS_NEWSTYLENUMBER flag is set */ - - binaryfunc nb_cmp; /* XXX this should be richcmpfunc */ - } PyNumberMethods; typedef struct { @@ -202,6 +199,7 @@ typedef int (*setattrofunc)(PyObject *, PyObject *, PyObject *); typedef int (*cmpfunc)(PyObject *, PyObject *); typedef PyObject *(*reprfunc)(PyObject *); typedef long (*hashfunc)(PyObject *); +typedef PyObject *(*richcmpfunc) (PyObject *, PyObject *, int); typedef struct _typeobject { PyObject_VAR_HEAD @@ -245,8 +243,10 @@ typedef struct _typeobject { /* delete references to contained objects */ inquiry tp_clear; + /* rich comparisons */ + richcmpfunc tp_richcompare; + /* More spares */ - long tp_xxx7; long tp_xxx8; #ifdef COUNT_ALLOCS @@ -267,6 +267,8 @@ extern DL_IMPORT(int) PyObject_Print(PyObject *, FILE *, int); extern DL_IMPORT(PyObject *) PyObject_Repr(PyObject *); extern DL_IMPORT(PyObject *) PyObject_Str(PyObject *); extern DL_IMPORT(int) PyObject_Compare(PyObject *, PyObject *); +extern DL_IMPORT(PyObject *) PyObject_RichCompare(PyObject *, PyObject *, int); +extern DL_IMPORT(int) PyObject_RichCompareBool(PyObject *, PyObject *, int); extern DL_IMPORT(PyObject *) PyObject_GetAttrString(PyObject *, char *); extern DL_IMPORT(int) PyObject_SetAttrString(PyObject *, char *, PyObject *); extern DL_IMPORT(int) PyObject_HasAttrString(PyObject *, char *); @@ -334,7 +336,7 @@ given type object has a specified feature. #define Py_TPFLAGS_HAVE_INPLACEOPS (1L<<3) /* PyNumberMethods do their own coercion */ -#define Py_TPFLAGS_NEWSTYLENUMBER (1L<<4) +#define Py_TPFLAGS_CHECKTYPES (1L<<4) #define Py_TPFLAGS_DEFAULT (Py_TPFLAGS_HAVE_GETCHARBUFFER | \ Py_TPFLAGS_HAVE_SEQUENCE_IN | \ @@ -457,6 +459,14 @@ extern DL_IMPORT(PyObject) _Py_NotImplementedStruct; /* Don't use this directly #define Py_NotImplemented (&_Py_NotImplementedStruct) +/* Rich comparison opcodes */ +#define Py_LT 0 +#define Py_LE 1 +#define Py_EQ 2 +#define Py_NE 3 +#define Py_GT 4 +#define Py_GE 5 + /* A common programming style in Python requires the forward declaration of static, initialized structures, e.g. for a type object that is used |