From cee5027972c5c5063f64f7f6624e20d1c9069497 Mon Sep 17 00:00:00 2001 From: Andreas Kloeckner Date: Sat, 31 Aug 2024 13:08:29 -0500 Subject: [PATCH] KeyBuilder: use system byteorder to match numpy Closes gh-259 --- pytools/persistent_dict.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/pytools/persistent_dict.py b/pytools/persistent_dict.py index 146d2554..3f89887f 100644 --- a/pytools/persistent_dict.py +++ b/pytools/persistent_dict.py @@ -174,6 +174,12 @@ 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 @@ -280,7 +286,10 @@ def update_for_int(key_hash: Hash, key: int) -> None: sz = 8 while True: try: - key_hash.update(key.to_bytes(sz, byteorder="little", signed=True)) + # 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