Skip to content

Commit

Permalink
Merge pull request #23271 from JuliaLang/rf/IOContext-pairs
Browse files Browse the repository at this point in the history
define IOContext(io, pairs...) and remove IOContext(io; kw...)
  • Loading branch information
rfourquet authored Aug 24, 2017
2 parents 98c7a06 + af2fd69 commit b056235
Show file tree
Hide file tree
Showing 10 changed files with 39 additions and 40 deletions.
8 changes: 8 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1722,6 +1722,14 @@ export hex2num
# PR #23373
@deprecate diagm(A::BitMatrix) diagm(vec(A))

# PR #23271
function IOContext(io::IO; kws...)
depwarn("IOContext(io, k=v, ...) is deprecated, use IOContext(io, :k => v, ...) instead.", :IOContext)
IOContext(io, (k=>v for (k, v) in kws)...)
end

@deprecate IOContext(io::IO, key, value) IOContext(io, key=>value)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
3 changes: 1 addition & 2 deletions base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -384,8 +384,7 @@ function code_warntype(io::IO, f, @nospecialize(t))
body.args = src.code
body.typ = rettype
# Fix slot names and types in function body
show_unquoted(IOContext(IOContext(emph_io, :SOURCEINFO => src),
:SOURCE_SLOTNAMES => slotnames),
show_unquoted(IOContext(emph_io, :SOURCEINFO => src, :SOURCE_SLOTNAMES => slotnames),
body, 2)
print(emph_io, '\n')
end
Expand Down
5 changes: 3 additions & 2 deletions base/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ function showerror(io::IO, ex::DomainError, bt; backtrace=true)
if isa(ex.val, AbstractArray)
compact = get(io, :compact, true)
limit = get(io, :limit, true)
print(IOContext(io, compact=compact, limit=limit), "DomainError with ", ex.val)
print(IOContext(io, :compact => compact, :limit => limit),
"DomainError with ", ex.val)
else
print(io, "DomainError with ", ex.val)
end
Expand Down Expand Up @@ -360,7 +361,7 @@ function showerror(io::IO, ex::MethodError)
print(io, "; ")
for (i, (k, v)) in enumerate(kwargs)
print(io, k, "=")
show(IOContext(io, :limit=>true), v)
show(IOContext(io, :limit => true), v)
i == length(kwargs) || print(io, ", ")
end
end
Expand Down
39 changes: 15 additions & 24 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,32 @@ struct IOContext{IO_t <: IO} <: AbstractPipe
end
end

"""
IOContext(io::IO; properties...)
unwrapcontext(io::IO) = io, ImmutableDict{Symbol,Any}()
unwrapcontext(io::IOContext) = io.io, io.dict

The same as `IOContext(io::IO, KV::Pair)`, but accepting properties as keyword arguments.
"""
IOContext(io::IO; kws...) = IOContext(convert(IOContext, io); kws...)
function IOContext(io::IOContext; kws...)
for (k, v) in kws
io = IOContext(io, k, v)
end
return io
function IOContext(io::IO, dict::ImmutableDict)
io0 = unwrapcontext(io)[1]
IOContext{typeof(io0)}(io0, dict)
end

convert(::Type{IOContext}, io::IOContext) = io
convert(::Type{IOContext}, io::IO) = IOContext(io, ImmutableDict{Symbol, Any}())
convert(::Type{IOContext}, io::IO) = IOContext(unwrapcontext(io)...)

IOContext(io::IOContext, dict::ImmutableDict) = typeof(io)(io.io, dict)
IOContext(io::IO, dict::ImmutableDict) = IOContext{typeof(io)}(io, dict)

IOContext(io::IOContext, key, value) = IOContext(io.io, ImmutableDict{Symbol, Any}(io.dict, key, value))
IOContext(io::IO, key, value) = IOContext(io, ImmutableDict{Symbol, Any}(key, value))

IOContext(io::IO, context::IO) = convert(IOContext, io)
function IOContext(io::IO, KV::Pair)
io0, d = unwrapcontext(io)
IOContext(io0, ImmutableDict{Symbol,Any}(d, KV[1], KV[2]))
end

"""
IOContext(io::IO, context::IOContext)
Create an `IOContext` that wraps an alternate `IO` but inherits the properties of `context`.
"""
IOContext(io::IO, context::IOContext) = IOContext(io, context.dict)
IOContext(io::IO, context::IO) = IOContext(unwrapcontext(io)[1], unwrapcontext(context)[2])

"""
IOContext(io::IO, KV::Pair)
IOContext(io::IO, KV::Pair...)
Create an `IOContext` that wraps a given stream, adding the specified `key=>value` pair to
Create an `IOContext` that wraps a given stream, adding the specified `key=>value` pairs to
the properties of that stream (note that `io` can itself be an `IOContext`).
- use `(key => value) in dict` to see if this particular combination is in the properties set
Expand Down Expand Up @@ -87,7 +78,7 @@ julia> f(IOContext(STDOUT, :short => true))
short
```
"""
IOContext(io::IO, KV::Pair) = IOContext(io, KV[1], KV[2])
IOContext(io::IO, KV::Pair, KVs::Pair...) = IOContext(IOContext(io, KV), KVs...)

show(io::IO, ctx::IOContext) = (print(io, "IOContext("); show(io, ctx.io); print(io, ")"))

Expand Down Expand Up @@ -1335,7 +1326,7 @@ function dumpsubtypes(io::IO, x::DataType, m::Module, n::Int, indent)
tp = t
while true
show(tvar_io, tp.var)
tvar_io = IOContext(tvar_io, :unionall_env, tp.var)
tvar_io = IOContext(tvar_io, :unionall_env => tp.var)
tp = tp.body
if isa(tp, UnionAll)
print(io, ", ")
Expand Down
6 changes: 3 additions & 3 deletions test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ for d in (Dict("\n" => "\n", "1" => "\n", "\n" => "2"),
for cols in (12, 40, 80), rows in (2, 10, 24)
# Ensure output is limited as requested
s = IOBuffer()
io = Base.IOContext(Base.IOContext(s, :limit => true), :displaysize => (rows, cols))
io = Base.IOContext(s, :limit => true, :displaysize => (rows, cols))
Base.show(io, MIME("text/plain"), d)
out = split(String(take!(s)),'\n')
for line in out[2:end]
Expand All @@ -278,7 +278,7 @@ for d in (Dict("\n" => "\n", "1" => "\n", "\n" => "2"),

for f in (keys, values)
s = IOBuffer()
io = Base.IOContext(Base.IOContext(s, :limit => true), :displaysize => (rows, cols))
io = Base.IOContext(s, :limit => true, :displaysize => (rows, cols))
Base.show(io, MIME("text/plain"), f(d))
out = split(String(take!(s)),'\n')
for line in out[2:end]
Expand Down Expand Up @@ -311,7 +311,7 @@ end
mutable struct Alpha end
Base.show(io::IO, ::Alpha) = print(io,"α")
let sbuff = IOBuffer(),
io = Base.IOContext(Base.IOContext(sbuff, :limit => true), :displaysize => (10, 20))
io = Base.IOContext(sbuff, :limit => true, :displaysize => (10, 20))

Base.show(io, MIME("text/plain"), Dict(Alpha()=>1))
local str = String(take!(sbuff))
Expand Down
2 changes: 1 addition & 1 deletion test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ smry = summary(v)
@test contains(smry, "OffsetArray{Float64,1")
@test contains(smry, "with indices -1:1")
function cmp_showf(printfunc, io, A)
ioc = IOContext(IOContext(io, :limit => true), :compact => true)
ioc = IOContext(io, :limit => true, :compact => true)
printfunc(ioc, A)
str1 = String(take!(io))
printfunc(ioc, parent(A))
Expand Down
2 changes: 1 addition & 1 deletion test/ranges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -855,7 +855,7 @@ end

# stringmime/show should display the range or linspace nicely
# to test print_range in range.jl
replstrmime(x) = sprint((io,x) -> show(IOContext(io, limit=true, displaysize=(24, 80)), MIME("text/plain"), x), x)
replstrmime(x) = sprint((io,x) -> show(IOContext(io, :limit => true, :displaysize => (24, 80)), MIME("text/plain"), x), x)
@test replstrmime(1:4) == "1:4"
@test stringmime("text/plain", 1:4) == "1:4"
@test stringmime("text/plain", linspace(1,5,7)) == "1.0:0.6666666666666666:5.0"
Expand Down
2 changes: 1 addition & 1 deletion test/replutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -610,7 +610,7 @@ end

@testset "Dict printing with limited rows" begin
buf = IOBuffer()
io = IOContext(IOContext(buf, :displaysize => (4, 80)), :limit => true)
io = IOContext(buf, :displaysize => (4, 80), :limit => true)
d = Base.ImmutableDict(1=>2)
show(io, MIME"text/plain"(), d)
@test String(take!(buf)) == "Base.ImmutableDict{$Int,$Int} with 1 entry: …"
Expand Down
4 changes: 2 additions & 2 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# For curmod_*
include("testenv.jl")

replstr(x) = sprint((io,x) -> show(IOContext(io, limit=true, displaysize=(24, 80)), MIME("text/plain"), x), x)
replstr(x) = sprint((io,x) -> show(IOContext(io, :limit => true, :displaysize => (24, 80)), MIME("text/plain"), x), x)

@test replstr(Array{Any}(2)) == "2-element Array{Any,1}:\n #undef\n #undef"
@test replstr(Array{Any}(2,2)) == "2×2 Array{Any,2}:\n #undef #undef\n #undef #undef"
Expand Down Expand Up @@ -924,7 +924,7 @@ end
@testset "Array printing with limited rows" begin
arrstr = let buf = IOBuffer()
function (A, rows)
Base.showarray(IOContext(buf, displaysize=(rows, 80), limit=true),
Base.showarray(IOContext(buf, :displaysize => (rows, 80), :limit => true),
A, false, header=true)
String(take!(buf))
end
Expand Down
8 changes: 4 additions & 4 deletions test/sparse/sparse.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1794,14 +1794,14 @@ end
show(io, MIME"text/plain"(), spzeros(Float32, Int64, 2, 2))
@test String(take!(io)) == "2×2 SparseMatrixCSC{Float32,Int64} with 0 stored entries"

ioc = IOContext(io, displaysize = (5, 80), limit = true)
ioc = IOContext(io, :displaysize => (5, 80), :limit => true)
show(ioc, MIME"text/plain"(), sparse(Int64[1], Int64[1], [1.0]))
@test String(take!(io)) == "1×1 SparseMatrixCSC{Float64,Int64} with 1 stored entry:\n [1, 1] = 1.0"
show(ioc, MIME"text/plain"(), sparse(Int64[1, 1], Int64[1, 2], [1.0, 2.0]))
@test String(take!(io)) == "1×2 SparseMatrixCSC{Float64,Int64} with 2 stored entries:\n"

# even number of rows
ioc = IOContext(io, displaysize = (8, 80), limit = true)
ioc = IOContext(io, :displaysize => (8, 80), :limit => true)
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4], Int64[1,1,2,2], [1.0,2.0,3.0,4.0]))
@test String(take!(io)) == string("4×2 SparseMatrixCSC{Float64,Int64} with 4 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 2.0\n [3, 2] = 3.0\n [4, 2] = 4.0")
Expand All @@ -1815,7 +1815,7 @@ end
" = 1.0\n\n [5, 3] = 1.0")

