When comparing objects of different types (which is done by comparing

the type names), make sure that numeric objects are considered smaller
than all other objects, by forcing their name to "".
This commit is contained in:
Guido van Rossum 1998-06-09 18:58:44 +00:00
parent 5d23758be7
commit cd5a5f627a
1 changed files with 14 additions and 7 deletions

View File

@ -284,7 +284,7 @@ int
PyObject_Compare(v, w) PyObject_Compare(v, w)
PyObject *v, *w; PyObject *v, *w;
{ {
PyTypeObject *tp; PyTypeObject *vtp, *wtp;
if (v == NULL || w == NULL) { if (v == NULL || w == NULL) {
PyErr_BadInternalCall(); PyErr_BadInternalCall();
return -1; return -1;
@ -309,9 +309,11 @@ PyObject_Compare(v, w)
Py_DECREF(res); Py_DECREF(res);
return (c < 0) ? -1 : (c > 0) ? 1 : 0; return (c < 0) ? -1 : (c > 0) ? 1 : 0;
} }
if ((tp = v->ob_type) != w->ob_type) { if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
if (tp->tp_as_number != NULL && char *vname = vtp->tp_name;
w->ob_type->tp_as_number != NULL) { char *wname = wtp->tp_name;
if (vtp->tp_as_number != NULL &&
wtp->tp_as_number != NULL) {
int err; int err;
err = PyNumber_CoerceEx(&v, &w); err = PyNumber_CoerceEx(&v, &w);
if (err < 0) if (err < 0)
@ -323,11 +325,16 @@ PyObject_Compare(v, w)
return cmp; return cmp;
} }
} }
return strcmp(tp->tp_name, w->ob_type->tp_name); else if (vtp->tp_as_number != NULL)
vname = "";
else if (wtp->tp_as_number != NULL)
wname = "";
/* Numerical types compare smaller than all other types */
return strcmp(vname, wname);
} }
if (tp->tp_compare == NULL) if (vtp->tp_compare == NULL)
return (v < w) ? -1 : 1; return (v < w) ? -1 : 1;
return (*tp->tp_compare)(v, w); return (*vtp->tp_compare)(v, w);
} }
long long