diff --git a/pytools/persistent_dict.py b/pytools/persistent_dict.py index 14eda873..96101f6e 100644 --- a/pytools/persistent_dict.py +++ b/pytools/persistent_dict.py @@ -163,12 +163,6 @@ class KeyBuilder: may stop working as early as 2022. .. versionadded:: 2021.2 - - .. note:: - - Some key-building uses system byte order, so the built keys may not match - across different systems. It would be desirable to fix this, but this is - not yet done. """ # this exists so that we can (conceivably) switch algorithms at some point @@ -274,16 +268,7 @@ def update_for_type(self, key_hash: Hash, key: type) -> None: update_for_ABCMeta = update_for_type def update_for_int(self, key_hash: Hash, key: int) -> None: - sz = 8 - while True: - try: - # Must match system byte order so that numpy and this - # generate the same string of bytes. - # https://github.com/inducer/pytools/issues/259 - key_hash.update(key.to_bytes(sz, byteorder=sys.byteorder, signed=True)) - return - except OverflowError: - sz *= 2 + key_hash.update(str(key).encode("utf-8")) def update_for_enum(self, key_hash: Hash, key: Enum) -> None: self.update_for_str(key_hash, str(key)) diff --git a/pytools/test/test_persistent_dict.py b/pytools/test/test_persistent_dict.py index b6dec54f..0b5cb29a 100644 --- a/pytools/test/test_persistent_dict.py +++ b/pytools/test/test_persistent_dict.py @@ -461,9 +461,16 @@ def test_scalar_hashing() -> None: assert keyb(np.int32(1)) == keyb(np.int32(1)) assert keyb(np.int32(2)) != keyb(np.int32(1)) assert keyb(np.int64(1)) == keyb(np.int64(1)) - assert keyb(1) == keyb(np.int64(1)) + assert keyb(1) != keyb(np.int64(1)) assert keyb(1) != keyb(np.int32(1)) + assert keyb(1) == keyb("1") + assert keyb(1.0) != keyb("1.0") # '1.0' uses hex representation + assert keyb(1+1j) == keyb("(1+1j)") + + assert keyb(np.float64(1.0)) != keyb(1.0) + assert keyb(np.complex128(1.0+2.0j)) != keyb(1.0+2.0j) + assert keyb(np.longlong(1)) == keyb(np.longlong(1)) assert keyb(np.float16(1.1)) == keyb(np.float16(1.1)) @@ -595,7 +602,7 @@ class MyDC: name: str value: int - assert keyb(MyDC("hi", 1)) == "d1a1079f1c10aa4f" + assert keyb(MyDC("hi", 1)) == "72c25f5d0ac2bda7" assert keyb(MyDC("hi", 1)) == keyb(MyDC("hi", 1)) assert keyb(MyDC("hi", 1)) != keyb(MyDC("hi", 2)) @@ -619,7 +626,7 @@ class MyAttrs: name: str value: int - assert (keyb(MyAttrs("hi", 1)) == "5b6c5da60eb2bd0f") # type: ignore[call-arg] + assert (keyb(MyAttrs("hi", 1)) == "6e8bfc4394f82985") # type: ignore[call-arg] assert keyb(MyAttrs("hi", 1)) == keyb(MyAttrs("hi", 1)) # type: ignore[call-arg] assert keyb(MyAttrs("hi", 1)) != keyb(MyAttrs("hi", 2)) # type: ignore[call-arg]