Skip to content

Commit 9e79899

Browse files
committed
Sync codebase
1 parent 6267f91 commit 9e79899

File tree

2 files changed

+13
-5
lines changed

2 files changed

+13
-5
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ This is the changelog for the open source version of tiktoken.
55
## [v0.5.2]
66
- Build wheels for Python 3.12
77
- Update version of PyO3 to allow multiple imports
8+
- Avoid permission errors when using default cache logic
89

910
## [v0.5.1]
1011
- Add `encoding_name_for_model`, undo some renames to variables that are implementation details

tiktoken/load.py

+12-5
Original file line numberDiff line numberDiff line change
@@ -27,12 +27,14 @@ def read_file(blobpath: str) -> bytes:
2727

2828

2929
def 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

Comments
 (0)