[Python-Dev] Rich Comparison from "C" API?
Tim Peters
tim.one@comcast.net
Wed, 12 Jun 2002 11:02:50 -0400
[David Abrahams]
> Suppose I want to execute, from "C", the same steps taken by Python in
> evaluating the expression
>
> x <= y
>
> I see no documented "C" API function which can do that. I'm guessing
> PyObject_RichCompare[Bool] may do what I want, but since it's
> undocumented I assume its untouchable. Guido?
It doesn't start with an underscore, and is advertised in object.h, so that
it's undocumented just means you didn't yet volunteer a doc patch <wink>.
/* Return -1 if error; 1 if v op w; 0 if not (v op w). */
int PyObject_RichCompareBool(PyObject *v, PyObject *w, int op)
where op is one of (also in object.h)
/* Rich comparison opcodes */
#define Py_LT 0
#define Py_LE 1
#define Py_EQ 2
#define Py_NE 3
#define Py_GT 4
#define Py_GE 5
So the answer to your question is
int result = PyObject_RichCompareBool(x, y, Py_LE);
if (result < 0)
return error_indicator;
/* now result is true/false */