[go: nahoru, domu]

Skip to content

Commit

Permalink
re-allow non-string values in ENV get! (JuliaLang#50771)
Browse files Browse the repository at this point in the history
  • Loading branch information
JeffBezanson committed Aug 3, 2023
1 parent a2f3d77 commit 6dd763b
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
4 changes: 4 additions & 0 deletions base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,10 @@ end
getindex(::EnvDict, k::AbstractString) = access_env(k->throw(KeyError(k)), k)
get(::EnvDict, k::AbstractString, def) = access_env(Returns(def), k)
get(f::Callable, ::EnvDict, k::AbstractString) = access_env(k->f(), k)
function get!(default::Callable, ::EnvDict, k::AbstractString)
haskey(ENV, k) && return ENV[k]
ENV[k] = default()
end
in(k::AbstractString, ::KeySet{String, EnvDict}) = _hasenv(k)
pop!(::EnvDict, k::AbstractString) = (v = ENV[k]; _unsetenv(k); v)
pop!(::EnvDict, k::AbstractString, def) = haskey(ENV,k) ? pop!(ENV,k) : def
Expand Down
7 changes: 6 additions & 1 deletion test/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ end
@test get!(ENV, key, "default") == "default"
@test haskey(ENV, key)
@test ENV[key] == "default"

key = randstring(25)
@test !haskey(ENV, key)
@test get!(ENV, key, 0) == 0
@test ENV[key] == "0"
end
@testset "#17956" begin
@test length(ENV) > 1
Expand Down Expand Up @@ -168,7 +173,7 @@ end
end

# Restore the original environment
for k in keys(ENV)
for k in collect(keys(ENV))
if !haskey(original_env, k)
delete!(ENV, k)
end
Expand Down

0 comments on commit 6dd763b

Please sign in to comment.