log.py: remove setenv calls (#48933)

This commit is contained in:
Harmen Stoppels 2025-02-10 09:53:36 +01:00 committed by GitHub
parent 04313afc63
commit 6d608a9664
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 20 additions and 50 deletions

View File

@ -344,26 +344,6 @@ def close(self):
self.file.close() self.file.close()
@contextmanager
def replace_environment(env):
"""Replace the current environment (`os.environ`) with `env`.
If `env` is empty (or None), this unsets all current environment
variables.
"""
env = env or {}
old_env = os.environ.copy()
try:
os.environ.clear()
for name, val in env.items():
os.environ[name] = val
yield
finally:
os.environ.clear()
for name, val in old_env.items():
os.environ[name] = val
def log_output(*args, **kwargs): def log_output(*args, **kwargs):
"""Context manager that logs its output to a file. """Context manager that logs its output to a file.
@ -447,7 +427,6 @@ def __init__(
self.echo = echo self.echo = echo
self.debug = debug self.debug = debug
self.buffer = buffer self.buffer = buffer
self.env = env # the environment to use for _writer_daemon
self.filter_fn = filter_fn self.filter_fn = filter_fn
self._active = False # used to prevent re-entry self._active = False # used to prevent re-entry
@ -519,21 +498,20 @@ def __enter__(self):
# just don't forward input if this fails # just don't forward input if this fails
pass pass
with replace_environment(self.env): self.process = multiprocessing.Process(
self.process = multiprocessing.Process( target=_writer_daemon,
target=_writer_daemon, args=(
args=( input_fd,
input_fd, read_fd,
read_fd, self.write_fd,
self.write_fd, self.echo,
self.echo, self.log_file,
self.log_file, child_pipe,
child_pipe, self.filter_fn,
self.filter_fn, ),
), )
) self.process.daemon = True # must set before start()
self.process.daemon = True # must set before start() self.process.start()
self.process.start()
finally: finally:
if input_fd: if input_fd:
@ -729,10 +707,7 @@ class winlog:
Does not support the use of 'v' toggling as nixlog does. Does not support the use of 'v' toggling as nixlog does.
""" """
def __init__( def __init__(self, file_like=None, echo=False, debug=0, buffer=False, filter_fn=None):
self, file_like=None, echo=False, debug=0, buffer=False, env=None, filter_fn=None
):
self.env = env
self.debug = debug self.debug = debug
self.echo = echo self.echo = echo
self.logfile = file_like self.logfile = file_like
@ -789,11 +764,10 @@ def background_reader(reader, echo_writer, _kill):
reader.close() reader.close()
self._active = True self._active = True
with replace_environment(self.env): self._thread = Thread(
self._thread = Thread( target=background_reader, args=(self.reader, self.echo_writer, self._kill)
target=background_reader, args=(self.reader, self.echo_writer, self._kill) )
) self._thread.start()
self._thread.start()
return self return self
def __exit__(self, exc_type, exc_val, exc_tb): def __exit__(self, exc_type, exc_val, exc_tb):

View File

@ -2436,11 +2436,7 @@ def _real_install(self) -> None:
# DEBUGGING TIP - to debug this section, insert an IPython # DEBUGGING TIP - to debug this section, insert an IPython
# embed here, and run the sections below without log capture # embed here, and run the sections below without log capture
log_contextmanager = log_output( log_contextmanager = log_output(
log_file, log_file, self.echo, True, filter_fn=self.filter_fn
self.echo,
True,
env=self.unmodified_env,
filter_fn=self.filter_fn,
) )
with log_contextmanager as logger: with log_contextmanager as logger: