Debian Codegraph @

Lib/test/test_ctypes/test_unions.py

raw
1 import unittest
2 from ctypes import Union, c_char
3 from ._support import (_CData, UnionType, Py_TPFLAGS_DISALLOW_INSTANTIATION,
4 Py_TPFLAGS_IMMUTABLETYPE)
5
6
7 class ArrayTestCase(unittest.TestCase):
8 def test_inheritance_hierarchy(self):
9 self.assertEqual(Union.mro(), [Union, _CData, object])
10
11 self.assertEqual(UnionType.__name__, "UnionType")
12 self.assertEqual(type(UnionType), type)
13
14 def test_type_flags(self):
15 for cls in Union, UnionType:
16 with self.subTest(cls=Union):
17 self.assertTrue(Union.__flags__ & Py_TPFLAGS_IMMUTABLETYPE)
18 self.assertFalse(Union.__flags__ & Py_TPFLAGS_DISALLOW_INSTANTIATION)
19
20 def test_metaclass_details(self):
21 # Abstract classes (whose metaclass __init__ was not called) can't be
22 # instantiated directly
23 NewUnion = UnionType.__new__(UnionType, 'NewUnion',
24 (Union,), {})
25 for cls in Union, NewUnion:
26 with self.subTest(cls=cls):
27 with self.assertRaisesRegex(TypeError, "abstract class"):
28 obj = cls()
29
30 # Cannot call the metaclass __init__ more than once
31 class T(Union):
32 _fields_ = [("x", c_char),
33 ("y", c_char)]
34 with self.assertRaisesRegex(SystemError, "already initialized"):
35 UnionType.__init__(T, 'ptr', (), {})