11import sys
22import types
3+ import unittest
34
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.
95
106# bpo-46417: Test that structseq types used by the sys module are still
117# valid when Py_Finalize()/Py_Initialize() are called multiple times.
12- class TestStructSeq :
8+ class TestStructSeq ( unittest . TestCase ) :
139 # test PyTypeObject members
14- def _check_structseq (self , obj_type ):
10+ def check_structseq (self , obj_type ):
1511 # ob_refcnt
16- assert sys .getrefcount (obj_type ) > 1
12+ self . assertGreaterEqual ( sys .getrefcount (obj_type ), 1 )
1713 # tp_base
18- assert issubclass (obj_type , tuple )
14+ self . assertTrue ( issubclass (obj_type , tuple ) )
1915 # tp_bases
20- assert obj_type .__bases__ == (tuple ,)
16+ self . assertEqual ( obj_type .__bases__ , (tuple ,) )
2117 # tp_dict
22- assert isinstance (obj_type .__dict__ , types .MappingProxyType )
18+ self . assertIsInstance (obj_type .__dict__ , types .MappingProxyType )
2319 # tp_mro
24- assert obj_type .__mro__ == (obj_type , tuple , object )
20+ self . assertEqual ( obj_type .__mro__ , (obj_type , tuple , object ) )
2521 # tp_name
26- assert isinstance (type .__name__ , str )
22+ self . assertIsInstance (type .__name__ , str )
2723 # tp_subclasses
28- assert obj_type .__subclasses__ () == []
24+ self . assertEqual ( obj_type .__subclasses__ (), [])
2925
3026 def test_sys_attrs (self ):
3127 for attr_name in (
@@ -36,23 +32,23 @@ def test_sys_attrs(self):
3632 'thread_info' , # ThreadInfoType
3733 'version_info' , # VersionInfoType
3834 ):
39- attr = getattr (sys , attr_name )
40- self ._check_structseq (type (attr ))
35+ with self .subTest (attr = attr_name ):
36+ attr = getattr (sys , attr_name )
37+ self .check_structseq (type (attr ))
4138
4239 def test_sys_funcs (self ):
4340 func_names = ['get_asyncgen_hooks' ] # AsyncGenHooksType
4441 if hasattr (sys , 'getwindowsversion' ):
4542 func_names .append ('getwindowsversion' ) # WindowsVersionType
4643 for func_name in func_names :
47- func = getattr (sys , func_name )
48- obj = func ()
49- self ._check_structseq (type (obj ))
44+ with self .subTest (func = func_name ):
45+ func = getattr (sys , func_name )
46+ obj = func ()
47+ self .check_structseq (type (obj ))
5048
5149
5250try :
53- tests = TestStructSeq ()
54- tests .test_sys_attrs ()
55- tests .test_sys_funcs ()
51+ unittest .main ()
5652except SystemExit as exc :
5753 if exc .args [0 ] != 0 :
5854 raise
0 commit comments