summaryrefslogtreecommitdiffstats
path: root/Modules
diff options
context:
space:
mode:
authorstratakis <cstratak@redhat.com>2017-11-02 10:32:54 (GMT)
committerNick Coghlan <ncoghlan@gmail.com>2017-11-02 10:32:54 (GMT)
commite8b19656396381407ad91473af5da8b0d4346e88 (patch)
tree16638970d5014728a49808d0c80c4af0fe6ccb91 /Modules
parent4f469c096628af730b17798d0ebfd8925bfde836 (diff)
downloadcpython-e8b19656396381407ad91473af5da8b0d4346e88.zip
cpython-e8b19656396381407ad91473af5da8b0d4346e88.tar.gz
cpython-e8b19656396381407ad91473af5da8b0d4346e88.tar.bz2
bpo-23699: Use a macro to reduce boilerplate code in rich comparison functions (GH-793)
Diffstat (limited to 'Modules')
-rw-r--r--Modules/_datetimemodule.c17
-rw-r--r--Modules/_tkinter.c34
-rw-r--r--Modules/parsermodule.c34
-rw-r--r--Modules/selectmodule.c22
4 files changed, 6 insertions, 101 deletions
diff --git a/Modules/_datetimemodule.c b/Modules/_datetimemodule.c
index 09b5579..b50cdda 100644
--- a/Modules/_datetimemodule.c
+++ b/Modules/_datetimemodule.c
@@ -1442,22 +1442,7 @@ build_struct_time(int y, int m, int d, int hh, int mm, int ss, int dstflag)
static PyObject *
diff_to_bool(int diff, int op)
{
- PyObject *result;
- int istrue;
-
- switch (op) {
- case Py_EQ: istrue = diff == 0; break;
- case Py_NE: istrue = diff != 0; break;
- case Py_LE: istrue = diff <= 0; break;
- case Py_GE: istrue = diff >= 0; break;
- case Py_LT: istrue = diff < 0; break;
- case Py_GT: istrue = diff > 0; break;
- default:
- Py_UNREACHABLE();
- }
- result = istrue ? Py_True : Py_False;
- Py_INCREF(result);
- return result;
+ Py_RETURN_RICHCOMPARE(diff, 0, op);
}
/* Raises a "can't compare" TypeError and returns NULL. */
diff --git a/Modules/_tkinter.c b/Modules/_tkinter.c
index 005214e..7123da5 100644
--- a/Modules/_tkinter.c
+++ b/Modules/_tkinter.c
@@ -864,13 +864,10 @@ PyTclObject_repr(PyTclObject *self)
return repr;
}
-#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
-
static PyObject *
PyTclObject_richcompare(PyObject *self, PyObject *other, int op)
{
int result;
- PyObject *v;
/* neither argument should be NULL, unless something's gone wrong */
if (self == NULL || other == NULL) {
@@ -880,8 +877,7 @@ PyTclObject_richcompare(PyObject *self, PyObject *other, int op)
/* both arguments should be instances of PyTclObject */
if (!PyTclObject_Check(self) || !PyTclObject_Check(other)) {
- v = Py_NotImplemented;
- goto finished;
+ Py_RETURN_NOTIMPLEMENTED;
}
if (self == other)
@@ -890,33 +886,7 @@ PyTclObject_richcompare(PyObject *self, PyObject *other, int op)
else
result = strcmp(Tcl_GetString(((PyTclObject *)self)->value),
Tcl_GetString(((PyTclObject *)other)->value));
- /* Convert return value to a Boolean */
- switch (op) {
- case Py_EQ:
- v = TEST_COND(result == 0);
- break;
- case Py_NE:
- v = TEST_COND(result != 0);
- break;
- 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 < 0);
- break;
- case Py_GT:
- v = TEST_COND(result > 0);
- break;
- default:
- PyErr_BadArgument();
- return NULL;
- }
- finished:
- Py_INCREF(v);
- return v;
+ Py_RETURN_RICHCOMPARE(result, 0, op);
}
PyDoc_STRVAR(get_typename__doc__, "name of the Tcl type");
diff --git a/Modules/parsermodule.c b/Modules/parsermodule.c
index 929f2de..2b98be4 100644
--- a/Modules/parsermodule.c
+++ b/Modules/parsermodule.c
@@ -299,13 +299,10 @@ parser_compare_nodes(node *left, node *right)
*
*/
-#define TEST_COND(cond) ((cond) ? Py_True : Py_False)
-
static PyObject *
parser_richcompare(PyObject *left, PyObject *right, int op)
{
int result;
- PyObject *v;
/* neither argument should be NULL, unless something's gone wrong */
if (left == NULL || right == NULL) {
@@ -315,8 +312,7 @@ parser_richcompare(PyObject *left, PyObject *right, int op)
/* both arguments should be instances of PyST_Object */
if (!PyST_Object_Check(left) || !PyST_Object_Check(right)) {
- v = Py_NotImplemented;
- goto finished;
+ Py_RETURN_NOTIMPLEMENTED;
}
if (left == right)
@@ -326,33 +322,7 @@ parser_richcompare(PyObject *left, PyObject *right, int op)
result = parser_compare_nodes(((PyST_Object *)left)->st_node,
((PyST_Object *)right)->st_node);
- /* Convert return value to a Boolean */
- switch (op) {
- case Py_EQ:
- v = TEST_COND(result == 0);
- break;
- case Py_NE:
- v = TEST_COND(result != 0);
- break;
- 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 < 0);
- break;
- case Py_GT:
- v = TEST_COND(result > 0);
- break;
- default:
- PyErr_BadArgument();
- return NULL;
- }
- finished:
- Py_INCREF(v);
- return v;
+ Py_RETURN_RICHCOMPARE(result, 0, op);
}
/* parser_newstobject(node* st)
diff --git a/Modules/selectmodule.c b/Modules/selectmodule.c
index 1cde6e8..f2f5cc8 100644
--- a/Modules/selectmodule.c
+++ b/Modules/selectmodule.c
@@ -1929,27 +1929,7 @@ kqueue_event_richcompare(kqueue_event_Object *s, kqueue_event_Object *o,
: 0;
#undef CMP
- switch (op) {
- case Py_EQ:
- result = (result == 0);
- break;
- case Py_NE:
- result = (result != 0);
- break;
- case Py_LE:
- result = (result <= 0);
- break;
- case Py_GE:
- result = (result >= 0);
- break;
- case Py_LT:
- result = (result < 0);
- break;
- case Py_GT:
- result = (result > 0);
- break;
- }
- return PyBool_FromLong((long)result);
+ Py_RETURN_RICHCOMPARE(result, 0, op);
}
static PyTypeObject kqueue_event_Type = {