<html>
<head>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body bgcolor="#FFFFFF" text="#330033">
<div class="moz-cite-prefix">On 4/28/2015 2:13 AM, Victor Stinner
wrote:<br>
</div>
<blockquote
cite="mid:CAMpsgwZZWuxV3HcLzCvTWwv8OOaQWnxmcd-2jC_Qzxz9dVN-Qg@mail.gmail.com"
type="cite">
<blockquote type="cite" style="color: #000000;">
<pre wrap="">#define Py_RETURN_RICHCOMPARE(val1, val2, op) \
<span class="moz-txt-citetags">> </span> do { \
<span class="moz-txt-citetags">> </span> switch (op) { \
<span class="moz-txt-citetags">> </span> case Py_EQ: if ((val1) == (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
<span class="moz-txt-citetags">> </span> case Py_NE: if ((val1) != (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
<span class="moz-txt-citetags">> </span> case Py_LT: if ((val1) < (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
<span class="moz-txt-citetags">> </span> case Py_GT: if ((val1) > (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
<span class="moz-txt-citetags">> </span> case Py_LE: if ((val1) <= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
<span class="moz-txt-citetags">> </span> case Py_GE: if ((val1) >= (val2)) Py_RETURN_TRUE; Py_RETURN_FALSE; \
<span class="moz-txt-citetags">> </span> } \
<span class="moz-txt-citetags">> </span> Py_RETURN_NOTIMPLEMENTED; \
<span class="moz-txt-citetags">> </span> } while (0)
</pre>
</blockquote>
<pre wrap="">I would prefer a function for that:
PyObject *Py_RichCompare(long val1, long2, int op);
</pre>
</blockquote>
Why would you prefer a function? As a macro, when the op is a
constant, most of the code would be optimized away by a decent
compiler.<br>
<br>
I suppose when the op is not a constant, then a function would save
code space.<br>
<br>
So I suppose it depends on the predominant use cases.<br>
<br>
<blockquote type="cite">
<pre wrap="">You should also handle invalid operator. PyUnicode_RichCompare() calls
PyErr_BadArgument() in this case.
</pre>
</blockquote>
One can quibble over the correct error return, but the above code
does handle invalid operators after the switch.<br>
<br>
Glenn<br>
</body>
</html>