Repository navigation
fix: report the missing text instead of crashing when stdin is closed - #312
Open
qianeric-backup wants to merge 1 commit into
Open
qianeric-backup wants to merge 1 commit into
qianeric-backup wants to merge 1 commit into
Conversation
With fd 0 closed sys.stdin is None, and click returns that unchanged, so get_text() calls .isatty() on None and pywhat dies with an AttributeError traceback. main() already reports the same condition, that there is no text to look at, with a sentence and exit 1. Look the stream up once and treat None as no piped input, so control falls through to that message.
qianeric-backup
marked this pull request as ready for review
November 28, 2026 00:53
|
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.



With stdin closed,
pywhat -dbdies with a traceback instead of saying what it needs:When fd 0 is closed,
sys.stdinisNone, and click's_force_correct_text_streamreturns it unchanged rather than substituting an empty stream, soget_text()calls.isatty()onNone.main()already has the answer for this condition — "there is no text to look at" — three lines of code that print a sentence and exit 1:read_stdin(), the next function down, also knows that stdin is not always a usable binary stream and says so in a comment.Change
get_text()looks the stream up once and treatsNoneas "no piped input", so control falls through to thatsys.exit:Result:
Verification
Two tests in a new
tests/test_closed_stdin.py: stdin closed outright, and stdin closed while still a pipe. They callmain.main(standalone_mode=False)rather than going throughCliRunner, becauseCliRunner.isolation()installs aBytesIOassys.stdinand would hide the bug. Each closes fd 0 for real and restores it afterwards.main2 failed—AttributeError: 'NoneType' object has no attribute 'isatty'atwhat.py:1052 passedFull suite:
2303 passed, 3 skippedbefore,2309 passed, 3 skippedafter, both exit 0,comm -3over theFAILEDlists empty — nothing was failing onmain. The collected-id difference is those two tests plus the four per-file lint checks the new file adds.Sixteen legal invocations (
argvtext,-db,--json,--format,--rarity 0:,-pt,--stream,--top --json,--include,--names,--tags, a file, piped input with and without-, and--interactive) produce 659 lines of output that match byte for byte before and after. Two of them cover the other branches of the changed condition.Lint at the versions pinned in the repository, in a clean environment so that no extra plugins interfere:
black 23.12.1,isort 5.13.2,flake8 7.5.0,mypy 1.14.1.Separate
pywhat --interactive <&-still ends inRuntimeError: input(): lost sys.stdinfromcmd.py. Different code path, happy to do it separately if you want it.