From a5c2418f652f490fbdb3bd5354f52ecb0919ad24 Mon Sep 17 00:00:00 2001 From: Fish Date: Thu, 31 Jul 2025 15:39:45 -0700 Subject: [PATCH] Fix: Allow multiple users to load the cache. --- pyvex/native.py | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/pyvex/native.py b/pyvex/native.py index 353b4a5b..b5d08ff6 100644 --- a/pyvex/native.py +++ b/pyvex/native.py @@ -17,13 +17,20 @@ def _parse_ffi_str(): hash_ = hashlib.md5(_ffi_str.encode("utf-8")).hexdigest() cache_location = os.path.join(tempfile.gettempdir(), f"pyvex_ffi_parser_cache.{hash_}") + cache_loaded = False if os.path.isfile(cache_location): # load the cache - with open(cache_location, "rb") as f: - cache = pickle.loads(f.read()) - ffi._parser._declarations = cache["_declarations"] - ffi._parser._int_constants = cache["_int_constants"] - else: + try: + with open(cache_location, "rb") as f: + cache = pickle.loads(f.read()) + ffi._parser._declarations = cache["_declarations"] + ffi._parser._int_constants = cache["_int_constants"] + cache_loaded = True + except Exception: + # failed to load the cache; fall back to parsing + cache_loaded = False + + if not cache_loaded: ffi.cdef(_ffi_str) # cache the result cache = { @@ -35,6 +42,7 @@ def _parse_ffi_str(): temp_file.write(pickle.dumps(cache)) temp_file_name = temp_file.name os.replace(temp_file_name, cache_location) + os.chmod(cache_location, 0o644) # allow other users on Linux to read the cache def _find_c_lib():