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