Skip to content

Commit 91121c7

Browse files
committed
Return nothing for methods with line == 0
Ref #48
1 parent 37f93c9 commit 91121c7

File tree

2 files changed

+19
-2
lines changed

2 files changed

+19
-2
lines changed

src/CodeTracking.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,13 +183,15 @@ end
183183
184184
Return a string with the code that defines `method`. Also return the first line of the
185185
definition, including the signature (which may not be the same line number returned
186-
by `whereis`).
186+
by `whereis`). If the method can't be located (line number 0), then `definition`
187+
instead returns `nothing.`
187188
188189
Note this may not be terribly useful for methods that are defined inside `@eval` statements;
189190
see [`definition(Expr, method::Method)`](@ref) instead.
190191
"""
191192
function definition(::Type{String}, method::Method)
192193
file, line = whereis(method)
194+
line == 0 && return nothing
193195
src = src_from_file_or_REPL(file)
194196
eol = isequal('\n')
195197
linestarts = Int[]
@@ -222,7 +224,8 @@ end
222224
ex = definition(Expr, method::Method)
223225
ex = definition(method::Method)
224226
225-
Return an expression that defines `method`.
227+
Return an expression that defines `method`. If the definition can't be found,
228+
returns `nothing`.
226229
"""
227230
function definition(::Type{Expr}, method::Method)
228231
file = String(method.file)

test/runtests.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,20 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
9898
lin = src.linetable[idx]
9999
file, line = whereis(lin, m)
100100
@test endswith(file, String(lin.file))
101+
102+
# Issues raised in #48
103+
m = @which(sum([1]; dims=1))
104+
if !isdefined(Main, :Revise)
105+
def = definition(String, m)
106+
@test def === nothing || isa(def[1], AbstractString)
107+
def = definition(Expr, m)
108+
@test def === nothing || isa(def, Expr)
109+
else
110+
def = definition(String, m)
111+
@test isa(def[1], AbstractString)
112+
def = definition(Expr, m)
113+
@test isa(def, Expr)
114+
end
101115
end
102116

103117
@testset "With Revise" begin

0 commit comments

Comments
 (0)