Skip to content

Commit 39fc6e5

Browse files
committed
fix #4163, remove xdump and simplify dump
1 parent 9ccc1a1 commit 39fc6e5

File tree

5 files changed

+88
-116
lines changed

5 files changed

+88
-116
lines changed

base/deprecated.jl

+3
Original file line numberDiff line numberDiff line change
@@ -1002,3 +1002,6 @@ export call
10021002

10031003
# 1933
10041004
@deprecate_binding SingleAsyncWork AsyncCondition
1005+
1006+
# #4163
1007+
@deprecate xdump dump

base/docs/helpdb/Base.jl

+1-8
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,7 @@ split
17201720
"""
17211721
dump(x)
17221722
1723-
Show all user-visible structure of a value.
1723+
Show every part of the representation of a value.
17241724
"""
17251725
dump
17261726

@@ -8964,13 +8964,6 @@ true
89648964
"""
89658965
applicable
89668966

8967-
"""
8968-
xdump(x)
8969-
8970-
Show all structure of a value, including all fields of objects.
8971-
"""
8972-
xdump
8973-
89748967
"""
89758968
Base.process_messages(instrm::AsyncStream, outstrm::AsyncStream)
89768969

base/exports.jl

-1
Original file line numberDiff line numberDiff line change
@@ -901,7 +901,6 @@ export
901901
utf32,
902902
warn,
903903
wstring,
904-
xdump,
905904

906905
# random numbers
907906
AbstractRNG,

base/show.jl

+84-106
Original file line numberDiff line numberDiff line change
@@ -883,99 +883,115 @@ function show(io::IO, tv::TypeVar)
883883
end
884884
end
885885

886-
# dump & xdump - structured tree representation like R's str()
887-
# - dump is for the user-facing structure
888-
# - xdump is for the internal structure
889-
#
890-
# x is the object
891-
# n is the depth of traversal in nested types (5 is the default)
892-
# indent is a character string of spaces that is incremented at
893-
# each descent.
894-
#
895-
# Package writers may overload dump for other nested types like lists
896-
# or DataFrames. If overloaded, check the nesting level (n), and if
897-
# n > 0, dump each component. Limit to the first 10 entries. When
898-
# dumping components, decrement n, and add two spaces to indent.
899-
#
900-
# Package writers should not overload xdump.
886+
function dump(io::IO, x::SimpleVector, n::Int, indent)
887+
if isempty(x)
888+
print(io, "empty SimpleVector")
889+
return
890+
end
891+
print(io, "SimpleVector")
892+
if n > 0
893+
for i = 1:length(x)
894+
println(io)
895+
print(io, indent, " ", i, ": ")
896+
if isassigned(x,i)
897+
dump(io, x[i], n - 1, string(indent, " "))
898+
else
899+
print(io, undef_ref_str)
900+
end
901+
end
902+
end
903+
end
901904

