code: 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)
  
imports: import contextlib
  import os
label_desc: Use image node_id 2 for calling a global function with example usage: modified_env(changes, snapshot) without return types