Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disentangle two uses of find_in_path: find_package, find_source_file #24320

Merged
merged 3 commits into from
Oct 25, 2017
Merged
Show file tree
Hide file tree
Changes from 2 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
14 changes: 2 additions & 12 deletions base/initdefs.jl
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,9 @@ isinteractive() = (is_interactive::Bool)

An array of paths as strings or custom loader objects for the `require`
function and `using` and `import` statements to consider when loading
code. To create a custom loader type, define the type and then add
appropriate methods to the `Base.load_hook` function with the following
signature:

Base.load_hook(loader::Loader, name::String, found::Any)

The `loader` argument is the current value in `LOAD_PATH`, `name` is the
name of the module to load, and `found` is the path of any previously
found code to provide `name`. If no provider has been found earlier in
`LOAD_PATH` then the value of `found` will be `nothing`. Custom loader
functionality is experimental and may break or change in Julia 1.0.
code.
"""
const LOAD_PATH = Any[]
const LOAD_PATH = String[]
const LOAD_CACHE_PATH = String[]

function init_load_path(JULIA_HOME = JULIA_HOME)
Expand Down
64 changes: 20 additions & 44 deletions base/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -80,52 +80,28 @@ else
end
end

function load_hook(prefix::String, name::String, ::Void)
name_jl = "$name.jl"
path = joinpath(prefix, name_jl)
isfile_casesensitive(path) && return abspath(path)
path = joinpath(prefix, name_jl, "src", name_jl)
isfile_casesensitive(path) && return abspath(path)
path = joinpath(prefix, name, "src", name_jl)
isfile_casesensitive(path) && return abspath(path)
return nothing
macro return_if_file(path)
quote
path = $(esc(path))
isfile_casesensitive(path) && return path
end
end
load_hook(prefix::String, name::String, path::String) = path
load_hook(prefix, name::String, ::Any) =
throw(ArgumentError("unrecognized custom loader in LOAD_PATH: $prefix"))

_str(x::AbstractString) = String(x)
_str(x) = x

# `wd` is a working directory to search. defaults to current working directory.
# if `wd === nothing`, no extra path is searched.
function find_in_path(name::String, wd::Union{Void,String})
isabspath(name) && return name
base = name
if endswith(name,".jl")
base = name[1:prevind(name, end-2)]
else
name = string(base,".jl")
function find_package(name::String)
endswith(name, ".jl") && (name = name[1:end-3])
Copy link
Sponsor Member

Choose a reason for hiding this comment

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

base = name[1:prevind(name, end - 2)] (prevind(end - 2) != end - 3)

for dir in [Pkg.dir(); LOAD_PATH]
dir = abspath(dir)
@return_if_file joinpath(dir, "$name.jl")
@return_if_file joinpath(dir, "$name.jl", "src", "$name.jl")
@return_if_file joinpath(dir, name, "src", "$name.jl")
end
if wd !== nothing
isfile_casesensitive(joinpath(wd,name)) && return joinpath(wd,name)
end
path = nothing
path = _str(load_hook(_str(Pkg.dir()), base, path))
for dir in LOAD_PATH
path = _str(load_hook(_str(dir), base, path))
end
return path
return nothing
end
find_in_path(name::AbstractString, wd::AbstractString = pwd()) =
find_in_path(String(name), String(wd))

function find_source_file(file::String)
(isabspath(file) || isfile(file)) && return file
file2 = find_in_path(file)
file2 !== nothing && return file2
file2 = joinpath(JULIA_HOME, DATAROOTDIR, "julia", "base", file)
return isfile(file2) ? file2 : nothing
function find_source_file(path::String)
(isabspath(path) || isfile(path)) && return path
base_path = joinpath(JULIA_HOME, DATAROOTDIR, "julia", "base", path)
return isfile(base_path) ? base_path : nothing
end

function find_all_in_cache_path(mod::Symbol)
Expand Down Expand Up @@ -416,7 +392,7 @@ function _require(mod::Symbol)
toplevel_load[] = false
# perform the search operation to select the module file require intends to load
name = string(mod)
path = find_in_path(name, nothing)
path = find_package(name)
if path === nothing
throw(ArgumentError("Module $name not found in current path.\nRun `Pkg.add(\"$name\")` to install the $name package."))
end
Expand Down Expand Up @@ -628,7 +604,7 @@ for important notes.
"""
function compilecache(name::String)
# decide where to get the source file from
path = find_in_path(name, nothing)
path = find_package(name)
path === nothing && throw(ArgumentError("$name not found in path"))
path = String(path)
# decide where to put the resulting cache file
Expand Down Expand Up @@ -778,7 +754,7 @@ function stale_cachefile(modpath::String, cachefile::String)
continue
end
name = string(mod)
path = find_in_path(name, nothing)
path = find_package(name)
if path === nothing
return true # Won't be able to fullfill dependency
end
Expand Down
7 changes: 2 additions & 5 deletions base/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1720,7 +1720,8 @@ precompile(Tuple{typeof(Base.Filesystem.samefile), String, String})
precompile(Tuple{typeof(Base.Filesystem.unlink), String})
precompile(Tuple{typeof(Base.finalizer), Base.Distributed.Future, typeof(Base.Distributed.finalize_ref)})
precompile(Tuple{typeof(Base.find_all_in_cache_path), Symbol})
precompile(Tuple{typeof(Base.find_in_path), String, Void})
precompile(Tuple{typeof(Base.find_package), String})
precompile(Tuple{typeof(Base.find_source_file), String})
precompile(Tuple{typeof(Base.getindex), Base.ObjectIdDict, Symbol})
precompile(Tuple{typeof(Base.getindex), Type{Tuple{String, Float64}}, Tuple{String, Float64}})
precompile(Tuple{typeof(Base.Grisu._show), Base.IOContext{Base.GenericIOBuffer{Array{UInt8, 1}}}, Float64, Int64, Int64, Bool, Bool})
Expand All @@ -1745,8 +1746,6 @@ precompile(Tuple{typeof(Base.isequal), String, Float64})
precompile(Tuple{typeof(Base.isequal), Tuple{String, Float64}, Tuple{String, Float64}})
precompile(Tuple{typeof(Base._jl_spawn), String, Array{String, 1}, Ptr{Void}, Base.Process, Base.PipeEndpoint, Base.TTY, Base.Filesystem.File})
precompile(Tuple{typeof(Base.julia_cmd)})
precompile(Tuple{typeof(Base.load_hook), String, String, String})
precompile(Tuple{typeof(Base.load_hook), String, String, Void})
precompile(Tuple{typeof(Base.next), Base.Generator{Array{Any, 1}, typeof(Base.FastMath.make_fastmath)}, Int64})
precompile(Tuple{typeof(Base.open), Base.CmdRedirect, String, Base.TTY})
precompile(Tuple{typeof(Base.open), String, String})
Expand Down Expand Up @@ -1836,8 +1835,6 @@ precompile(Tuple{typeof(Base.stale_cachefile), String, String})
precompile(Tuple{typeof(Base.start), Tuple{Symbol, UInt64}})
precompile(Tuple{typeof(Base.start), Tuple{Void, Void}})
precompile(Tuple{typeof(Base.:(>)), String, String})
precompile(Tuple{typeof(Base._str), String})
precompile(Tuple{typeof(Base._str), Void})
precompile(Tuple{typeof(Base.sync_add), Base.Distributed.Future})
precompile(Tuple{typeof(Base.trunc), Float64, Int64, Int64})
precompile(Tuple{typeof(Base.union!), Base.Set{Tuple{String, Float64}}, Tuple{Tuple{String, Float64}}})
Expand Down
27 changes: 0 additions & 27 deletions test/loading.jl
Original file line number Diff line number Diff line change
Expand Up @@ -78,30 +78,3 @@ mktempdir() do dir
end
end
end

