Skip to content

Commit

Permalink
Add Base.isdone(itr::EachLine, state...)
Browse files Browse the repository at this point in the history
With this method, `isempty` won't consume values from the iterator (See JuliaLang#27412).
  • Loading branch information
cmcaine committed Feb 7, 2021
1 parent 59808cb commit 5ef6ffa
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 0 deletions.
2 changes: 2 additions & 0 deletions base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1023,6 +1023,8 @@ eltype(::Type{<:EachLine}) = String

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

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

struct ReadEachIterator{T, IOT <: IO}
stream::IOT
end
Expand Down
9 changes: 9 additions & 0 deletions test/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -617,3 +617,12 @@ let p = Pipe()
wait(t)
close(p)
end

@testset "issue #27412" begin
itr = eachline(IOBuffer("a"))
@test !isempty(itr)
# check that the earlier isempty did not consume the iterator
@test !isempty(itr)
first(itr) # consume the iterator
@test isempty(itr) # now it is empty
end

0 comments on commit 5ef6ffa

Please sign in to comment.