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