Skip to content

Commit ede6486

Browse files
authored
Add Base.isdone(itr::EachLine, state...) (#39562)
With this method, `isempty` won't consume values from the iterator (See #27412).
1 parent fd43841 commit ede6486

2 files changed

Lines changed: 11 additions & 0 deletions

File tree

base/io.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1023,6 +1023,8 @@ eltype(::Type{<:EachLine}) = String
10231023

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

1026+
isdone(itr::EachLine, state...) = eof(itr.stream)
1027+
10261028
struct ReadEachIterator{T, IOT <: IO}
10271029
stream::IOT
10281030
end

test/read.jl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,3 +617,12 @@ let p = Pipe()
617617
wait(t)
618618
close(p)
619619
end
620+
621+
@testset "issue #27412" begin
622+
itr = eachline(IOBuffer("a"))
623+
@test !isempty(itr)
624+
# check that the earlier isempty did not consume the iterator
625+
@test !isempty(itr)
626+
first(itr) # consume the iterator
627+
@test isempty(itr) # now it is empty
628+
end

0 commit comments

Comments
 (0)