Commit cad848ac authored by Tres Seaver's avatar Tres Seaver

Merge pull request #22 from NextThought/issue21

Use `ctypes.c_long` to get the native long type on all platforms; fixes 32-bit TimeStamp hashcode
parents 663497de 84390bc9
...@@ -4,7 +4,7 @@ ...@@ -4,7 +4,7 @@
4.0.10 (unreleased) 4.0.10 (unreleased)
------------------- -------------------
- TBD - Fix the hashcode of the Python ``TimeStamp`` on 32-bit platforms.
4.0.9 (2015-04-08) 4.0.9 (2015-04-08)
------------------ ------------------
......
...@@ -253,6 +253,36 @@ class PyAndCComparisonTests(unittest.TestCase): ...@@ -253,6 +253,36 @@ class PyAndCComparisonTests(unittest.TestCase):
c, py = self._make_C_and_Py(*self.now_ts_args) c, py = self._make_C_and_Py(*self.now_ts_args)
self.assertEqual(hash(c), hash(py)) self.assertEqual(hash(c), hash(py))
def test_py_hash_32_64_bit(self):
# We happen to know that on a 32-bit platform, the hashcode
# of the c version should be exactly
# -1419374591
# and the 64-bit should be exactly:
# -3850693964765720575
# Fake out the python version to think it's on a 32-bit
# platform and test the same; also verify 64 bit
bit_32_hash = -1419374591
bit_64_hash = -3850693964765720575
import persistent.timestamp
import ctypes
orig_c_long = persistent.timestamp.c_long
try:
persistent.timestamp.c_long = ctypes.c_int32
py = self._makePy(*self.now_ts_args)
self.assertEqual(hash(py), bit_32_hash)
persistent.timestamp.c_long = ctypes.c_int64
self.assertEqual(hash(py), bit_64_hash)
finally:
persistent.timestamp.c_long = orig_c_long
if orig_c_long is ctypes.c_int32:
self.assertEqual(py.__hash__(), bit_32_hash)
elif orig_c_long is ctypes.c_int64:
self.assertEqual(py.__hash__(), bit_64_hash)
else:
self.fail("Unknown bitness")
def test_hash_equal_constants(self): def test_hash_equal_constants(self):
# The simple constants make it easier to diagnose # The simple constants make it easier to diagnose
# a difference in algorithms # a difference in algorithms
......
...@@ -13,7 +13,7 @@ ...@@ -13,7 +13,7 @@
############################################################################## ##############################################################################
__all__ = ('TimeStamp',) __all__ = ('TimeStamp',)
from ctypes import c_int64 from ctypes import c_long
import datetime import datetime
import math import math
import struct import struct
...@@ -158,7 +158,7 @@ class pyTimeStamp(object): ...@@ -158,7 +158,7 @@ class pyTimeStamp(object):
# Make sure to overflow and wraparound just # Make sure to overflow and wraparound just
# like the C code does. # like the C code does.
x = c_int64(x).value x = c_long(x).value
if x == -1: #pragma: no cover if x == -1: #pragma: no cover
# The C version has this condition, but it's not clear # The C version has this condition, but it's not clear
# why; it's also not immediately obvious what bytestring # why; it's also not immediately obvious what bytestring
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment