99 c_bool , c_float , c_double , c_longdouble , py_object )
1010
1111
12- if sys .byteorder == "little" :
13- THIS_ENDIAN = "<"
14- OTHER_ENDIAN = ">"
15- else :
16- THIS_ENDIAN = ">"
17- OTHER_ENDIAN = "<"
18-
19-
20- def normalize (format ):
21- # Remove current endian specifier and white space from a format
22- # string
23- if format is None :
24- return ""
25- format = format .replace (OTHER_ENDIAN , THIS_ENDIAN )
26- return re .sub (r"\s" , "" , format )
27-
28-
2912class Test (unittest .TestCase ):
3013 def test_native_types (self ):
3114 for tp , fmt , shape , itemtp in native_types :
3215 ob = tp ()
3316 v = memoryview (ob )
34- self .assertEqual (normalize ( v .format ), normalize ( fmt ) )
17+ self .assertEqual (v .format , fmt )
3518 if shape :
3619 self .assertEqual (len (v ), shape [0 ])
3720 else :
@@ -73,6 +56,15 @@ def test_endian_types(self):
7356 n = n * dim
7457 self .assertEqual (n * v .itemsize , len (v .tobytes ()))
7558
59+ def test_memoryview_supports_ctypes_arrays (self ):
60+ ArrayType = c_int * 5
61+ a = ArrayType (123 , 42 , 1 , 2 , 3 )
62+ m = memoryview (a )
63+ self .assertEqual (m .shape , (5 ,))
64+ self .assertEqual (m .format , c_int ._type_ )
65+ self .assertEqual (list (m ), [123 , 42 , 1 , 2 , 3 ])
66+ self .assertEqual (m [1 ], 42 )
67+
7668
7769# define some structure classes
7870
@@ -124,8 +116,7 @@ class Complete(Structure):
124116
125117################################################################
126118#
127- # This table contains format strings as they look on little endian
128- # machines. The test replaces '<' with '>' on big endian machines.
119+ # This table contains format strings with native endianness.
129120#
130121
131122# Platform-specific type codes
@@ -160,67 +151,67 @@ class Complete(Structure):
160151
161152 ## simple types
162153
163- (c_char , "< c" , (), c_char ),
164- (c_byte , "< b" , (), c_byte ),
165- (c_ubyte , "< B" , (), c_ubyte ),
166- (c_short , "<" + s_short , (), c_short ),
167- (c_ushort , "<" + s_ushort , (), c_ushort ),
154+ (c_char , "c" , (), c_char ),
155+ (c_byte , "b" , (), c_byte ),
156+ (c_ubyte , "B" , (), c_ubyte ),
157+ (c_short , s_short , (), c_short ),
158+ (c_ushort , s_ushort , (), c_ushort ),
168159
169- (c_int , "<" + s_int , (), c_int ),
170- (c_uint , "<" + s_uint , (), c_uint ),
160+ (c_int , s_int , (), c_int ),
161+ (c_uint , s_uint , (), c_uint ),
171162
172- (c_long , "<" + s_long , (), c_long ),
173- (c_ulong , "<" + s_ulong , (), c_ulong ),
163+ (c_long , s_long , (), c_long ),
164+ (c_ulong , s_ulong , (), c_ulong ),
174165
175- (c_longlong , "<" + s_longlong , (), c_longlong ),
176- (c_ulonglong , "<" + s_ulonglong , (), c_ulonglong ),
166+ (c_longlong , s_longlong , (), c_longlong ),
167+ (c_ulonglong , s_ulonglong , (), c_ulonglong ),
177168
178- (c_float , "< f" , (), c_float ),
179- (c_double , "< d" , (), c_double ),
169+ (c_float , "f" , (), c_float ),
170+ (c_double , "d" , (), c_double ),
180171
181- (c_longdouble , "<" + s_longdouble , (), c_longdouble ),
172+ (c_longdouble , s_longdouble , (), c_longdouble ),
182173
183- (c_bool , "<" + s_bool , (), c_bool ),
184- (py_object , "< O" , (), py_object ),
174+ (c_bool , s_bool , (), c_bool ),
175+ (py_object , "O" , (), py_object ),
185176
186177 ## pointers
187178
188- (POINTER (c_byte ), "&< b" , (), POINTER (c_byte )),
189- (POINTER (POINTER (c_long )), "&&< " + s_long , (), POINTER (POINTER (c_long ))),
179+ (POINTER (c_byte ), "&b" , (), POINTER (c_byte )),
180+ (POINTER (POINTER (c_long )), "&&" + s_long , (), POINTER (POINTER (c_long ))),
190181
191182 ## arrays and pointers
192183
193- (c_double * 4 , "< d" , (4 ,), c_double ),
194- (c_double * 0 , "< d" , (0 ,), c_double ),
195- (c_float * 4 * 3 * 2 , "< f" , (2 ,3 ,4 ), c_float ),
196- (c_float * 4 * 0 * 2 , "< f" , (2 ,0 ,4 ), c_float ),
197- (POINTER (c_short ) * 2 , "&< " + s_short , (2 ,), POINTER (c_short )),
198- (POINTER (c_short ) * 2 * 3 , "&< " + s_short , (3 ,2 ,), POINTER (c_short )),
199- (POINTER (c_short * 2 ), "&(2)< " + s_short , (), POINTER (c_short )),
184+ (c_double * 4 , "d" , (4 ,), c_double ),
185+ (c_double * 0 , "d" , (0 ,), c_double ),
186+ (c_float * 4 * 3 * 2 , "f" , (2 ,3 ,4 ), c_float ),
187+ (c_float * 4 * 0 * 2 , "f" , (2 ,0 ,4 ), c_float ),
188+ (POINTER (c_short ) * 2 , "&" + s_short , (2 ,), POINTER (c_short )),
189+ (POINTER (c_short ) * 2 * 3 , "&" + s_short , (3 ,2 ,), POINTER (c_short )),
190+ (POINTER (c_short * 2 ), "&(2)" + s_short , (), POINTER (c_short )),
200191
201192 ## structures and unions
202193
203- (Point2 , "T{< l:x:< l:y:}" .replace ('l' , s_long ), (), Point2 ),
204- (Point , "T{< l:x:< l:y:}" .replace ('l' , s_long ), (), Point ),
205- (PackedPoint , "T{< l:x:< l:y:}" .replace ('l' , s_long ), (), PackedPoint ),
206- (PointMidPad , "T{< b:x:3x<I :y:}" .replace ('I' , s_uint ), (), PointMidPad ),
207- (PackedPointMidPad , "T{< b:x:x<Q :y:}" , (), PackedPointMidPad ),
208- (PointEndPad , "T{< I:x:< b:y:3x}" .replace ('I' , s_uint ), (), PointEndPad ),
209- (PackedPointEndPad , "T{< Q:x:< b:y:x}" , (), PackedPointEndPad ),
210- (EmptyStruct , "T{}" , (), EmptyStruct ),
194+ (Point2 , "T{l:x:l:y:}" .replace ('l' , s_long ), (), Point2 ),
195+ (Point , "T{l:x:l:y:}" .replace ('l' , s_long ), (), Point ),
196+ (PackedPoint , "T{l:x:l:y:}" .replace ('l' , s_long ), (), PackedPoint ),
197+ (PointMidPad , "T{b:x:3xI :y:}" .replace ('I' , s_uint ), (), PointMidPad ),
198+ (PackedPointMidPad , "T{b:x:xQ :y:}" , (), PackedPointMidPad ),
199+ (PointEndPad , "T{I:x:b:y:3x}" .replace ('I' , s_uint ), (), PointEndPad ),
200+ (PackedPointEndPad , "T{Q:x:b:y:x}" , (), PackedPointEndPad ),
201+ (EmptyStruct , "T{}" , (), EmptyStruct ),
211202 # the pep doesn't support unions
212- (aUnion , "B" , (), aUnion ),
203+ (aUnion , "B" , (), aUnion ),
213204 # structure with sub-arrays
214- (StructWithArrays , "T{(2,3)< l:x:(4)T{< l:x:< l:y:}:y:}" .replace ('l' , s_long ), (), StructWithArrays ),
215- (StructWithArrays * 3 , "T{(2,3)< l:x:(4)T{< l:x:< l:y:}:y:}" .replace ('l' , s_long ), (3 ,), StructWithArrays ),
205+ (StructWithArrays , "T{(2,3)l:x:(4)T{l:x:l:y:}:y:}" .replace ('l' , s_long ), (), StructWithArrays ),
206+ (StructWithArrays * 3 , "T{(2,3)l:x:(4)T{l:x:l:y:}:y:}" .replace ('l' , s_long ), (3 ,), StructWithArrays ),
216207
217208 ## pointer to incomplete structure
218209 (Incomplete , "B" , (), Incomplete ),
219210 (POINTER (Incomplete ), "&B" , (), POINTER (Incomplete )),
220211
221212 # 'Complete' is a structure that starts incomplete, but is completed after the
222213 # pointer type to it has been created.
223- (Complete , "T{< l:a:}" .replace ('l' , s_long ), (), Complete ),
214+ (Complete , "T{l:a:}" .replace ('l' , s_long ), (), Complete ),
224215 # Unfortunately the pointer format string is not fixed...
225216 (POINTER (Complete ), "&B" , (), POINTER (Complete )),
226217
@@ -241,12 +232,20 @@ class LEPoint(LittleEndianStructure):
241232
242233# This table contains format strings as they really look, on both big
243234# and little endian machines.
244- endian_types = [
245- (BEPoint , "T{>l:x:>l:y:}" .replace ('l' , s_long ), (), BEPoint ),
246- (LEPoint * 1 , "T{<l:x:<l:y:}" .replace ('l' , s_long ), (1 ,), LEPoint ),
247- (POINTER (BEPoint ), "&T{>l:x:>l:y:}" .replace ('l' , s_long ), (), POINTER (BEPoint )),
248- (POINTER (LEPoint ), "&T{<l:x:<l:y:}" .replace ('l' , s_long ), (), POINTER (LEPoint )),
249- ]
235+ if sys .byteorder == "little" :
236+ endian_types = [
237+ (BEPoint , "T{>l:x:>l:y:}" .replace ('l' , s_long ), (), BEPoint ),
238+ (LEPoint * 1 , "T{l:x:l:y:}" .replace ('l' , s_long ), (1 ,), LEPoint ),
239+ (POINTER (BEPoint ), "&T{>l:x:>l:y:}" .replace ('l' , s_long ), (), POINTER (BEPoint )),
240+ (POINTER (LEPoint ), "&T{l:x:l:y:}" .replace ('l' , s_long ), (), POINTER (LEPoint )),
241+ ]
242+ else :
243+ endian_types = [
244+ (BEPoint * 1 , "T{l:x:l:y:}" .replace ('l' , s_long ), (1 ,), BEPoint ),
245+ (LEPoint , "T{<l:x:<l:y:}" .replace ('l' , s_long ), (), LEPoint ),
246+ (POINTER (BEPoint ), "&T{l:x:l:y:}" .replace ('l' , s_long ), (), POINTER (BEPoint )),
247+ (POINTER (LEPoint ), "&T{<l:x:<l:y:}" .replace ('l' , s_long ), (), POINTER (LEPoint )),
248+ ]
250249
251250
252251if __name__ == "__main__" :
0 commit comments