From 8005c0f63512d65c9ce7ad7f094a6a386da4b624 Mon Sep 17 00:00:00 2001 From: Jeff Bezanson Date: Fri, 20 May 2016 15:44:14 -0400 Subject: [PATCH] use multiline=true, limit=false in `reprmime` and `stringmime` some small tweaks to output function changes and doc updates --- base/docs/helpdb/Base.jl | 6 +++--- base/linalg/bidiag.jl | 2 +- base/multimedia.jl | 10 ++++++---- base/show.jl | 18 ++++++++---------- doc/stdlib/io-network.rst | 2 +- test/nullable.jl | 7 ++++++- test/ranges.jl | 4 +++- test/replutil.jl | 9 ++++----- 8 files changed, 32 insertions(+), 26 deletions(-) diff --git a/base/docs/helpdb/Base.jl b/base/docs/helpdb/Base.jl index d66e0d8638ff7..2b427f2be1526 100644 --- a/base/docs/helpdb/Base.jl +++ b/base/docs/helpdb/Base.jl @@ -2888,9 +2888,9 @@ Show an expression and result, returning the result. """ showcompact(x) -Show a more compact representation of a value. This is used for printing array elements. If -a new type has a different compact representation, -it should test `get(io, :limit, false)` in its normal `show` method. +Show a more compact representation of a value. This is used for printing array elements. +If a new type has a different compact representation, +it should test `get(io, :compact, false)` in its normal `show` method. """ showcompact diff --git a/base/linalg/bidiag.jl b/base/linalg/bidiag.jl index 36d493e4e855f..efe2f07c28ef0 100644 --- a/base/linalg/bidiag.jl +++ b/base/linalg/bidiag.jl @@ -121,7 +121,7 @@ svdfact(M::Bidiagonal; thin::Bool=true) = svdfact!(copy(M),thin=thin) function show(io::IO, M::Bidiagonal) if get(io, :multiline, false) - invoke(show, (IO, AbstractArray), io, M) + Base.showarray(io, M) else println(io, summary(M), ":") print(io, " diag:") diff --git a/base/multimedia.jl b/base/multimedia.jl index b28e36efbb412..9f089802ec703 100644 --- a/base/multimedia.jl +++ b/base/multimedia.jl @@ -57,19 +57,21 @@ mimewritable(m::AbstractString, x) = mimewritable(MIME(m), x) # format and is returned unmodified. This is useful so that raw data can be # passed to display(m::MIME, x). +verbose_writemime(io, m, x) = writemime(IOContext(io,multiline=true,limit=false), m, x) + macro textmime(mime) quote mimeT = MIME{Symbol($mime)} # avoid method ambiguities with the general definitions below: # (Q: should we treat Vector{UInt8} as a String?) - Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(writemime, m, x) + Base.Multimedia.reprmime(m::mimeT, x::Vector{UInt8}) = sprint(verbose_writemime, m, x) Base.Multimedia.stringmime(m::mimeT, x::Vector{UInt8}) = reprmime(m, x) Base.Multimedia.istextmime(::mimeT) = true if $(mime != "text/plain") # strings are shown escaped for text/plain Base.Multimedia.reprmime(m::mimeT, x::AbstractString) = x end - Base.Multimedia.reprmime(m::mimeT, x) = sprint(writemime, m, x) + Base.Multimedia.reprmime(m::mimeT, x) = sprint(verbose_writemime, m, x) Base.Multimedia.stringmime(m::mimeT, x) = reprmime(m, x) end end @@ -77,11 +79,11 @@ end istextmime(::MIME) = false function reprmime(m::MIME, x) s = IOBuffer() - writemime(s, m, x) + verbose_writemime(s, m, x) takebuf_array(s) end reprmime(m::MIME, x::Vector{UInt8}) = x -stringmime(m::MIME, x) = base64encode(writemime, m, x) +stringmime(m::MIME, x) = base64encode(verbose_writemime, m, x) stringmime(m::MIME, x::Vector{UInt8}) = base64encode(write, x) # it is convenient to accept strings instead of ::MIME diff --git a/base/show.jl b/base/show.jl index 54a35e2d2617d..fe59d3ac02b53 100644 --- a/base/show.jl +++ b/base/show.jl @@ -1434,11 +1434,8 @@ function print_matrix_repr(io, X::AbstractArray) if compact && !haskey(io, :compact) io = IOContext(io, :compact => compact) end - prefix *= "[" - ind = " "^length(prefix) - print(io, prefix) + print(io, prefix, "[") for i=1:size(X,1) - i > 1 && print(io, ind) for j=1:size(X,2) j > 1 && print(io, " ") if !isassigned(X,i,j) @@ -1449,14 +1446,15 @@ function print_matrix_repr(io, X::AbstractArray) end end if i < size(X,1) - print(io, ";") - else - print(io, "]") + print(io, "; ") end end + print(io, "]") end -function show(io::IO, X::AbstractArray) +show(io::IO, X::AbstractArray) = showarray(io, X) + +function showarray(io::IO, X::AbstractArray) repr = !get(io, :multiline, false) if repr && ndims(X) == 1 return show_vector(io, X, "[", "]") @@ -1491,8 +1489,8 @@ function show(io::IO, X::AbstractArray) print_matrix(io, X, punct...) else show_nd(io, X, - (io, slice) -> print_matrix(io, slice, punct...), - !repr) + (io, slice) -> print_matrix(io, slice, punct...), + !repr) end end end diff --git a/doc/stdlib/io-network.rst b/doc/stdlib/io-network.rst index 3e8448777a4e2..3135d5f61073a 100644 --- a/doc/stdlib/io-network.rst +++ b/doc/stdlib/io-network.rst @@ -434,7 +434,7 @@ Text I/O .. Docstring generated from Julia source - Show a more compact representation of a value. This is used for printing array elements. If a new type has a different compact representation, it should test ``Base.limit_output(io)`` in its normal ``show`` method. + Show a more compact representation of a value. This is used for printing array elements. If a new type has a different compact representation, it should test ``get(io, :compact, false)`` in its normal ``show`` method. .. function:: showall(x) diff --git a/test/nullable.jl b/test/nullable.jl index 21ebcbe62ef39..daec8def96fd9 100644 --- a/test/nullable.jl +++ b/test/nullable.jl @@ -92,8 +92,13 @@ for (i, T) in enumerate(types) @test takebuf_string(io1) == @sprintf("Nullable{%s}(%s)", T, takebuf_string(io2)) a1 = [x2] + show(IOContext(io1, compact=false), a1) + show(IOContext(io2, compact=false), x2) + @test takebuf_string(io1) == + @sprintf("Nullable{%s}[%s]", string(T), takebuf_string(io2)) + show(io1, a1) - show(io2, x2) + show(IOContext(io2, compact=true), x2) @test takebuf_string(io1) == @sprintf("Nullable{%s}[%s]", string(T), takebuf_string(io2)) end diff --git a/test/ranges.jl b/test/ranges.jl index e9a51a73ca3fe..f8c5f8e337f2d 100644 --- a/test/ranges.jl +++ b/test/ranges.jl @@ -571,7 +571,9 @@ end # to test print_range in range.jl replstrmime(x) = sprint((io,x) -> writemime(IOContext(io, multiline=true, limit=true), MIME("text/plain"), x), x) @test replstrmime(1:4) == "1:4" -@test replstrmime(linspace(1,5,7)) == "7-element LinSpace{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0" +@test stringmime("text/plain", 1:4) == "1:4" +@test stringmime("text/plain", linspace(1,5,7)) == "7-element LinSpace{Float64}:\n 1.0,1.66667,2.33333,3.0,3.66667,4.33333,5.0" +@test repr(linspace(1,5,7)) == "linspace(1.0,5.0,7)" @test replstrmime(0:100.) == "0.0:1.0:100.0" # next is to test a very large range, which should be fast because print_range # only examines spacing of the left and right edges of the range, sufficient diff --git a/test/replutil.jl b/test/replutil.jl index 38c8fc85ef884..136c024fcf99e 100644 --- a/test/replutil.jl +++ b/test/replutil.jl @@ -262,9 +262,8 @@ let err_str, err_str = @except_str randn(1)() MethodError @test contains(err_str, "MethodError: objects of type Array{Float64,1} are not callable") end -strmime(x) = sprint((io,x)->writemime(IOContext(io,multiline=true), MIME("text/plain"), x), x) -@test strmime(FunctionLike()) == "(::FunctionLike) (generic function with 0 methods)" -@test ismatch(r"^@doc \(macro with \d+ method[s]?\)$", strmime(getfield(Base, Symbol("@doc")))) +@test stringmime("text/plain", FunctionLike()) == "(::FunctionLike) (generic function with 0 methods)" +@test ismatch(r"^@doc \(macro with \d+ method[s]?\)$", stringmime("text/plain", getfield(Base, Symbol("@doc")))) method_defs_lineno = @__LINE__ Base.Symbol() = throw(ErrorException("1")) @@ -292,8 +291,8 @@ let err_str, @test sprint(show, which(reinterpret(EightBitTypeT{Int32}, 0x54), Tuple{})) == "(::EightBitTypeT)() at $sp:$(method_defs_lineno + 6)" @test startswith(sprint(show, which(getfield(Base, Symbol("@doc")), Tuple{Vararg{Any}})), "@doc(x...) at boot.jl:") @test startswith(sprint(show, which(FunctionLike(), Tuple{})), "(::FunctionLike)() at $sp:$(method_defs_lineno + 7)") - @test strmime(FunctionLike()) == "(::FunctionLike) (generic function with 1 method)" - @test strmime(Core.arraysize) == "arraysize (built-in function)" + @test stringmime("text/plain", FunctionLike()) == "(::FunctionLike) (generic function with 1 method)" + @test stringmime("text/plain", Core.arraysize) == "arraysize (built-in function)" err_str = @except_stackframe Symbol() ErrorException @test err_str == " in Symbol() at $sn:$(method_defs_lineno + 0)"