[Python-Dev] A macro for easier rich comparisons
David Malcolm
dmalcolm at redhat.com
Tue Apr 28 20:12:10 CEST 2015
On Tue, 2015-04-28 at 10:50 -0700, Glenn Linderman wrote:
> On 4/28/2015 2:13 AM, Victor Stinner wrote:
>
> > > #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; \
> > > > } \
> > > > Py_RETURN_NOTIMPLEMENTED; \
> > > > } while (0)
> > I would prefer a function for that:
> >
> > PyObject *Py_RichCompare(long val1, long2, int op);
> 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.
>
> I suppose when the op is not a constant, then a function would save
> code space.
>
> So I suppose it depends on the predominant use cases.
There's also the possibility of wrapping C++ code that uses overloaded
operators: having it as a macro could allow those C++ operators to be be
mapped into Python.
Hope this is constructive
Dave
More information about the Python-Dev
mailing list