mirror of
https://github.com/python/cpython.git
synced 2025-08-04 00:48:58 +00:00
bpo-23699: Use a macro to reduce boilerplate code in rich comparison functions (GH-793)
This commit is contained in:
parent
4f469c0966
commit
e8b1965639
16 changed files with 75 additions and 316 deletions
|
@ -11152,14 +11152,10 @@ _PyUnicode_EqualToASCIIId(PyObject *left, _Py_Identifier *right)
|
|||
return unicode_compare_eq(left, right_uni);
|
||||
}
|
||||
|
||||
#define TEST_COND(cond) \
|
||||
((cond) ? Py_True : Py_False)
|
||||
|
||||
PyObject *
|
||||
PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
|
||||
{
|
||||
int result;
|
||||
PyObject *v;
|
||||
|
||||
if (!PyUnicode_Check(left) || !PyUnicode_Check(right))
|
||||
Py_RETURN_NOTIMPLEMENTED;
|
||||
|
@ -11174,13 +11170,11 @@ PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
|
|||
case Py_LE:
|
||||
case Py_GE:
|
||||
/* a string is equal to itself */
|
||||
v = Py_True;
|
||||
break;
|
||||
Py_RETURN_TRUE;
|
||||
case Py_NE:
|
||||
case Py_LT:
|
||||
case Py_GT:
|
||||
v = Py_False;
|
||||
break;
|
||||
Py_RETURN_FALSE;
|
||||
default:
|
||||
PyErr_BadArgument();
|
||||
return NULL;
|
||||
|
@ -11189,32 +11183,12 @@ PyUnicode_RichCompare(PyObject *left, PyObject *right, int op)
|
|||
else if (op == Py_EQ || op == Py_NE) {
|
||||
result = unicode_compare_eq(left, right);
|
||||
result ^= (op == Py_NE);
|
||||
v = TEST_COND(result);
|
||||
return PyBool_FromLong(result);
|
||||
}
|
||||
else {
|
||||
result = unicode_compare(left, right);
|
||||
|
||||
/* Convert the return value to a Boolean */
|
||||
switch (op) {
|
||||
case Py_LE:
|
||||
v = TEST_COND(result <= 0);
|
||||
break;
|
||||
case Py_GE:
|
||||
v = TEST_COND(result >= 0);
|
||||
break;
|
||||
case Py_LT:
|
||||
v = TEST_COND(result == -1);
|
||||
break;
|
||||
case Py_GT:
|
||||
v = TEST_COND(result == 1);
|
||||
break;
|
||||
default:
|
||||
PyErr_BadArgument();
|
||||
return NULL;
|
||||
}
|
||||
Py_RETURN_RICHCOMPARE(result, 0, op);
|
||||
}
|
||||
Py_INCREF(v);
|
||||
return v;
|
||||
}
|
||||
|
||||
int
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue