Skip to content

narrow is_stdin() to documented fileno() failure modes#1870

Open
HrachShah wants to merge 1 commit into
httpie:masterfrom
HrachShah:fix/uploads-is-stdin-narrow
Open

narrow is_stdin() to documented fileno() failure modes#1870
HrachShah wants to merge 1 commit into
httpie:masterfrom
HrachShah:fix/uploads-is-stdin-narrow

Conversation

@HrachShah

@HrachShah HrachShah commented Jun 16, 2026

Copy link
Copy Markdown

The bare except Exception in is_stdin() (httpie/uploads.py) was too broad: it would swallow every error that fileno() could raise, including ones that signal real bugs in the function itself (NameError, MemoryError, etc.) and ones that no caller expects is_stdin() to handle. KeyboardInterrupt and SystemExit both inherit from BaseException so they still propagate, but the policy in this codebase (and the policy followed in several other recent PRs) is to narrow broad handlers to the documented failure modes of the underlying call.

The actual documented failure modes for file.fileno() on a file-like object are:

  • AttributeError — object has no fileno attribute at all (plain BytesIO, custom streams, anything that doesn't expose a real fd).
  • io.UnsupportedOperation — stream types like StringIO that explicitly disallow fileno(). This is a subclass of OSError, so catching OSError transitively covers it.
  • ValueError — "I/O operation on closed file" for closed file objects (a closed tempfile, a closed BytesIO, etc.).
  • OSError — low-level errors on a corrupted/closed fd (EBADF, EINVAL, etc.).

Narrowed the handler to (OSError, ValueError, AttributeError) and kept the same return False semantics for the not-stdin case. Added a short comment listing the four documented failure modes.

Added a TestIsStdin class to tests/test_uploads.py with five unit tests: no fileno attribute, closed file, StringIO, BytesIO, and a real os.pipe() fd that is not stdin. The last test patches sys.stdin via monkeypatch with a surrogate so the comparison side is deterministic; pytest's own capture machinery replaces sys.stdin with a DontReadFromInput whose fileno() raises io.UnsupportedOperation, which would otherwise make the test depend on the runner.

The bare 'except Exception' in is_stdin() swallowed every error that
fileno() could raise, including ones that signal real bugs elsewhere
(NameError, MemoryError, etc.) and ones that no caller expects the
function to handle (KeyboardInterrupt inherits from BaseException so
it still propagates, but SystemExit inherits from BaseException too
and would also be caught — which we never wanted).

The actual documented failure modes for file.fileno() on a
file-like object are:

  - AttributeError: object has no fileno attribute at all
    (plain BytesIO, custom streams, anything that doesn't expose
    a real fd);
  - io.UnsupportedOperation: stream types like StringIO that
    explicitly disallow fileno() — this is a subclass of OSError,
    so catching OSError transitively covers it;
  - ValueError: 'I/O operation on closed file' for closed
    file objects (a closed tempfile, a closed BytesIO, etc.);
  - OSError: low-level errors on a corrupted/closed fd
    (EBADF, EINVAL, etc.).

Narrow the handler to (OSError, ValueError, AttributeError), keep
the same return-False semantics for the not-stdin case, and add a
short comment listing the four documented failure modes.

Adds a TestIsStdin class to tests/test_uploads.py with five unit
tests: no fileno attribute, closed file, StringIO, BytesIO, and
a real os.pipe() fd that is not stdin. The last test patches
sys.stdin via monkeypatch with a surrogate so the comparison
side is deterministic; pytest's own capture machinery replaces
sys.stdin with a DontReadFromInput whose fileno() raises
io.UnsupportedOperation, which would otherwise make the test
depend on the runner.
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