Skip to content

Commit 5c825b9

Browse files
committed
Use PyLong_AsInt32 in richcompare()
1 parent 97612e1 commit 5c825b9

File tree

1 file changed

+27
-5
lines changed

1 file changed

+27
-5
lines changed

gmp.c

Lines changed: 27 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -691,14 +691,36 @@ to_float(PyObject *self)
691691
static PyObject *
692692
richcompare(PyObject *self, PyObject *other, int op)
693693
{
694-
MPZ_Object *u = (MPZ_Object *)self, *v = NULL;
694+
zz_t *u = &((MPZ_Object *)self)->z;
695+
zz_ord r;
695696

696-
assert(MPZ_Check(self));
697-
CHECK_OP(v, other);
697+
if (MPZ_Check(other)) {
698+
r = zz_cmp(u, &((MPZ_Object *)other)->z);
699+
}
700+
else if (PyLong_Check(other)) {
701+
int32_t v;
698702

699-
zz_ord r = zz_cmp(&u->z, &v->z);
703+
if (PyLong_AsInt32(other, &v) == 0) {
704+
r = zz_cmp_i32(u, v);
705+
}
706+
else {
707+
PyErr_Clear();
700708

701-
Py_DECREF(v);
709+
MPZ_Object *v = MPZ_from_int(other);
710+
711+
if (!v) {
712+
goto end; /* LCOV_EXCL_LINE */
713+
}
714+
r = zz_cmp(u, &v->z);
715+
Py_DECREF(v);
716+
}
717+
}
718+
else if (Number_Check(other)) {
719+
goto numbers;
720+
}
721+
else {
722+
goto fallback;
723+
}
702724
switch (op) {
703725
case Py_LT:
704726
return PyBool_FromLong(r == ZZ_LT);

0 commit comments

Comments
 (0)