2022-01-20 20:42:25 -04:00
|
|
|
import sys
|
|
|
|
import types
|
|
|
|
|
2023-04-22 16:39:37 -03:00
|
|
|
# Note: This test file can't import `unittest` since the runtime can't
|
|
|
|
# currently guarantee that it will not leak memory. Doing so will mark
|
|
|
|
# the test as passing but with reference leaks. This can safely import
|
|
|
|
# the `unittest` library once there's a strict guarantee of no leaks
|
|
|
|
# during runtime shutdown.
|
2022-01-20 20:42:25 -04:00
|
|
|
|
|
|
|
# bpo-46417: Test that structseq types used by the sys module are still
|
|
|
|
# valid when Py_Finalize()/Py_Initialize() are called multiple times.
|
2023-04-22 16:39:37 -03:00
|
|
|
class TestStructSeq:
|
2022-01-20 20:42:25 -04:00
|
|
|
# test PyTypeObject members
|
2023-04-22 16:39:37 -03:00
|
|
|
def _check_structseq(self, obj_type):
|
2022-01-20 20:42:25 -04:00
|
|
|
# ob_refcnt
|
2023-04-22 16:39:37 -03:00
|
|
|
assert sys.getrefcount(obj_type) > 1
|
2022-01-20 20:42:25 -04:00
|
|
|
# tp_base
|
2023-04-22 16:39:37 -03:00
|
|
|
assert issubclass(obj_type, tuple)
|
2022-01-20 20:42:25 -04:00
|
|
|
# tp_bases
|
2023-04-22 16:39:37 -03:00
|
|
|
assert obj_type.__bases__ == (tuple,)
|
2022-01-20 20:42:25 -04:00
|
|
|
# tp_dict
|
2023-04-22 16:39:37 -03:00
|
|
|
assert isinstance(obj_type.__dict__, types.MappingProxyType)
|
2022-01-20 20:42:25 -04:00
|
|
|
# tp_mro
|
2023-04-22 16:39:37 -03:00
|
|
|
assert obj_type.__mro__ == (obj_type, tuple, object)
|
2022-01-20 20:42:25 -04:00
|
|
|
# tp_name
|
2023-04-22 16:39:37 -03:00
|
|
|
assert isinstance(type.__name__, str)
|
2022-01-20 20:42:25 -04:00
|
|
|
# tp_subclasses
|
2023-04-22 16:39:37 -03:00
|
|
|
assert obj_type.__subclasses__() == []
|
2022-01-20 20:42:25 -04: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
|
|
|
|
):
|
2023-04-22 16:39:37 -03:00
|
|
|
attr = getattr(sys, attr_name)
|
|
|
|
self._check_structseq(type(attr))
|
2022-01-20 20:42:25 -04: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:
|
2023-04-22 16:39:37 -03:00
|
|
|
func = getattr(sys, func_name)
|
|
|
|
obj = func()
|
|
|
|
self._check_structseq(type(obj))
|
2022-01-20 20:42:25 -04:00
|
|
|
|
|
|
|
|
|
|
|
try:
|
2023-04-22 16:39:37 -03:00
|
|
|
tests = TestStructSeq()
|
|
|
|
tests.test_sys_attrs()
|
|
|
|
tests.test_sys_funcs()
|
2022-01-20 20:42:25 -04:00
|
|
|
except SystemExit as exc:
|
|
|
|
if exc.args[0] != 0:
|
|
|
|
raise
|
|
|
|
print("Tests passed")
|