Handle empty cache files#5192
Conversation
Add test for empty cache file in black formatter
|
Thanks! Please add a changelog entry. |
|
Added the requested changelog entry in fcf00f6. |
|
Could you use one of the pre-existing categories instead of adding a new one? (Caching changes have gone in Configuration in the past) |
|
Moved the entry under the existing Configuration category in 66e62ac. |
DTiming24
left a comment
There was a problem hiding this comment.
Catching EOFError seems like the right symmetry with malformed pickle handling. One subtle edge case: if an external process truncates the file while Black is running, silently treating it as a cache miss is probably preferable to surfacing cache corruption to the user. The regression test covers the repair path well.
|
Nice fix. Since empty cache files now fall back the same way as other malformed cache data, a regression test for a zero-byte file created by an interrupted write would help lock in the new behavior. |
|
Thanks for taking a look. The current PR already has a zero-byte cache regression test: |
Summary
Treat empty or truncated cache files the same way as other malformed cache files.
Details
Cache.read()already ignores malformed pickle data so a later cache write can repair the file, but an empty cache file raisesEOFError, which was not caught. This can happen if cache creation is interrupted or an external process truncates the file.This change catches
EOFErrorwhile reading the cache and adds a regression test for an empty cache file.Test
python -m pytest tests/test_black.py::TestCaching::test_cache_empty_file tests/test_black.py::TestCaching::test_cache_broken_file -q