Skip to content

Commit

Permalink
Add isdone(itr::ReadEachIterator, state...) (#39662)
Browse files Browse the repository at this point in the history
This prevents `isempty` and friends from advancing the iterator when
they shouldn't.

Note: `collect(readeach(io, T))` can throw an exception if `io` isn't a
multiple of the right size. `isempty` will still return `false`, even if
the next call to `iterate` will throw an exception.

Similar to: #27412
  • Loading branch information
cmcaine committed Feb 15, 2021
1 parent 631f257 commit 2e80fc8
Show file tree
Hide file tree
Showing 2 changed files with 3 additions and 2 deletions.
2 changes: 2 additions & 0 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1059,6 +1059,8 @@ eltype(::Type{ReadEachIterator{T}}) where T = T

IteratorSize(::Type{<:ReadEachIterator}) = SizeUnknown()

isdone(itr::ReadEachIterator, state...) = eof(itr.stream)

# IOStream Marking
# Note that these functions expect that io.mark exists for
# the concrete IO type. This may not be true for IO types
Expand Down
3 changes: 1 addition & 2 deletions test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -618,8 +618,7 @@ let p = Pipe()
close(p)
end

@testset "issue #27412" begin
itr = eachline(IOBuffer("a"))
@testset "issue #27412" for itr in [eachline(IOBuffer("a")), readeach(IOBuffer("a"), Char)]
@test !isempty(itr)
# check that the earlier isempty did not consume the iterator
@test !isempty(itr)
Expand Down

0 comments on commit 2e80fc8

Please sign in to comment.