Skip to content

Commit

Permalink
Sync codebase
Browse files Browse the repository at this point in the history
  • Loading branch information
hauntsaninja committed Dec 3, 2023
1 parent 6267f91 commit 9e79899
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 5 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ This is the changelog for the open source version of tiktoken.
## [v0.5.2]
- Build wheels for Python 3.12
- Update version of PyO3 to allow multiple imports
- Avoid permission errors when using default cache logic

## [v0.5.1]
- Add `encoding_name_for_model`, undo some renames to variables that are implementation details
Expand Down
17 changes: 12 additions & 5 deletions tiktoken/load.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ def read_file(blobpath: str) -> bytes:


def read_file_cached(blobpath: str) -> bytes:
user_specified_cache = True
if "TIKTOKEN_CACHE_DIR" in os.environ:
cache_dir = os.environ["TIKTOKEN_CACHE_DIR"]
elif "DATA_GYM_CACHE_DIR" in os.environ:
cache_dir = os.environ["DATA_GYM_CACHE_DIR"]
else:
cache_dir = os.path.join(tempfile.gettempdir(), "data-gym-cache")
user_specified_cache = False

if cache_dir == "":
# disable caching
Expand All @@ -47,11 +49,16 @@ def read_file_cached(blobpath: str) -> bytes:

contents = read_file(blobpath)

os.makedirs(cache_dir, exist_ok=True)
tmp_filename = cache_path + "." + str(uuid.uuid4()) + ".tmp"
with open(tmp_filename, "wb") as f:
f.write(contents)
os.rename(tmp_filename, cache_path)
try:
os.makedirs(cache_dir, exist_ok=True)
tmp_filename = cache_path + "." + str(uuid.uuid4()) + ".tmp"
with open(tmp_filename, "wb") as f:
f.write(contents)
os.rename(tmp_filename, cache_path)
except OSError:
# don't raise if we can't write to the default cache, e.g. issue #75
if user_specified_cache:
raise

return contents

Expand Down

0 comments on commit 9e79899

Please sign in to comment.