Skip to content

Commit 5773e7f

Browse files
authored
Merge pull request #49 from timholy/teh/kwmethnone
Return `nothing` for methods with line == 0
2 parents 3df13a7 + 91121c7 commit 5773e7f

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
6868
m = first(methods(replfunc))
6969
@test whereis(m) == ("REPL[1]", 1)
7070
# Test with broken lookup
71+
oldlookup = CodeTracking.method_lookup_callback[]
7172
CodeTracking.method_lookup_callback[] = m -> error("oops")
7273
@test whereis(m) == ("REPL[1]", 1)
7374
# Test with definition(String, m)
@@ -81,6 +82,7 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
8182
@test definition(String, first(methods(f))) == (fstr, 1)
8283
pop!(hp.history)
8384
end
85+
CodeTracking.method_lookup_callback[] = oldlookup
8486

8587
m = first(methods(Test.eval))
8688
@test occursin(Sys.STDLIB, whereis(m)[1])
@@ -96,6 +98,20 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
9698
lin = src.linetable[idx]
9799
file, line = whereis(lin, m)
98100
@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
99115
end
100116

101117
@testset "With Revise" begin

0 commit comments

Comments
 (0)