[pypy-commit] pypy py3.5: datetime.__hash__() was still deterministic if tzoff!=None.

arigo pypy.commits at gmail.com
Sun Feb 12 12:31:58 EST 2017


Author: Armin Rigo <arigo at tunes.org>
Branch: py3.5
Changeset: r90072:7e28f9b94af3
Date: 2017-02-12 18:30 +0100
http://bitbucket.org/pypy/pypy/changeset/7e28f9b94af3/

Log:	datetime.__hash__() was still deterministic if tzoff!=None.

diff --git a/lib-python/3/datetime.py b/lib-python/3/datetime.py
--- a/lib-python/3/datetime.py
+++ b/lib-python/3/datetime.py
@@ -1766,9 +1766,16 @@
             if tzoff is None:
                 self._hashcode = hash(self._getstate()[0])
             else:
+                # PyPy: uses an algo that relies on the hash of strings,
+                # giving a nondeterministic result.  CPython doesn't do
+                # that if there is a tzoff (but does if there is no
+                # tzoff).
                 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)
+                delta = timedelta(days, seconds, self.microsecond) - tzoff
+                temp1 = '%d&%d&%d' % (delta.days, delta.seconds,
+                                      delta.microseconds)
+                self._hashcode = hash(temp1)
         return self._hashcode
 
     # Pickle support.


More information about the pypy-commit mailing list