Skip to content

Commit

Permalink
switch to using loaded_modules_array
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasIsensee committed Nov 23, 2024
1 parent 131d498 commit c97edd1
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 32 deletions.
31 changes: 7 additions & 24 deletions src/compression.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,35 +33,18 @@ end
# Dynamic Package Loading Logic copied from FileIO
const load_locker = Base.ReentrantLock()

function _findmod(f::Symbol)
for (u,v) in Base.loaded_modules
(Symbol(v) == f) && return u
end
nothing
end

function topimport(modname)
@info "Attempting to dynamically load $modname"
@eval Base.__toplevel__ import $modname
u = _findmod(modname)
@eval $modname = Base.loaded_modules[$u]
end

function checked_import(pkg::Symbol)
lock(load_locker) do
# kludge for test suite
if isdefined(Main, pkg)
m1 = getfield(Main, pkg)
isa(m1, Module) && return false, m1
for m in Base.loaded_modules_array()
(Symbol(m) == pkg) && return false, m
end
if isdefined(JLD2, pkg)
m1 = getfield(JLD2, pkg)
isa(m1, Module) && return false, m1
@info "Attempting to dynamically load $pkg"
@eval Base.__toplevel__ import $pkg
for m in Base.loaded_modules_array()
(Symbol(m) == pkg) && return true, m
end
m = _findmod(pkg)
(m === nothing) || return false, Base.loaded_modules[m]
topimport(pkg)
return true, Base.loaded_modules[_findmod(pkg)]
throw(InternalError("Module $pkg could not be loaded."))

Check warning on line 47 in src/compression.jl

View check run for this annotation

Codecov / codecov/patch

src/compression.jl#L47

Added line #L47 was not covered by tests
end
end

Expand Down
2 changes: 1 addition & 1 deletion src/data/reconstructing_datatypes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -308,7 +308,7 @@ function _resolve_type(rr::MappedRepr{T,DataTypeODR},
hasparams::Bool,
params) where T
parts = split(mypath, '.')
for mod in values(Base.loaded_modules)
for mod in Base.loaded_modules_array()
resolution_attempt = _resolve_type_singlemodule(rr,
mod,
parts,
Expand Down
14 changes: 8 additions & 6 deletions src/data/specialcased_types.jl
Original file line number Diff line number Diff line change
Expand Up @@ -304,13 +304,15 @@ wconvert(::Type{String}, x::Module) = string(x)
function rconvert(::Type{Module}, x::String)
pkg = Symbol(x)
# Try to find the module
# Start with the method used to find compression libraries
m =_findmod(pkg)
isnothing(m) || return Base.loaded_modules[m]
@info "Encountered reference to module $x, but it is not currently loaded."
for m in Base.loaded_modules_array()
(Symbol(m) == pkg) && return m
end
@warn "Encountered reference to module $x, but it is not currently loaded."

Check warning on line 310 in src/data/specialcased_types.jl

View check run for this annotation

Codecov / codecov/patch

src/data/specialcased_types.jl#L307-L310

Added lines #L307 - L310 were not covered by tests
return try
topimport(pkg)
Base.loaded_modules[_findmod(pkg)]
@eval Base.__toplevel__ import $pkg
for m in Base.loaded_modules_array()
(Symbol(m) == pkg) && return m
end

Check warning on line 315 in src/data/specialcased_types.jl

View check run for this annotation

Codecov / codecov/patch

src/data/specialcased_types.jl#L312-L315

Added lines #L312 - L315 were not covered by tests
catch
@warn "Could not load module $x. Returning a dummy module"
Module(Symbol(x*"_dummy"))
Expand Down
2 changes: 1 addition & 1 deletion test/modules.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ S = BType(x)
end

@testset "name collisions" begin
mods = collect(values(Base.loaded_modules))
mods = Base.loaded_modules_array()
# use whichever module would not be found first in a linear search
M = findfirst(==(A), mods) < findfirst(==(B), mods) ? B : A
x = M.SameNameType(42)
Expand Down

0 comments on commit c97edd1

Please sign in to comment.