From af2fd69aef0738d6c877163223fce2d2df1c9a43 Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Tue, 15 Aug 2017 14:41:57 +0200 Subject: [PATCH] define IOContext(io, pairs...) and remove IOContext(io; kw...) * Usually a context is defined via a pair (`IOContext(io, :k=>v)`), but if another pair has to be pushed, one had to either add an IOContext call (`IOContext(IOContext(io, :k=>v), :x=>y)`) or to switch to kwargs syntax (`IOContext(io, k=v, x=y)`), none of which is satisfactory. * As a consequence of allowing passing multiple pairs, the method using keyword arguments is obsoleted (and hence deprecated). * Also, the method `IOContext(io, key, value)` is deprecated in favor of `IOContext(io, key=>value)`. --- base/deprecated.jl | 8 ++++++++ base/interactiveutil.jl | 3 +-- base/replutil.jl | 5 +++-- base/show.jl | 39 +++++++++++++++------------------------ test/dict.jl | 6 +++--- test/offsetarray.jl | 2 +- test/ranges.jl | 2 +- test/replutil.jl | 2 +- test/show.jl | 4 ++-- test/sparse/sparse.jl | 8 ++++---- 10 files changed, 39 insertions(+), 40 deletions(-) diff --git a/base/deprecated.jl b/base/deprecated.jl index a2b7a78baa660..7d1201f00db2a 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -1719,6 +1719,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 diff --git a/base/interactiveutil.jl b/base/interactiveutil.jl index 1e50a94bebbf5..aa0ad2e7ffa5b 100644 --- a/base/interactiveutil.jl +++ b/base/interactiveutil.jl @@ -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 diff --git a/base/replutil.jl b/base/replutil.jl index d5329602aacc0..5582c7a6b01fb 100644 --- a/base/replutil.jl +++ b/base/replutil.jl @@ -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 @@ -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 diff --git a/base/show.jl b/base/show.jl index 79678aab589a3..38a8343af2d6e 100644 --- a/base/show.jl +++ b/base/show.jl @@ -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 @@ -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, ")")) @@ -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, ", ") diff --git a/test/dict.jl b/test/dict.jl index 3954b342ed05f..dbd4771b2dfe1 100644 --- a/test/dict.jl +++ b/test/dict.jl @@ -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] @@ -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] @@ -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)) diff --git a/test/offsetarray.jl b/test/offsetarray.jl index 46ace5061822f..b2c642cdfced3 100644 --- a/test/offsetarray.jl +++ b/test/offsetarray.jl @@ -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)) diff --git a/test/ranges.jl b/test/ranges.jl index a953599223ad2..69fecf89d2630 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -850,7 +850,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" diff --git a/test/replutil.jl b/test/replutil.jl index 7049511f1ae52..4f91dd949ffdb 100644 --- a/test/replutil.jl +++ b/test/replutil.jl @@ -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: …" diff --git a/test/show.jl b/test/show.jl index 5d576dcd2920d..ebe4a6681f40f 100644 --- a/test/show.jl +++ b/test/show.jl @@ -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" @@ -923,7 +923,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 diff --git a/test/sparse/sparse.jl b/test/sparse/sparse.jl index 30e62f354afab..10719cce3c0c1 100644 --- a/test/sparse/sparse.jl +++ b/test/sparse/sparse.jl @@ -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") @@ -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") @@ -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")