SAVED_LOAD_PATH = copy(LOAD_PATH)
empty!(LOAD_PATH)
dir = abspath(@__DIR__)
push!(LOAD_PATH, dir)

@test Base.find_in_path("test_sourcepath") == joinpath(dir, "test_sourcepath.jl")
@test Base.find_in_path(GenericString("test_sourcepath")) == joinpath(dir, "test_sourcepath.jl")
LOAD_PATH[end] = GenericString(LOAD_PATH[end])
@test Base.find_in_path("test_sourcepath") == joinpath(dir, "test_sourcepath.jl")

struct CustomLoader
path::String
end
push!(LOAD_PATH, CustomLoader("abc"))
let name = randstring(20)
@test_throws ArgumentError Base.find_in_path(name, nothing)
Base.load_hook(prefix::CustomLoader, name::String, found) = joinpath(prefix.path, name)
@test Base.find_in_path(name, nothing) == joinpath("abc", name)
end
@test Base.find_in_path("test_sourcepath", nothing) == joinpath("abc", "test_sourcepath")
Base.load_hook(prefix::CustomLoader, name::String, found::String) = found
@test Base.find_in_path("test_sourcepath", nothing) == joinpath(dir, "test_sourcepath.jl")

empty!(LOAD_PATH)
append!(LOAD_PATH, SAVED_LOAD_PATH)
@test LOAD_PATH == SAVED_LOAD_PATH