Commit 4d310723 authored by Anthony Sottile's avatar Anthony Sottile Committed by Jelle Zijlstra
Browse files

Atomically write cache files (#674)

parent bc7e5c94
Loading
Loading
Loading
Loading
+5 −4
Changes for black.py: 5 added lines, 4 removed lines.
Original line number Diff line number Diff line
@@ -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

+4 −1
Changes for blib2to3/pgen2/grammar.py: 4 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -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."""