Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(7)

Side by Side Diff: Lib/ctypes/test/test_byteswap.py

Issue 10639: reindent.py converts newlines to platform default
Patch Set: Created 8 years, 8 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Lib/ctypes/_endian.py ('k') | Lib/distutils/core.py » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 import sys, unittest, struct, math, ctypes 1 import sys, unittest, struct, math
2 from binascii import hexlify 2 from binascii import hexlify
3 3
4 from ctypes import * 4 from ctypes import *
5 5
6 def bin(s): 6 def bin(s):
7 return hexlify(memoryview(s)).decode().upper() 7 return hexlify(memoryview(s)).decode().upper()
8 8
9 # Each *simple* type that supports different byte orders has an 9 # Each *simple* type that supports different byte orders has an
10 # __ctype_be__ attribute that specifies the same type in BIG ENDIAN 10 # __ctype_be__ attribute that specifies the same type in BIG ENDIAN
11 # byte order, and a __ctype_le__ attribute that is the same type in 11 # byte order, and a __ctype_le__ attribute that is the same type in
(...skipping 166 matching lines...) Expand 10 before | Expand all | Expand 10 after
178 T._fields_ = _fields_ 178 T._fields_ = _fields_
179 179
180 # these fields do not support different byte order: 180 # these fields do not support different byte order:
181 for typ in c_wchar, c_void_p, POINTER(c_int): 181 for typ in c_wchar, c_void_p, POINTER(c_int):
182 _fields_.append(("x", typ)) 182 _fields_.append(("x", typ))
183 class T(base): 183 class T(base):
184 pass 184 pass
185 self.assertRaises(TypeError, setattr, T, "_fields_", [("x", typ)]) 185 self.assertRaises(TypeError, setattr, T, "_fields_", [("x", typ)])
186 186
187 def test_struct_struct(self): 187 def test_struct_struct(self):
188 # nested structures with different byteorders 188 # Nested structures with different byte order not (yet) supported
189 if sys.byteorder == "little":
190 base = BigEndianStructure
191 else:
192 base = LittleEndianStructure
189 193
190 # create nested structures with given byteorders and set memory to data 194 class T(Structure):
191 195 _fields_ = [("a", c_int),
192 for nested, data in ( 196 ("b", c_int)]
193 (BigEndianStructure, b'\0\0\0\1\0\0\0\2'), 197 class S(base):
194 (LittleEndianStructure, b'\1\0\0\0\2\0\0\0'), 198 pass
195 ): 199 self.assertRaises(TypeError, setattr, S, "_fields_", [("s", T)])
196 for parent in (
197 BigEndianStructure,
198 LittleEndianStructure,
199 Structure,
200 ):
201 class NestedStructure(nested):
202 _fields_ = [("x", c_uint32),
203 ("y", c_uint32)]
204
205 class TestStructure(parent):
206 _fields_ = [("point", NestedStructure)]
207
208 self.assertEqual(len(data), sizeof(TestStructure))
209 ptr = POINTER(TestStructure)
210 s = cast(data, ptr)[0]
211 del ctypes._pointer_type_cache[TestStructure]
212 self.assertEqual(s.point.x, 1)
213 self.assertEqual(s.point.y, 2)
214 200
215 def test_struct_fields_2(self): 201 def test_struct_fields_2(self):
216 # standard packing in struct uses no alignment. 202 # standard packing in struct uses no alignment.
217 # So, we have to align using pad bytes. 203 # So, we have to align using pad bytes.
218 # 204 #
219 # Unaligned accesses will crash Python (on those platforms that 205 # Unaligned accesses will crash Python (on those platforms that
220 # don't allow it, like sparc solaris). 206 # don't allow it, like sparc solaris).
221 if sys.byteorder == "little": 207 if sys.byteorder == "little":
222 base = BigEndianStructure 208 base = BigEndianStructure
223 fmt = ">bxhid" 209 fmt = ">bxhid"
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
285 s1 = S() 271 s1 = S()
286 s1.b = 0x12 272 s1.b = 0x12
287 s1.h = 0x1234 273 s1.h = 0x1234
288 s1.i = 0x12345678 274 s1.i = 0x12345678
289 s1.d = 3.14 275 s1.d = 3.14
290 s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14) 276 s2 = struct.pack(fmt, 0x12, 0x1234, 0x12345678, 3.14)
291 self.assertEqual(bin(s1), bin(s2)) 277 self.assertEqual(bin(s1), bin(s2))
292 278
293 if __name__ == "__main__": 279 if __name__ == "__main__":
294 unittest.main() 280 unittest.main()
OLDNEW
« no previous file with comments | « Lib/ctypes/_endian.py ('k') | Lib/distutils/core.py » ('j') | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+