Skip to content

Commit

Permalink
fix JuliaLang#37701: support keys(::IOContext) (JuliaLang#37753)
Browse files Browse the repository at this point in the history
  • Loading branch information
simeonschaub committed Oct 26, 2020
1 parent c3c4dab commit c293757
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,8 @@ New library features
* New `append!(vector, collections...)` and `prepend!(vector, collections...)` methods accept multiple
collections to be appended or prepended ([#36227]).
* The postfix operator `'ᵀ` can now be used as an alias for `transpose` ([#38043]).
* `keys(io::IO)` has been added, which returns all keys of `io` if `io` is an `IOContext` and an empty
`Base.KeySet` otherwise ([#37753]).

Standard library changes
------------------------
Expand Down
2 changes: 2 additions & 0 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,8 @@ getindex(io::IOContext, key) = getindex(io.dict, key)
getindex(io::IO, key) = throw(KeyError(key))
get(io::IOContext, key, default) = get(io.dict, key, default)
get(io::IO, key, default) = default
keys(io::IOContext) = keys(io.dict)
keys(io::IO) = keys(ImmutableDict{Symbol,Any}())

displaysize(io::IOContext) = haskey(io, :displaysize) ? io[:displaysize]::Tuple{Int,Int} : displaysize(io.io)

Expand Down
3 changes: 3 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ showstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :limit => true, :d
@test ioc.io == io
@test ioc.dict == Base.ImmutableDict(Base.ImmutableDict{Symbol, Any}(:x, 1),
:y => 2)
@test Base.ImmutableDict((key => ioc[key] for key in keys(ioc))...) == ioc.dict
@test keys(IOBuffer()) isa Base.KeySet
@test length(keys(IOBuffer())) == 0
end

@test replstr(Array{Any}(undef, 2)) == "2-element Vector{Any}:\n #undef\n #undef"
Expand Down

0 comments on commit c293757

Please sign in to comment.