Commit 03766f51 authored by Matt VanEseltine's avatar Matt VanEseltine Committed by Łukasz Langa
Browse files

Do not load incompatible cache (#875) (#1034)

A black cache created in Python 3.8 throws an unhandled
ValueError in earlier versions. This is because 3.6 does
not recognize the pickle protocol used as default in 3.8.
Accordingly, this commit:

  - Fixes read_cache to return an empty cache instead.

  - Changes the pickle protocol to 4 as the highest protocol
    fully supported by black's supported Python versions.
parent 000147c0
Loading
Loading
Loading
Loading
+2 −2
Changes for black.py: 2 added lines, 2 removed lines.
Original line number Diff line number Diff line
@@ -4006,7 +4006,7 @@ def read_cache(mode: FileMode) -> Cache:
    with cache_file.open("rb") as fobj:
        try:
            cache: Cache = pickle.load(fobj)
        except pickle.UnpicklingError:
        except (pickle.UnpicklingError, ValueError):
            return {}

    return cache
@@ -4041,7 +4041,7 @@ def write_cache(cache: Cache, sources: Iterable[Path], mode: FileMode) -> None:
        CACHE_DIR.mkdir(parents=True, exist_ok=True)
        new_cache = {**cache, **{src.resolve(): get_cache_info(src) for src in sources}}
        with tempfile.NamedTemporaryFile(dir=str(cache_file.parent), delete=False) as f:
            pickle.dump(new_cache, f, protocol=pickle.HIGHEST_PROTOCOL)
            pickle.dump(new_cache, f, protocol=4)
        os.replace(f.name, cache_file)
    except OSError:
        pass