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

Skip to content

Commit d1f06b9

Browse files
committed
Check the Py_TPFLAGS_HAVE_RICHCOMPARE flag before using the
tp_richcompare field! (Hopefully this will make Python 2.1 binary compatible with certain Zope extensions. :-)
1 parent bacca54 commit d1f06b9

1 file changed

Lines changed: 9 additions & 7 deletions

File tree

Objects/object.c

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,10 @@ PyObject_Unicode(PyObject *v)
371371
}
372372

373373

374+
/* Macro to get the tp_richcompare field of a type if defined */
375+
#define RICHCOMPARE(t) (PyType_HasFeature((t), Py_TPFLAGS_HAVE_RICHCOMPARE) \
376+
? (t)->tp_richcompare : NULL)
377+
374378
/* Map rich comparison operators to their swapped version, e.g. LT --> GT */
375379
static int swapped_op[] = {Py_GT, Py_GE, Py_EQ, Py_NE, Py_LT, Py_LE};
376380

@@ -387,13 +391,13 @@ try_rich_compare(PyObject *v, PyObject *w, int op)
387391
richcmpfunc f;
388392
PyObject *res;
389393

390-
if ((f = v->ob_type->tp_richcompare) != NULL) {
394+
if ((f = RICHCOMPARE(v->ob_type)) != NULL) {
391395
res = (*f)(v, w, op);
392396
if (res != Py_NotImplemented)
393397
return res;
394398
Py_DECREF(res);
395399
}
396-
if ((f = w->ob_type->tp_richcompare) != NULL) {
400+
if ((f = RICHCOMPARE(w->ob_type)) != NULL) {
397401
return (*f)(w, v, swapped_op[op]);
398402
}
399403
res = Py_NotImplemented;
@@ -414,8 +418,7 @@ try_rich_compare_bool(PyObject *v, PyObject *w, int op)
414418
PyObject *res;
415419
int ok;
416420

417-
if (v->ob_type->tp_richcompare == NULL &&
418-
w->ob_type->tp_richcompare == NULL)
421+
if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
419422
return 2; /* Shortcut, avoid INCREF+DECREF */
420423
res = try_rich_compare(v, w, op);
421424
if (res == NULL)
@@ -447,8 +450,7 @@ try_rich_to_3way_compare(PyObject *v, PyObject *w)
447450
};
448451
int i;
449452

450-
if (v->ob_type->tp_richcompare == NULL &&
451-
w->ob_type->tp_richcompare == NULL)
453+
if (RICHCOMPARE(v->ob_type) == NULL && RICHCOMPARE(w->ob_type) == NULL)
452454
return 2; /* Shortcut */
453455

454456
for (i = 0; i < 3; i++) {
@@ -954,7 +956,7 @@ PyObject_Hash(PyObject *v)
954956
PyTypeObject *tp = v->ob_type;
955957
if (tp->tp_hash != NULL)
956958
return (*tp->tp_hash)(v);
957-
if (tp->tp_compare == NULL && tp->tp_richcompare == NULL) {
959+
if (tp->tp_compare == NULL && RICHCOMPARE(tp) == NULL) {
958960
return _Py_HashPointer(v); /* Use address as hash value */
959961
}
960962
/* If there's a cmp but no hash defined, the object can't be hashed */

0 commit comments

Comments
 (0)