code: def environment(**kv):
      
      added = []
      changed = {}
      for key, value in kv.items():
          if key not in os.environ:
              added.append(key)
          else:
              changed[key] = os.environ[key]
          os.environ[key] = value
  
      yield
  
      # restore pristine process environment
      for key in added:
          del os.environ[key]
      for key in changed:
          os.environ[key] = changed[key]
docs: Context manager to run Python code with a modified UNIX process environment.
  
      All key/value pairs in the keyword arguments are added (or changed, if the
      key names an existing environmental variable) in the process environment
      upon entrance into the context. Changes are undone upon exit: added
      environmental variables are removed from the environment, and those whose
      value was changed are reset to their pristine value.