| 1 |
import unittest |
| 2 |
from ctypes import * |
| 3 |
import re, sys |
| 4 |
|
| 5 |
if sys.byteorder == "little": |
| 6 |
THIS_ENDIAN = "<" |
| 7 |
OTHER_ENDIAN = ">" |
| 8 |
else: |
| 9 |
THIS_ENDIAN = ">" |
| 10 |
OTHER_ENDIAN = "<" |
| 11 |
|
| 12 |
def normalize(format): |
| 13 |
|
| 14 |
|
| 15 |
if format is None: |
| 16 |
return "" |
| 17 |
format = format.replace(OTHER_ENDIAN, THIS_ENDIAN) |
| 18 |
return re.sub(r"\s", "", format) |
| 19 |
|
| 20 |
class Test(unittest.TestCase): |
| 21 |
|
| 22 |
def test_native_types(self): |
| 23 |
for tp, fmt, shape, itemtp in native_types: |
| 24 |
ob = tp() |
| 25 |
v = memoryview(ob) |
| 26 |
try: |
| 27 |
self.assertEqual(normalize(v.format), normalize(fmt)) |
| 28 |
if shape is not None: |
| 29 |
self.assertEqual(len(v), shape[0]) |
| 30 |
else: |
| 31 |
self.assertEqual(len(v) * sizeof(itemtp), sizeof(ob)) |
| 32 |
self.assertEqual(v.itemsize, sizeof(itemtp)) |
| 33 |
self.assertEqual(v.shape, shape) |
| 34 |
|
| 35 |
self.assertEqual(v.strides, None) |
| 36 |
|
| 37 |
self.assertFalse(v.readonly) |
| 38 |
|
| 39 |
if v.shape: |
| 40 |
n = 1 |
| 41 |
for dim in v.shape: |
| 42 |
n = n * dim |
| 43 |
self.assertEqual(n * v.itemsize, len(v.tobytes())) |
| 44 |
except: |
| 45 |
|
| 46 |
print(tp) |
| 47 |
raise |
| 48 |
|
| 49 |
def test_endian_types(self): |
| 50 |
for tp, fmt, shape, itemtp in endian_types: |
| 51 |
ob = tp() |
| 52 |
v = memoryview(ob) |
| 53 |
try: |
| 54 |
self.assertEqual(v.format, fmt) |
| 55 |
if shape is not None: |
| 56 |
self.assertEqual(len(v), shape[0]) |
| 57 |
else: |
| 58 |
self.assertEqual(len(v) * sizeof(itemtp), sizeof(ob)) |
| 59 |
self.assertEqual(v.itemsize, sizeof(itemtp)) |
| 60 |
self.assertEqual(v.shape, shape) |
| 61 |
|
| 62 |
self.assertEqual(v.strides, None) |
| 63 |
|
| 64 |
self.assertFalse(v.readonly) |
| 65 |
|
| 66 |
if v.shape: |
| 67 |
n = 1 |
| 68 |
for dim in v.shape: |
| 69 |
n = n * dim |
| 70 |
self.assertEqual(n, len(v)) |
| 71 |
except: |
| 72 |
|
| 73 |
print(tp) |
| 74 |
raise |
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
class Point(Structure): |
| 79 |
_fields_ = [("x", c_long), ("y", c_long)] |
| 80 |
|
| 81 |
class PackedPoint(Structure): |
| 82 |
_pack_ = 2 |
| 83 |
_fields_ = [("x", c_long), ("y", c_long)] |
| 84 |
|
| 85 |
class Point2(Structure): |
| 86 |
pass |
| 87 |
Point2._fields_ = [("x", c_long), ("y", c_long)] |
| 88 |
|
| 89 |
class EmptyStruct(Structure): |
| 90 |
_fields_ = [] |
| 91 |
|
| 92 |
class aUnion(Union): |
| 93 |
_fields_ = [("a", c_int)] |
| 94 |
|
| 95 |
class StructWithArrays(Structure): |
| 96 |
_fields_ = [("x", c_long * 3 * 2), ("y", Point * 4)] |
| 97 |
|
| 98 |
|
| 99 |
class Incomplete(Structure): |
| 100 |
pass |
| 101 |
|
| 102 |
class Complete(Structure): |
| 103 |
pass |
| 104 |
PComplete = POINTER(Complete) |
| 105 |
Complete._fields_ = [("a", c_long)] |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
|
| 113 |
|
| 114 |
s_bool = {1: '?', 2: 'H', 4: 'L', 8: 'Q'}[sizeof(c_bool)] |
| 115 |
s_short = {2: 'h', 4: 'l', 8: 'q'}[sizeof(c_short)] |
| 116 |
s_ushort = {2: 'H', 4: 'L', 8: 'Q'}[sizeof(c_ushort)] |
| 117 |
s_int = {2: 'h', 4: 'i', 8: 'q'}[sizeof(c_int)] |
| 118 |
s_uint = {2: 'H', 4: 'I', 8: 'Q'}[sizeof(c_uint)] |
| 119 |
s_long = {4: 'l', 8: 'q'}[sizeof(c_long)] |
| 120 |
s_ulong = {4: 'L', 8: 'Q'}[sizeof(c_ulong)] |
| 121 |
s_longlong = "q" |
| 122 |
s_ulonglong = "Q" |
| 123 |
s_float = "f" |
| 124 |
s_double = "d" |
| 125 |
s_longdouble = "g" |
| 126 |
|
| 127 |
|
| 128 |
if c_int is c_long: |
| 129 |
s_int = s_long |
| 130 |
if c_uint is c_ulong: |
| 131 |
s_uint = s_ulong |
| 132 |
if c_longlong is c_long: |
| 133 |
s_longlong = s_long |
| 134 |
if c_ulonglong is c_ulong: |
| 135 |
s_ulonglong = s_ulong |
| 136 |
if c_longdouble is c_double: |
| 137 |
s_longdouble = s_double |
| 138 |
|
| 139 |
|
| 140 |
native_types = [ |
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
(c_char, "<c", None, c_char), |
| 146 |
(c_byte, "<b", None, c_byte), |
| 147 |
(c_ubyte, "<B", None, c_ubyte), |
| 148 |
(c_short, "<" + s_short, None, c_short), |
| 149 |
(c_ushort, "<" + s_ushort, None, c_ushort), |
| 150 |
|
| 151 |
(c_int, "<" + s_int, None, c_int), |
| 152 |
(c_uint, "<" + s_uint, None, c_uint), |
| 153 |
|
| 154 |
(c_long, "<" + s_long, None, c_long), |
| 155 |
(c_ulong, "<" + s_ulong, None, c_ulong), |
| 156 |
|
| 157 |
(c_longlong, "<" + s_longlong, None, c_longlong), |
| 158 |
(c_ulonglong, "<" + s_ulonglong, None, c_ulonglong), |
| 159 |
|
| 160 |
(c_float, "<f", None, c_float), |
| 161 |
(c_double, "<d", None, c_double), |
| 162 |
|
| 163 |
(c_longdouble, "<" + s_longdouble, None, c_longdouble), |
| 164 |
|
| 165 |
(c_bool, "<" + s_bool, None, c_bool), |
| 166 |
(py_object, "<O", None, py_object), |
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
(POINTER(c_byte), "&<b", None, POINTER(c_byte)), |
| 171 |
(POINTER(POINTER(c_long)), "&&<" + s_long, None, POINTER(POINTER(c_long))), |
| 172 |
|
| 173 |
|
| 174 |
|
| 175 |
(c_double * 4, "<d", (4,), c_double), |
| 176 |
(c_float * 4 * 3 * 2, "<f", (2,3,4), c_float), |
| 177 |
(POINTER(c_short) * 2, "&<" + s_short, (2,), POINTER(c_short)), |
| 178 |
(POINTER(c_short) * 2 * 3, "&<" + s_short, (3,2,), POINTER(c_short)), |
| 179 |
(POINTER(c_short * 2), "&(2)<" + s_short, None, POINTER(c_short)), |
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
(Point, "T{<l:x:<l:y:}".replace('l', s_long), None, Point), |
| 184 |
|
| 185 |
(PackedPoint, "B", None, PackedPoint), |
| 186 |
(Point2, "T{<l:x:<l:y:}".replace('l', s_long), None, Point2), |
| 187 |
(EmptyStruct, "T{}", None, EmptyStruct), |
| 188 |
|
| 189 |
(aUnion, "B", None, aUnion), |
| 190 |
|
| 191 |
(StructWithArrays, "T{(2,3)<l:x:(4)T{<l:x:<l:y:}:y:}".replace('l', s_long), None, StructWithArrays), |
| 192 |
(StructWithArrays * 3, "T{(2,3)<l:x:(4)T{<l:x:<l:y:}:y:}".replace('l', s_long), (3,), StructWithArrays), |
| 193 |
|
| 194 |
|
| 195 |
(Incomplete, "B", None, Incomplete), |
| 196 |
(POINTER(Incomplete), "&B", None, POINTER(Incomplete)), |
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
(Complete, "T{<l:a:}".replace('l', s_long), None, Complete), |
| 201 |
|
| 202 |
(POINTER(Complete), "&B", None, POINTER(Complete)), |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
(CFUNCTYPE(None), "X{}", None, CFUNCTYPE(None)), |
| 208 |
|
| 209 |
] |
| 210 |
|
| 211 |
class BEPoint(BigEndianStructure): |
| 212 |
_fields_ = [("x", c_long), ("y", c_long)] |
| 213 |
|
| 214 |
class LEPoint(LittleEndianStructure): |
| 215 |
_fields_ = [("x", c_long), ("y", c_long)] |
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
endian_types = [ |
| 223 |
(BEPoint, "T{>l:x:>l:y:}".replace('l', s_long), None, BEPoint), |
| 224 |
(LEPoint, "T{<l:x:<l:y:}".replace('l', s_long), None, LEPoint), |
| 225 |
(POINTER(BEPoint), "&T{>l:x:>l:y:}".replace('l', s_long), None, POINTER(BEPoint)), |
| 226 |
(POINTER(LEPoint), "&T{<l:x:<l:y:}".replace('l', s_long), None, POINTER(LEPoint)), |
| 227 |
] |
| 228 |
|
| 229 |
if __name__ == "__main__": |
| 230 |
unittest.main() |