From 17170a6736a01d0f4a59fde2cad34a12c2fa7409 Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Mon, 8 Feb 2021 20:30:22 +0000 Subject: [PATCH] Add `Base.isdone(itr::EachLine, state...)` (#39562) With this method, `isempty` won't consume values from the iterator (See #27412). --- base/io.jl | 2 ++ test/read.jl | 9 +++++++++ 2 files changed, 11 insertions(+) diff --git a/base/io.jl b/base/io.jl index eb9eff598f712..e15551cfbdfa7 100644 --- a/base/io.jl +++ b/base/io.jl @@ -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 diff --git a/test/read.jl b/test/read.jl index b63658d225111..b7e8c2260a4f9 100644 --- a/test/read.jl +++ b/test/read.jl @@ -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