Skip to content

Commit

Permalink
allow IOContext to propagate more
Browse files Browse the repository at this point in the history
- use 2-argument form of `summary`
- pass types directly to print instead of using string interpolation
  • Loading branch information
JeffBezanson committed Oct 2, 2018
1 parent 5d90fd6 commit f2afdbb
Show file tree
Hide file tree
Showing 17 changed files with 53 additions and 45 deletions.
6 changes: 4 additions & 2 deletions base/abstractdict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ struct ValueIterator{T<:AbstractDict}
dict::T
end

summary(iter::T) where {T<:Union{KeySet,ValueIterator}} =
string(T.name, " for a ", summary(iter.dict))
function summary(io::IO, iter::T) where {T<:Union{KeySet,ValueIterator}}
print(io, T.name, " for a ")
summary(io, iter.dict)
end

show(io::IO, iter::Union{KeySet,ValueIterator}) = show_vector(io, iter)

Expand Down
2 changes: 1 addition & 1 deletion base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,7 +410,7 @@ _show_nonempty(::IO, ::AbstractVector, ::String) =
_show_nonempty(io::IO, X::AbstractArray{T,0} where T, prefix::String) = print_array(io, X)

# NOTE: it's not clear how this method could use the :typeinfo attribute
_show_empty(io::IO, X::Array{T}) where {T} = print(io, "Array{$T}(", join(size(X),','), ')')
_show_empty(io::IO, X::Array{T}) where {T} = print(io, "Array{", T, "}(", join(size(X),','), ')')
_show_empty(io, X) = nothing # by default, we don't know this constructor

# typeinfo aware (necessarily)
Expand Down
40 changes: 21 additions & 19 deletions base/errorshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ function showerror(io::IO, ex::BoundsError)
if isdefined(ex, :a)
print(io, ": attempt to access ")
if isa(ex.a, AbstractArray)
print(io, summary(ex.a))
summary(io, ex.a)
else
show(io, MIME"text/plain"(), ex.a)
end
Expand All @@ -54,19 +54,19 @@ end
function showerror(io::IO, ex::TypeError)
print(io, "TypeError: ")
if ex.expected === Bool
print(io, "non-boolean ($(typeof(ex.got))) used in boolean context")
print(io, "non-boolean (", typeof(ex.got), ") used in boolean context")
else
if isa(ex.got, Type)
tstr = "Type{$(ex.got)}"
targs = ("Type{", ex.got, "}")
else
tstr = string(typeof(ex.got))
targs = (typeof(ex.got),)
end
if isempty(ex.context)
ctx = "in $(ex.func)"
else
ctx = "in $(ex.func), in $(ex.context)"
end
print(io, ctx, ", expected $(ex.expected), got ", tstr)
print(io, ctx, ", expected ", ex.expected, ", got ", targs...)
end
end

Expand All @@ -90,7 +90,7 @@ showerror(io::IO, ex::LoadError) = showerror(io, ex, [])
function showerror(io::IO, ex::InitError, bt; backtrace=true)
print(io, "InitError: ")
showerror(io, ex.error, bt, backtrace=backtrace)
print(io, "\nduring initialization of module $(ex.mod)")
print(io, "\nduring initialization of module ", ex.mod)
end
showerror(io::IO, ex::InitError) = showerror(io, ex, [])

