From 37f93c97afd20d19e239127c148fb2cfcb6cb9a9 Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 23 Aug 2019 10:27:47 -0500 Subject: [PATCH 1/2] Restore method lookup callback for future tests --- test/runtests.jl | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index d203172..923cddb 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -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) @@ -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]) From 91121c787c728f9be7cbf00633b43effe9a9739c Mon Sep 17 00:00:00 2001 From: Tim Holy Date: Fri, 23 Aug 2019 10:28:22 -0500 Subject: [PATCH 2/2] Return `nothing` for methods with line == 0 Ref #48 --- src/CodeTracking.jl | 7 +++++-- test/runtests.jl | 14 ++++++++++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/CodeTracking.jl b/src/CodeTracking.jl index efe1416..c4db1d9 100644 --- a/src/CodeTracking.jl +++ b/src/CodeTracking.jl @@ -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[] @@ -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) diff --git a/test/runtests.jl b/test/runtests.jl index 923cddb..a396232 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -98,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