(root)/
Python-3.11.7/
Lib/
test/
libregrtest/
setup.py
       1  import faulthandler
       2  import gc
       3  import os
       4  import random
       5  import signal
       6  import sys
       7  import unittest
       8  from test import support
       9  from test.support.os_helper import TESTFN_UNDECODABLE, FS_NONASCII
      10  
      11  from .filter import set_match_tests
      12  from .runtests import RunTests
      13  from .utils import (
      14      setup_unraisable_hook, setup_threading_excepthook, fix_umask,
      15      adjust_rlimit_nofile)
      16  
      17  
      18  UNICODE_GUARD_ENV = "PYTHONREGRTEST_UNICODE_GUARD"
      19  
      20  
      21  def setup_test_dir(testdir: str | None) -> None:
      22      if testdir:
      23          # Prepend test directory to sys.path, so runtest() will be able
      24          # to locate tests
      25          sys.path.insert(0, os.path.abspath(testdir))
      26  
      27  
      28  def setup_process():
      29      fix_umask()
      30  
      31      try:
      32          stderr_fd = sys.__stderr__.fileno()
      33      except (ValueError, AttributeError):
      34          # Catch ValueError to catch io.UnsupportedOperation on TextIOBase
      35          # and ValueError on a closed stream.
      36          #
      37          # Catch AttributeError for stderr being None.
      38          stderr_fd = None
      39      else:
      40          # Display the Python traceback on fatal errors (e.g. segfault)
      41          faulthandler.enable(all_threads=True, file=stderr_fd)
      42  
      43          # Display the Python traceback on SIGALRM or SIGUSR1 signal
      44          signals = []
      45          if hasattr(signal, 'SIGALRM'):
      46              signals.append(signal.SIGALRM)
      47          if hasattr(signal, 'SIGUSR1'):
      48              signals.append(signal.SIGUSR1)
      49          for signum in signals:
      50              faulthandler.register(signum, chain=True, file=stderr_fd)
      51  
      52      adjust_rlimit_nofile()
      53  
      54      support.record_original_stdout(sys.stdout)
      55  
      56      # Some times __path__ and __file__ are not absolute (e.g. while running from
      57      # Lib/) and, if we change the CWD to run the tests in a temporary dir, some
      58      # imports might fail.  This affects only the modules imported before os.chdir().
      59      # These modules are searched first in sys.path[0] (so '' -- the CWD) and if
      60      # they are found in the CWD their __file__ and __path__ will be relative (this
      61      # happens before the chdir).  All the modules imported after the chdir, are
      62      # not found in the CWD, and since the other paths in sys.path[1:] are absolute
      63      # (site.py absolutize them), the __file__ and __path__ will be absolute too.
      64      # Therefore it is necessary to absolutize manually the __file__ and __path__ of
      65      # the packages to prevent later imports to fail when the CWD is different.
      66      for module in sys.modules.values():
      67          if hasattr(module, '__path__'):
      68              for index, path in enumerate(module.__path__):
      69                  module.__path__[index] = os.path.abspath(path)
      70          if getattr(module, '__file__', None):
      71              module.__file__ = os.path.abspath(module.__file__)
      72  
      73      if hasattr(sys, 'addaudithook'):
      74          # Add an auditing hook for all tests to ensure PySys_Audit is tested
      75          def _test_audit_hook(name, args):
      76              pass
      77          sys.addaudithook(_test_audit_hook)
      78  
      79      setup_unraisable_hook()
      80      setup_threading_excepthook()
      81  
      82      # Ensure there's a non-ASCII character in env vars at all times to force
      83      # tests consider this case. See BPO-44647 for details.
      84      if TESTFN_UNDECODABLE and os.supports_bytes_environ:
      85          os.environb.setdefault(UNICODE_GUARD_ENV.encode(), TESTFN_UNDECODABLE)
      86      elif FS_NONASCII:
      87          os.environ.setdefault(UNICODE_GUARD_ENV, FS_NONASCII)
      88  
      89  
      90  def setup_tests(runtests: RunTests):
      91      support.verbose = runtests.verbose
      92      support.failfast = runtests.fail_fast
      93      support.PGO = runtests.pgo
      94      support.PGO_EXTENDED = runtests.pgo_extended
      95  
      96      set_match_tests(runtests.match_tests)
      97  
      98      if runtests.use_junit:
      99          support.junit_xml_list = []
     100          from .testresult import RegressionTestResult
     101          RegressionTestResult.USE_XML = True
     102      else:
     103          support.junit_xml_list = None
     104  
     105      if runtests.memory_limit is not None:
     106          support.set_memlimit(runtests.memory_limit)
     107  
     108      support.suppress_msvcrt_asserts(runtests.verbose >= 2)
     109  
     110      support.use_resources = runtests.use_resources
     111  
     112      timeout = runtests.timeout
     113      if timeout is not None:
     114          # For a slow buildbot worker, increase SHORT_TIMEOUT and LONG_TIMEOUT
     115          support.LOOPBACK_TIMEOUT = max(support.LOOPBACK_TIMEOUT, timeout / 120)
     116          # don't increase INTERNET_TIMEOUT
     117          support.SHORT_TIMEOUT = max(support.SHORT_TIMEOUT, timeout / 40)
     118          support.LONG_TIMEOUT = max(support.LONG_TIMEOUT, timeout / 4)
     119  
     120          # If --timeout is short: reduce timeouts
     121          support.LOOPBACK_TIMEOUT = min(support.LOOPBACK_TIMEOUT, timeout)
     122          support.INTERNET_TIMEOUT = min(support.INTERNET_TIMEOUT, timeout)
     123          support.SHORT_TIMEOUT = min(support.SHORT_TIMEOUT, timeout)
     124          support.LONG_TIMEOUT = min(support.LONG_TIMEOUT, timeout)
     125  
     126      if runtests.hunt_refleak:
     127          # private attribute that mypy doesn't know about:
     128          unittest.BaseTestSuite._cleanup = False  # type: ignore[attr-defined]
     129  
     130      if runtests.gc_threshold is not None:
     131          gc.set_threshold(runtests.gc_threshold)
     132  
     133      random.seed(runtests.random_seed)