Skip to content

Warn when writing or closing a cache file fails - #15044

Open
barveP wants to merge 1 commit into
pytest-dev:mainfrom
barveP:codex/pytest-second-fix
Open

barveP wants to merge 1 commit into
pytest-dev:mainfrom
barveP:codex/pytest-second-fix

Conversation

@barveP

@barveP barveP commented Sep 16, 2026

Copy link
Copy Markdown

Fixes #15043.

cache.set() already warns when opening a cache file fails, but an OSError from writing or closing it currently escapes. Move the existing write into the same error handler so these failures also produce PytestCacheWarning.

The regression test covers open, write, and close failures. The write and close cases fail before the fix; all three pass afterward. A separate reproduction using a real file-size limit also fails on pytest 9.1.1 and unmodified main, then produces warnings with this fix.

Checks on macOS:

  • Full Python 3.13 suite, tox -e py313 -- -q: 4,607 passed, 51 skipped, 15 xfailed, 5 xpassed.
  • Cache tests on Python 3.12 and 3.13: 68 passed on each.
  • tox -e linting: passed.

JSON serialization and warning filters keep their existing behavior.

Co-developed by Poorva Barve and Codex.

Co-authored-by: Codex <noreply@openai.com>
@psf-chronographer psf-chronographer Bot added the bot:chronographer:provided (automation) changelog entry is part of PR label Sep 16, 2026

@RonnyPfannschmidt RonnyPfannschmidt left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good

data = json.dumps(value, ensure_ascii=False, indent=2)
try:
f = path.open("w", encoding="UTF-8")
with path.open("w", encoding="UTF-8") as f:

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nitpick we now can use path.write_text

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bot:chronographer:provided (automation) changelog entry is part of PR

Projects

None yet

Development

Successfully merging this pull request may close these issues.

cache.set() raises on write and close errors instead of warning

2 participants