2022-01-21 01:42:25 +01:00
|
|
|
import sys
|
|
|
|
import types
|
2024-08-15 13:55:09 +02:00
|
|
|
import unittest
|
2022-01-21 01:42:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
# bpo-46417: Test that structseq types used by the sys module are still
|
|
|
|
# valid when Py_Finalize()/Py_Initialize() are called multiple times.
|
2024-08-15 13:55:09 +02:00
|
|
|
class TestStructSeq(unittest.TestCase):
|
2022-01-21 01:42:25 +01:00
|
|
|
# test PyTypeObject members
|
2024-08-15 13:55:09 +02:00
|
|
|
def check_structseq(self, obj_type):
|
2022-01-21 01:42:25 +01:00
|
|
|
# ob_refcnt
|
2024-08-15 13:55:09 +02:00
|
|
|
self.assertGreaterEqual(sys.getrefcount(obj_type), 1)
|
2022-01-21 01:42:25 +01:00
|
|
|
# tp_base
|
2024-08-15 13:55:09 +02:00
|
|
|
self.assertTrue(issubclass(obj_type, tuple))
|
2022-01-21 01:42:25 +01:00
|
|
|
# tp_bases
|
2024-08-15 13:55:09 +02:00
|
|
|
self.assertEqual(obj_type.__bases__, (tuple,))
|
2022-01-21 01:42:25 +01:00
|
|
|
# tp_dict
|
2024-08-15 13:55:09 +02:00
|
|
|
self.assertIsInstance(obj_type.__dict__, types.MappingProxyType)
|
2022-01-21 01:42:25 +01:00
|
|
|
# tp_mro
|
2024-08-15 13:55:09 +02:00
|
|
|
self.assertEqual(obj_type.__mro__, (obj_type, tuple, object))
|
2022-01-21 01:42:25 +01:00
|
|
|
# tp_name
|
2024-08-15 13:55:09 +02:00
|
|
|
self.assertIsInstance(type.__name__, str)
|
2022-01-21 01:42:25 +01:00
|
|
|
# tp_subclasses
|
2024-08-15 13:55:09 +02:00
|
|
|
self.assertEqual(obj_type.__subclasses__(), [])
|
2022-01-21 01:42:25 +01:00
|
|
|
|
|
|
|
def test_sys_attrs(self):
|
|
|
|
for attr_name in (
|
|
|
|
'flags', # FlagsType
|
|
|
|
'float_info', # FloatInfoType
|
|
|
|
'hash_info', # Hash_InfoType
|
|
|
|
'int_info', # Int_InfoType
|
|
|
|
'thread_info', # ThreadInfoType
|
|
|
|
'version_info', # VersionInfoType
|
|
|
|
):
|
2024-08-15 13:55:09 +02:00
|
|
|
with self.subTest(attr=attr_name):
|
|
|
|
attr = getattr(sys, attr_name)
|
|
|
|
self.check_structseq(type(attr))
|
2022-01-21 01:42:25 +01:00
|
|
|
|
|
|
|
def test_sys_funcs(self):
|
|
|
|
func_names = ['get_asyncgen_hooks'] # AsyncGenHooksType
|
|
|
|
if hasattr(sys, 'getwindowsversion'):
|
|
|
|
func_names.append('getwindowsversion') # WindowsVersionType
|
|
|
|
for func_name in func_names:
|
2024-08-15 13:55:09 +02:00
|
|
|
with self.subTest(func=func_name):
|
|
|
|
func = getattr(sys, func_name)
|
|
|
|
obj = func()
|
|
|
|
self.check_structseq(type(obj))
|
2022-01-21 01:42:25 +01:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
2024-09-13 13:13:52 +02:00
|
|
|
unittest.main(
|
|
|
|
module=(
|
|
|
|
'__main__'
|
|
|
|
if __name__ == '__main__'
|
|
|
|
# Avoiding a circular import:
|
|
|
|
else sys.modules['test._test_embed_structseq']
|
|
|
|
)
|
|
|
|
)
|
2022-01-21 01:42:25 +01:00
|
|
|
except SystemExit as exc:
|
|
|
|
if exc.args[0] != 0:
|
|
|
|
raise
|
|
|
|
print("Tests passed")
|