902-
function xdump(fn::Function, io::IO, x, n::Int, indent)
905+
function dump(io::IO, x::ANY, n::Int, indent)
903906
T = typeof(x)
904-
print(io, T, " ")
905-
if isa(T, DataType) && nfields(T) > 0
906-
println(io)
907+
if isa(x, Function)
908+
print(io, x, " (function of type ", T, ")")
909+
else
910+
print(io, T)
911+
end
912+
if nfields(T) > 0
907913
if n > 0
908-
for field in fieldnames(T)
909-
if field != symbol("") # prevents segfault if symbol is blank
910-
print(io, indent, " ", field, ": ")
911-
if isdefined(x,field)
912-
fn(io, getfield(x, field), n - 1, string(indent, " "))
913-
else
914-
println(io, undef_ref_str)
915-
end
914+
for field in (isa(x,Tuple) ? (1:length(x)) : fieldnames(T))
915+
println(io)
916+
print(io, indent, " ", field, ": ")
917+
if isdefined(x,field)
918+
dump(io, getfield(x, field), n - 1, string(indent, " "))
919+
else
920+
print(io, undef_ref_str)
916921
end
917922
end
918923
end
919924
else
920-
println(io, x)
925+
!isa(x,Function) && print(io, " ", x)
921926
end
927+
nothing
922928
end
923-
function xdump(fn::Function, io::IO, x::Module, n::Int, indent)
924-
print(io, Module, " ")
925-
println(io, x)
926-
end
927-
function xdump_elts(fn::Function, io::IO, x::Array{Any}, n::Int, indent, i0, i1)
929+
930+
dump(io::IO, x::Module, n::Int, indent) = print(io, "Module ", x)
931+
932+
function dump_elts(io::IO, x::Array, n::Int, indent, i0, i1)
928933
for i in i0:i1
929934
print(io, indent, " ", i, ": ")
930935
if !isdefined(x,i)
931-
println(io, undef_ref_str)
936+
print(io, undef_ref_str)
932937
else
933-
fn(io, x[i], n - 1, string(indent, " "))
938+
dump(io, x[i], n - 1, string(indent, " "))
934939
end
940+
i < i1 && println(io)
935941
end
936942
end
937-
function xdump(fn::Function, io::IO, x::Array{Any}, n::Int, indent)
938-
println(io, "Array($(eltype(x)),$(size(x)))")
939-
if n > 0
940-
xdump_elts(fn, io, x, n, indent, 1, (length(x) <= 10 ? length(x) : 5))
941-
if length(x) > 10
942-
println(io, indent, " ...")
943-
xdump_elts(fn, io, x, n, indent, length(x)-4, length(x))
943+
944+
function dump(io::IO, x::Array, n::Int, indent)
945+
print(io, "Array($(eltype(x)),$(size(x)))")
946+
if eltype(x) <: Number
947+
print(io, " ")
948+
show(io, x)
949+
else
950+
if n > 0 && !isempty(x)
951+
println(io)
952+
if limit_output(io)
953+
dump_elts(io, x, n, indent, 1, (length(x) <= 10 ? length(x) : 5))
954+
if length(x) > 10
955+
println(io)
956+
println(io, indent, " ...")
957+
dump_elts(io, x, n, indent, length(x)-4, length(x))
958+
end
959+
else
960+
dump_elts(io, x, n, indent, 1, length(x))
961+
end
944962
end
945963
end
946964
end
947-
xdump(fn::Function, io::IO, x::Symbol, n::Int, indent) = println(io, typeof(x), " ", x)
948-
xdump(fn::Function, io::IO, x::Function, n::Int, indent) = println(io, x)
949-
xdump(fn::Function, io::IO, x::Array, n::Int, indent) =
950-
(print(io, "Array($(eltype(x)),$(size(x))) ");
951-
show(io, x); println(io))
965+
dump(io::IO, x::Symbol, n::Int, indent) = print(io, typeof(x), " ", x)
952966

953967
# Types
954-
xdump(fn::Function, io::IO, x::Union, n::Int, indent) = println(io, x)
955-
function xdump(fn::Function, io::IO, x::DataType, n::Int, indent)
956-
println(io, x, "::", typeof(x), " ", " <: ", supertype(x))
957-
fields = fieldnames(x)
958-
if n > 0
959-
for idx in 1:min(10, length(fields))
960-
if fields[idx] != symbol("") # prevents segfault if symbol is blank
968+
dump(io::IO, x::Union, n::Int, indent) = print(io, x)
969+
970+
function dump(io::IO, x::DataType, n::Int, indent)
971+
print(io, x)
972+
if x !== Any
973+
print(io, " <: ", supertype(x))
974+
end
975+
if !(x <: Tuple)
976+
fields = fieldnames(x)
977+
if n > 0
978+
for idx in 1:length(fields)
979+
println(io)
961980
print(io, indent, " ", fields[idx], "::")
962-
if isa(x.types[idx], DataType)
963-
xdump(fn, io, fieldtype(x,idx), n - 1, string(indent, " "))
964-
else
965-
println(io, fieldtype(x,idx))
966-
end
981+
#if isa(x.types[idx], DataType)
982+
# xdump(fn, io, fieldtype(x,idx), n - 1, string(indent, " "))
983+
#else
984+
print(io, fieldtype(x,idx))
985+
#end
967986
end
968987
end
969-
if length(fields) > 10
970-
println(io, indent, " ...")
971-
end
972988
end
973989
end
974990

975991
# dumptype is for displaying abstract type hierarchies like Jameson
976992
# Nash's wiki page: https://github.com/JuliaLang/julia/wiki/Types-Hierarchy
977993

