1- from ctypes import *
2- import unittest
31import 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
48
59def valid_ranges (* types ):
610 # given a sequence of numeric types, collect their _type_
@@ -21,29 +25,11 @@ def valid_ranges(*types):
2125
2226ArgType = type (byref (c_int (0 )))
2327
24- unsigned_types = [c_ubyte , c_ushort , c_uint , c_ulong ]
28+ unsigned_types = [c_ubyte , c_ushort , c_uint , c_ulong , c_ulonglong ]
2529signed_types = [c_byte , c_short , c_int , c_long , c_longlong ]
26-
27- bool_types = []
28-
30+ bool_types = [c_bool ]
2931float_types = [c_double , c_float ]
3032
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-
4733unsigned_ranges = valid_ranges (* unsigned_types )
4834signed_ranges = valid_ranges (* signed_types )
4935bool_values = [True , False , 0 , 1 , - 1 , 5000 , 'test' , [], [1 ]]
@@ -71,7 +57,6 @@ def test_signed_values(self):
7157 self .assertEqual (t (h ).value , h )
7258
7359 def test_bool_values (self ):
74- from operator import truth
7560 for t , v in zip (bool_types , bool_values ):
7661 self .assertEqual (t (v ).value , truth (v ))
7762
@@ -161,7 +146,6 @@ def test_alignments(self):
161146 (code , align ))
162147
163148 def test_int_from_address (self ):
164- from array import array
165149 for t in signed_types + unsigned_types :
166150 # the array module doesn't support all format codes
167151 # (no 'q' or 'Q')
@@ -182,7 +166,6 @@ def test_int_from_address(self):
182166
183167
184168 def test_float_from_address (self ):
185- from array import array
186169 for t in float_types :
187170 a = array (t ._type_ , [3.14 ])
188171 v = t .from_address (a .buffer_info ()[0 ])
@@ -193,9 +176,6 @@ def test_float_from_address(self):
193176 self .assertIs (type (v ), t )
194177
195178 def test_char_from_address (self ):
196- from ctypes import c_char
197- from array import array
198-
199179 a = array ('b' , [0 ])
200180 a [0 ] = ord ('x' )
201181 v = c_char .from_address (a .buffer_info ()[0 ])
@@ -208,8 +188,6 @@ def test_char_from_address(self):
208188 # array does not support c_bool / 't'
209189 @unittest .skip ('test disabled' )
210190 def test_bool_from_address (self ):
211- from ctypes import c_bool
212- from array import array
213191 a = array (c_bool ._type_ , [True ])
214192 v = t .from_address (a .buffer_info ()[0 ])
215193 self .assertEqual (v .value , a [0 ])
@@ -225,7 +203,6 @@ def test_init(self):
225203 self .assertRaises (TypeError , c_int , c_long (42 ))
226204
227205 def test_float_overflow (self ):
228- import sys
229206 big_int = int (sys .float_info .max ) * 2
230207 for t in float_types + [c_longdouble ]:
231208 self .assertRaises (OverflowError , t , big_int )
@@ -238,7 +215,6 @@ def test_float_overflow(self):
238215 def test_perf (self ):
239216 check_perf ()
240217
241- from ctypes import _SimpleCData
242218class c_int_S (_SimpleCData ):
243219 _type_ = "i"
244220 __slots__ = []
0 commit comments