Repository navigation
capture: report which test created a closed capture stream - #15215
Closed
isHeSatoshi wants to merge 1 commit into
Closed
isHeSatoshi wants to merge 1 commit into
isHeSatoshi wants to merge 1 commit into
Conversation
Writing to a stream which pytest closed (for example a ``sys.stdout`` obtained from the ``capsys`` fixture and kept alive by a logging handler) now raises an error which names the test that created the stream, instead of a bare ``I/O operation on closed file``.
Member
|
unattended agent |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The problem
When a test keeps a reference to a stream which pytest captures — the classic case being a
logging.StreamHandler(sys.stdout)installed by one test and never removed — writing tothat stream from a later test fails with:
That says nothing about where the stream came from, so the leak is hard to track down.
#5743 has the original report, and the agreed direction there (from @nicoddemus) is:
The fix
EncodedFile(fd capture: the default--capture=fdandcapfd) andCaptureIO(sys capture:
capsysand--capture=sys) now share a_CaptureStreambase which knowswhich test created the stream, and turns a write to an already-closed stream into:
CaptureFixture._start()passesself.request.node.nodeiddown to the capture class,which forwards it to the stream it creates. Global (session-wide) captures have no owning
test, so their message keeps the general wording without the nodeid. Encoding errors are
ValueErrorsubclasses as well, so they are re-raised unchanged —self.closedis whatdistinguishes the two cases.
This is complementary to #14981, which warns at startup on Windows when a handler holds a
stream pytest replaced before it took over; this change improves the error at the point
where the write actually fails, on every platform.
Tests
testing/test_capture.py::TestClosedCaptureStream— closedCaptureIOwith and withouta nodeid, closed
EncodedFile, and that encoding errors are left alone.testing/test_capture.py::test_write_to_stream_of_finished_test[capsys|capfd]— theend-to-end version: a test leaks
sys.stdout, a later test writes to it and checks themessage names the first test.
All five of these fail on
mainwithout thesrc/_pytest/capture.pychange (verified byreverting that file), and pass with it.
Commands run against this branch:
python -m pytest testing/test_capture.py testing/test_subtests.py -q→172 passed, 3 skipped, 1 xfailedpython -m pytest testing/test_terminal.py testing/test_runner.py testing/logging -q→398 passed, 2 skipped, 1 xfailed, with one failure intesting/logging/test_reporting.py::test_messages_loggedcaused by the machine runningout of disk space mid-run; that test passes when re-run on its own.
python -m mypy src/_pytest/capture.py testing/test_capture.py→Success: no issues foundruff checkandruff format --checkon both files → cleanchangelog/5743.improvement.rstis included.AI assistance (disclosure per CONTRIBUTING.rst)
This change was prepared with an AI coding agent (Factory Droid) on behalf of
@isHeSatoshi, who is submitting it and will respond to review feedback. Everything above
was verified by actually running the commands shown against this branch rather than being
asserted. The agent is credited here rather than in a
Co-authored-bytrailer; happy toadd a trailer to the commits if that is preferred.
Fixes #5743