From 898b219a94e39f70bb2a6da1ecba333e03ce2b7c Mon Sep 17 00:00:00 2001 From: Eric Hanson <5846501+ericphanson@users.noreply.github.com> Date: Thu, 27 Jan 2022 11:08:48 -0500 Subject: [PATCH] relax `withenv` signature from Function to Any (#43584) --- base/env.jl | 6 +++--- test/env.jl | 10 ++++++++++ 2 files changed, 13 insertions(+), 3 deletions(-) diff --git a/base/env.jl b/base/env.jl index 7d47a4de090a3a..4fdc02e582a4c6 100644 --- a/base/env.jl +++ b/base/env.jl @@ -155,7 +155,7 @@ 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 @@ -163,7 +163,7 @@ by zero or more `"var"=>val` arguments `kv`. `withenv` is generally used via the 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) @@ -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 diff --git a/test/env.jl b/test/env.jl index 176074201f23e9..644d956af8fd47 100644 --- a/test/env.jl +++ b/test/env.jl @@ -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)