Skip to content

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

Merged
RonnyPfannschmidt merged 2 commits into
pytest-dev:mainfrom
barveP:codex/pytest-second-fix
Sep 18, 2026
Merged

RonnyPfannschmidt merged 2 commits into
pytest-dev:mainfrom
barveP:codex/pytest-second-fix

Conversation

@barveP

@barveP barveP commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

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

Comment thread src/_pytest/cacheprovider.py Outdated
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

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ah yes! switched to path.write_text

Co-authored-by: Codex <noreply@openai.com>
@RonnyPfannschmidt
RonnyPfannschmidt merged commit 6a0de9b into pytest-dev:main Sep 18, 2026
36 checks passed
mtcd001-tech added a commit to mtcd001-tech/pytest that referenced this pull request Sep 20, 2026
Warn when writing or closing a cache file fails (pytest-dev#15044)
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