Atomically write cache files (#674)
This commit is contained in:
parent
bc7e5c949f
commit
4d3107233f
9
black.py
9
black.py
@ -14,6 +14,7 @@
|
||||
import re
|
||||
import signal
|
||||
import sys
|
||||
import tempfile
|
||||
import tokenize
|
||||
from typing import (
|
||||
Any,
|
||||
@ -3647,11 +3648,11 @@ def write_cache(
|
||||
"""Update the cache file."""
|
||||
cache_file = get_cache_file(line_length, mode)
|
||||
try:
|
||||
if not CACHE_DIR.exists():
|
||||
CACHE_DIR.mkdir(parents=True)
|
||||
CACHE_DIR.mkdir(parents=True, exist_ok=True)
|
||||
new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
|
||||
with cache_file.open("wb") as fobj:
|
||||
pickle.dump(new_cache, fobj, protocol=pickle.HIGHEST_PROTOCOL)
|
||||
with tempfile.NamedTemporaryFile(dir=str(cache_file.parent), delete=False) as f:
|
||||
pickle.dump(new_cache, f, protocol=pickle.HIGHEST_PROTOCOL)
|
||||
os.replace(f.name, cache_file)
|
||||
except OSError:
|
||||
pass
|
||||
|
||||
|
@ -13,7 +13,9 @@
|
||||
"""
|
||||
|
||||
# Python imports
|
||||
import os.path
|
||||
import pickle
|
||||
import tempfile
|
||||
|
||||
# Local imports
|
||||
from . import token
|
||||
@ -86,8 +88,9 @@ def __init__(self):
|
||||
|
||||
def dump(self, filename):
|
||||
"""Dump the grammar tables to a pickle file."""
|
||||
with open(filename, "wb") as f:
|
||||
with tempfile.NamedTemporaryFile(dir=os.path.dirname(filename), delete=False) as f:
|
||||
pickle.dump(self.__dict__, f, pickle.HIGHEST_PROTOCOL)
|
||||
os.replace(f.name, filename)
|
||||
|
||||
def load(self, filename):
|
||||
"""Load the grammar tables from a pickle file."""
|
||||
|
Loading…
Reference in New Issue
Block a user