Skip to content

Commit

Permalink
Deprecate showcompact()
Browse files Browse the repository at this point in the history
There is no obvious reason to provide a special function for the :compact IOContext property
but not for other properties. showcompact() used to be useful to print a single-line
representation of an array at the REPL, but now show() does the same thing. So it should only
be needed for collections to print their elements when implementing show(), and for tests
(most of the changes here).

Also improve a few docstrings.
  • Loading branch information
nalimilan committed Feb 16, 2018
1 parent b0303ca commit daf2846
Show file tree
Hide file tree
Showing 16 changed files with 45 additions and 74 deletions.
7 changes: 6 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -1061,7 +1061,11 @@ Deprecated or removed
* The fallback method `^(x, p::Integer)` is deprecated. If your type relied on this definition,
add a method such as `^(x::MyType, p::Integer) = Base.power_by_squaring(x, p)` ([#23332]).

* `wait` and `fetch` on `Task` now resemble the interface of `Future`
* `wait` and `fetch` on `Task` now resemble the interface of `Future`.

* `showcompact(io, x...)` has been deprecated in favor of
`show(IOContext(io, :compact => true), x...)` ([#26080]).
Use `sprint(show, x..., context=:compact => true)` instead of `sprint(showcompact, x...)`.

Command-line option changes
---------------------------
Expand Down Expand Up @@ -1324,3 +1328,4 @@ Command-line option changes
[#25745]: https://github.com/JuliaLang/julia/issues/25745
[#25896]: https://github.com/JuliaLang/julia/issues/25896
[#25998]: https://github.com/JuliaLang/julia/issues/25998
[#26080]: https://github.com/JuliaLang/julia/issues/26080
2 changes: 1 addition & 1 deletion base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ macro enum(T, syms...)
print(io, x)
else
print(io, x, "::")
showcompact(io, typeof(x))
show(IOContext(io, :compact => true), typeof(x))
print(io, " = ", $basetype(x))
end
end
Expand Down
4 changes: 4 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1400,6 +1400,10 @@ end
Base.@deprecate log Base.log
end

@deprecate showcompact(x) show(IOContext(STDOUT, :compact => true), x)
@deprecate showcompact(io, x) show(IOContext(io, :compact => true), x)
@deprecate sprint(::typeof(showcompact), args...) sprint(show, args...; context=:compact => true)

# END 0.7 deprecations

# BEGIN 1.0 deprecations
Expand Down
1 change: 0 additions & 1 deletion base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -624,7 +624,6 @@ export
println,
printstyled,
show,
showcompact,
showerror,
sprint,
summary,
Expand Down
5 changes: 3 additions & 2 deletions base/irrationals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,8 @@ big(::Type{<:AbstractIrrational}) = BigFloat

# align along = for nice Array printing
function alignment(io::IO, x::AbstractIrrational)
m = match(r"^(.*?)(=.*)$", sprint(showcompact, x, context=io, sizehint=0))
m === nothing ? (length(sprint(showcompact, x, context=io, sizehint=0)), 0) :
ctx = IOContext(io, :compact=>true)
m = match(r"^(.*?)(=.*)$", sprint(show, x, context=ctx, sizehint=0))
m === nothing ? (length(sprint(show, x, context=ctx, sizehint=0)), 0) :
(length(m.captures[1]), length(m.captures[2]))
end
38 changes: 2 additions & 36 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,8 @@ IOContext(io::IO, context::IO) = IOContext(unwrapcontext(io)[1], unwrapcontext(c
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
- use `get(dict, key, default)` to retrieve the most recent value for a particular key
- use `(key => value) in io` to see if this particular combination is in the properties set
- use `get(io, key, default)` to retrieve the most recent value for a particular key
The following properties are in common use:
Expand Down Expand Up @@ -1946,40 +1946,6 @@ function Base.showarg(io::IO, r::Iterators.Pairs{<:Any, <:Any, I, D}, toplevel)
print(io, "Iterators.Pairs(::$D, ::$I)")
end

"""
showcompact(x)
showcompact(io::IO, x)
Show a compact representation of a value to `io`. If `io` is not specified, the
default is to print to [`STDOUT`](@ref).
This is used for printing array elements without repeating type information (which would
be redundant with that printed once for the whole array), and without line breaks inside
the representation of an element.
To offer a compact representation different from its standard one, a custom type should
test `get(io, :compact, false)` in its normal [`show`](@ref) method.
# Examples
```jldoctest
julia> A = [1. 2.; 3. 4]
2×2 Array{Float64,2}:
1.0 2.0
3.0 4.0
julia> showcompact(A)
[1.0 2.0; 3.0 4.0]
```
"""
showcompact(x) = showcompact(STDOUT, x)
function showcompact(io::IO, x)
if get(io, :compact, false)
show(io, x)
else
show(IOContext(io, :compact => true), x)
end
end


# printing BitArrays

Expand Down
10 changes: 8 additions & 2 deletions base/strings/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -69,15 +69,21 @@ println(io::IO, xs...) = print(io, xs..., '\n')
## conversion of general objects to strings ##

"""
sprint(f::Function, args...)
sprint(f::Function, args...; context=nothing, sizehint::Integer=0)
Call the given function with an I/O stream and the supplied extra arguments.
Everything written to this I/O stream is returned as a string.
`context` can be either an [`IOContext`](@ref) whose properties will be used,
or a `Pair` specifying a property and its value. `sizehint` suggests the capacity
of the buffer (in bytes).
# Examples
```jldoctest
julia> sprint(showcompact, 66.66666)
julia> sprint(show, 66.66666; context=:compact => true)
"66.6667"
julia> sprint(showerror, BoundsError([1], 100))
"BoundsError: attempt to access 1-element Array{Int64,1} at index [100]"
```
"""
function sprint(f::Function, args...; context=nothing, sizehint::Integer=0)
Expand Down
1 change: 0 additions & 1 deletion doc/src/base/io-network.md
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ Base.IOContext(::IO, ::IOContext)

```@docs
Base.show(::Any)
Base.showcompact
Base.summary
Base.print
Base.println
Expand Down
5 changes: 3 additions & 2 deletions doc/src/manual/networking-and-streams.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,9 @@ julia> print(STDOUT, 0x61)

Sometimes IO output can benefit from the ability to pass contextual information into show methods.
The [`IOContext`](@ref) object provides this framework for associating arbitrary metadata with an IO object.
For example, [`showcompact`](@ref) adds a hinting parameter to the IO object that the invoked show method
should print a shorter output (if applicable).
For example, `:compact => true` adds a hinting parameter to the IO object that the invoked show method
should print a shorter output (if applicable). See the [`IOContext`](@ref) documentation for a list
of common properties.

## Working with Files

Expand Down
4 changes: 2 additions & 2 deletions test/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ for T in (Int64, Float64)
@test complex(Complex{T}) == Complex{T}
end

#showcompact
@test sprint(showcompact, complex(1, 0)) == "1+0im"
#show
@test sprint(show, complex(1, 0), context=:compact => true) == "1+0im"
@test sprint(show, complex(true, true)) == "Complex(true,true)"

@testset "arithmetic" begin
Expand Down
2 changes: 1 addition & 1 deletion test/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -350,7 +350,7 @@ end
@testset "Issue #15739" begin # Compact REPL printouts of an `AbstractDict` use brackets when appropriate
d = Dict((1=>2) => (3=>45), (3=>10) => (10=>11))
buf = IOBuffer()
showcompact(buf, d)
show(IOContext(buf, :compact => true), d)

# Check explicitly for the expected strings, since the CPU bitness effects
# dictionary ordering.
Expand Down
4 changes: 2 additions & 2 deletions test/float16.jl
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ end
@test NaN16 != NaN16
@test isequal(NaN16, NaN16)
@test repr(NaN16) == "NaN16"
@test sprint(showcompact, NaN16) == "NaN"
@test sprint(show, NaN16, context=:compact => true) == "NaN"

@test isinf(Inf16)
@test isinf(-Inf16)
Expand All @@ -120,7 +120,7 @@ end
@test -Inf16 < Inf16
@test isequal(Inf16, Inf16)
@test repr(Inf16) == "Inf16"
@test sprint(showcompact, Inf16) == "Inf"
@test sprint(show, Inf16, context=:compact => true) == "Inf"

@test isnan(reinterpret(Float16,0x7c01))
@test !isinf(reinterpret(Float16,0x7c01))
Expand Down
2 changes: 1 addition & 1 deletion test/missing.jl
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ end

@testset "printing" begin
@test sprint(show, missing) == "missing"
@test sprint(showcompact, missing) == "missing"
@test sprint(show, missing, context=:compact => true) == "missing"
@test sprint(show, [missing]) == "$Missing[missing]"
@test sprint(show, [1 missing]) == "$(Union{Int, Missing})[1 missing]"
b = IOBuffer()
Expand Down
6 changes: 3 additions & 3 deletions test/mpfr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -895,7 +895,7 @@ end
ends::String="",
starts::String="")
sx = sprint(show, x)
scx = sprint(showcompact, x)
scx = sprint(show, x, context=:compact => true)
strx = string(x)
@test sx == strx
@test length(scx) < 20
Expand All @@ -915,8 +915,8 @@ end
test_show_bigfloat(big"-2.3457645687563543266576889678956787e-10000", starts="-2.345", ends="e-10000")

for to_string in [string,
x->sprint(show, x),
x->sprint(showcompact,x)]
x->sprint(show, x),
x->sprint(show, x, context=:compact => true)]
@test to_string(big"0.0") == "0.0"
@test to_string(big"-0.0") == "-0.0"
@test to_string(big"1.0") == "1.0"
Expand Down
18 changes: 9 additions & 9 deletions test/numbers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -410,14 +410,14 @@ end
@test repr(-NaN) == "NaN"
@test repr(Float64(pi)) == "3.141592653589793"
# issue 6608
@test sprint(showcompact, 666666.6) == "6.66667e5"
@test sprint(showcompact, 666666.049) == "666666.0"
@test sprint(showcompact, 666665.951) == "666666.0"
@test sprint(showcompact, 66.66666) == "66.6667"
@test sprint(showcompact, -666666.6) == "-6.66667e5"
@test sprint(showcompact, -666666.049) == "-666666.0"
@test sprint(showcompact, -666665.951) == "-666666.0"
@test sprint(showcompact, -66.66666) == "-66.6667"
@test sprint(show, 666666.6, context=:compact => true) == "6.66667e5"
@test sprint(show, 666666.049, context=:compact => true) == "666666.0"
@test sprint(show, 666665.951, context=:compact => true) == "666666.0"
@test sprint(show, 66.66666, context=:compact => true) == "66.6667"
@test sprint(show, -666666.6, context=:compact => true) == "-6.66667e5"
@test sprint(show, -666666.049, context=:compact => true) == "-666666.0"
@test sprint(show, -666665.951, context=:compact => true) == "-666666.0"
@test sprint(show, -66.66666, context=:compact => true) == "-66.6667"

@test repr(1.0f0) == "1.0f0"
@test repr(-1.0f0) == "-1.0f0"
Expand Down Expand Up @@ -2580,7 +2580,7 @@ end
(-T(Inf), "-Inf")]
@assert x isa T
@test string(x) == sx
@test sprint(io -> show(IOContext(io, :compact => true), x)) == sx
@test sprint(show, x, context=:compact => true) == sx
@test sprint(print, x) == sx
end
end
Expand Down
10 changes: 0 additions & 10 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -738,16 +738,6 @@ end
@test !contains(repr(fill(1.,10,10)), "\u2026")
@test contains(sprint((io, x) -> show(IOContext(io, :limit => true), x), fill(1.,30,30)), "\u2026")

# showcompact() also sets :multiline=>false (#16817)
let io = IOBuffer(),
x = [1, 2]

showcompact(io, x)
@test String(take!(io)) == "[1, 2]"
showcompact(IOContext(io, :compact => true), x)
@test String(take!(io)) == "[1, 2]"
end

let io = IOBuffer()
ioc = IOContext(io, :limit => true)
@test sprint(show, ioc) == "IOContext($(sprint(show, ioc.io)))"
Expand Down

0 comments on commit daf2846

Please sign in to comment.