RichCompare and RichCompareBool

Aaron Brady castironpi at gmail.com
Mon Mar 2 06:29:57 EST 2009


On Mar 2, 5:00 am, Duncan Booth <duncan.bo... at invalid.invalid> wrote:
> Aaron Brady <castiro... at gmail.com> wrote:
> > Hi,
>
> > In the source for 3.0.1, PyObject_RichCompareBool seems to perform an
> > extra check on identity that PyObjecct_RichCompare does not perform.
snip
> > The code for PyObject_RichCompare does not contain this, it doesn't
> > seem.  Is it a bug?
>
> Without looking at the code it sounds like a bug: identity doesn't always
> imply equality.
>
> It may be that the code you've found only gets called in specific cases
> where you can make that assumption, but otherwise it should be removed.
>
> --
> Duncan Boothhttp://kupuguy.blogspot.com

For reference, here's what the docs say (3.0.1):

'''
PyObject* PyObject_RichCompare(PyObject *o1, PyObject *o2, int opid)
Return value: New reference.
Compare the values of o1 and o2 using the operation specified by opid,
which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE,
corresponding to <, <=, ==, !=, >, or >= respectively. This is the
equivalent of the Python expression o1 op o2, where op is the operator
corresponding to opid. Returns the value of the comparison on success,
or NULL on failure.

int PyObject_RichCompareBool(PyObject *o1, PyObject *o2, int opid)
Compare the values of o1 and o2 using the operation specified by opid,
which must be one of Py_LT, Py_LE, Py_EQ, Py_NE, Py_GT, or Py_GE,
corresponding to <, <=, ==, !=, >, or >= respectively. Returns -1 on
error, 0 if the result is false, 1 otherwise. This is the equivalent
of the Python expression o1 op o2, where op is the operator
corresponding to opid.
'''

As you can see, the only difference is the statement of the return
value.

As an aside, though, it's interesting that '__eq__' &c. can return any
object, in addition to True and False.  I hadn't thought of that.
'RichCompareBool' just calls 'PyObject_IsTrue' on the result.



More information about the Python-list mailing list