python (3.12.0)

(root)/
lib/
python3.12/
test/
_test_embed_structseq.py
       1  import sys
       2  import types
       3  
       4  # Note: This test file can't import `unittest` since the runtime can't
       5  # currently guarantee that it will not leak memory. Doing so will mark
       6  # the test as passing but with reference leaks. This can safely import
       7  # the `unittest` library once there's a strict guarantee of no leaks
       8  # during runtime shutdown.
       9  
      10  # bpo-46417: Test that structseq types used by the sys module are still
      11  # valid when Py_Finalize()/Py_Initialize() are called multiple times.
      12  class ESC[4;38;5;81mTestStructSeq:
      13      # test PyTypeObject members
      14      def _check_structseq(self, obj_type):
      15          # ob_refcnt
      16          assert sys.getrefcount(obj_type) > 1
      17          # tp_base
      18          assert issubclass(obj_type, tuple)
      19          # tp_bases
      20          assert obj_type.__bases__ == (tuple,)
      21          # tp_dict
      22          assert isinstance(obj_type.__dict__, types.MappingProxyType)
      23          # tp_mro
      24          assert obj_type.__mro__ == (obj_type, tuple, object)
      25          # tp_name
      26          assert isinstance(type.__name__, str)
      27          # tp_subclasses
      28          assert obj_type.__subclasses__() == []
      29  
      30      def test_sys_attrs(self):
      31          for attr_name in (
      32              'flags',          # FlagsType
      33              'float_info',     # FloatInfoType
      34              'hash_info',      # Hash_InfoType
      35              'int_info',       # Int_InfoType
      36              'thread_info',    # ThreadInfoType
      37              'version_info',   # VersionInfoType
      38          ):
      39              attr = getattr(sys, attr_name)
      40              self._check_structseq(type(attr))
      41  
      42      def test_sys_funcs(self):
      43          func_names = ['get_asyncgen_hooks']  # AsyncGenHooksType
      44          if hasattr(sys, 'getwindowsversion'):
      45              func_names.append('getwindowsversion')  # WindowsVersionType
      46          for func_name in func_names:
      47              func = getattr(sys, func_name)
      48              obj = func()
      49              self._check_structseq(type(obj))
      50  
      51  
      52  try:
      53      tests = TestStructSeq()
      54      tests.test_sys_attrs()
      55      tests.test_sys_funcs()
      56  except SystemExit as exc:
      57      if exc.args[0] != 0:
      58          raise
      59  print("Tests passed")