Skip to content

isempty(::EachLine) expends a line #27412

@oxinabox

Description

@oxinabox

Eachline is (almost?) always a stateful iterator.
This means the default isempty which called iterate will consume a line if there is one to consume.

Sometimes (always?) in the use of Eachline (but not more generally), a defensive deepcopy can protect against that.

This is in 0.7
I feel like I (or someone else) opened a similar issue for this in 0.6
but I could be mis-remembering.

Downside of this fix, is that I'm not sure it works for all uses of Eachline.
It works for IOBuffer (and I think IOStream) because there is a pointer integer that is copied.
But if it was consuming unbuffered network data, then not possible because what is gone is gone.
(idk that it is possible to access unbuffered network data in julia)

Minimum Breaking Example

julia> data = IOBuffer("A\nB\nC\nD")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=7, maxsize=Inf, ptr=1, mark=-1)

julia> itr  = eachline(data)
Base.EachLine(IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=7, maxsize=Inf, ptr=1, mark=-1), getfield(Base, Symbol("##289#292"))(), false)

julia> isempty(itr)
false

julia> collect(itr)
3-element Array{String,1}:
 "B"
 "C"
 "D"

Defensive-copy work-around

julia> data = IOBuffer("A\nB\nC\nD")
IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=7, maxsize=Inf, ptr=1, mark=-1)

julia> itr = eachline(data)
Base.EachLine(IOBuffer(data=UInt8[...], readable=true, writable=false, seekable=true, append=false, size=7, maxsize=Inf, ptr=1, mark=-1), getfield(Base, Symbol("##289#292"))(), false)

julia> isempty(deepcopy(itr))
false

julia> collect(itr)
4-element Array{String,1}:
 "A"
 "B"
 "C"
 "D"

Metadata

Metadata

Assignees

No one assigned

    Labels

    ioInvolving the I/O subsystem: libuv, read, write, etc.iterationInvolves iteration or the iteration protocol

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions