narrow is_stdin() to documented fileno() failure modes#1870
Open
HrachShah wants to merge 1 commit into
Open
Conversation
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.
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 bare
except Exceptioninis_stdin()(httpie/uploads.py) was too broad: it would swallow every error thatfileno()could raise, including ones that signal real bugs in the function itself (NameError,MemoryError, etc.) and ones that no caller expectsis_stdin()to handle.KeyboardInterruptandSystemExitboth inherit fromBaseExceptionso 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 nofilenoattribute at all (plainBytesIO, custom streams, anything that doesn't expose a real fd).io.UnsupportedOperation— stream types likeStringIOthat explicitly disallowfileno(). This is a subclass ofOSError, so catchingOSErrortransitively covers it.ValueError— "I/O operation on closed file" for closed file objects (a closedtempfile, a closedBytesIO, etc.).OSError— low-level errors on a corrupted/closed fd (EBADF,EINVAL, etc.).Narrowed the handler to
(OSError, ValueError, AttributeError)and kept the samereturn Falsesemantics for the not-stdin case. Added a short comment listing the four documented failure modes.Added a
TestIsStdinclass totests/test_uploads.pywith five unit tests: nofilenoattribute, closed file,StringIO,BytesIO, and a realos.pipe()fd that is not stdin. The last test patchessys.stdinviamonkeypatchwith a surrogate so the comparison side is deterministic; pytest's own capture machinery replacessys.stdinwith aDontReadFromInputwhosefileno()raisesio.UnsupportedOperation, which would otherwise make the test depend on the runner.