[Datetime-SIG] Another round on error-checking

Tim Peters tim.peters at gmail.com
Mon Aug 31 21:20:18 CEST 2015


[Tim]
>> BTW, it just occurred to me that PEP 495 has already broken
>> datetime.__hash__.  ...

[Alex]
> The PEP did not and in the reference implementation, I am careful to reset
> fold/first before computing the hash:
>
> https://github.com/abalkin/cpython/blob/issue24773/Lib/datetime.py#L1178

But you're pointing to time.__hash__ there.  I'm talking about
datetime.__hash__.  You replace `first` there too, but _only_ if
.utcoffset() returns None:

    def __hash__(self):
        if self._hashcode == -1:
            tzoff = self.utcoffset()
            if tzoff is None:
                self._hashcode = hash(self.replace(first=True)._getstate()[0])
            else:
                days = _ymd2ord(self.year, self.month, self.day)
                seconds = self.hour * 3600 + self.minute * 60 + self.second
                self._hashcode = hash(timedelta(days, seconds,
self.microsecond) - tzoff)
        return self._hashcode

So it's the case that two datetimes that compare true may have
different hashes, when they represent the earlier and later times in a
fold.  I didn't say "it's a puzzle" lightly ;-)


More information about the Datetime-SIG mailing list