Skip to content
Closed
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
12 changes: 10 additions & 2 deletions src/CodeTracking.jl
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ const juliastdlib = joinpath("julia", "stdlib", "v$(VERSION.major).$(VERSION.min

Return the file and line of the definition of `method`. `line`
is the first line of the method's body.
If for some reason the file can not be determined, then return `nothing`
"""
function whereis(method::Method)
file, line = String(method.file), method.line
method.file == :none && return nothing

file = String(method.file)
line = method.line
startswith(file, "REPL[") && return file, line
lin = get(method_info, method.sig, nothing)
if lin === nothing
Expand Down Expand Up @@ -184,12 +188,16 @@ 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`).
If the method could not be found for some reason, then return `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)
loc = whereis(method)
loc === nothing && return nothing

file, line = loc
src = src_from_file_or_REPL(file)
eol = isequal('\n')
linestarts = Int[]
Expand Down
8 changes: 8 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,14 @@ isdefined(Main, :Revise) ? includet("script.jl") : include("script.jl")
lin = src.linetable[idx]
file, line = whereis(lin, m)
@test endswith(file, String(lin.file))


# kwargs that result in not finding the file
m = @which sum([1]; dims=1)
loc = whereis(m)
@test loc != ("none", 0) # old incorrect behavour.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test is redundant with the one below.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is indeed, but it serves to illustrate what was wrong before.
It can be removed, but I thought it might be helpful in future changes.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it should be removed?

Suggested change
@test loc != ("none", 0) # old incorrect behavour.

@test loc === nothing
@test definition(String, m) === nothing
end

@testset "With Revise" begin
Expand Down