| 1 | n/a | import unittest |
|---|
| 2 | n/a | from ctypes import * |
|---|
| 3 | n/a | import re, sys |
|---|
| 4 | n/a | |
|---|
| 5 | n/a | if sys.byteorder == "little": |
|---|
| 6 | n/a | THIS_ENDIAN = "<" |
|---|
| 7 | n/a | OTHER_ENDIAN = ">" |
|---|
| 8 | n/a | else: |
|---|
| 9 | n/a | THIS_ENDIAN = ">" |
|---|
| 10 | n/a | OTHER_ENDIAN = "<" |
|---|
| 11 | n/a | |
|---|
| 12 | n/a | def normalize(format): |
|---|
| 13 | n/a | # Remove current endian specifier and white space from a format |
|---|
| 14 | n/a | # string |
|---|
| 15 | n/a | if format is None: |
|---|
| 16 | n/a | return "" |
|---|
| 17 | n/a | format = format.replace(OTHER_ENDIAN, THIS_ENDIAN) |
|---|
| 18 | n/a | return re.sub(r"\s", "", format) |
|---|
| 19 | n/a | |
|---|
| 20 | n/a | class Test(unittest.TestCase): |
|---|
| 21 | n/a | |
|---|
| 22 | n/a | def test_native_types(self): |
|---|
| 23 | n/a | for tp, fmt, shape, itemtp in native_types: |
|---|
| 24 | n/a | ob = tp() |
|---|
| 25 | n/a | v = memoryview(ob) |
|---|
| 26 | n/a | try: |
|---|
| 27 | n/a | self.assertEqual(normalize(v.format), normalize(fmt)) |
|---|
| 28 | n/a | if shape: |
|---|
| 29 | n/a | self.assertEqual(len(v), shape[0]) |
|---|
| 30 | n/a | else: |
|---|
| 31 | n/a | self.assertEqual(len(v) * sizeof(itemtp), sizeof(ob)) |
|---|
| 32 | n/a | self.assertEqual(v.itemsize, sizeof(itemtp)) |
|---|
| 33 | n/a | self.assertEqual(v.shape, shape) |
|---|
| 34 | n/a | # XXX Issue #12851: PyCData_NewGetBuffer() must provide strides |
|---|
| 35 | n/a | # if requested. memoryview currently reconstructs missing |
|---|
| 36 | n/a | # stride information, so this assert will fail. |
|---|
| 37 | n/a | # self.assertEqual(v.strides, ()) |
|---|
| 38 | n/a | |
|---|
| 39 | n/a | # they are always read/write |
|---|
| 40 | n/a | self.assertFalse(v.readonly) |
|---|
| 41 | n/a | |
|---|
| 42 | n/a | if v.shape: |
|---|
| 43 | n/a | n = 1 |
|---|
| 44 | n/a | for dim in v.shape: |
|---|
| 45 | n/a | n = n * dim |
|---|
| 46 | n/a | self.assertEqual(n * v.itemsize, len(v.tobytes())) |
|---|
| 47 | n/a | except: |
|---|
| 48 | n/a | # so that we can see the failing type |
|---|
| 49 | n/a | print(tp) |
|---|
| 50 | n/a | raise |
|---|
| 51 | n/a | |
|---|
| 52 | n/a | def test_endian_types(self): |
|---|
| 53 | n/a | for tp, fmt, shape, itemtp in endian_types: |
|---|
| 54 | n/a | ob = tp() |
|---|
| 55 | n/a | v = memoryview(ob) |
|---|
| 56 | n/a | try: |
|---|
| 57 | n/a | self.assertEqual(v.format, fmt) |
|---|
| 58 | n/a | if shape: |
|---|
| 59 | n/a | self.assertEqual(len(v), shape[0]) |
|---|
| 60 | n/a | else: |
|---|
| 61 | n/a | self.assertEqual(len(v) * sizeof(itemtp), sizeof(ob)) |
|---|
| 62 | n/a | self.assertEqual(v.itemsize, sizeof(itemtp)) |
|---|
| 63 | n/a | self.assertEqual(v.shape, shape) |
|---|
| 64 | n/a | # XXX Issue #12851 |
|---|
| 65 | n/a | # self.assertEqual(v.strides, ()) |
|---|
| 66 | n/a | |
|---|
| 67 | n/a | # they are always read/write |
|---|
| 68 | n/a | self.assertFalse(v.readonly) |
|---|
| 69 | n/a | |
|---|
| 70 | n/a | if v.shape: |
|---|
| 71 | n/a | n = 1 |
|---|
| 72 | n/a | for dim in v.shape: |
|---|
| 73 | n/a | n = n * dim |
|---|
| 74 | n/a | self.assertEqual(n, len(v)) |
|---|
| 75 | n/a | except: |
|---|
| 76 | n/a | # so that we can see the failing type |
|---|
| 77 | n/a | print(tp) |
|---|
| 78 | n/a | raise |
|---|
| 79 | n/a | |
|---|
| 80 | n/a | # define some structure classes |
|---|
| 81 | n/a | |
|---|
| 82 | n/a | class Point(Structure): |
|---|
| 83 | n/a | _fields_ = [("x", c_long), ("y", c_long)] |
|---|
| 84 | n/a | |
|---|
| 85 | n/a | class PackedPoint(Structure): |
|---|
| 86 | n/a | _pack_ = 2 |
|---|
| 87 | n/a | _fields_ = [("x", c_long), ("y", c_long)] |
|---|
| 88 | n/a | |
|---|
| 89 | n/a | class Point2(Structure): |
|---|
| 90 | n/a | pass |
|---|
| 91 | n/a | Point2._fields_ = [("x", c_long), ("y", c_long)] |
|---|
| 92 | n/a | |
|---|
| 93 | n/a | class EmptyStruct(Structure): |
|---|
| 94 | n/a | _fields_ = [] |
|---|
| 95 | n/a | |
|---|
| 96 | n/a | class aUnion(Union): |
|---|
| 97 | n/a | _fields_ = [("a", c_int)] |
|---|
| 98 | n/a | |
|---|
| 99 | n/a | class StructWithArrays(Structure): |
|---|
| 100 | n/a | _fields_ = [("x", c_long * 3 * 2), ("y", Point * 4)] |
|---|
| 101 | n/a | |
|---|
| 102 | n/a | class Incomplete(Structure): |
|---|
| 103 | n/a | pass |
|---|
| 104 | n/a | |
|---|
| 105 | n/a | class Complete(Structure): |
|---|
| 106 | n/a | pass |
|---|
| 107 | n/a | PComplete = POINTER(Complete) |
|---|
| 108 | n/a | Complete._fields_ = [("a", c_long)] |
|---|
| 109 | n/a | |
|---|
| 110 | n/a | ################################################################ |
|---|
| 111 | n/a | # |
|---|
| 112 | n/a | # This table contains format strings as they look on little endian |
|---|
| 113 | n/a | # machines. The test replaces '<' with '>' on big endian machines. |
|---|
| 114 | n/a | # |
|---|
| 115 | n/a | native_types = [ |
|---|
| 116 | n/a | # type format shape calc itemsize |
|---|
| 117 | n/a | |
|---|
| 118 | n/a | ## simple types |
|---|
| 119 | n/a | |
|---|
| 120 | n/a | (c_char, "<c", (), c_char), |
|---|
| 121 | n/a | (c_byte, "<b", (), c_byte), |
|---|
| 122 | n/a | (c_ubyte, "<B", (), c_ubyte), |
|---|
| 123 | n/a | (c_short, "<h", (), c_short), |
|---|
| 124 | n/a | (c_ushort, "<H", (), c_ushort), |
|---|
| 125 | n/a | |
|---|
| 126 | n/a | # c_int and c_uint may be aliases to c_long |
|---|
| 127 | n/a | #(c_int, "<i", (), c_int), |
|---|
| 128 | n/a | #(c_uint, "<I", (), c_uint), |
|---|
| 129 | n/a | |
|---|
| 130 | n/a | (c_long, "<l", (), c_long), |
|---|
| 131 | n/a | (c_ulong, "<L", (), c_ulong), |
|---|
| 132 | n/a | |
|---|
| 133 | n/a | # c_longlong and c_ulonglong are aliases on 64-bit platforms |
|---|
| 134 | n/a | #(c_longlong, "<q", None, c_longlong), |
|---|
| 135 | n/a | #(c_ulonglong, "<Q", None, c_ulonglong), |
|---|
| 136 | n/a | |
|---|
| 137 | n/a | (c_float, "<f", (), c_float), |
|---|
| 138 | n/a | (c_double, "<d", (), c_double), |
|---|
| 139 | n/a | # c_longdouble may be an alias to c_double |
|---|
| 140 | n/a | |
|---|
| 141 | n/a | (c_bool, "<?", (), c_bool), |
|---|
| 142 | n/a | (py_object, "<O", (), py_object), |
|---|
| 143 | n/a | |
|---|
| 144 | n/a | ## pointers |
|---|
| 145 | n/a | |
|---|
| 146 | n/a | (POINTER(c_byte), "&<b", (), POINTER(c_byte)), |
|---|
| 147 | n/a | (POINTER(POINTER(c_long)), "&&<l", (), POINTER(POINTER(c_long))), |
|---|
| 148 | n/a | |
|---|
| 149 | n/a | ## arrays and pointers |
|---|
| 150 | n/a | |
|---|
| 151 | n/a | (c_double * 4, "<d", (4,), c_double), |
|---|
| 152 | n/a | (c_float * 4 * 3 * 2, "<f", (2,3,4), c_float), |
|---|
| 153 | n/a | (POINTER(c_short) * 2, "&<h", (2,), POINTER(c_short)), |
|---|
| 154 | n/a | (POINTER(c_short) * 2 * 3, "&<h", (3,2,), POINTER(c_short)), |
|---|
| 155 | n/a | (POINTER(c_short * 2), "&(2)<h", (), POINTER(c_short)), |
|---|
| 156 | n/a | |
|---|
| 157 | n/a | ## structures and unions |
|---|
| 158 | n/a | |
|---|
| 159 | n/a | (Point, "T{<l:x:<l:y:}", (), Point), |
|---|
| 160 | n/a | # packed structures do not implement the pep |
|---|
| 161 | n/a | (PackedPoint, "B", (), PackedPoint), |
|---|
| 162 | n/a | (Point2, "T{<l:x:<l:y:}", (), Point2), |
|---|
| 163 | n/a | (EmptyStruct, "T{}", (), EmptyStruct), |
|---|
| 164 | n/a | # the pep does't support unions |
|---|
| 165 | n/a | (aUnion, "B", (), aUnion), |
|---|
| 166 | n/a | # structure with sub-arrays |
|---|
| 167 | n/a | (StructWithArrays, "T{(2,3)<l:x:(4)T{<l:x:<l:y:}:y:}", (), StructWithArrays), |
|---|
| 168 | n/a | (StructWithArrays * 3, "T{(2,3)<l:x:(4)T{<l:x:<l:y:}:y:}", (3,), StructWithArrays), |
|---|
| 169 | n/a | |
|---|
| 170 | n/a | ## pointer to incomplete structure |
|---|
| 171 | n/a | (Incomplete, "B", (), Incomplete), |
|---|
| 172 | n/a | (POINTER(Incomplete), "&B", (), POINTER(Incomplete)), |
|---|
| 173 | n/a | |
|---|
| 174 | n/a | # 'Complete' is a structure that starts incomplete, but is completed after the |
|---|
| 175 | n/a | # pointer type to it has been created. |
|---|
| 176 | n/a | (Complete, "T{<l:a:}", (), Complete), |
|---|
| 177 | n/a | # Unfortunately the pointer format string is not fixed... |
|---|
| 178 | n/a | (POINTER(Complete), "&B", (), POINTER(Complete)), |
|---|
| 179 | n/a | |
|---|
| 180 | n/a | ## other |
|---|
| 181 | n/a | |
|---|
| 182 | n/a | # function signatures are not implemented |
|---|
| 183 | n/a | (CFUNCTYPE(None), "X{}", (), CFUNCTYPE(None)), |
|---|
| 184 | n/a | |
|---|
| 185 | n/a | ] |
|---|
| 186 | n/a | |
|---|
| 187 | n/a | class BEPoint(BigEndianStructure): |
|---|
| 188 | n/a | _fields_ = [("x", c_long), ("y", c_long)] |
|---|
| 189 | n/a | |
|---|
| 190 | n/a | class LEPoint(LittleEndianStructure): |
|---|
| 191 | n/a | _fields_ = [("x", c_long), ("y", c_long)] |
|---|
| 192 | n/a | |
|---|
| 193 | n/a | ################################################################ |
|---|
| 194 | n/a | # |
|---|
| 195 | n/a | # This table contains format strings as they really look, on both big |
|---|
| 196 | n/a | # and little endian machines. |
|---|
| 197 | n/a | # |
|---|
| 198 | n/a | endian_types = [ |
|---|
| 199 | n/a | (BEPoint, "T{>l:x:>l:y:}", (), BEPoint), |
|---|
| 200 | n/a | (LEPoint, "T{<l:x:<l:y:}", (), LEPoint), |
|---|
| 201 | n/a | (POINTER(BEPoint), "&T{>l:x:>l:y:}", (), POINTER(BEPoint)), |
|---|
| 202 | n/a | (POINTER(LEPoint), "&T{<l:x:<l:y:}", (), POINTER(LEPoint)), |
|---|
| 203 | n/a | ] |
|---|
| 204 | n/a | |
|---|
| 205 | n/a | if __name__ == "__main__": |
|---|
| 206 | n/a | unittest.main() |
|---|