@@ -27,12 +27,14 @@ def read_file(blobpath: str) -> bytes:
27
27
28
28
29
29
def read_file_cached (blobpath : str ) -> bytes :
30
+ user_specified_cache = True
30
31
if "TIKTOKEN_CACHE_DIR" in os .environ :
31
32
cache_dir = os .environ ["TIKTOKEN_CACHE_DIR" ]
32
33
elif "DATA_GYM_CACHE_DIR" in os .environ :
33
34
cache_dir = os .environ ["DATA_GYM_CACHE_DIR" ]
34
35
else :
35
36
cache_dir = os .path .join (tempfile .gettempdir (), "data-gym-cache" )
37
+ user_specified_cache = False
36
38
37
39
if cache_dir == "" :
38
40
# disable caching
@@ -47,11 +49,16 @@ def read_file_cached(blobpath: str) -> bytes:
47
49
48
50
contents = read_file (blobpath )
49
51
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
55
62
56
63
return contents
57
64
0 commit comments