# odd number of rows
ioc = IOContext(io, displaysize = (9, 80), limit = true)
ioc = IOContext(io, :displaysize => (9, 80), :limit => true)
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4,5], Int64[1,1,2,2,3], [1.0,2.0,3.0,4.0,5.0]))
@test String(take!(io)) == string("5×3 SparseMatrixCSC{Float64,Int64} with 5 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 2.0\n [3, 2] = 3.0\n [4, 2] = 4.0\n [5, 3] = 5.0")
Expand All @@ -1828,7 +1828,7 @@ end
@test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,$Int} with 18 stored entries:\n [1, 1]",
" = 1.0\n [2, 1] = 1.0\n\n [5, 3] = 1.0\n [6, 3] = 1.0")

ioc = IOContext(io, displaysize = (9, 80))
ioc = IOContext(io, :displaysize => (9, 80))
show(ioc, MIME"text/plain"(), sparse(Int64[1,2,3,4,5,6], Int64[1,1,2,2,3,3], [1.0,2.0,3.0,4.0,5.0,6.0]))
@test String(take!(io)) == string("6×3 SparseMatrixCSC{Float64,Int64} with 6 stored entries:\n [1, 1] = 1.0\n",
" [2, 1] = 2.0\n [3, 2] = 3.0\n [4, 2] = 4.0\n [5, 3] = 5.0\n [6, 3] = 6.0")
Expand Down

0 comments on commit b056235

Please sign in to comment.