Skip to content

fix: report the missing text instead of crashing when stdin is closed - #312

Open
qianeric-backup wants to merge 1 commit into
bee-san:mainfrom
qianeric-backup:closed-stdin-text-expected
Open

qianeric-backup wants to merge 1 commit into
bee-san:mainfrom
qianeric-backup:closed-stdin-text-expected

Conversation

@qianeric-backup

Copy link
Copy Markdown

With stdin closed, pywhat -db dies with a traceback instead of saying what it needs:

$ pywhat -db <&-
  File ".../click/core.py", line 2322, in process_value
    value = self.callback(ctx, self, value)
  File ".../pywhat/what.py", line 105, in get_text
    and not click.get_text_stream("stdin").isatty()
AttributeError: 'NoneType' object has no attribute 'isatty'

When fd 0 is closed, sys.stdin is None, and click's _force_correct_text_stream returns it unchanged rather than substituting an empty stream, so get_text() calls .isatty() on None.

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:

    if not kwargs["text_input"] and not kwargs["interactive"]:
        sys.exit("Text input expected. Run 'pywhat --help' for help")

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 treats None as "no piped input", so control falls through to that sys.exit:

+    stdin = click.get_text_stream("stdin")
...
-        and not click.get_text_stream("stdin").isatty()
+        and stdin is not None
+        and not stdin.isatty()

Result:

$ pywhat -db <&-
Text input expected. Run 'pywhat --help' for help

Verification

Two tests in a new tests/test_closed_stdin.py: stdin closed outright, and stdin closed while still a pipe. They call main.main(standalone_mode=False) rather than going through CliRunner, because CliRunner.isolation() installs a BytesIO as sys.stdin and would hide the bug. Each closes fd 0 for real and restores it afterwards.

tree new tests
main 2 failed — AttributeError: 'NoneType' object has no attribute 'isatty' at what.py:105
with this patch 2 passed

Full suite: 2303 passed, 3 skipped before, 2309 passed, 3 skipped after, both exit 0, comm -3 over the FAILED lists empty — nothing was failing on main. The collected-id difference is those two tests plus the four per-file lint checks the new file adds.

Sixteen legal invocations (argv text, -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 in RuntimeError: input(): lost sys.stdin from cmd.py. Different code path, happy to do it separately if you want it.

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
qianeric-backup marked this pull request as ready for review November 28, 2026 00:53
@sonarqubecloud

Copy link
Copy Markdown

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant