-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Expand file tree
/
Copy pathtest_c_simple_type_meta.py
More file actions
384 lines (286 loc) · 13.2 KB
/
test_c_simple_type_meta.py
File metadata and controls
384 lines (286 loc) · 13.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
import unittest
from test.support import MS_WINDOWS
import ctypes
from ctypes import POINTER, Structure, c_void_p
from ._support import PyCSimpleType, PyCPointerType, PyCStructType
def set_non_ctypes_pointer_type(cls, pointer_type):
cls.__pointer_type__ = pointer_type
class PyCSimpleTypeAsMetaclassTest(unittest.TestCase):
def test_creating_pointer_in_dunder_new_1(self):
# Test metaclass whose instances are C types; when the type is
# created it automatically creates a pointer type for itself.
# The pointer type is also an instance of the metaclass.
# Such an implementation is used in `IUnknown` of the `comtypes`
# project. See gh-124520.
class ct_meta(type):
def __new__(cls, name, bases, namespace):
self = super().__new__(cls, name, bases, namespace)
# Avoid recursion: don't set up a pointer to
# a pointer (to a pointer...)
if bases == (c_void_p,):
# When creating PtrBase itself, the name
# is not yet available
return self
if issubclass(self, PtrBase):
return self
if bases == (object,):
ptr_bases = (self, PtrBase)
else:
ptr_bases = (self, POINTER(bases[0]))
p = p_meta(f"POINTER({self.__name__})", ptr_bases, {})
set_non_ctypes_pointer_type(self, p)
return self
class p_meta(PyCSimpleType, ct_meta):
pass
class PtrBase(c_void_p, metaclass=p_meta):
pass
ptr_base_pointer = POINTER(PtrBase)
class CtBase(object, metaclass=ct_meta):
pass
ct_base_pointer = POINTER(CtBase)
class Sub(CtBase):
pass
sub_pointer = POINTER(Sub)
class Sub2(Sub):
pass
sub2_pointer = POINTER(Sub2)
self.assertIsNot(ptr_base_pointer, ct_base_pointer)
self.assertIsNot(ct_base_pointer, sub_pointer)
self.assertIsNot(sub_pointer, sub2_pointer)
self.assertIsInstance(POINTER(Sub2), p_meta)
self.assertIsSubclass(POINTER(Sub2), Sub2)
self.assertIsSubclass(POINTER(Sub2), POINTER(Sub))
self.assertIsSubclass(POINTER(Sub), POINTER(CtBase))
self.assertIs(POINTER(Sub2), sub2_pointer)
self.assertIs(POINTER(Sub), sub_pointer)
self.assertIs(POINTER(CtBase), ct_base_pointer)
def test_creating_pointer_in_dunder_new_2(self):
# A simpler variant of the above, used in `CoClass` of the `comtypes`
# project.
class ct_meta(type):
def __new__(cls, name, bases, namespace):
self = super().__new__(cls, name, bases, namespace)
if isinstance(self, p_meta):
return self
p = p_meta(f"POINTER({self.__name__})", (self, c_void_p), {})
set_non_ctypes_pointer_type(self, p)
return self
class p_meta(PyCSimpleType, ct_meta):
pass
class Core(object):
pass
with self.assertRaisesRegex(TypeError, "must have storage info"):
POINTER(Core)
class CtBase(Core, metaclass=ct_meta):
pass
ct_base_pointer = POINTER(CtBase)
class Sub(CtBase):
pass
sub_pointer = POINTER(Sub)
self.assertIsNot(ct_base_pointer, sub_pointer)
self.assertIsInstance(POINTER(Sub), p_meta)
self.assertIsSubclass(POINTER(Sub), Sub)
self.assertIs(POINTER(Sub), sub_pointer)
self.assertIs(POINTER(CtBase), ct_base_pointer)
def test_creating_pointer_in_dunder_init_1(self):
class ct_meta(type):
def __init__(self, name, bases, namespace):
super().__init__(name, bases, namespace)
# Avoid recursion.
# (See test_creating_pointer_in_dunder_new_1)
if bases == (c_void_p,):
return
if issubclass(self, PtrBase):
return
if bases == (object,):
ptr_bases = (self, PtrBase)
else:
ptr_bases = (self, POINTER(bases[0]))
p = p_meta(f"POINTER({self.__name__})", ptr_bases, {})
set_non_ctypes_pointer_type(self, p)
class p_meta(PyCSimpleType, ct_meta):
pass
class PtrBase(c_void_p, metaclass=p_meta):
pass
ptr_base_pointer = POINTER(PtrBase)
class CtBase(object, metaclass=ct_meta):
pass
ct_base_pointer = POINTER(CtBase)
class Sub(CtBase):
pass
sub_pointer = POINTER(Sub)
class Sub2(Sub):
pass
sub2_pointer = POINTER(Sub2)
self.assertIsNot(ptr_base_pointer, ct_base_pointer)
self.assertIsNot(ct_base_pointer, sub_pointer)
self.assertIsNot(sub_pointer, sub2_pointer)
self.assertIsInstance(POINTER(Sub2), p_meta)
self.assertIsSubclass(POINTER(Sub2), Sub2)
self.assertIsSubclass(POINTER(Sub2), POINTER(Sub))
self.assertIsSubclass(POINTER(Sub), POINTER(CtBase))
self.assertIs(POINTER(PtrBase), ptr_base_pointer)
self.assertIs(POINTER(CtBase), ct_base_pointer)
self.assertIs(POINTER(Sub), sub_pointer)
self.assertIs(POINTER(Sub2), sub2_pointer)
def test_creating_pointer_in_dunder_init_2(self):
class ct_meta(type):
def __init__(self, name, bases, namespace):
super().__init__(name, bases, namespace)
# Avoid recursion.
# (See test_creating_pointer_in_dunder_new_2)
if isinstance(self, p_meta):
return
p = p_meta(f"POINTER({self.__name__})", (self, c_void_p), {})
set_non_ctypes_pointer_type(self, p)
class p_meta(PyCSimpleType, ct_meta):
pass
class Core(object):
pass
class CtBase(Core, metaclass=ct_meta):
pass
ct_base_pointer = POINTER(CtBase)
class Sub(CtBase):
pass
sub_pointer = POINTER(Sub)
self.assertIsNot(ct_base_pointer, sub_pointer)
self.assertIsInstance(POINTER(Sub), p_meta)
self.assertIsSubclass(POINTER(Sub), Sub)
self.assertIs(POINTER(CtBase), ct_base_pointer)
self.assertIs(POINTER(Sub), sub_pointer)
def test_bad_type_message(self):
"""Verify the error message that lists all available type codes"""
# (The string is generated at runtime, so this checks the underlying
# set of types as well as correct construction of the string.)
with self.assertRaises(AttributeError) as cm:
class F(metaclass=PyCSimpleType):
_type_ = "\0"
message = str(cm.exception)
expected_type_chars = list('cbBhHiIlLdDFGfuzZqQPXOv?g')
if not hasattr(ctypes, 'c_float_complex'):
expected_type_chars.remove('F')
expected_type_chars.remove('D')
expected_type_chars.remove('G')
if not MS_WINDOWS:
expected_type_chars.remove('X')
self.assertIn("'" + ''.join(expected_type_chars) + "'", message)
def test_creating_pointer_in_dunder_init_3(self):
"""Check if interfcase subclasses properly creates according internal
pointer types. But not the same as external pointer types.
"""
class StructureMeta(PyCStructType):
def __new__(cls, name, bases, dct, /, create_pointer_type=True):
assert len(bases) == 1, bases
return super().__new__(cls, name, bases, dct)
def __init__(self, name, bases, dct, /, create_pointer_type=True):
super().__init__(name, bases, dct)
if create_pointer_type:
p_bases = (POINTER(bases[0]),)
ns = {'_type_': self}
internal_pointer_type = PointerMeta(f"p{name}", p_bases, ns)
assert isinstance(internal_pointer_type, PyCPointerType)
assert self.__pointer_type__ is internal_pointer_type
class PointerMeta(PyCPointerType):
def __new__(cls, name, bases, dct):
target = dct.get('_type_', None)
if target is None:
# Create corresponding interface type and then set it as target
target = StructureMeta(
f"_{name}_",
(bases[0]._type_,),
{},
create_pointer_type=False
)
dct['_type_'] = target
pointer_type = super().__new__(cls, name, bases, dct)
assert not hasattr(target, '__pointer_type__')
return pointer_type
def __init__(self, name, bases, dct, /, create_pointer_type=True):
target = dct.get('_type_', None)
assert not hasattr(target, '__pointer_type__')
super().__init__(name, bases, dct)
assert target.__pointer_type__ is self
class Interface(Structure, metaclass=StructureMeta, create_pointer_type=False):
pass
class pInterface(POINTER(c_void_p), metaclass=PointerMeta):
_type_ = Interface
class IUnknown(Interface):
pass
class pIUnknown(pInterface):
pass
self.assertTrue(issubclass(POINTER(IUnknown), pInterface))
self.assertIs(POINTER(Interface), pInterface)
self.assertIsNot(POINTER(IUnknown), pIUnknown)
def test_creating_pointer_in_dunder_init_4(self):
"""Check if interfcase subclasses properly creates according internal
pointer types, the same as external pointer types.
"""
class StructureMeta(PyCStructType):
def __new__(cls, name, bases, dct, /, create_pointer_type=True):
assert len(bases) == 1, bases
return super().__new__(cls, name, bases, dct)
def __init__(self, name, bases, dct, /, create_pointer_type=True):
super().__init__(name, bases, dct)
if create_pointer_type:
p_bases = (POINTER(bases[0]),)
ns = {'_type_': self}
internal_pointer_type = PointerMeta(f"p{name}", p_bases, ns)
assert isinstance(internal_pointer_type, PyCPointerType)
assert self.__pointer_type__ is internal_pointer_type
class PointerMeta(PyCPointerType):
def __new__(cls, name, bases, dct):
target = dct.get('_type_', None)
assert target is not None
pointer_type = getattr(target, '__pointer_type__', None)
if pointer_type is None:
pointer_type = super().__new__(cls, name, bases, dct)
return pointer_type
def __init__(self, name, bases, dct, /, create_pointer_type=True):
target = dct.get('_type_', None)
if not hasattr(target, '__pointer_type__'):
# target.__pointer_type__ was created by super().__new__
super().__init__(name, bases, dct)
assert target.__pointer_type__ is self
class Interface(Structure, metaclass=StructureMeta, create_pointer_type=False):
pass
class pInterface(POINTER(c_void_p), metaclass=PointerMeta):
_type_ = Interface
class IUnknown(Interface):
pass
class pIUnknown(pInterface):
_type_ = IUnknown
self.assertTrue(issubclass(POINTER(IUnknown), pInterface))
self.assertIs(POINTER(Interface), pInterface)
self.assertIs(POINTER(IUnknown), pIUnknown)
def test_custom_pointer_cache_for_ctypes_type1(self):
# Check if PyCPointerType.__init__() caches a pointer type
# customized in the metatype's __new__().
class PointerMeta(PyCPointerType):
def __new__(cls, name, bases, namespace):
namespace["_type_"] = C
return super().__new__(cls, name, bases, namespace)
def __init__(self, name, bases, namespace):
assert not hasattr(C, '__pointer_type__')
super().__init__(name, bases, namespace)
assert C.__pointer_type__ is self
class C(c_void_p): # ctypes type
pass
class P(ctypes._Pointer, metaclass=PointerMeta):
pass
self.assertIs(P._type_, C)
self.assertIs(P, POINTER(C))
def test_custom_pointer_cache_for_ctypes_type2(self):
# Check if PyCPointerType.__init__() caches a pointer type
# customized in the metatype's __init__().
class PointerMeta(PyCPointerType):
def __init__(self, name, bases, namespace):
self._type_ = namespace["_type_"] = C
assert not hasattr(C, '__pointer_type__')
super().__init__(name, bases, namespace)
assert C.__pointer_type__ is self
class C(c_void_p): # ctypes type
pass
class P(ctypes._Pointer, metaclass=PointerMeta):
pass
self.assertIs(P._type_, C)
self.assertIs(P, POINTER(C))