code: def _runtest_env_changed_exc(
      result: TestResult,
      runtests: RunTests,
      display_failure: bool = True,
  ) -> None:
      # Handle exceptions, detect environment changes.
  
      # Reset the environment_altered flag to detect if a test altered
      # the environment
      support.environment_altered = False
  
      pgo = runtests.pgo
      if pgo:
          display_failure = False
      quiet = runtests.quiet
  
      test_name = result.test_name
      try:
          clear_caches()
          support.gc_collect()
  
          with saved_test_environment(
              test_name, runtests.verbose, quiet, pgo=pgo
          ):
              _load_run_test(result, runtests)
      except support.ResourceDenied as exc:
          if not quiet and not pgo:
              print(f"{test_name} skipped -- {exc}", flush=True)
          result.state = State.RESOURCE_DENIED
          return
      except unittest.SkipTest as exc:
          if not quiet and not pgo:
              print(f"{test_name} skipped -- {exc}", flush=True)
          result.state = State.SKIPPED
          return
      except support.TestFailedWithDetails as exc:
          msg = f"test {test_name} failed"
          if display_failure:
              msg = f"{msg} -- {exc}"
          print(msg, file=sys.stderr, flush=True)
          result.state = State.FAILED
          result.errors = exc.errors
          result.failures = exc.failures
          result.stats = exc.stats
          return
      except support.TestFailed as exc:
          msg = f"test {test_name} failed"
          if display_failure:
              msg = f"{msg} -- {exc}"
          print(msg, file=sys.stderr, flush=True)
          result.state = State.FAILED
          result.stats = exc.stats
          return
      except support.TestDidNotRun:
          result.state = State.DID_NOT_RUN
          return
      except KeyboardInterrupt:
          print()
          result.state = State.INTERRUPTED
          return
      except:
          if not pgo:
              msg = traceback.format_exc()
              print(
                  f"test {test_name} crashed -- {msg}",
                  file=sys.stderr,
                  flush=True,
              )
          result.state = State.UNCAUGHT_EXC
          return
  
      if support.environment_altered:
          result.set_env_changed()
      # Don't override the state if it was already set (REFLEAK or ENV_CHANGED)
      if result.state is None:
          result.state = State.PASSED
  
imports: import faulthandler
  import gc
  import importlib
  import io
  import sys
  import time
  import traceback
  import unittest
  from test import support
  from test.support import threading_helper
  from .filter import match_test
  from .result import State, TestResult, TestStats
  from .runtests import RunTests
  from .save_env import saved_test_environment
  from .setup import setup_tests
  from .testresult import get_test_runner
  from .utils import TestName, clear_caches, remove_testfn, abs_module_name, print_warning
label_desc: Use image node_id 6 for calling a global function with example usage: _runtest_env_changed_exc(result, runtests, display_failure) without return types