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

RFC: recshow #1139

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 5 additions & 2 deletions base/base.jl
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,14 @@ type ShowError <: Exception
err::Exception
end

show(io, bt::BackTrace) = show(io,bt.e)
export rshow
rshow(io, x) = show(io, x) # hook to allow showing of self-referential data

show(io, bt::BackTrace) = rshow(io,bt.e)

function show(io, se::ShowError)
println("Error showing value of type ", typeof(se.val), ":")
show(io, se.err)
rshow(io, se.err)
end

method_missing(f, args...) = throw(MethodError(f, args))
Expand Down
4 changes: 2 additions & 2 deletions base/complex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,14 @@ reim(z) = (real(z), imag(z))
function _jl_show(io, z::Complex, compact::Bool)
r, i = reim(z)
if isnan(r) || isfinite(i)
compact ? showcompact(io,r) : show(io,r)
compact ? showcompact(io,r) : rshow(io,r)
if signbit(i)==1 && !isnan(i)
i = -i
print(io, compact ? "-" : " - ")
else
print(io, compact ? "+" : " + ")
end
compact ? showcompact(io, i) : show(io, i)
compact ? showcompact(io, i) : rshow(io, i)
if !(isa(i,Integer) || isa(i,Rational) ||
isa(i,Float) && isfinite(i))
print(io, "*")
Expand Down
4 changes: 2 additions & 2 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ function show(io, t::Associative)
for (k, v) = t
first || print(io, ',')
first = false
show(io, k)
rshow(io, k)
print(io, "=>")
show(io, v)
rshow(io, v)
end
print(io, "}")
end
Expand Down
4 changes: 2 additions & 2 deletions base/expr.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,13 @@ isequal(x::Symbol , y::SymbolNode) = is(x,y.name)

function show(io, tv::TypeVar)
if !is(tv.lb, None)
show(io, tv.lb)
rshow(io, tv.lb)
print(io, "<:")
end
print(io, tv.name)
if !is(tv.ub, Any)
print(io, "<:")
show(io, tv.ub)
rshow(io, tv.ub)
end
end

Expand Down
2 changes: 1 addition & 1 deletion base/io.jl
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ sprint(f::Function, args...) = sprint(0, f, args...)

function repr(x)
s = memio(0, false)
show(s, x)
rshow(s, x)
takebuf_string(s)
end

Expand Down
2 changes: 1 addition & 1 deletion base/rational.jl
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ function show(io, x::Rational)
if isinf(x)
print(io, x.num > 0 ? "Inf" : "-Inf")
else
show(io, num(x)); print(io, "//"); show(io, den(x))
rshow(io, num(x)); print(io, "//"); rshow(io, den(x))
end
end

Expand Down
8 changes: 4 additions & 4 deletions base/regex.jl
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ function show(io, re::Regex)
if (re.options & PCRE.EXTENDED ) != 0; print(io, 'x'); end
else
print(io, "Regex(")
show(io, re.pattern)
rshow(io, re.pattern)
print(io, ',')
show(io, re.options)
rshow(io, re.options)
print(io, ')')
end
end
Expand All @@ -70,12 +70,12 @@ end

function show(io, m::RegexMatch)
print(io, "RegexMatch(")
show(io, m.match)
rshow(io, m.match)
if !isempty(m.captures)
print(io, ", ")
for i = 1:length(m.captures)
print(io, i, "=")
show(io, m.captures[i])
rshow(io, m.captures[i])
if i < length(m.captures)
print(io, ", ")
end
Expand Down
2 changes: 1 addition & 1 deletion base/set.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ Set() = Set{Any}()
Set(x...) = Set{Any}(x...)
Set{T}(x::T...) = Set{T}(x...)

show(io, s::Set) = (show(io, typeof(s)); show_comma_array(io, s,'(',')'))
show(io, s::Set) = (rshow(io, typeof(s)); show_comma_array(io, s,'(',')'))

isempty(s::Set) = isempty(s.hash)
length(s::Set) = length(s.hash)
Expand Down
61 changes: 44 additions & 17 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,40 @@
show(x) = show(OUTPUT_STREAM::IOStream, x)

print(io::IOStream, s::Symbol) = ccall(:jl_print_symbol, Void, (Ptr{Void}, Any,), io, s)
show(io, x) = ccall(:jl_show_any, Void, (Any, Any,), io::IOStream, x)

showcompact(io, x) = show(io, x)
function show(io, x)
if isa(io, IOStream) ccall(:jl_show_any, Void, (Any, Any,), io, x)
else default_show(io, x)
end
end

default_show(io::IO, x::Union(Type, Function)) = print(io, repr(x))
function default_show(io::IO, x)
isa(typeof(x), CompositeKind) ? show_composite(io, x) : print(io, repr(x))
end

