-
Notifications
You must be signed in to change notification settings - Fork 26
fix eof issues and add tests #170
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
| if eof && sloweof(stream) | ||
| throw(EOFError()) | ||
| if eof(stream) | ||
| ready_to_read!(stream) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be implied by eof
| ready_to_read!(stream) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change means an EOFError is thrown when reading a byte from a closed stream.
This seems to match what happens with IOStream
julia> p, io = mktemp()
("/tmp/jl_GKa3GO", IOStream(<fd 21>))
julia> close(io)
julia> read(io, UInt8)
ERROR: EOFError: read end of file
Stacktrace:
[1] read(s::IOStream, ::Type{UInt8})
@ Base ./iostream.jl:400
[2] top-level scope
@ REPL[43]:1But doesn't match with what IOBuffer does:
julia> stream = IOBuffer();
julia> close(stream)
julia> read(stream, UInt8)
ERROR: ArgumentError: read failed, IOBuffer is not readable
Stacktrace:
[1] _throw_not_readable()
@ Base ./iobuffer.jl:165
[2] read(from::IOBuffer, ::Type{UInt8})
@ Base ./iobuffer.jl:218
[3] top-level scope
@ REPL[49]:1There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
True, though I think eof could optionally throw a state-exception in that case also sometimes. The ambiguity occurs when a stream is ambiguously readable or writable, which sometimes cannot be avoided. So seekable files usually close both ends at once (and eof is a transient condition, which changes to an error after calling close), whereas (non-seekable) streams the eof is a property of reading and isopen is a property of writing
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm undoing the change because I like how IOBuffer is handling this better than IOStream.
For example, I think read(io, String) after close should error instead of returning an empty string:
julia> p, io = mktemp()
julia> close(io)
julia> read(io, String)
""julia> stream = IOBuffer();
julia> close(stream)
julia> read(stream, String)
ERROR: ArgumentError: read failed, IOBuffer is not readable
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, but that is already eof’s job to throw when there is a state violation, so it should not be necessary for read to also redundantly check
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This would break this test that was added in #32:
TranscodingStreams.jl/test/codecnoop.jl
Lines 283 to 287 in 92adcd8
| stream = NoopStream(IOBuffer("")) | |
| unsafe_write(stream, C_NULL, 0) | |
| @test eof(stream) # write | |
| close(stream) | |
| @test eof(stream) # close |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This fixes the issues with
eofdiscussed in #168 (comment)If
eofreturns false it is safe to read a byte. This means when the stream is in:writemode,eofmust return true.Also, if the stream is stopped when writing (
stop_on_end=true)eofshould not suddenly change from true to false. To fix this I added a:donestate.