New submission from Karthikeyan Singaravelan <tir.kar...@gmail.com>:

As reported by Serhiy on https://bugs.python.org/issue37555#msg347733 there is 
a difference in __eq__ definition in datetime module's C and Python 
implementation for timedelta and time. When the other in __eq__ is not of the 
same type NotImplemented is not returned in Python implementation like C 
causing equality to behave differently.

Difference in C implementation and Python implementation for 
datetime.timedelta.__eq__

https://github.com/python/cpython/blob/c8e7146de257930ea8d0d4aa74b3a64fcaa79d4b/Modules/_datetimemodule.c#L2152

static PyObject *
delta_richcompare(PyObject *self, PyObject *other, int op)
{
    if (PyDelta_Check(other)) {
        int diff = delta_cmp(self, other);
        return diff_to_bool(diff, op);
    }
    else {
        Py_RETURN_NOTIMPLEMENTED;
    }
}


https://github.com/python/cpython/blob/c8e7146de257930ea8d0d4aa74b3a64fcaa79d4b/Lib/datetime.py#L732

def __eq__(self, other):
    if isinstance(other, timedelta):
        return self._cmp(other) == 0
    else:
        return False


I will add a PR for this with test.

----------
components: Library (Lib)
messages: 347773
nosy: belopolsky, p-ganssle, serhiy.storchaka, xtreak
priority: normal
severity: normal
status: open
title: Difference in equality check between C and Python implementation for 
datetime module's timedelta and time
type: behavior
versions: Python 3.7, Python 3.8, Python 3.9

_______________________________________
Python tracker <rep...@bugs.python.org>
<https://bugs.python.org/issue37579>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to