Based on Inerdial's push in the right direction, here is how I dug down to it. Any feedback on a better way to go about this would be appreciated.
ack --type=cc __contains__
leads to operator.c
spam2(contains,__contains__,
"contains(a, b) -- Same as b in a (note reversed operands).")
which renames "contains" to "op_contains"
which in turn points to PySequence_Contains (not in this file)
ack --type=cc PySequence_Contains
leads to the definition in abstract.c (just the return below)
result = _PySequence_IterSearch(seq, ob, PY_ITERSEARCH_CONTAINS);
which leads to _PySequence_IterSearch (just the comparison below)
cmp = PyObject_RichCompareBool(obj, item, Py_EQ);
which leads to PyObject_RichCompareBool (not in this file)
ack --type=cc PyObject_RichCompareBool
leads to the definition in object.c where I finally find the implementation of the comparison that is doing identity check before the actual equality check (my original question about the comparison).
/* Quick result when objects are the same.
Guarantees that identity implies equality. */
if (v == w) {
if (op == Py_EQ)
return 1;
else if (op == Py_NE)
return 0;
}