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

Better precompilability #287

Merged
merged 3 commits into from
Jan 7, 2021
Merged
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
17 changes: 1 addition & 16 deletions src/error_handling.jl
Original file line number Diff line number Diff line change
Expand Up @@ -83,20 +83,5 @@ handle_error(e, q) = throw(e)

function handle_error(e::NotInstalledError, q)
println("Library \"", e.library, "\" is not installed but is recommended as a library to load format: \"", file_extension(q), "\"")
!isinteractive() && rethrow(e) # if we're not in interactive mode just throw
while true
println("Should we install \"", e.library, "\" for you? (y/n):")
input = lowercase(chomp(strip(readline(stdin))))
if input == "y"
@info(string("Start installing ", e.library, "..."))
Pkg.add(string(e.library))
return false # don't continue
elseif input == "n"
@info(string("Not installing ", e.library))
return true # User does not install, continue going through errors.
else
println("$input is not a valid choice. Try typing y or n")
end
end
true # User does not install, continue going through errors.
rethrow(e)
end
13 changes: 9 additions & 4 deletions src/loadsave.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ function checked_import(pkg::Symbol)
end
end

applicable_error(applicable, sym) = error("No $applicable found for $sym")

for (applicable_, add_, dict_) in (
(:applicable_loaders, :add_loader, :sym2loader),
Expand All @@ -44,7 +45,7 @@ for (applicable_, add_, dict_) in (
if haskey($dict_, sym)
return $dict_[sym]
end
error("No $($applicable_) found for $(sym)")
Base.invokelatest(applicable_error, $applicable_, sym)
end
function $add_(@nospecialize(fmt::Type{<:DataFormat}), pkg::Symbol)
sym = formatname(fmt)
Expand Down Expand Up @@ -182,14 +183,18 @@ end
# Handlers for formatted files/streams

for fn in (:load, :loadstreaming, :metadata)
fn_func_name = Symbol(fn, "_filename")
gen2_func_name = Symbol("fileio_", fn)
@eval function $fn(@nospecialize(q::Formatted), @nospecialize(args...); @nospecialize(options...))
Base.invokelatest($fn_func_name, q, filename(q), args...; options...)
end
@eval function $fn_func_name(@nospecialize(q::Formatted), filename, @nospecialize(args...); @nospecialize(options...))
if unknown(q)
isfile(filename(q)) || open(filename(q)) # force systemerror
isfile(filename) || open(filename) # force systemerror
throw(UnknownFormat(q))
end
if q isa File
!isfile(filename(q)) && throw(ArgumentError("No file exists at given path: $(filename(q))"))
!isfile(filename) && throw(ArgumentError("No file exists at given path: $(filename)"))
end
libraries = applicable_loaders(q)
failures = Any[]
Expand All @@ -207,7 +212,7 @@ for fn in (:load, :loadstreaming, :metadata)
push!(failures, (e, q))
end
end
handle_exceptions(failures, "loading $(repr(filename(q)))")
handle_exceptions(failures, "loading $(repr(filename))")
end
end

Expand Down
6 changes: 6 additions & 0 deletions src/precompile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@ function _precompile_()
@assert precompile(Tuple{typeof(load),File})
@assert precompile(Tuple{typeof(load),Formatted})
@assert precompile(Tuple{typeof(load),String})
@assert precompile(Tuple{typeof(FileIO.load_filename),Formatted,String})
if isdefined(Base, :bodyfunction)
fbody = Base.bodyfunction(which(FileIO.load_filename, (Formatted, String)))
@assert precompile(fbody, (Any, typeof(FileIO.load_filename), Formatted, String))
@assert precompile(fbody, (Any, typeof(FileIO.load_filename), Formatted, String, Vararg{Any,100}))
end

@assert precompile(Tuple{typeof(query),String})
@assert precompile(Tuple{typeof(query),IOStream})
Expand Down