• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1import unittest
2from ctypes import *
3
4class StructFieldsTestCase(unittest.TestCase):
5    # Structure/Union classes must get 'finalized' sooner or
6    # later, when one of these things happen:
7    #
8    # 1. _fields_ is set.
9    # 2. An instance is created.
10    # 3. The type is used as field of another Structure/Union.
11    # 4. The type is subclassed
12    #
13    # When they are finalized, assigning _fields_ is no longer allowed.
14
15    def test_1_A(self):
16        class X(Structure):
17            pass
18        self.assertEqual(sizeof(X), 0) # not finalized
19        X._fields_ = [] # finalized
20        self.assertRaises(AttributeError, setattr, X, "_fields_", [])
21
22    def test_1_B(self):
23        class X(Structure):
24            _fields_ = [] # finalized
25        self.assertRaises(AttributeError, setattr, X, "_fields_", [])
26
27    def test_2(self):
28        class X(Structure):
29            pass
30        X()
31        self.assertRaises(AttributeError, setattr, X, "_fields_", [])
32
33    def test_3(self):
34        class X(Structure):
35            pass
36        class Y(Structure):
37            _fields_ = [("x", X)] # finalizes X
38        self.assertRaises(AttributeError, setattr, X, "_fields_", [])
39
40    def test_4(self):
41        class X(Structure):
42            pass
43        class Y(X):
44            pass
45        self.assertRaises(AttributeError, setattr, X, "_fields_", [])
46        Y._fields_ = []
47        self.assertRaises(AttributeError, setattr, X, "_fields_", [])
48
49    def test_5(self):
50        class X(Structure):
51            _fields_ = (("char", c_char * 5),)
52
53        x = X(b'#' * 5)
54        x.char = b'a\0b\0'
55        self.assertEqual(bytes(x), b'a\x00###')
56
57    def test_6(self):
58        class X(Structure):
59            _fields_ = [("x", c_int)]
60        CField = type(X.x)
61        self.assertRaises(TypeError, CField)
62
63    def test_gh99275(self):
64        class BrokenStructure(Structure):
65            def __init_subclass__(cls, **kwargs):
66                cls._fields_ = []  # This line will fail, `stgdict` is not ready
67
68        with self.assertRaisesRegex(TypeError,
69                                    'ctypes state is not initialized'):
70            class Subclass(BrokenStructure): ...
71
72    # __set__ and __get__ should raise a TypeError in case their self
73    # argument is not a ctype instance.
74    def test___set__(self):
75        class MyCStruct(Structure):
76            _fields_ = (("field", c_int),)
77        self.assertRaises(TypeError,
78                          MyCStruct.field.__set__, 'wrong type self', 42)
79
80        class MyCUnion(Union):
81            _fields_ = (("field", c_int),)
82        self.assertRaises(TypeError,
83                          MyCUnion.field.__set__, 'wrong type self', 42)
84
85    def test___get__(self):
86        class MyCStruct(Structure):
87            _fields_ = (("field", c_int),)
88        self.assertRaises(TypeError,
89                          MyCStruct.field.__get__, 'wrong type self', 42)
90
91        class MyCUnion(Union):
92            _fields_ = (("field", c_int),)
93        self.assertRaises(TypeError,
94                          MyCUnion.field.__get__, 'wrong type self', 42)
95
96if __name__ == "__main__":
97    unittest.main()
98