Skip to content

Commit 3619248

Browse files
authoredOct 25, 2017
disentangle 2 uses of find_in_path (#24320)
separate into `find_package` and `find_source_file`
1 parent b867d8b commit 3619248

File tree

4 files changed

+24
-88
lines changed

4 files changed

+24
-88
lines changed
 

‎base/initdefs.jl

+2-12
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,9 @@ isinteractive() = (is_interactive::Bool)
5454
5555
An array of paths as strings or custom loader objects for the `require`
5656
function and `using` and `import` statements to consider when loading
57-
code. To create a custom loader type, define the type and then add
58-
appropriate methods to the `Base.load_hook` function with the following
59-
signature:
60-
61-
Base.load_hook(loader::Loader, name::String, found::Any)
62-
63-
The `loader` argument is the current value in `LOAD_PATH`, `name` is the
64-
name of the module to load, and `found` is the path of any previously
65-
found code to provide `name`. If no provider has been found earlier in
66-
`LOAD_PATH` then the value of `found` will be `nothing`. Custom loader
67-
functionality is experimental and may break or change in Julia 1.0.
57+
code.
6858
"""
69-
const LOAD_PATH = Any[]
59+
const LOAD_PATH = String[]
7060
const LOAD_CACHE_PATH = String[]
7161

7262
function init_load_path(JULIA_HOME = JULIA_HOME)

‎base/loading.jl

+20-44
Original file line numberDiff line numberDiff line change
@@ -80,52 +80,28 @@ else
8080
end
8181
end
8282

83-
function load_hook(prefix::String, name::String, ::Void)
84-
name_jl = "$name.jl"
85-
path = joinpath(prefix, name_jl)
86-
isfile_casesensitive(path) && return abspath(path)
87-
path = joinpath(prefix, name_jl, "src", name_jl)
88-
isfile_casesensitive(path) && return abspath(path)
89-
path = joinpath(prefix, name, "src", name_jl)
90-
isfile_casesensitive(path) && return abspath(path)
91-
return nothing
83+
macro return_if_file(path)
84+
quote
85+
path = $(esc(path))
86+
isfile_casesensitive(path) && return path
87+
end
9288
end
93-
load_hook(prefix::String, name::String, path::String) = path
94-
load_hook(prefix, name::String, ::Any) =
95-
throw(ArgumentError("unrecognized custom loader in LOAD_PATH: $prefix"))
9689

97-
_str(x::AbstractString) = String(x)
98-
_str(x) = x
99-
100-
# `wd` is a working directory to search. defaults to current working directory.
101-
# if `wd === nothing`, no extra path is searched.
102-
function find_in_path(name::String, wd::Union{Void,String})
103-
isabspath(name) && return name
104-
base = name
105-
if endswith(name,".jl")
106-
base = name[1:prevind(name, end-2)]
107-
else
108-
name = string(base,".jl")
90+
function find_package(name::String)
91+
endswith(name, ".jl") && (name = chop(name, 0, 3))
92+
for dir in [Pkg.dir(); LOAD_PATH]
93+
dir = abspath(dir)
94+
@return_if_file joinpath(dir, "$name.jl")
95+
@return_if_file joinpath(dir, "$name.jl", "src", "$name.jl")
96+
@return_if_file joinpath(dir, name, "src", "$name.jl")
10997
end
110-
if wd !== nothing
111-
isfile_casesensitive(joinpath(wd,name)) && return joinpath(wd,name)
112-
end
113-
path = nothing
114-
path = _str(load_hook(_str(Pkg.dir()), base, path))
115-
for dir in LOAD_PATH
116-
path = _str(load_hook(_str(dir), base, path))
117-
end
118-
return path
98+
return nothing
11999
end
120-
find_in_path(name::AbstractString, wd::AbstractString = pwd()) =
121-
find_in_path(String(name), String(wd))
122100

123-
function find_source_file(file::String)
124-
(isabspath(file) || isfile(file)) && return file
125-
file2 = find_in_path(file)
126-
file2 !== nothing && return file2
127-
file2 = joinpath(JULIA_HOME, DATAROOTDIR, "julia", "base", file)
128-
return isfile(file2) ? file2 : nothing
101+
function find_source_file(path::String)
102+
(isabspath(path) || isfile(path)) && return path
103+
base_path = joinpath(JULIA_HOME, DATAROOTDIR, "julia", "base", path)
104+
return isfile(base_path) ? base_path : nothing
129105
end
130106

131107
function find_all_in_cache_path(mod::Symbol)
@@ -416,7 +392,7 @@ function _require(mod::Symbol)
416392
toplevel_load[] = false
417393
# perform the search operation to select the module file require intends to load
418394
name = string(mod)
419-
path = find_in_path(name, nothing)
395+
path = find_package(name)
420396
if path === nothing
421397
throw(ArgumentError("Module $name not found in current path.\nRun `Pkg.add(\"$name\")` to install the $name package."))
422398
end
@@ -628,7 +604,7 @@ for important notes.
628604
"""
629605
function compilecache(name::String)
630606
# decide where to get the source file from
631-
path = find_in_path(name, nothing)
607+
path = find_package(name)
632608
path === nothing && throw(ArgumentError("$name not found in path"))
633609
path = String(path)
634610
# decide where to put the resulting cache file
@@ -778,7 +754,7 @@ function stale_cachefile(modpath::String, cachefile::String)
778754
continue
779755
end
780756
name = string(mod)
781-
path = find_in_path(name, nothing)
757+
path = find_package(name)
782758
if path === nothing
783759
return true # Won't be able to fullfill dependency
784760
end

‎base/precompile.jl

+2-5
Original file line numberDiff line numberDiff line change
@@ -1720,7 +1720,8 @@ precompile(Tuple{typeof(Base.Filesystem.samefile), String, String})
17201720
precompile(Tuple{typeof(Base.Filesystem.unlink), String})
17211721
precompile(Tuple{typeof(Base.finalizer), Base.Distributed.Future, typeof(Base.Distributed.finalize_ref)})
17221722
precompile(Tuple{typeof(Base.find_all_in_cache_path), Symbol})
1723-
precompile(Tuple{typeof(Base.find_in_path), String, Void})
1723+
precompile(Tuple{typeof(Base.find_package), String})
1724+
precompile(Tuple{typeof(Base.find_source_file), String})
17241725
precompile(Tuple{typeof(Base.getindex), Base.ObjectIdDict, Symbol})
17251726
precompile(Tuple{typeof(Base.getindex), Type{Tuple{String, Float64}}, Tuple{String, Float64}})
17261727
precompile(Tuple{typeof(Base.Grisu._show), Base.IOContext{Base.GenericIOBuffer{Array{UInt8, 1}}}, Float64, Int64, Int64, Bool, Bool})
@@ -1745,8 +1746,6 @@ precompile(Tuple{typeof(Base.isequal), String, Float64})
17451746
precompile(Tuple{typeof(Base.isequal), Tuple{String, Float64}, Tuple{String, Float64}})
17461747
precompile(Tuple{typeof(Base._jl_spawn), String, Array{String, 1}, Ptr{Void}, Base.Process, Base.PipeEndpoint, Base.TTY, Base.Filesystem.File})
17471748
precompile(Tuple{typeof(Base.julia_cmd)})
1748-
precompile(Tuple{typeof(Base.load_hook), String, String, String})
1749-
precompile(Tuple{typeof(Base.load_hook), String, String, Void})
17501749
precompile(Tuple{typeof(Base.next), Base.Generator{Array{Any, 1}, typeof(Base.FastMath.make_fastmath)}, Int64})
17511750
precompile(Tuple{typeof(Base.open), Base.CmdRedirect, String, Base.TTY})
17521751
precompile(Tuple{typeof(Base.open), String, String})
@@ -1836,8 +1835,6 @@ precompile(Tuple{typeof(Base.stale_cachefile), String, String})
18361835
precompile(Tuple{typeof(Base.start), Tuple{Symbol, UInt64}})
18371836
precompile(Tuple{typeof(Base.start), Tuple{Void, Void}})
18381837
precompile(Tuple{typeof(Base.:(>)), String, String})
1839-
precompile(Tuple{typeof(Base._str), String})
1840-
precompile(Tuple{typeof(Base._str), Void})
18411838
precompile(Tuple{typeof(Base.sync_add), Base.Distributed.Future})
18421839
precompile(Tuple{typeof(Base.trunc), Float64, Int64, Int64})
18431840
precompile(Tuple{typeof(Base.union!), Base.Set{Tuple{String, Float64}}, Tuple{Tuple{String, Float64}}})

‎test/loading.jl

-27
Original file line numberDiff line numberDiff line change
@@ -78,30 +78,3 @@ mktempdir() do dir
7878
end
7979
end
8080
end
81-
82-
SAVED_LOAD_PATH = copy(LOAD_PATH)
83-
empty!(LOAD_PATH)
84-
dir = abspath(@__DIR__)
85-
push!(LOAD_PATH, dir)
86-
87-
@test Base.find_in_path("test_sourcepath") == joinpath(dir, "test_sourcepath.jl")
88-
@test Base.find_in_path(GenericString("test_sourcepath")) == joinpath(dir, "test_sourcepath.jl")
89-
LOAD_PATH[end] = GenericString(LOAD_PATH[end])
90-
@test Base.find_in_path("test_sourcepath") == joinpath(dir, "test_sourcepath.jl")
91-
92-
struct CustomLoader
93-
path::String
94-
end
95-
push!(LOAD_PATH, CustomLoader("abc"))
96-
let name = randstring(20)
97-
@test_throws ArgumentError Base.find_in_path(name, nothing)
98-
Base.load_hook(prefix::CustomLoader, name::String, found) = joinpath(prefix.path, name)
99-
@test Base.find_in_path(name, nothing) == joinpath("abc", name)
100-
end
101-
@test Base.find_in_path("test_sourcepath", nothing) == joinpath("abc", "test_sourcepath")
102-
Base.load_hook(prefix::CustomLoader, name::String, found::String) = found
103-
@test Base.find_in_path("test_sourcepath", nothing) == joinpath(dir, "test_sourcepath.jl")
104-
105-
empty!(LOAD_PATH)
106-
append!(LOAD_PATH, SAVED_LOAD_PATH)
107-
@test LOAD_PATH == SAVED_LOAD_PATH

0 commit comments

Comments
 (0)
Please sign in to comment.