@@ -27,12 +27,14 @@ def read_file(blobpath: str) -> bytes:
2727
2828
2929def read_file_cached (blobpath : str ) -> bytes :
30+ user_specified_cache = True
3031 if "TIKTOKEN_CACHE_DIR" in os .environ :
3132 cache_dir = os .environ ["TIKTOKEN_CACHE_DIR" ]
3233 elif "DATA_GYM_CACHE_DIR" in os .environ :
3334 cache_dir = os .environ ["DATA_GYM_CACHE_DIR" ]
3435 else :
3536 cache_dir = os .path .join (tempfile .gettempdir (), "data-gym-cache" )
37+ user_specified_cache = False
3638
3739 if cache_dir == "" :
3840 # disable caching
@@ -47,11 +49,16 @@ def read_file_cached(blobpath: str) -> bytes:
4749
4850 contents = read_file (blobpath )
4951
50- os .makedirs (cache_dir , exist_ok = True )
51- tmp_filename = cache_path + "." + str (uuid .uuid4 ()) + ".tmp"
52- with open (tmp_filename , "wb" ) as f :
53- f .write (contents )
54- os .rename (tmp_filename , cache_path )
52+ try :
53+ os .makedirs (cache_dir , exist_ok = True )
54+ tmp_filename = cache_path + "." + str (uuid .uuid4 ()) + ".tmp"
55+ with open (tmp_filename , "wb" ) as f :
56+ f .write (contents )
57+ os .rename (tmp_filename , cache_path )
58+ except OSError :
59+ # don't raise if we can't write to the default cache, e.g. issue #75
60+ if user_specified_cache :
61+ raise
5562
5663 return contents
5764
0 commit comments