function show_composite(io, x)
T::CompositeKind = typeof(x)
names = filter(name->(name!=symbol("")), [T.names...])
values = {}
for name in names
try push(values, getfield(x, name))
catch err; error("default_show: Error accessing field \"$name\" in $T")
end
end
print(io, T.name, '('); show_comma_list(io, values...); print(io, ')')
end
show_comma_list(io::IO) = nothing
function show_comma_list(io::IO, arg, args...)
rshow(io, arg)
for arg in args print(io, ", "); rshow(io, arg) end
end

showcompact(io, x) = rshow(io, x)
showcompact(x) = showcompact(OUTPUT_STREAM::IOStream, x)

show(io, s::Symbol) = print(io, s)
show(io, tn::TypeName) = show(io, tn.name)
show(io, tn::TypeName) = rshow(io, tn.name)
show(io, ::Nothing) = print(io, "nothing")
show(io, b::Bool) = print(io, b ? "true" : "false")
show(io, n::Integer) = (write(io, dec(n));nothing)
Expand All @@ -30,7 +57,7 @@ end

function show(io, l::LambdaStaticData)
print(io, "AST(")
show(io, l.ast)
rshow(io, l.ast)
print(io, ")")
end

Expand All @@ -46,7 +73,7 @@ function show_delim_array(io, itr, op, delim, cl, delim_one)
if newline
if multiline; println(io); end
end
show(io, x)
rshow(io, x)
if done(itr,state)
if delim_one && first
print(io, delim)
Expand Down Expand Up @@ -94,29 +121,29 @@ function show(io, e::Expr)
elseif is(hd,:return)
print(io, "return $(e.args[1])")
elseif is(hd,:string)
show(io, e.args[1])
rshow(io, e.args[1])
elseif is(hd,symbol("::"))
show(io, e.args[1])
rshow(io, e.args[1])
print(io, "::")
show(io, e.args[2])
rshow(io, e.args[2])
elseif is(hd,:quote)
show_quoted_expr(io, e.args[1])
elseif is(hd,:body) || is(hd,:block)
println(io, "\nbegin")
for a in e.args
print(io, " ")
show(io, a)
rshow(io, a)
println(io)
end
println(io, "end")
elseif is(hd,:comparison)
for a in e.args
show(io, a)
rshow(io, a)
end
elseif is(hd,:(.))
show(io, e.args[1])
rshow(io, e.args[1])
print(io, '.')
show(io, e.args[2])
rshow(io, e.args[2])
else
print(io, hd)
show_comma_array(io, e.args,'(',')')
Expand All @@ -136,7 +163,7 @@ function show_quoted_expr(io, a1)
println(io, "\nquote")
for a in a1.args
print(io, " ")
show(io, a)
rshow(io, a)
println(io, )
end
println(io, "end")
Expand Down Expand Up @@ -166,7 +193,7 @@ function show(io, e::TypeError)
end
end

show(io, e::LoadError) = (show(io, e.error); print(io, "\nat $(e.file):$(e.line)"))
show(io, e::LoadError) = (rshow(io, e.error); print(io, "\nat $(e.file):$(e.line)"))
show(io, e::SystemError) = print(io, "$(e.prefix): $(strerror(e.errnum))")
show(io, ::DivideByZeroError) = print(io, "error: integer divide by zero")
show(io, ::StackOverflowError) = print(io, "error: stack overflow")
Expand All @@ -186,7 +213,7 @@ function show(io, e::MethodError)
end

function show(io, bt::BackTrace)
show(io, bt.e)
rshow(io, bt.e)
t = bt.trace
# we may not declare :_jl_eval_user_input
# directly so that we get a compile error
Expand All @@ -212,7 +239,7 @@ function show(io, m::Method)
if !isempty(tv)
show_delim_array(io, tv, '{', ',', '}', false)
end
show(io, m.sig)
rshow(io, m.sig)
li = m.func.code
if li.line > 0
print(io, " at ", li.file, ":", li.line)
Expand All @@ -225,7 +252,7 @@ function show(io, mt::MethodTable)
d = mt.defs
while !is(d,())
print(io, name)
show(io, d)
rshow(io, d)
d = d.next
if !is(d,())
println(io)
Expand Down
2 changes: 1 addition & 1 deletion base/string.jl
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
## core text I/O ##

print(io::IO, x) = show(io, x)
print(io::IO, x) = rshow(io, x)
print(io::IO, xs...) = for x in xs print(io, x) end
println(io::IO, xs...) = print(io, xs..., '\n')

Expand Down
2 changes: 1 addition & 1 deletion base/util.jl
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function whicht(f, types)
while !is(d,())
if is(d.func.code, lsd)
print(stdout_stream, f.env.name)
show(stdout_stream, d); println(stdout_stream)
rshow(stdout_stream, d); println(stdout_stream)
return
end
d = d.next
Expand Down
Loading