The plumbum LocalMachine default of passing the parent process environment variables to Popen makes sense; however, it is either non-obvious or non-trivial to disable this behavior for command execution without the inherited environment – in a thread-safe manner in particular.
(I've written a good bit below; but, I'm curious if I'm missing anything or if there are any thoughts on how this might be approached in plumbum.)
Generally, it now appears that this might be done as follows:
with local.env():
local.env.clear()
# run commands
local['env']()
However:
It is non-obvious that clear() must be called here – (perhaps it's worth documenting)
It might instead make sense, (though it also might not), that when local.env() is called without arguments that local.env.clear() is called on the user's behalf – (otherwise, why are they calling env() at all?) I.e.:
if not args and not kwargs:
self.clear()
(As a side note: the local.env() context manager constructor accepts *args, **kwargs, and documents both; yet, args are ignored.)
Matters appear worse for disabling the inherited environment in a thread-safe manner:
Because the LocalEnv of local.env is a class-level attribute of LocalMachine, a purpose-dedicated machine may be constructed only from a subclass.
A LocalCommand retrieved from such a subclass of LocalMachine still executes in the environment of plumbum.local – because LocalCommand.machine simply returns plumbum.local (rather than storing the machine from which its was retrieved).
And, the LocalMachine construction of LocalCommand (its choice of constructor) is not trivially configurable/changeable.
Certainly, worrying about the above might be avoided by enabling LocalMachine._popen to somehow ignore its own env (local.env); though, it is not obvious how to achieve this in the "nicest" way. (E.g.: Another keyword argument: _popen(…, isolate=True)? A very special env to pass like: _popen(…, env=IsolatedEnv(…))?)
As a final note, obviously, all of this can be avoided by nesting commands under /usr/bin/env; though, this defeats the purpose of environment management in plumbum, and it is a shame when subprocess.Popen itself supports a cleared environment straight-forwardly.
The plumbum
LocalMachine
default of passing the parent process environment variables toPopen
makes sense; however, it is either non-obvious or non-trivial to disable this behavior for command execution without the inherited environment – in a thread-safe manner in particular.(I've written a good bit below; but, I'm curious if I'm missing anything or if there are any thoughts on how this might be approached in plumbum.)
Generally, it now appears that this might be done as follows:
However:
It is non-obvious that
clear()
must be called here – (perhaps it's worth documenting)It might instead make sense, (though it also might not), that when
local.env()
is called without arguments thatlocal.env.clear()
is called on the user's behalf – (otherwise, why are they callingenv()
at all?) I.e.:(As a side note: the
local.env()
context manager constructor accepts*args, **kwargs
, and documents both; yet,args
are ignored.)Matters appear worse for disabling the inherited environment in a thread-safe manner:
LocalEnv
oflocal.env
is a class-level attribute ofLocalMachine
, a purpose-dedicated machine may be constructed only from a subclass.LocalCommand
retrieved from such a subclass ofLocalMachine
still executes in the environment ofplumbum.local
– becauseLocalCommand.machine
simply returnsplumbum.local
(rather than storing the machine from which its was retrieved).LocalMachine
construction ofLocalCommand
(its choice of constructor) is not trivially configurable/changeable.Certainly, worrying about the above might be avoided by enabling
LocalMachine._popen
to somehow ignore its own env (local.env
); though, it is not obvious how to achieve this in the "nicest" way. (E.g.: Another keyword argument:_popen(…, isolate=True)
? A very special env to pass like:_popen(…, env=IsolatedEnv(…))
?)As a final note, obviously, all of this can be avoided by nesting commands under
/usr/bin/env
; though, this defeats the purpose of environment management in plumbum, and it is a shame whensubprocess.Popen
itself supports a cleared environment straight-forwardly.