diff options
author | stratakis <cstratak@redhat.com> | 2017-11-02 11:32:54 +0100 |
---|---|---|
committer | Nick Coghlan <ncoghlan@gmail.com> | 2017-11-02 20:32:54 +1000 |
commit | e8b19656396381407ad91473af5da8b0d4346e88 (patch) | |
tree | 16638970d5014728a49808d0c80c4af0fe6ccb91 /Objects/bytearrayobject.c | |
parent | 4f469c096628af730b17798d0ebfd8925bfde836 (diff) | |
download | cpython-git-e8b19656396381407ad91473af5da8b0d4346e88.tar.gz |
bpo-23699: Use a macro to reduce boilerplate code in rich comparison functions (GH-793)
Diffstat (limited to 'Objects/bytearrayobject.c')
-rw-r--r-- | Objects/bytearrayobject.c | 37 |
1 files changed, 11 insertions, 26 deletions
diff --git a/Objects/bytearrayobject.c b/Objects/bytearrayobject.c index c92cfc01fd..83c3549d50 100644 --- a/Objects/bytearrayobject.c +++ b/Objects/bytearrayobject.c @@ -1012,8 +1012,6 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op) { Py_ssize_t self_size, other_size; Py_buffer self_bytes, other_bytes; - PyObject *res; - Py_ssize_t minsize; int cmp, rc; /* Bytes can be compared to anything that supports the (binary) @@ -1049,38 +1047,25 @@ bytearray_richcompare(PyObject *self, PyObject *other, int op) if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { /* Shortcut: if the lengths differ, the objects differ */ - cmp = (op == Py_NE); + PyBuffer_Release(&self_bytes); + PyBuffer_Release(&other_bytes); + return PyBool_FromLong((op == Py_NE)); } else { - minsize = self_size; - if (other_size < minsize) - minsize = other_size; - - cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); + cmp = memcmp(self_bytes.buf, other_bytes.buf, + Py_MIN(self_size, other_size)); /* In ISO C, memcmp() guarantees to use unsigned bytes! */ - if (cmp == 0) { - if (self_size < other_size) - cmp = -1; - else if (self_size > other_size) - cmp = 1; - } + PyBuffer_Release(&self_bytes); + PyBuffer_Release(&other_bytes); - switch (op) { - case Py_LT: cmp = cmp < 0; break; - case Py_LE: cmp = cmp <= 0; break; - case Py_EQ: cmp = cmp == 0; break; - case Py_NE: cmp = cmp != 0; break; - case Py_GT: cmp = cmp > 0; break; - case Py_GE: cmp = cmp >= 0; break; + if (cmp != 0) { + Py_RETURN_RICHCOMPARE(cmp, 0, op); } + + Py_RETURN_RICHCOMPARE(self_size, other_size, op); } - res = cmp ? Py_True : Py_False; - PyBuffer_Release(&self_bytes); - PyBuffer_Release(&other_bytes); - Py_INCREF(res); - return res; } static void |