Skip to content

Commit d9ae7fd

Browse files
authored
Add Base.parse method for CacheFlags (#59268)
This will be used by Pkg instead of parsing a single UInt8, so #59035 and other changes to CacheFlags can be made without breaking Pkg in the future, as mentioned in JuliaLang/Pkg.jl#4347 (also ref: JuliaLang/Pkg.jl#4347 (comment)).
1 parent 19b72ab commit d9ae7fd

File tree

2 files changed

+19
-0
lines changed

2 files changed

+19
-0
lines changed

base/loading.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1729,6 +1729,22 @@ function show(io::IO, cf::CacheFlags)
17291729
print(io, ")")
17301730
end
17311731

1732+
function Base.parse(::Type{CacheFlags}, s::AbstractString)
1733+
e = Meta.parse(s)
1734+
if !(e isa Expr && e.head === :call && length(e.args) == 2 &&
1735+
e.args[1] === :CacheFlags &&
1736+
e.args[2] isa Expr && e.args[2].head == :parameters)
1737+
throw(ArgumentError("Malformed CacheFlags string"))
1738+
end
1739+
params = Dict{Symbol, Any}(p.args[1] => p.args[2] for p in e.args[2].args)
1740+
use_pkgimages = get(params, :use_pkgimages, nothing)
1741+
debug_level = get(params, :debug_level, nothing)
1742+
check_bounds = get(params, :check_bounds, nothing)
1743+
inline = get(params, :inline, nothing)
1744+
opt_level = get(params, :opt_level, nothing)
1745+
return CacheFlags(; use_pkgimages, debug_level, check_bounds, inline, opt_level)
1746+
end
1747+
17321748
struct ImageTarget
17331749
name::String
17341750
flags::Int32

test/loading.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1310,6 +1310,9 @@ end
13101310
@test cf.inline
13111311
@test cf.opt_level == 3
13121312
@test repr(cf) == "CacheFlags(; use_pkgimages=true, debug_level=3, check_bounds=3, inline=true, opt_level=3)"
1313+
1314+
# Round trip CacheFlags
1315+
@test parse(Base.CacheFlags, repr(cf)) == cf
13131316
end
13141317

13151318
empty!(Base.DEPOT_PATH)

0 commit comments

Comments
 (0)