Skip to content

Commit 0b53af8

Browse files
authored
Some robustness improvements (#69)
* Check funcdef expressions and give up faster * Check for existence of file before trying to load the source
1 parent 9fc72d2 commit 0b53af8

File tree

5 files changed

+21
-3
lines changed

5 files changed

+21
-3
lines changed

Project.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ julia = "1"
1313
[extras]
1414
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
1515
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
16+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
1617
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"
1718

1819
[targets]
19-
test = ["ColorTypes", "LinearAlgebra", "Test"]
20+
test = ["ColorTypes", "LinearAlgebra", "SparseArrays", "Test"]

src/CodeTracking.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ function definition(::Type{String}, method::Method)
206206
file, line = whereis(method)
207207
line == 0 && return nothing
208208
src = src_from_file_or_REPL(file)
209+
src === nothing && return nothing
209210
src = replace(src, "\r"=>"")
210211
eol = isequal('\n')
211212
linestarts = Int[]
@@ -222,7 +223,8 @@ function definition(::Type{String}, method::Method)
222223
end
223224
# The function declaration was presumably on a previous line
224225
lineindex = lastindex(linestarts)
225-
while !isfuncexpr(ex, method.name) && lineindex > 0
226+
linestop = max(0, lineindex - 20)
227+
while !isfuncexpr(ex, method.name) && lineindex > linestop
226228
istart = linestarts[lineindex]
227229
try
228230
ex, iend = Meta.parse(src, istart)
@@ -231,6 +233,7 @@ function definition(::Type{String}, method::Method)
231233
lineindex -= 1
232234
line -= 1
233235
end
236+
lineindex <= linestop && return nothing
234237
return chomp(src[istart:iend-1]), line
235238
end
236239

src/utils.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ function checkname(fdef::Expr, name)
77
# E.g. `function Mod.bar.foo(a, b)`
88
return checkname(fproto.args[end], name)
99
end
10+
isa(fproto, Symbol) || isa(fproto, QuoteNode) || isa(fproto, Expr) || return false
1011
return checkname(fproto, name)
1112
end
1213
checkname(fname::Symbol, name::Symbol) = begin
@@ -75,6 +76,7 @@ function src_from_file_or_REPL(origin::AbstractString, args...)
7576
if m !== nothing
7677
return src_from_REPL(m.captures[1], args...)
7778
end
79+
isfile(origin) || return nothing
7880
return read(origin, String)
7981
end
8082

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
ColorTypes = "3da002f7-5984-5a60-b8a6-cbb66c0b333f"
33
InteractiveUtils = "b77e0a4c-d291-57a0-90e8-8db25a27a240"
44
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
5+
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
56
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

test/runtests.jl

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# Note: some of CodeTracking's functionality can only be tested by Revise
22

33
using CodeTracking
4-
using Test, InteractiveUtils, LinearAlgebra
4+
using Test, InteractiveUtils, LinearAlgebra, SparseArrays
55
# Note: ColorTypes needs to be installed, but note the intentional absence of `using ColorTypes`
66

77
using CodeTracking: line_is_decl
@@ -127,6 +127,17 @@ isdefined(Main, :Revise) ? Main.Revise.includet("script.jl") : include("script.j
127127
B = Hermitian(hcat([one(BigFloat) + im]))
128128
m = @which cholesky(B)
129129
@test startswith(definition(String, m)[1], "cholesky")
130+
131+
# Ensure that we don't error on difficult cases
132+
m = which(+, (AbstractSparseVector, AbstractSparseVector)) # defined inside an `@eval`
133+
d = definition(String, m)
134+
@test d === nothing || isa(d[1], String)
135+
136+
# Check for existence of file
137+
id = Base.PkgId("__PackagePrecompilationStatementModule")
138+
mod = Base.root_module(id)
139+
m = first(methods(getfield(mod, :eval)))
140+
@test definition(String, m) === nothing
130141
end
131142

132143
@testset "With Revise" begin

0 commit comments

Comments
 (0)