summaryrefslogtreecommitdiffstats
path: root/Include/object.h
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 /Include/object.h
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 'Include/object.h')
-rw-r--r--Include/object.h19
1 files changed, 19 insertions, 0 deletions
diff --git a/Include/object.h b/Include/object.h
index cb57359..b7051f3 100644
--- a/Include/object.h
+++ b/Include/object.h
@@ -931,6 +931,25 @@ PyAPI_DATA(PyObject) _Py_NotImplementedStruct; /* Don't use this directly */
#define Py_GT 4
#define Py_GE 5
+/*
+ * Macro for implementing rich comparisons
+ *
+ * Needs to be a macro because any C-comparable type can be used.
+ */
+#define Py_RETURN_RICHCOMPARE(val1, val2, op) \
+ do { \
+ switch (op) { \
+ case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
+ case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
+ case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
+ case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
+ case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
+ case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
+ default: \
+ Py_UNREACHABLE(); \
+ } \
+ } while (0)
+
#ifndef Py_LIMITED_API
/* Maps Py_LT to Py_GT, ..., Py_GE to Py_LE.
* Defined in object.c.