1
- from ctypes import *
2
- import unittest
3
1
import struct
2
+ import sys
3
+ import unittest
4
+ from array import array
5
+ from operator import truth
6
+ from ctypes import *
7
+ from ctypes import _SimpleCData
4
8
5
9
def valid_ranges (* types ):
6
10
# given a sequence of numeric types, collect their _type_
@@ -21,29 +25,11 @@ def valid_ranges(*types):
21
25
22
26
ArgType = type (byref (c_int (0 )))
23
27
24
- unsigned_types = [c_ubyte , c_ushort , c_uint , c_ulong ]
28
+ unsigned_types = [c_ubyte , c_ushort , c_uint , c_ulong , c_ulonglong ]
25
29
signed_types = [c_byte , c_short , c_int , c_long , c_longlong ]
26
-
27
- bool_types = []
28
-
30
+ bool_types = [c_bool ]
29
31
float_types = [c_double , c_float ]
30
32
31
- try :
32
- c_ulonglong
33
- c_longlong
34
- except NameError :
35
- pass
36
- else :
37
- unsigned_types .append (c_ulonglong )
38
- signed_types .append (c_longlong )
39
-
40
- try :
41
- c_bool
42
- except NameError :
43
- pass
44
- else :
45
- bool_types .append (c_bool )
46
-
47
33
unsigned_ranges = valid_ranges (* unsigned_types )
48
34
signed_ranges = valid_ranges (* signed_types )
49
35
bool_values = [True , False , 0 , 1 , - 1 , 5000 , 'test' , [], [1 ]]
@@ -71,7 +57,6 @@ def test_signed_values(self):
71
57
self .assertEqual (t (h ).value , h )
72
58
73
59
def test_bool_values (self ):
74
- from operator import truth
75
60
for t , v in zip (bool_types , bool_values ):
76
61
self .assertEqual (t (v ).value , truth (v ))
77
62
@@ -161,7 +146,6 @@ def test_alignments(self):
161
146
(code , align ))
162
147
163
148
def test_int_from_address (self ):
164
- from array import array
165
149
for t in signed_types + unsigned_types :
166
150
# the array module doesn't support all format codes
167
151
# (no 'q' or 'Q')
@@ -182,7 +166,6 @@ def test_int_from_address(self):
182
166
183
167
184
168
def test_float_from_address (self ):
185
- from array import array
186
169
for t in float_types :
187
170
a = array (t ._type_ , [3.14 ])
188
171
v = t .from_address (a .buffer_info ()[0 ])
@@ -193,9 +176,6 @@ def test_float_from_address(self):
193
176
self .assertIs (type (v ), t )
194
177
195
178
def test_char_from_address (self ):
196
- from ctypes import c_char
197
- from array import array
198
-
199
179
a = array ('b' , [0 ])
200
180
a [0 ] = ord ('x' )
201
181
v = c_char .from_address (a .buffer_info ()[0 ])
@@ -208,8 +188,6 @@ def test_char_from_address(self):
208
188
# array does not support c_bool / 't'
209
189
@unittest .skip ('test disabled' )
210
190
def test_bool_from_address (self ):
211
- from ctypes import c_bool
212
- from array import array
213
191
a = array (c_bool ._type_ , [True ])
214
192
v = t .from_address (a .buffer_info ()[0 ])
215
193
self .assertEqual (v .value , a [0 ])
@@ -225,7 +203,6 @@ def test_init(self):
225
203
self .assertRaises (TypeError , c_int , c_long (42 ))
226
204
227
205
def test_float_overflow (self ):
228
- import sys
229
206
big_int = int (sys .float_info .max ) * 2
230
207
for t in float_types + [c_longdouble ]:
231
208
self .assertRaises (OverflowError , t , big_int )
@@ -238,7 +215,6 @@ def test_float_overflow(self):
238
215
def test_perf (self ):
239
216
check_perf ()
240
217
241
- from ctypes import _SimpleCData
242
218
class c_int_S (_SimpleCData ):
243
219
_type_ = "i"
244
220
__slots__ = []
0 commit comments