4
4
Py_TPFLAGS_IMMUTABLETYPE )
5
5
6
6
7
- class StructFieldsTestCase (unittest .TestCase ):
7
+ NOTHING = object ()
8
+
9
+ class FieldsTestBase :
8
10
# Structure/Union classes must get 'finalized' sooner or
9
11
# later, when one of these things happen:
10
12
#
@@ -14,42 +16,47 @@ class StructFieldsTestCase(unittest.TestCase):
14
16
# 4. The type is subclassed
15
17
#
16
18
# When they are finalized, assigning _fields_ is no longer allowed.
19
+
20
+ def assert_final_fields (self , cls , expected = NOTHING ):
21
+ self .assertRaises (AttributeError , setattr , cls , "_fields_" , [])
22
+ self .assertEqual (getattr (cls , "_fields_" , NOTHING ), expected )
23
+
17
24
def test_1_A (self ):
18
- class X (Structure ):
25
+ class X (self . cls ):
19
26
pass
20
27
self .assertEqual (sizeof (X ), 0 ) # not finalized
21
28
X ._fields_ = [] # finalized
22
- self .assertRaises ( AttributeError , setattr , X , "_fields_" , [])
29
+ self .assert_final_fields ( X , expected = [])
23
30
24
31
def test_1_B (self ):
25
- class X (Structure ):
32
+ class X (self . cls ):
26
33
_fields_ = [] # finalized
27
- self .assertRaises ( AttributeError , setattr , X , "_fields_" , [])
34
+ self .assert_final_fields ( X , expected = [])
28
35
29
36
def test_2 (self ):
30
- class X (Structure ):
37
+ class X (self . cls ):
31
38
pass
32
39
X ()
33
- self .assertRaises ( AttributeError , setattr , X , "_fields_" , [] )
40
+ self .assert_final_fields ( X )
34
41
35
42
def test_3 (self ):
36
- class X (Structure ):
43
+ class X (self . cls ):
37
44
pass
38
- class Y (Structure ):
45
+ class Y (self . cls ):
39
46
_fields_ = [("x" , X )] # finalizes X
40
- self .assertRaises ( AttributeError , setattr , X , "_fields_" , [] )
47
+ self .assert_final_fields ( X )
41
48
42
49
def test_4 (self ):
43
- class X (Structure ):
50
+ class X (self . cls ):
44
51
pass
45
52
class Y (X ):
46
53
pass
47
- self .assertRaises ( AttributeError , setattr , X , "_fields_" , [] )
54
+ self .assert_final_fields ( X )
48
55
Y ._fields_ = []
49
- self .assertRaises ( AttributeError , setattr , X , "_fields_" , [] )
56
+ self .assert_final_fields ( X )
50
57
51
58
def test_5 (self ):
52
- class X (Structure ):
59
+ class X (self . cls ):
53
60
_fields_ = (("char" , c_char * 5 ),)
54
61
55
62
x = X (b'#' * 5 )
@@ -59,14 +66,8 @@ class X(Structure):
59
66
def test_6 (self ):
60
67
self .assertRaises (TypeError , CField )
61
68
62
- def test_cfield_type_flags (self ):
63
- self .assertTrue (CField .__flags__ & Py_TPFLAGS_IMMUTABLETYPE )
64
-
65
- def test_cfield_inheritance_hierarchy (self ):
66
- self .assertEqual (CField .mro (), [CField , object ])
67
-
68
69
def test_gh99275 (self ):
69
- class BrokenStructure (Structure ):
70
+ class BrokenStructure (self . cls ):
70
71
def __init_subclass__ (cls , ** kwargs ):
71
72
cls ._fields_ = [] # This line will fail, `stginfo` is not ready
72
73
@@ -77,26 +78,28 @@ class Subclass(BrokenStructure): ...
77
78
# __set__ and __get__ should raise a TypeError in case their self
78
79
# argument is not a ctype instance.
79
80
def test___set__ (self ):
80
- class MyCStruct (Structure ):
81
+ class MyCStruct (self . cls ):
81
82
_fields_ = (("field" , c_int ),)
82
83
self .assertRaises (TypeError ,
83
84
MyCStruct .field .__set__ , 'wrong type self' , 42 )
84
85
85
- class MyCUnion (Union ):
86
- _fields_ = (("field" , c_int ),)
87
- self .assertRaises (TypeError ,
88
- MyCUnion .field .__set__ , 'wrong type self' , 42 )
89
-
90
86
def test___get__ (self ):
91
- class MyCStruct (Structure ):
87
+ class MyCStruct (self . cls ):
92
88
_fields_ = (("field" , c_int ),)
93
89
self .assertRaises (TypeError ,
94
90
MyCStruct .field .__get__ , 'wrong type self' , 42 )
95
91
96
- class MyCUnion (Union ):
97
- _fields_ = (("field" , c_int ),)
98
- self .assertRaises (TypeError ,
99
- MyCUnion .field .__get__ , 'wrong type self' , 42 )
92
+ class StructFieldsTestCase (unittest .TestCase , FieldsTestBase ):
93
+ cls = Structure
94
+
95
+ def test_cfield_type_flags (self ):
96
+ self .assertTrue (CField .__flags__ & Py_TPFLAGS_IMMUTABLETYPE )
97
+
98
+ def test_cfield_inheritance_hierarchy (self ):
99
+ self .assertEqual (CField .mro (), [CField , object ])
100
+
101
+ class UnionFieldsTestCase (unittest .TestCase , FieldsTestBase ):
102
+ cls = Union
100
103
101
104
102
105
if __name__ == "__main__" :
0 commit comments