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

Skip to content

Commit cd5a5f6

Browse files
committed
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 "".
1 parent 5d23758 commit cd5a5f6

1 file changed

Lines changed: 15 additions & 8 deletions

File tree

Objects/object.c

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ int
284284
PyObject_Compare(v, w)
285285
PyObject *v, *w;
286286
{
287-
PyTypeObject *tp;
287+
PyTypeObject *vtp, *wtp;
288288
if (v == NULL || w == NULL) {
289289
PyErr_BadInternalCall();
290290
return -1;
@@ -309,9 +309,11 @@ PyObject_Compare(v, w)
309309
Py_DECREF(res);
310310
return (c < 0) ? -1 : (c > 0) ? 1 : 0;
311311
}
312-
if ((tp = v->ob_type) != w->ob_type) {
313-
if (tp->tp_as_number != NULL &&
314-
w->ob_type->tp_as_number != NULL) {
312+
if ((vtp = v->ob_type) != (wtp = w->ob_type)) {
313+
char *vname = vtp->tp_name;
314+
char *wname = wtp->tp_name;
315+
if (vtp->tp_as_number != NULL &&
316+
wtp->tp_as_number != NULL) {
315317
int err;
316318
err = PyNumber_CoerceEx(&v, &w);
317319
if (err < 0)
@@ -323,11 +325,16 @@ PyObject_Compare(v, w)
323325
return cmp;
324326
}
325327
}
326-
return strcmp(tp->tp_name, w->ob_type->tp_name);
327-
}
328-
if (tp->tp_compare == NULL)
328+
else if (vtp->tp_as_number != NULL)
329+
vname = "";
330+
else if (wtp->tp_as_number != NULL)
331+
wname = "";
332+
/* Numerical types compare smaller than all other types */
333+
return strcmp(vname, wname);
334+
}
335+
if (vtp->tp_compare == NULL)
329336
return (v < w) ? -1 : 1;
330-
return (*tp->tp_compare)(v, w);
337+
return (*vtp->tp_compare)(v, w);
331338
}
332339

333340
long

0 commit comments

Comments
 (0)