/usr/lib/python3/dist-packages/testpath/env.py is in python3-testpath 0.2-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | import contextlib
import os
@contextlib.contextmanager
def temporary_env(newenv):
"""Completely replace the environment variables with the specified dict.
Use as a context manager::
with temporary_env({'PATH': my_path}):
...
"""
orig_env = os.environ.copy()
os.environ.clear()
os.environ.update(newenv)
try:
yield
finally:
os.environ.clear()
os.environ.update(orig_env)
@contextlib.contextmanager
def modified_env(changes, snapshot=True):
"""Temporarily modify environment variables.
Specify the changes as a dictionary mapping names to new values, using
None as the value for names that should be deleted.
Example use::
with modified_env({'SHELL': 'bash', 'PYTHONPATH': None}):
...
When the context exits, there are two possible ways to restore the
environment. If *snapshot* is True, the default, it will reset the whole
environment to its state when the context was entered. If *snapshot* is
False, it will restore only the specific variables it modified, leaving
any changes made to other environment variables in the context.
"""
def update_del(changes):
for k, v in changes.items():
if v is None:
os.environ.pop(k, None)
else:
os.environ[k] = v
if snapshot:
saved_variables = os.environ.copy()
else:
saved_variables = {}
for k,v in changes.items():
saved_variables[k] = os.environ.get(k, None)
update_del(changes)
try:
yield
finally:
if snapshot:
os.environ.clear()
os.environ.update(saved_variables)
else:
update_del(saved_variables)
def make_env_restorer():
"""Snapshot the current environment, return a function to restore that.
This is intended to produce cleanup functions for tests. For example,
using the :class:`unittest.TestCase` API::
def setUp(self):
self.addCleanup(testpath.make_env_restorer())
Any changes a test makes to the environment variables will be wiped out
before the next test is run.
"""
orig_env = os.environ.copy()
def restore():
os.environ.clear()
os.environ.update(orig_env)
return restore
|