Skip to content
Merged
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 src/CodeTracking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -183,13 +183,15 @@ end

Return a string with the code that defines `method`. Also return the first line of the
definition, including the signature (which may not be the same line number returned
by `whereis`).
by `whereis`). If the method can't be located (line number 0), then `definition`
instead returns `nothing.`

Note this may not be terribly useful for methods that are defined inside `@eval` statements;
see [`definition(Expr, method::Method)`](@ref) instead.
"""
function definition(::Type{String}, method::Method)
file, line = whereis(method)
line == 0 && return nothing
src = src_from_file_or_REPL(file)
eol = isequal('\n')
linestarts = Int[]
Expand Down Expand Up @@ -222,7 +224,8 @@ end
ex = definition(Expr, method::Method)
ex = definition(method::Method)

Return an expression that defines `method`.
Return an expression that defines `method`. If the definition can't be found,
returns `nothing`.
"""
function definition(::Type{Expr}, method::Method)
file = String(method.file)
Expand Down
16 changes: 16 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
m = first(methods(replfunc))
@test whereis(m) == ("REPL[1]", 1)
# Test with broken lookup
oldlookup = CodeTracking.method_lookup_callback[]
CodeTracking.method_lookup_callback[] = m -> error("oops")
@test whereis(m) == ("REPL[1]", 1)
# Test with definition(String, m)
Expand All @@ -81,6 +82,7 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
@test definition(String, first(methods(f))) == (fstr, 1)
pop!(hp.history)
end
CodeTracking.method_lookup_callback[] = oldlookup

m = first(methods(Test.eval))
@test occursin(Sys.STDLIB, whereis(m)[1])
Expand All @@ -96,6 +98,20 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
lin = src.linetable[idx]
file, line = whereis(lin, m)
@test endswith(file, String(lin.file))

# Issues raised in #48
m = @which(sum([1]; dims=1))
if !isdefined(Main, :Revise)
def = definition(String, m)
@test def === nothing || isa(def[1], AbstractString)
def = definition(Expr, m)
@test def === nothing || isa(def, Expr)
else
def = definition(String, m)
@test isa(def[1], AbstractString)
def = definition(Expr, m)
@test isa(def, Expr)
end
end

@testset "With Revise" begin
Expand Down