| 1 |
import re |
| 2 |
import sys |
| 3 |
import unittest |
| 4 |
from ctypes import (CFUNCTYPE, POINTER, sizeof, Union, |
| 5 |
Structure, LittleEndianStructure, BigEndianStructure, |
| 6 |
c_char, c_byte, c_ubyte, |
| 7 |
c_short, c_ushort, c_int, c_uint, |
| 8 |
c_long, c_ulong, c_longlong, c_ulonglong, c_uint64, |
| 9 |
c_bool, c_float, c_double, c_longdouble, py_object) |
| 10 |
|
| 11 |
|
| 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 |
|
| 29 |
class Test(unittest.TestCase): |
| 30 |
def test_native_types(self): |
| 31 |
for tp, fmt, shape, itemtp in native_types: |
| 32 |
ob = tp() |
| 33 |
v = memoryview(ob) |
| 34 |
self.assertEqual(normalize(v.format), normalize(fmt)) |
| 35 |
if shape: |
| 36 |
self.assertEqual(len(v), shape[0]) |
| 37 |
else: |
| 38 |
self.assertRaises(TypeError, len, v) |
| 39 |
self.assertEqual(v.itemsize, sizeof(itemtp)) |
| 40 |
self.assertEqual(v.shape, shape) |
| 41 |
# XXX Issue #12851: PyCData_NewGetBuffer() must provide strides |
| 42 |
# if requested. memoryview currently reconstructs missing |
| 43 |
# stride information, so this assert will fail. |
| 44 |
# self.assertEqual(v.strides, ()) |
| 45 |
|
| 46 |
# they are always read/write |
| 47 |
self.assertFalse(v.readonly) |
| 48 |
|
| 49 |
n = 1 |
| 50 |
for dim in v.shape: |
| 51 |
n = n * dim |
| 52 |
self.assertEqual(n * v.itemsize, len(v.tobytes())) |
| 53 |
|
| 54 |
def test_endian_types(self): |
| 55 |
for tp, fmt, shape, itemtp in endian_types: |
| 56 |
ob = tp() |
| 57 |
v = memoryview(ob) |
| 58 |
self.assertEqual(v.format, fmt) |
| 59 |
if shape: |
| 60 |
self.assertEqual(len(v), shape[0]) |
| 61 |
else: |
| 62 |
self.assertRaises(TypeError, len, v) |
| 63 |
self.assertEqual(v.itemsize, sizeof(itemtp)) |
| 64 |
self.assertEqual(v.shape, shape) |
| 65 |
# XXX Issue #12851 |
| 66 |
# self.assertEqual(v.strides, ()) |
| 67 |
|
| 68 |
# they are always read/write |
| 69 |
self.assertFalse(v.readonly) |
| 70 |
|
| 71 |
n = 1 |
| 72 |
for dim in v.shape: |
| 73 |
n = n * dim |
| 74 |
self.assertEqual(n * v.itemsize, len(v.tobytes())) |
| 75 |
|
| 76 |
|
| 77 |
# define some structure classes |
| 78 |
|
| 79 |
class Point(Structure): |
| 80 |
_fields_ = [("x", c_long), ("y", c_long)] |
| 81 |
|
| 82 |
class PackedPoint(Structure): |
| 83 |
_pack_ = 2 |
| 84 |
_layout_ = 'ms' |
| 85 |
_fields_ = [("x", c_long), ("y", c_long)] |
| 86 |
|
| 87 |
class PointMidPad(Structure): |
| 88 |
_fields_ = [("x", c_byte), ("y", c_uint)] |
| 89 |
|
| 90 |
class PackedPointMidPad(Structure): |
| 91 |
_pack_ = 2 |
| 92 |
_layout_ = 'ms' |
| 93 |
_fields_ = [("x", c_byte), ("y", c_uint64)] |
| 94 |
|
| 95 |
class PointEndPad(Structure): |
| 96 |
_fields_ = [("x", c_uint), ("y", c_byte)] |
| 97 |
|
| 98 |
class PackedPointEndPad(Structure): |
| 99 |
_pack_ = 2 |
| 100 |
_layout_ = 'ms' |
| 101 |
_fields_ = [("x", c_uint64), ("y", c_byte)] |
| 102 |
|
| 103 |
class Point2(Structure): |
| 104 |
pass |
| 105 |
Point2._fields_ = [("x", c_long), ("y", c_long)] |
| 106 |
|
| 107 |
class EmptyStruct(Structure): |
| 108 |
_fields_ = [] |
| 109 |
|
| 110 |
class aUnion(Union): |
| 111 |
_fields_ = [("a", c_int)] |
| 112 |
|
| 113 |
class StructWithArrays(Structure): |
| 114 |
_fields_ = [("x", c_long * 3 * 2), ("y", Point * 4)] |
| 115 |
|
| 116 |
class Incomplete(Structure): |
| 117 |
pass |
| 118 |
|
| 119 |
class Complete(Structure): |
| 120 |
pass |
| 121 |
PComplete = POINTER(Complete) |
| 122 |
Complete._fields_ = [("a", c_long)] |
| 123 |
|
| 124 |
|
| 125 |
################################################################ |
| 126 |
# |
| 127 |
# This table contains format strings as they look on little endian |
| 128 |
# machines. The test replaces '<' with '>' on big endian machines. |
| 129 |
# |
| 130 |
|
| 131 |
# Platform-specific type codes |
| 132 |
s_bool = {1: '?', 2: 'H', 4: 'L', 8: 'Q'}[sizeof(c_bool)] |
| 133 |
s_short = {2: 'h', 4: 'l', 8: 'q'}[sizeof(c_short)] |
| 134 |
s_ushort = {2: 'H', 4: 'L', 8: 'Q'}[sizeof(c_ushort)] |
| 135 |
s_int = {2: 'h', 4: 'i', 8: 'q'}[sizeof(c_int)] |
| 136 |
s_uint = {2: 'H', 4: 'I', 8: 'Q'}[sizeof(c_uint)] |
| 137 |
s_long = {4: 'l', 8: 'q'}[sizeof(c_long)] |
| 138 |
s_ulong = {4: 'L', 8: 'Q'}[sizeof(c_ulong)] |
| 139 |
s_longlong = "q" |
| 140 |
s_ulonglong = "Q" |
| 141 |
s_float = "f" |
| 142 |
s_double = "d" |
| 143 |
s_longdouble = "g" |
| 144 |
|
| 145 |
# Alias definitions in ctypes/__init__.py |
| 146 |
if c_int is c_long: |
| 147 |
s_int = s_long |
| 148 |
if c_uint is c_ulong: |
| 149 |
s_uint = s_ulong |
| 150 |
if c_longlong is c_long: |
| 151 |
s_longlong = s_long |
| 152 |
if c_ulonglong is c_ulong: |
| 153 |
s_ulonglong = s_ulong |
| 154 |
if c_longdouble is c_double: |
| 155 |
s_longdouble = s_double |
| 156 |
|
| 157 |
|
| 158 |
native_types = [ |
| 159 |
# type format shape calc itemsize |
| 160 |
|
| 161 |
## simple types |
| 162 |
|
| 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), |
| 168 |
|
| 169 |
(c_int, "<" + s_int, (), c_int), |
| 170 |
(c_uint, "<" + s_uint, (), c_uint), |
| 171 |
|
| 172 |
(c_long, "<" + s_long, (), c_long), |
| 173 |
(c_ulong, "<" + s_ulong, (), c_ulong), |
| 174 |
|
| 175 |
(c_longlong, "<" + s_longlong, (), c_longlong), |
| 176 |
(c_ulonglong, "<" + s_ulonglong, (), c_ulonglong), |
| 177 |
|
| 178 |
(c_float, "<f", (), c_float), |
| 179 |
(c_double, "<d", (), c_double), |
| 180 |
|
| 181 |
(c_longdouble, "<" + s_longdouble, (), c_longdouble), |
| 182 |
|
| 183 |
(c_bool, "<" + s_bool, (), c_bool), |
| 184 |
(py_object, "<O", (), py_object), |
| 185 |
|
| 186 |
## pointers |
| 187 |
|
| 188 |
(POINTER(c_byte), "&<b", (), POINTER(c_byte)), |
| 189 |
(POINTER(POINTER(c_long)), "&&<" + s_long, (), POINTER(POINTER(c_long))), |
| 190 |
|
| 191 |
## arrays and pointers |
| 192 |
|
| 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)), |
| 200 |
|
| 201 |
## structures and unions |
| 202 |
|
| 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), |
| 211 |
# the pep doesn't support unions |
| 212 |
(aUnion, "B", (), aUnion), |
| 213 |
# 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), |
| 216 |
|
| 217 |
## pointer to incomplete structure |
| 218 |
(Incomplete, "B", (), Incomplete), |
| 219 |
(POINTER(Incomplete), "&B", (), POINTER(Incomplete)), |
| 220 |
|
| 221 |
# 'Complete' is a structure that starts incomplete, but is completed after the |
| 222 |
# pointer type to it has been created. |
| 223 |
(Complete, "T{<l:a:}".replace('l', s_long), (), Complete), |
| 224 |
# Unfortunately the pointer format string is not fixed... |
| 225 |
(POINTER(Complete), "&B", (), POINTER(Complete)), |
| 226 |
|
| 227 |
## other |
| 228 |
|
| 229 |
# function signatures are not implemented |
| 230 |
(CFUNCTYPE(None), "X{}", (), CFUNCTYPE(None)), |
| 231 |
|
| 232 |
] |
| 233 |
|
| 234 |
|
| 235 |
class BEPoint(BigEndianStructure): |
| 236 |
_fields_ = [("x", c_long), ("y", c_long)] |
| 237 |
|
| 238 |
class LEPoint(LittleEndianStructure): |
| 239 |
_fields_ = [("x", c_long), ("y", c_long)] |
| 240 |
|
| 241 |
|
| 242 |
# This table contains format strings as they really look, on both big |
| 243 |
# 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 |
] |
| 250 |
|
| 251 |
|
| 252 |
if __name__ == "__main__": |
| 253 |
unittest.main() |