Expand Down Expand Up @@ -128,7 +128,9 @@ function showerror(io::IO, ex::ErrorException)
print(io, "Use `codeunits(str)` instead.")
end
end
showerror(io::IO, ex::KeyError) = print(io, "KeyError: key $(repr(ex.key)) not found")
showerror(io::IO, ex::KeyError) = (print(io, "KeyError: key ");
show(io, ex.key);
print(io, " not found"))
showerror(io::IO, ex::InterruptException) = print(io, "InterruptException:")
showerror(io::IO, ex::ArgumentError) = print(io, "ArgumentError: $(ex.msg)")
showerror(io::IO, ex::AssertionError) = print(io, "AssertionError: $(ex.msg)")
Expand Down Expand Up @@ -188,9 +190,9 @@ function showerror(io::IO, ex::MethodError)
print(io, "Cannot `convert` an object of type ", arg_types_param[2], " to an object of type ", T)
end
elseif isempty(methods(f)) && isa(f, DataType) && f.abstract
print(io, "no constructors have been defined for $f")
print(io, "no constructors have been defined for ", f)
elseif isempty(methods(f)) && !isa(f, Function) && !isa(f, Type)
print(io, "objects of type $ft are not callable")
print(io, "objects of type ", ft, " are not callable")
else
if ft <: Function && isempty(ft.parameters) &&
isdefined(ft.name.module, name) &&
Expand All @@ -204,7 +206,7 @@ function showerror(io::IO, ex::MethodError)
end
print(io, "(")
for (i, typ) in enumerate(arg_types_param)
print(io, "::$typ")
print(io, "::", typ)
i == length(arg_types_param) || print(io, ", ")
end
if !isempty(kwargs)
Expand Down Expand Up @@ -341,10 +343,10 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
# If isvarargtype then it checks whether the rest of the input arguments matches
# the varargtype
if Base.isvarargtype(sig[i])
sigstr = string(unwrap_unionall(sig[i]).parameters[1], "...")
sigstr = (unwrap_unionall(sig[i]).parameters[1], "...")
j = length(t_i)
else
sigstr = string(sig[i])
sigstr = (sig[i],)
j = i
end
# Checks if the type of arg 1:i of the input intersects with the current method
Expand All @@ -356,18 +358,18 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
if t_in === Union{}
if get(io, :color, false)
Base.with_output_color(Base.error_color(), iob) do iob
print(iob, "::$sigstr")
print(iob, "::", sigstr...)
end
else
print(iob, "!Matched::$sigstr")
print(iob, "!Matched::", sigstr...)
end
# If there is no typeintersect then the type signature from the method is
# inserted in t_i this ensures if the type at the next i matches the type
# signature then there will be a type intersect
t_i[i] = sig[i]
else
right_matches += j==i ? 1 : 0
print(iob, "::$sigstr")
print(iob, "::", sigstr...)
end
end
special && right_matches == 0 && continue
Expand All @@ -389,19 +391,19 @@ function show_method_candidates(io::IO, ex::MethodError, @nospecialize kwargs=()
for (k, sigtype) in enumerate(sig[length(t_i)+1:end])
sigtype = isvarargtype(sigtype) ? unwrap_unionall(sigtype) : sigtype
if Base.isvarargtype(sigtype)
sigstr = string(sigtype.parameters[1], "...")
sigstr = (sigtype.parameters[1], "...")
else
sigstr = string(sigtype)
sigstr = (sigtype,)
end
if !((min(length(t_i), length(sig)) == 0) && k==1)
print(iob, ", ")
end
if get(io, :color, false)
Base.with_output_color(Base.error_color(), iob) do iob
print(iob, "::$sigstr")
print(iob, "::", sigstr...)
end
else
print(iob, "!Matched::$sigstr")
print(iob, "!Matched::", sigstr...)
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/methodshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -319,7 +319,7 @@ function show(io::IO, mime::MIME"text/plain", mt::AbstractVector{Method})
end

function show(io::IO, mime::MIME"text/html", mt::AbstractVector{Method})
print(io, summary(mt))
summary(io, mt)
if !isempty(mt)
print(io, ":<ul>")
for d in mt
Expand Down
20 changes: 12 additions & 8 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ function show(io::IO, ::MIME"text/plain", r::LinRange)
# range(1, stop=3, length=7)
# 7-element LinRange{Float64}:
# 1.0,1.33333,1.66667,2.0,2.33333,2.66667,3.0
print(io, summary(r))
summary(io, r)
if !isempty(r)
println(io, ":")
print_range(io, r)
Expand Down Expand Up @@ -41,7 +41,7 @@ function show(io::IO, ::MIME"text/plain", f::Function)
end

function show(io::IO, ::MIME"text/plain", iter::Union{KeySet,ValueIterator})
print(io, summary(iter))
summary(io, iter)
isempty(iter) && return
print(io, ". ", isa(iter,KeySet) ? "Keys" : "Values", ":")
limit::Bool = get(io, :limit, false)
Expand Down Expand Up @@ -78,7 +78,7 @@ function show(io::IO, ::MIME"text/plain", t::AbstractDict{K,V}) where {K,V}
recur_io = IOContext(recur_io, :compact => true)
end

print(io, summary(t))
summary(io, t)
isempty(t) && return
print(io, ":")
show_circular(io, t) && return
Expand Down Expand Up @@ -1653,7 +1653,7 @@ function dump_elts(io::IOContext, x::Array, n::Int, indent, i0, i1)
end

function dump(io::IOContext, x::Array, n::Int, indent)
print(io, "Array{$(eltype(x))}($(size(x)))")
print(io, "Array{", eltype(x), "}(", size(x), ")")
if eltype(x) <: Number
print(io, " ")
show(io, x)
Expand Down Expand Up @@ -1738,7 +1738,11 @@ MyStruct
y: Tuple{Int64,Int64}
```
"""
dump(arg; maxdepth=DUMP_DEFAULT_MAXDEPTH) = dump(IOContext(stdout::IO, :limit => true), arg; maxdepth=maxdepth)
function dump(arg; maxdepth=DUMP_DEFAULT_MAXDEPTH)
# this is typically used interactively, so default to being in Main
mod = get(stdout, :module, Main)
dump(IOContext(stdout::IO, :limit => true, :module => mod), arg; maxdepth=maxdepth)
end


"""
Expand Down Expand Up @@ -1908,22 +1912,22 @@ function showarg(io::IO, r::ReshapedArray, toplevel)
end

function showarg(io::IO, r::ReinterpretArray{T}, toplevel) where {T}
print(io, "reinterpret($T, ")
print(io, "reinterpret(", T, ", ")
showarg(io, parent(r), false)
print(io, ')')
end

# pretty printing for Iterators.Pairs
function Base.showarg(io::IO, r::Iterators.Pairs{<:Integer, <:Any, <:Any, T}, toplevel) where T<:AbstractArray
print(io, "pairs(IndexLinear(), ::$T)")
print(io, "pairs(IndexLinear(), ::", T, ")")
end

function Base.showarg(io::IO, r::Iterators.Pairs{Symbol, <:Any, <:Any, T}, toplevel) where {T <: NamedTuple}
print(io, "pairs(::NamedTuple)")
end

function Base.showarg(io::IO, r::Iterators.Pairs{<:Any, <:Any, I, D}, toplevel) where {D, I}
print(io, "Iterators.Pairs(::$D, ::$I)")
print(io, "Iterators.Pairs(::", D, ", ::", I, ")")
end

# printing BitArrays
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/bidiag.jl
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ end

function show(io::IO, M::Bidiagonal)
# TODO: make this readable and one-line
println(io, summary(M), ":")
summary(io, M); println(io, ":")
print(io, " diag:")
print_matrix(io, (M.dv)')
print(io, M.uplo == 'U' ? "\n super:" : "\n sub:")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/bunchkaufman.jl
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ issuccess(B::BunchKaufman) = B.info == 0

function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, B::BunchKaufman)
if issuccess(B)
println(io, summary(B))
summary(io, B); println(io)
println(io, "D factor:")
show(io, mime, B.D)
println(io, "\n$(B.uplo) factor:")
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/cholesky.jl
Original file line number Diff line number Diff line change
Expand Up @@ -379,7 +379,7 @@ issuccess(C::Cholesky) = C.info == 0

function show(io::IO, mime::MIME{Symbol("text/plain")}, C::Cholesky{<:Any,<:AbstractMatrix})
if issuccess(C)
println(io, summary(C))
summary(io, C); println(io)
println(io, "$(C.uplo) factor:")
show(io, mime, C.UL)
else
Expand All @@ -388,7 +388,7 @@ function show(io::IO, mime::MIME{Symbol("text/plain")}, C::Cholesky{<:Any,<:Abst
end

function show(io::IO, mime::MIME{Symbol("text/plain")}, C::CholeskyPivoted{<:Any,<:AbstractMatrix})
println(io, summary(C))
summary(io, C); println(io)
println(io, "$(C.uplo) factor with rank $(rank(C)):")
show(io, mime, C.uplo == 'U' ? C.U : C.L)
println(io, "\npermutation:")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/eigen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ julia> eigvecs(A, B)
eigvecs(A::AbstractMatrix, B::AbstractMatrix) = eigvecs(eigen(A, B))

function show(io::IO, mime::MIME{Symbol("text/plain")}, F::Union{Eigen,GeneralizedEigen})
println(io, summary(F))
summary(io, F); println(io)
println(io, "eigenvalues:")
show(io, mime, F.values)
println(io, "\neigenvectors:")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/lq.jl
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ getindex(A::LQPackedQ, i::Integer, j::Integer) =
lmul!(A, setindex!(zeros(eltype(A), size(A, 2)), 1, j))[i]

function show(io::IO, C::LQ)
println(io, "$(typeof(C)) with factors L and Q:")
println(io, typeof(C), " with factors L and Q:")
show(io, C.L)
println(io)
show(io, C.Q)
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/lu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ issuccess(F::LU) = F.info == 0

function show(io::IO, mime::MIME{Symbol("text/plain")}, F::LU)
if issuccess(F)
println(io, summary(F))
summary(io, F); println(io)
println(io, "L factor:")
show(io, mime, F.L)
println(io, "\nU factor:")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/qr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ Matrix(F::QRPivoted) = Array(AbstractArray(F))
Array(F::QRPivoted) = Matrix(F)

function show(io::IO, mime::MIME{Symbol("text/plain")}, F::Union{QR, QRCompactWY, QRPivoted})
println(io, summary(F))
summary(io, F); println(io)
println(io, "Q factor:")
show(io, mime, F.Q)
println(io, "\nR factor:")
Expand Down
4 changes: 2 additions & 2 deletions stdlib/LinearAlgebra/src/schur.jl
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Base.propertynames(F::Schur) =
(:Schur, :vectors, fieldnames(typeof(F))...)

function show(io::IO, mime::MIME{Symbol("text/plain")}, F::Schur)
println(io, summary(F))
summary(io, F); println(io)
println(io, "T factor:")
show(io, mime, F.T)
println(io, "\nZ factor:")
Expand Down Expand Up @@ -261,7 +261,7 @@ Base.propertynames(F::GeneralizedSchur) =
(:values, :left, :right, fieldnames(typeof(F))...)

function show(io::IO, mime::MIME{Symbol("text/plain")}, F::GeneralizedSchur)
println(io, summary(F))
summary(io, F); println(io)
println(io, "S factor:")
show(io, mime, F.S)
println(io, "\nT factor:")
Expand Down
2 changes: 1 addition & 1 deletion stdlib/LinearAlgebra/src/uniformscaling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function show(io::IO, J::UniformScaling)
if occursin(r"\w+\s*[\+\-]\s*\w+", s)
s = "($s)"
end
print(io, "$(typeof(J))\n$s*I")
print(io, typeof(J), "\n$s*I")
end
copy(J::UniformScaling) = UniformScaling(J.λ)

Expand Down
2 changes: 1 addition & 1 deletion stdlib/SharedArrays/src/SharedArrays.jl
Original file line number Diff line number Diff line change
Expand Up @@ -487,7 +487,7 @@ function show(io::IO, mime::MIME"text/plain", S::SharedArray)
invoke(show, Tuple{IO,MIME"text/plain",DenseArray}, io, MIME"text/plain"(), S)
else
# retrieve from the first worker mapping the array.
println(io, summary(S), ":")
summary(io, S); println(io, ":")
Base.print_array(io, remotecall_fetch(sharr->sharr.s, S.pids[1], S))
end
end
Expand Down
2 changes: 1 addition & 1 deletion stdlib/SharedArrays/test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -306,5 +306,5 @@ end

let S = SharedArray(Int64[]) # Issue #26582
@test sprint(show, S) == "Int64[]"
@test sprint(show, "text/plain", S) == "0-element SharedArray{Int64,1}:\n"
@test sprint(show, "text/plain", S, context = :module=>@__MODULE__) == "0-element SharedArray{Int64,1}:\n"
end
2 changes: 1 addition & 1 deletion stdlib/SuiteSparse/src/spqr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -321,7 +321,7 @@ function Base.propertynames(F::QRSparse, private::Bool=false)
end

function Base.show(io::IO, mime::MIME{Symbol("text/plain")}, F::QRSparse)
println(io, summary(F))
summary(io, F); println(io)
println(io, "Q factor:")
show(io, mime, F.Q)
println(io, "\nR factor:")
Expand Down

0 comments on commit f2afdbb

Please sign in to comment.