From 679a64b6a14bbad1d298b5d86e811b185da5ff8f Mon Sep 17 00:00:00 2001 From: Jason Madden Date: Thu, 9 Apr 2015 09:06:13 -0500 Subject: [PATCH] Detect the bitness of the current process in order to better match the C TimeStamp hashcode. Fixes zopefoundation/persistent#21 --- persistent/timestamp.py | 13 +++++++++++-- 1 file changed, 11 insertions(+), 2 deletions(-) diff --git a/persistent/timestamp.py b/persistent/timestamp.py index 71dac08..148ef0f 100644 --- a/persistent/timestamp.py +++ b/persistent/timestamp.py @@ -13,12 +13,21 @@ ############################################################################## __all__ = ('TimeStamp',) -from ctypes import c_int64 import datetime import math import struct import sys +# We need to use a native ctype to get the overflow behaviour in the +# __hash__ function to match the C implementation. However, which one +# to use depends on whether we were built 64-bit or 32-bit. +if sys.maxsize > 2**32: + # the `platform` docs say this is the most reliable way to + # detect 64-bit + from ctypes import c_int64 as c_long +else: + from ctypes import c_long + _RAWTYPE = bytes def _makeOctets(s): @@ -158,7 +167,7 @@ def __hash__(self): # Make sure to overflow and wraparound just # like the C code does. - x = c_int64(x).value + x = c_long(x).value if x == -1: #pragma: no cover # The C version has this condition, but it's not clear # why; it's also not immediately obvious what bytestring