Skip to content

Commit

Permalink
relax withenv signature from Function to Any (JuliaLang#43584)
Browse files Browse the repository at this point in the history
  • Loading branch information
ericphanson authored and LilithHafner committed Mar 8, 2022
1 parent b4639b5 commit 898b219
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 3 deletions.
6 changes: 3 additions & 3 deletions base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -155,15 +155,15 @@ function show(io::IO, ::EnvDict)
end

"""
withenv(f::Function, kv::Pair...)
withenv(f, kv::Pair...)
Execute `f` in an environment that is temporarily modified (not replaced as in `setenv`)
by zero or more `"var"=>val` arguments `kv`. `withenv` is generally used via the
`withenv(kv...) do ... end` syntax. A value of `nothing` can be used to temporarily unset an
environment variable (if it is set). When `withenv` returns, the original environment has
been restored.
"""
function withenv(f::Function, keyvals::Pair{T}...) where T<:AbstractString
function withenv(f, keyvals::Pair{T}...) where T<:AbstractString
old = Dict{T,Any}()
for (key,val) in keyvals
old[key] = get(ENV,key,nothing)
Expand All @@ -176,4 +176,4 @@ function withenv(f::Function, keyvals::Pair{T}...) where T<:AbstractString
end
end
end
withenv(f::Function) = f() # handle empty keyvals case; see #10853
withenv(f) = f() # handle empty keyvals case; see #10853
10 changes: 10 additions & 0 deletions test/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,16 @@ end
@test isempty(ENV) || first(ENV) in c
end
end

# issue #43486
struct Obj43486 end
(::Obj43486)() = ENV["KEY"] == "VALUE"
let
f = Obj43486()
@test !(f isa Function)
@test withenv(f, "KEY" => "VALUE")
end

@testset "non-existent keys" begin
key = randstring(25)
@test !haskey(ENV,key)
Expand Down

0 comments on commit 898b219

Please sign in to comment.