From 1e7e5a81613f94f382aa8b6abff36c4bb9152e95 Mon Sep 17 00:00:00 2001 From: Colin Caine Date: Sun, 14 Feb 2021 21:52:17 +0000 Subject: [PATCH] Add isdone(itr::ReadEachIterator, state...) 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 --- base/io.jl | 2 ++ test/read.jl | 3 +-- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/base/io.jl b/base/io.jl index e15551cfbdfa7..45faa621f8fa6 100644 --- a/base/io.jl +++ b/base/io.jl @@ -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 diff --git a/test/read.jl b/test/read.jl index b7e8c2260a4f9..aa897342393a1 100644 --- a/test/read.jl +++ b/test/read.jl @@ -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)