[Python-ideas] A macro for easier rich comparisons

Petr Viktorin encukou at gmail.com
Mon Mar 23 16:39:01 CET 2015


On Fri, Mar 20, 2015 at 11:19 PM, Andrew Barnert <abarnert at yahoo.com> wrote:
> On Mar 20, 2015, at 7:24 AM, Petr Viktorin <encukou at gmail.com> wrote:
>>
>> A sequence of comparisons is less efficient than a switch statement.
>> Same with PyBool_FromLong vs. Py_RETURN_*
>> Also, it might be too complex for a macro.
>> These points could be solved by making it a function instead.
>
> Or by just making the macro a statement instead of a function; then the code can be exactly the same code you're abstracting out, and instead of using it like this:
>
>      return PY_RICHCOMPARE(mytype_get_data(obj1), mytype_get_data(obj2), op);
>
> ... you use it like this:
>
>     PY_RETURNRICHCOMPARE(mytype_get_data(obj1), mytype_get_data(obj2), op);

Thanks Andrew.
This makes it harder to do cleanup after the comparison (e.g. jump to
a common block of Py_DECREFs, or do PyFPE_END_PROTECT in float code).
But those should be rare enough, or can be easily worked around, so a
"return" macro is still useful.


More information about the Python-ideas mailing list