Repository: psf/black
File: src/black/cache.py (Python)

Code before:

    @classmethod
    def read(cls, mode: Mode) -> Self:
        """Read the cache if it exists and is well formed.

        If it is not well formed, the call to write later should
        resolve the issue.
        """
        cache_file = get_cache_file(mode)
        if not cache_file.exists():
            return cls(mode, cache_file)

        with cache_file.open("rb") as fobj:
            try:
                file_data: Dict[str, FileData] = pickle.load(fobj)
            except (pickle.UnpicklingError, ValueError, IndexError):
                return cls(mode, cache_file)

        return cls(mode, cache_file, file_data)

    @staticmethod
    def hash_digest(path: Path) -> str:
        """Return hash digest for path."""

        with open(path, "rb") as fp:
            data = fp.read()
        return hashlib.sha256(data).hexdigest()

    @staticmethod
    def get_file_data(path: Path) -> FileData:
        """Return file data for path."""

        stat = path.stat()
        hash = Cache.hash_digest(path)
        return FileData(stat.st_mtime, stat.st_size, hash)

    def is_changed(self, source: Path) -> bool:
        """Check if source has changed compared to cached version."""
        res_src = source.resolve()
        old = self.file_data.get(str(res_src))
        if old is None:
            return True

        st = res_src.stat()
        if st.st_size != old.st_size:
            return True
        if int(st.st_mtime) != int(old.st_mtime):
            new_hash = Cache.hash_digest(res_src)
            if new_hash != old.hash:
                return True
        return False

Review comment (suggestion):
```suggestion
        data = path.read_bytes()
```

Code after:

    @classmethod
    def read(cls, mode: Mode) -> Self:
        """Read the cache if it exists and is well formed.

        If it is not well formed, the call to write later should
        resolve the issue.
        """
        cache_file = get_cache_file(mode)
        if not cache_file.exists():
            return cls(mode, cache_file)

        with cache_file.open("rb") as fobj:
            try:
                file_data: Dict[str, FileData] = pickle.load(fobj)
            except (pickle.UnpicklingError, ValueError, IndexError):
                return cls(mode, cache_file)

        return cls(mode, cache_file, file_data)

    @staticmethod
    def hash_digest(path: Path) -> str:
        """Return hash digest for path."""

        data = path.read_bytes()
        return hashlib.sha256(data).hexdigest()

    @staticmethod
    def get_file_data(path: Path) -> FileData:
        """Return file data for path."""

        stat = path.stat()
        hash = Cache.hash_digest(path)
        return FileData(stat.st_mtime, stat.st_size, hash)

    def is_changed(self, source: Path) -> bool:
        """Check if source has changed compared to cached version."""
        res_src = source.resolve()
        old = self.file_data.get(str(res_src))
        if old is None:
            return True

        st = res_src.stat()
        if st.st_size != old.st_size:
            return True
        if int(st.st_mtime) != int(old.st_mtime):
            new_hash = Cache.hash_digest(res_src)
            if new_hash != old.hash:
                return True
        return False