From 9f739d03bda43e4858ee9839f77b309fdcb8e5e8 Mon Sep 17 00:00:00 2001 From: Matthias Diener Date: Tue, 3 Sep 2024 09:16:12 -0500 Subject: [PATCH] KeyBuilder: change int to direct str representation --- pytools/persistent_dict.py | 11 +---------- pytools/test/test_persistent_dict.py | 10 +++++++--- 2 files changed, 8 insertions(+), 13 deletions(-) diff --git a/pytools/persistent_dict.py b/pytools/persistent_dict.py index 3f89887f..2fca2dcf 100644 --- a/pytools/persistent_dict.py +++ b/pytools/persistent_dict.py @@ -283,16 +283,7 @@ def update_for_type(key_hash: Hash, key: type) -> None: @staticmethod def update_for_int(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")) @classmethod def update_for_enum(cls, key_hash: Hash, key: Enum) -> None: diff --git a/pytools/test/test_persistent_dict.py b/pytools/test/test_persistent_dict.py index b04d43a0..a7e43e51 100644 --- a/pytools/test/test_persistent_dict.py +++ b/pytools/test/test_persistent_dict.py @@ -432,9 +432,13 @@ 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.longlong(1)) == keyb(np.longlong(1)) assert keyb(np.float16(1.1)) == keyb(np.float16(1.1)) @@ -566,7 +570,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)) @@ -590,7 +594,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]