From 7962155a33d8557fa5af158e60cc9061a1ea0e1a Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Tue, 15 Mar 2022 15:27:08 +0100 Subject: [PATCH 1/4] let code_string return nothing if definition returns nothing As suggested by @KristofferC in https://github.com/JuliaDocs/Documenter.jl/issues/1779#issuecomment-1066713491. This is related to problems using CodeTracking.jl with Documenter.jl (see https://github.com/JuliaDocs/Documenter.jl/issues/1779) or Jupyter notebooks (see https://github.com/timholy/CodeTracking.jl/issues/51). Instead of throwing an exception, `code_string` now returns `nothing` if `definition` returns `nothing`. This makes it easier to handle such a problematic case by the user. --- src/CodeTracking.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/CodeTracking.jl b/src/CodeTracking.jl index d1a6f42..8ab7db9 100644 --- a/src/CodeTracking.jl +++ b/src/CodeTracking.jl @@ -296,7 +296,14 @@ end Returns the code-string for the method definition for `f` with the specified types. """ -code_string(f, t) = definition(String, which(f, t))[1] +function code_string(f, t) + def = definition(String, which(f, t)) + if def === nothing + return nothing + else + return def[1] + end +end macro code_string(ex0...) InteractiveUtils.gen_call_with_extracted_types_and_kwargs(__module__, :code_string, ex0) end From ccb69f56a348b55c880044911f963e2819b2ef2b Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Mon, 21 Mar 2022 17:05:57 +0100 Subject: [PATCH 2/4] Suggestions from code review Co-authored-by: Tim Holy --- src/CodeTracking.jl | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/src/CodeTracking.jl b/src/CodeTracking.jl index 8ab7db9..867c436 100644 --- a/src/CodeTracking.jl +++ b/src/CodeTracking.jl @@ -298,11 +298,7 @@ Returns the code-string for the method definition for `f` with the specified typ """ function code_string(f, t) def = definition(String, which(f, t)) - if def === nothing - return nothing - else - return def[1] - end + return def === nothing ? nothing : def[1] end macro code_string(ex0...) InteractiveUtils.gen_call_with_extracted_types_and_kwargs(__module__, :code_string, ex0) From 6ee2a4d8d1383abbe708758d426d18e829da663d Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Tue, 19 Apr 2022 07:33:32 +0200 Subject: [PATCH 3/4] add test Co-authored-by: Tim Holy --- test/runtests.jl | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 560de1b..835b5cc 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -148,6 +148,13 @@ isdefined(Main, :Revise) ? Main.Revise.includet("script.jl") : include("script.j m = first(methods(getfield(mod, :eval))) @test definition(String, m) === nothing end + + # Related to issue [#51](https://github.com/timholy/CodeTracking.jl/issues/51) + # and https://github.com/JuliaDocs/Documenter.jl/issues/1779 + ex = :(f_no_linenum(::Int) = 1) + deleteat!(ex.args[2].args, 1) # delete the file & line number info + eval(ex) + @test_nowarn code_string(f_no_linenum, (Int,)) end @testset "With Revise" begin From 65ff7899be42968bc90263be9cd802aee08ff00d Mon Sep 17 00:00:00 2001 From: Hendrik Ranocha Date: Tue, 19 Apr 2022 13:12:47 +0200 Subject: [PATCH 4/4] update test Co-authored-by: Kristoffer Carlsson --- test/runtests.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/runtests.jl b/test/runtests.jl index 835b5cc..4c255c6 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -154,7 +154,7 @@ isdefined(Main, :Revise) ? Main.Revise.includet("script.jl") : include("script.j ex = :(f_no_linenum(::Int) = 1) deleteat!(ex.args[2].args, 1) # delete the file & line number info eval(ex) - @test_nowarn code_string(f_no_linenum, (Int,)) + @test code_string(f_no_linenum, (Int,)) === nothing end @testset "With Revise" begin