978-
function dumptype(io::IO, x, n::Int, indent)
994+
function dumptype(io::IO, x::ANY, n::Int, indent)
979995
# based on Jameson Nash's examples/typetree.jl
980996
println(io, x)
981997
if n == 0 # too deeply nested
@@ -1016,50 +1032,12 @@ end
10161032

10171033
# For abstract types, use _dumptype only if it's a form that will be called
10181034
# interactively.
1019-
xdump(fn::Function, io::IO, x::DataType) = x.abstract ? dumptype(io, x, 5, "") : xdump(fn, io, x, 5, "")
1020-
xdump(fn::Function, io::IO, x::DataType, n::Int) = x.abstract ? dumptype(io, x, n, "") : xdump(fn, io, x, n, "")
1021-
1022-
# defaults:
1023-
xdump(fn::Function, io::IO, x) = xdump(xdump, io, x, 5, "") # default is 5 levels
1024-
xdump(fn::Function, io::IO, x, n::Int) = xdump(xdump, io, x, n, "")
1025-
xdump(fn::Function, io::IO, args...) = throw(ArgumentError("invalid arguments to xdump"))
1026-
xdump(fn::Function, args...) = xdump(fn, STDOUT::IO, args...)
1027-
xdump(io::IO, args...) = xdump(xdump, io, args...)
1028-
xdump(args...) = xdump(xdump, IOContext(STDOUT::IO, :limit_output => true), args...)
1029-
xdump(arg::IO) = xdump(xdump, STDOUT::IO, arg)
1030-
1031-
# Here are methods specifically for dump:
1032-
dump(io::IO, x, n::Int) = dump(io, x, n, "")
1033-
dump(io::IO, x) = dump(io, x, 5, "") # default is 5 levels
1034-
dump(io::IO, x::AbstractString, n::Int, indent) =
1035-
(print(io, typeof(x), " ");
1036-
show(io, x); println(io))
1037-
dump(io::IO, x, n::Int, indent) = xdump(dump, io, x, n, indent)
1038-
dump(io::IO, args...) = throw(ArgumentError("invalid arguments to dump"))
1039-
dump(arg::IO) = xdump(dump, STDOUT::IO, arg)
1040-
dump(args...) = dump(IOContext(STDOUT::IO, :limit_output => true), args...)
1041-
1042-
function dump(io::IO, x::Dict, n::Int, indent)
1043-
println(io, typeof(x), " len ", length(x))
1044-
if n > 0
1045-
i = 1
1046-
for (k,v) in x
1047-
print(io, indent, " ", k, ": ")
1048-
dump(io, v, n - 1, string(indent, " "))
1049-
if i > 10
1050-
println(io, indent, " ...")
1051-
break
1052-
end
1053-
i += 1
1054-
end
1055-
end
1056-
end
1035+
dflt_io() = IOContext(STDOUT::IO, :limit_output => true)
1036+
dump(io::IO, x::DataType; maxdepth=8) = (x.abstract ? dumptype : dump)(io, x, maxdepth, "")
1037+
dump(x::DataType; maxdepth=8) = (x.abstract ? dumptype : dump)(dflt_io(), x, maxdepth, "")
10571038

1058-
# More generic representation for common types:
1059-
dump(io::IO, x::DataType, n::Int, indent) = println(io, x.name)
1060-
dump(io::IO, x::DataType, n::Int) = dump(io, x, n, "")
1061-
dump(io::IO, x::DataType) = dump(io, x, 5, "")
1062-
dump(io::IO, x::TypeVar, n::Int, indent) = println(io, x.name)
1039+
dump(io::IO, arg; maxdepth=8) = dump(io, arg, maxdepth, "")
1040+
dump(arg; maxdepth=8) = dump(dflt_io(), arg, maxdepth, "")
10631041

10641042

10651043
"""

test/show.jl

-1
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,6 @@ oldout = STDOUT
279279
try
280280
rd, wr = redirect_stdout()
281281
@test dump(STDERR) == nothing
282-
@test xdump(STDERR) == nothing
283282
finally
284283
redirect_stdout(oldout)
285284
end

0 commit comments

Comments
 (0)