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

Skip to content

Commit 53451b3

Browse files
committed
Use rich comparisons in min and max.
1 parent ac7be68 commit 53451b3

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

Python/bltinmodule.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1371,7 +1371,7 @@ Return the dictionary containing the current scope's local variables.";
13711371

13721372

13731373
static PyObject *
1374-
min_max(PyObject *args, int sign)
1374+
min_max(PyObject *args, int op)
13751375
{
13761376
int i;
13771377
PyObject *v, *w, *x;
@@ -1401,16 +1401,16 @@ min_max(PyObject *args, int sign)
14011401
if (w == NULL)
14021402
w = x;
14031403
else {
1404-
int c = PyObject_Compare(x, w);
1405-
if (c && PyErr_Occurred()) {
1404+
int cmp = PyObject_RichCompareBool(x, w, op);
1405+
if (cmp > 0) {
1406+
Py_DECREF(w);
1407+
w = x;
1408+
}
1409+
else if (cmp < 0) {
14061410
Py_DECREF(x);
14071411
Py_XDECREF(w);
14081412
return NULL;
14091413
}
1410-
if (c * sign > 0) {
1411-
Py_DECREF(w);
1412-
w = x;
1413-
}
14141414
else
14151415
Py_DECREF(x);
14161416
}
@@ -1424,7 +1424,7 @@ min_max(PyObject *args, int sign)
14241424
static PyObject *
14251425
builtin_min(PyObject *self, PyObject *v)
14261426
{
1427-
return min_max(v, -1);
1427+
return min_max(v, Py_LT);
14281428
}
14291429

14301430
static char min_doc[] =
@@ -1438,7 +1438,7 @@ With two or more arguments, return the smallest argument.";
14381438
static PyObject *
14391439
builtin_max(PyObject *self, PyObject *v)
14401440
{
1441-
return min_max(v, 1);
1441+
return min_max(v, Py_GT);
14421442
}
14431443

14441444
static char max_doc[] =

0 commit comments

Comments
 (0)