Fix echo_via_pager closing sys.stdout when used with CliRunner#3494
Closed
jesustorres-code wants to merge 1 commit into
Closed
Fix echo_via_pager closing sys.stdout when used with CliRunner#3494jesustorres-code wants to merge 1 commit into
jesustorres-code wants to merge 1 commit into
Conversation
MaybeStripAnsi wraps the underlying binary buffer (sys.stdout.buffer) in an io.TextIOWrapper subclass. TextIOWrapper takes ownership of its buffer and closes it on GC, which silently closes sys.stdout. This caused CliRunner.invoke() to raise ValueError: I/O operation on closed file when it tried to flush sys.stdout in its finally block after the command ran. Fix: detach the buffer from MaybeStripAnsi after flushing so the buffer is not closed when the wrapper is garbage collected. Fixes pallets#3449
Member
|
AI junk, ignored existing open PR |
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.
Fixes #3449
Root cause
MaybeStripAnsisubclassesio.TextIOWrapperand wrapssys.stdout.buffer.io.TextIOWrappertakes ownership of its underlying buffer and closes it when the wrapper is closed or garbage collected.After
echo_via_pagerreturns and thewith get_pager_file()context exits, theMaybeStripAnsiinstance goes out of scope. When Python GC collects it,TextIOWrapper.__del__closes the buffer — which effectively closessys.stdout. ThenCliRunner.invoke'sfinallyblock callssys.stdout.flush()and getsValueError: I/O operation on closed file.Fix
After
stream.flush()inget_pager_file'sfinallyblock, callstream.detach()when the stream is aMaybeStripAnsi.detach()dissociates theTextIOWrapperfrom the buffer without closing it, sosys.stdout.buffer(andsys.stdout) remain open.Test
Added
test_echo_via_pager_in_cli_runnertotests/test_testing.py— a minimal reproduction of the reported bug that previously crashed withValueError.