Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve show for 0-dim array #33206

Merged
merged 11 commits into from
Oct 3, 2019
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Language changes

* Calling `show` or `repr` on an `undef`/`UndefInitializer()` array initializer now shows valid Julia code ([#33211]).

* Calling `show` or `repr` on a 0-dimensional `AbstractArray` now shows valid code for creating an equivalent 0-dimensional array, instead of only showing the contained value. ([#33206])

Multi-threading changes
-----------------------

Expand All @@ -38,6 +40,7 @@ Standard library changes

* Verbose `display` of `Char` (`text/plain` output) now shows the codepoint value in standard-conforming `"U+XXXX"` format ([#33291]).


#### Libdl

#### LinearAlgebra
Expand Down
25 changes: 18 additions & 7 deletions base/arrayshow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -299,14 +299,12 @@ end

# print_array: main helper functions for show(io, text/plain, array)
# typeinfo agnostic

# 0-dimensional arrays
print_array(io::IO, X::AbstractArray{T,0} where T) =
isassigned(X) ? show(io, X[]) :
print(io, undef_ref_str)

# Note that this is for showing the content inside the array, and for `MIME"text/plain".
# There are `show(::IO, ::A) where A<:AbstractArray` methods that don't use this
# e.g. show_vector, show_zero_dim
print_array(io::IO, X::AbstractArray{<:Any, 0}) =
isassigned(X) ? show(io, X[]) : print(io, undef_ref_str)
nickrobinson251 marked this conversation as resolved.
Show resolved Hide resolved
print_array(io::IO, X::AbstractVecOrMat) = print_matrix(io, X)

print_array(io::IO, X::AbstractArray) = show_nd(io, X, print_matrix, true)

# typeinfo aware
Expand Down Expand Up @@ -415,6 +413,7 @@ _show_empty(io, X) = nothing # by default, we don't know this constructor

# typeinfo aware (necessarily)
function show(io::IO, X::AbstractArray)
ndims(X) == 0 && return show_zero_dim(io, X)
ndims(X) == 1 && return show_vector(io, X)
nickrobinson251 marked this conversation as resolved.
Show resolved Hide resolved
prefix = typeinfo_prefix(io, X)
io = IOContext(io, :typeinfo => eltype(X))
Expand All @@ -423,6 +422,18 @@ function show(io::IO, X::AbstractArray)
_show_nonempty(io, X, prefix)
end

### 0-dimensional arrays (#31481)
function show_zero_dim(io::IO, X::AbstractArray{T, 0}) where T
if isassigned(X)
print(io, "fill(")
show(io, X[])
else
print(io, "Array{$T,0}(")
show(io, undef)
end
print(io, ")")
end

### Vector arrays

# typeinfo aware
Expand Down
2 changes: 1 addition & 1 deletion test/offsetarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ targets1 = ["0-dimensional $OAs_name.OffsetArray{Float64,0,Array{Float64,0}}:\n1
"1×1 $OAs_name.OffsetArray{Float64,2,Array{Float64,2}} with indices 2:2×3:3:\n 1.0",
"1×1×1 $OAs_name.OffsetArray{Float64,3,Array{Float64,3}} with indices 2:2×3:3×4:4:\n[:, :, 4] =\n 1.0",
"1×1×1×1 $OAs_name.OffsetArray{Float64,4,Array{Float64,4}} with indices 2:2×3:3×4:4×5:5:\n[:, :, 4, 5] =\n 1.0"]
targets2 = ["(1.0, 1.0)",
targets2 = ["(fill(1.0), fill(1.0))",
"([1.0], [1.0])",
"([1.0], [1.0])",
"([1.0], [1.0])",
Expand Down
27 changes: 27 additions & 0 deletions test/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1583,3 +1583,30 @@ end
@test sdastr(2, 2) == "[2, 3]"
@test sdastr(3, 3) == "[3, 4, 5]"
end

@testset "0-dimensional Array. Issue #31481" begin
for x in (zeros(Int32), collect('b'), fill(nothing), BitArray(0))
@test eval(Meta.parse(repr(x))) == x
end
@test showstr(zeros(Int32)) == "fill(0)"
@test showstr(collect('b')) == "fill('b')"
@test showstr(fill(nothing)) == "fill(nothing)"
@test showstr(BitArray(0)) == "fill(false)"
mbauman marked this conversation as resolved.
Show resolved Hide resolved

@test replstr(zeros(Int32)) == "0-dimensional Array{Int32,0}:\n0"
@test replstr(collect('b')) == "0-dimensional Array{Char,0}:\n'b'"
@test replstr(fill(nothing)) == "0-dimensional Array{Nothing,0}:\nnothing"
@test replstr(BitArray(0)) == "0-dimensional BitArray{0}:\n0"

# UndefInitializer
@test showstr(fill(undef)) == "fill($undef)"
@test replstr(fill(undef)) == "0-dimensional Array{UndefInitializer,0}:\n$undef"

# `#undef` values
@test showstr(Array{String, 0}(undef)) == "Array{String,0}($undef)"
@test replstr(Array{String, 0}(undef)) == "0-dimensional Array{String,0}:\n$(Base.undef_ref_str)"

# "undef" with isbits type
@test startswith(showstr(Array{Int32, 0}(undef)), "fill(")
@test startswith(replstr(Array{Int32, 0}(undef)), "0-dimensional Array{Int32,0}:\n")
end