-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
/
Copy pathrepl.jl
174 lines (160 loc) · 5.47 KB
/
repl.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
# fallback text/plain representation of any type:
writemime(io, ::MIME"text/plain", x) = showlimited(io, x)
function writemime(io::IO, ::MIME"text/plain", f::Function)
if isgeneric(f)
n = length(f.env)
m = n==1 ? "method" : "methods"
print(io, "$(f.env.name) (generic function with $n $m)")
else
show(io, f)
end
end
function writemime(io::IO, ::MIME"text/plain", v::AbstractVector)
if isa(v, Ranges)
show(io, v)
else
print(io, summary(v))
if !isempty(v)
println(io, ":")
print_matrix(io, v)
end
end
end
function writemime(io::IO, ::MIME"text/plain", v::DataType)
show(io, v)
methods(v) # force constructor creation
if isgeneric(v)
n = length(v.env)
m = n==1 ? "method" : "methods"
print(io, " (constructor with $n $m)")
end
end
# showing exception objects as descriptive error messages
showerror(io::IO, e) = show(io, e)
function showerror(io::IO, e::TypeError)
ctx = isempty(e.context) ? "" : "in $(e.context), "
if e.expected === Bool
print(io, "type: non-boolean ($(typeof(e.got))) ",
"used in boolean context")
else
if isa(e.got,Type)
tstr = "Type{$(e.got)}"
else
tstr = string(typeof(e.got))
end
print(io, "type: $(e.func): ",
"$(ctx)expected $(e.expected), ",
"got $tstr")
if e.func === :apply && e.expected <: Function && isa(e.got,AbstractArray)
println(io)
print(io, "Use square brackets [] for indexing.")
end
end
end
function showerror(io::IO, e, bt)
try
showerror(io, e)
finally
show_backtrace(io, bt)
end
end
showerror(io::IO, e::LoadError) = showerror(io, e, {})
function showerror(io::IO, e::LoadError, bt)
showerror(io, e.error, bt)
print(io, "\nat $(e.file):$(e.line)")
end
function showerror(io::IO, e::DomainError, bt)
print(io, "DomainError")
for b in bt
code = ccall(:jl_lookup_code_address, Any, (Ptr{Void}, Int32), b, 0)
if length(code) == 3
if code[1] in (:log, :log2, :log10, :sqrt) # TODO add :besselj, :besseli, :bessely, :besselk
print(io, "\n", code[1],
" will only return a complex result if called with a complex argument.",
"\ntry ", code[1], "(complex(x))")
end
break
end
end
show_backtrace(io, bt)
end
showerror(io::IO, e::SystemError) = print(io, "$(e.prefix): $(strerror(e.errnum))")
showerror(io::IO, ::DivideError) = print(io, "integer division error")
showerror(io::IO, ::StackOverflowError) = print(io, "stack overflow")
showerror(io::IO, ::UndefRefError) = print(io, "access to undefined reference")
showerror(io::IO, ::EOFError) = print(io, "read: end of file")
showerror(io::IO, e::ErrorException) = print(io, e.msg)
showerror(io::IO, e::KeyError) = (print(io, "key not found: "); show(io, e.key))
showerror(io::IO, e::InterruptException) = print(io, "interrupt")
function showerror(io::IO, e::MethodError)
name = e.f.env.name
if is(e.f,convert) && length(e.args)==2
print(io, "no method $(name)(Type{$(e.args[1])},$(typeof(e.args[2])))")
elseif isa(e.f, DataType)
print(io, "no method $(e.f)$(typeof(e.args))")
else
print(io, "no method $(name)$(typeof(e.args))")
end
if isdefined(Base,name)
f = eval(Base,name)
if f !== e.f && isgeneric(f) && applicable(f,e.args...)
println(io)
print(io, "you may have intended to import Base.$(name)")
end
end
end
function show_trace_entry(io, fname, file, line, n)
print(io, "\n")
print(io, " in ", fname, " at ", file)
if line >= 1
try
print(io, ":", line)
catch
print(io, '?') #for when dec is not yet defined
end
end
if n > 1
print(io, " (repeats ", n, " times)")
end
end
function show_backtrace(io::IO, t, set=1:typemax(Int))
# we may not declare :eval_user_input
# directly so that we get a compile error
# in case its name changes in the future
show_backtrace(io, try
symbol(string(eval_user_input))
catch
:(:) #for when client.jl is not yet defined
end, t, set)
end
# show the backtrace, up to (but not including) top_function
function show_backtrace(io::IO, top_function::Symbol, t, set)
n = 1
lastfile = ""; lastline = -11; lastname = symbol("#")
local fname, file, line
count = 0
for i = 1:length(t)
lkup = ccall(:jl_lookup_code_address, Any, (Ptr{Void}, Int32), t[i], 0)
if lkup === ()
continue
end
fname, file, line = lkup
if i == 1 && fname == :error; continue; end
if fname == top_function; break; end
count += 1
if !in(count, set); continue; end
if file != lastfile || line != lastline || fname != lastname
if lastline != -11
show_trace_entry(io, lastname, lastfile, lastline, n)
end
n = 1
lastfile = file; lastline = line; lastname = fname
else
n += 1
end
end
if n > 1 || lastline != -11
show_trace_entry(io, lastname, lastfile, lastline, n)
end
@windows_only if WORD_SIZE == 64; println(); warn_once("backtraces on your platform are often misleading or partially incorrect") end
end