Skip to content

Commit c293757

Browse files
authored
fix #37701: support keys(::IOContext) (#37753)
1 parent c3c4dab commit c293757

File tree

3 files changed

+7
-0
lines changed

3 files changed

+7
-0
lines changed

NEWS.md

+2
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ New library features
114114
* New `append!(vector, collections...)` and `prepend!(vector, collections...)` methods accept multiple
115115
collections to be appended or prepended ([#36227]).
116116
* The postfix operator `'ᵀ` can now be used as an alias for `transpose` ([#38043]).
117+
* `keys(io::IO)` has been added, which returns all keys of `io` if `io` is an `IOContext` and an empty
118+
`Base.KeySet` otherwise ([#37753]).
117119

118120
Standard library changes
119121
------------------------

base/show.jl

+2
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,8 @@ getindex(io::IOContext, key) = getindex(io.dict, key)
341341
getindex(io::IO, key) = throw(KeyError(key))
342342
get(io::IOContext, key, default) = get(io.dict, key, default)
343343
get(io::IO, key, default) = default
344+
keys(io::IOContext) = keys(io.dict)
345+
keys(io::IO) = keys(ImmutableDict{Symbol,Any}())
344346

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

test/show.jl

+3
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ showstr(x, kv::Pair...) = sprint((io,x) -> show(IOContext(io, :limit => true, :d
2020
@test ioc.io == io
2121
@test ioc.dict == Base.ImmutableDict(Base.ImmutableDict{Symbol, Any}(:x, 1),
2222
:y => 2)
23+
@test Base.ImmutableDict((key => ioc[key] for key in keys(ioc))...) == ioc.dict
24+
@test keys(IOBuffer()) isa Base.KeySet
25+
@test length(keys(IOBuffer())) == 0
2326
end
2427

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

0 commit comments

Comments
 (0)