Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Enhance tests
  • Loading branch information
vstinner committed Jan 20, 2022
commit 5624ebd34f7c03516b58c093c1c35015800c6a60
45 changes: 45 additions & 0 deletions Lib/test/_test_embed_structseq.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import sys
import types
import unittest


class TestStructseq(unittest.TestCase):
def check_structseq(self, obj_type):
self.assertGreaterEqual(sys.getrefcount(obj_type), 1)
self.assertIsInstance(type.__name__, str)
self.assertTrue(issubclass(obj_type, tuple))
self.assertEqual(obj_type.__bases__, (tuple,))
self.assertEqual(obj_type.__mro__, (obj_type, tuple, object))
self.assertIsInstance(obj_type.__dict__, types.MappingProxyType)
self.assertEqual(obj_type.__subclasses__(), [])

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
):
with self.subTest(attr=attr_name):
attr = getattr(sys, attr_name)
self.check_structseq(type(attr))

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:
with self.subTest(func=func_name):
func = getattr(sys, func_name)
obj = func()
self.check_structseq(type(obj))


try:
unittest.main()
except SystemExit as exc:
if exc.args[0] != 0:
raise
print("Tests passed")
28 changes: 6 additions & 22 deletions Lib/test/test_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -333,29 +333,13 @@ def test_finalize_structseq(self):
# bpo-46417: Py_Finalize() clears structseq static types. Check that
# sys attributes using struct types still work when
# Py_Finalize()/Py_Initialize() is called multiple times.
code = textwrap.dedent(r'''
import sys
print(sys.get_asyncgen_hooks())
print(sys.flags)
print(sys.float_info)
print(sys.hash_info)
print(sys.int_info)
print(sys.thread_info)
print(f"{sys.version=!r}", flush=True)
''')
if hasattr(sys, 'getwindowsversion'):
code += '\n' + 'print(sys.getwindowsversion())'
expected = textwrap.dedent(r'''
asyncgen_hooks(.*)
sys.flags(.*)
sys.float_info(.*)
sys.hash_info(.*)
sys.int_info(.*)
sys.thread_info(.*)
sys.version='.*'
''')
# print() calls type->tp_repr(instance) and so checks that the types
# are still working properly.
script = support.findfile('_test_embed_structseq.py')
with open(script, encoding="utf-8") as fp:
code = fp.read()
out, err = self.run_embedded_interpreter("test_repeated_init_exec", code)
self.assertRegex(out, expected)
self.assertEqual(out, 'Tests passed\n' * INIT_LOOPS)


class InitConfigTests(EmbeddingTestsMixin, unittest.TestCase):
Expand Down