]> granicus.if.org Git - python/commitdiff
When comparing objects, always check that tp_compare is not NULL
authorGuido van Rossum <guido@python.org>
Tue, 21 Jul 1998 21:56:41 +0000 (21:56 +0000)
committerGuido van Rossum <guido@python.org>
Tue, 21 Jul 1998 21:56:41 +0000 (21:56 +0000)
before calling it.  This check was there when the objects were of the
same type *before* coercion, but not if they initially differed but
became the same *after* coercion.

Objects/object.c

index 2428cc9331ca7702175a61aa1967a05c899cd3c2..b62477a812f35300f8801810e426ea90a0265a44 100644 (file)
@@ -312,14 +312,18 @@ PyObject_Compare(v, w)
        if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
                char *vname = vtp->tp_name;
                char *wname = wtp->tp_name;
-               if (vtp->tp_as_number != NULL &&
-                               wtp->tp_as_number != NULL) {
+               if (vtp->tp_as_number != NULL && wtp->tp_as_number != NULL) {
                        int err;
                        err = PyNumber_CoerceEx(&v, &w);
                        if (err < 0)
                                return -1;
                        else if (err == 0) {
-                               int cmp = (*v->ob_type->tp_compare)(v, w);
+                               int cmp;
+                               vtp = v->ob_type;
+                               if (vtp->tp_compare == NULL)
+                                       cmp = (v < w) ? -1 : 1;
+                               else
+                                       cmp = (*vtp->tp_compare)(v, w);
                                Py_DECREF(v);
                                Py_DECREF(w);
                                return cmp;