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

Skip to content

Commit 8f9143d

Browse files
committed
Once again, numeric-smelling objects compare smaller than non-numeric
ones.
1 parent af4c942 commit 8f9143d

1 file changed

Lines changed: 17 additions & 2 deletions

File tree

Objects/object.c

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ static int
522522
default_3way_compare(PyObject *v, PyObject *w)
523523
{
524524
int c;
525+
char *vname, *wname;
525526

526527
if (v->ob_type == w->ob_type) {
527528
/* When comparing these pointers, they must be cast to
@@ -550,8 +551,22 @@ default_3way_compare(PyObject *v, PyObject *w)
550551
}
551552

552553
/* different type: compare type names */
553-
c = strcmp(v->ob_type->tp_name, w->ob_type->tp_name);
554-
return (c < 0) ? -1 : (c > 0) ? 1 : 0;
554+
if (v->ob_type->tp_as_number)
555+
vname = "";
556+
else
557+
vname = v->ob_type->tp_name;
558+
if (w->ob_type->tp_as_number)
559+
wname = "";
560+
else
561+
wname = w->ob_type->tp_name;
562+
c = strcmp(vname, wname);
563+
if (c < 0)
564+
return -1;
565+
if (c > 0)
566+
return 1;
567+
/* Same type name, or (more likely) incomparable numeric types */
568+
return ((Py_uintptr_t)(v->ob_type) < (
569+
Py_uintptr_t)(w->ob_type)) ? -1 : 1;
555570
}
556571

557572
#define CHECK_TYPES(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_CHECKTYPES)

0 commit comments

Comments
 (0)