1
from __future__ import unicode_literals
4
# Tests buffer format string parsing.
7
from libc cimport stdlib
10
cdef unsigned int n = 1
11
return (<unsigned char*>&n)[0] != 0
20
cdef struct align_of_float_helper:
23
cdef struct align_of_int_helper:
26
float_align = sizeof(align_of_float_helper) - sizeof(float)
27
int_align = sizeof(align_of_int_helper) - sizeof(int)
28
if float_align != 4 or sizeof(float) != 4:
29
raise RuntimeError("Alignment or size of float is %d on this system, please report to cython-dev for a testcase fix" % float_align)
30
if int_align != 4 or sizeof(int) != 4:
31
raise RuntimeError("Alignment or size of int is %d on this system, please report to cython-dev for a testcase fix" % int_align)
36
cdef Py_ssize_t minusone
40
def __init__(self, format, itemsize):
41
self.format = unicode(format).encode(u"ASCII")
42
self.itemsize = itemsize
46
def __getbuffer__(self, Py_buffer* info, int flags):
48
info.strides = &self.zero
49
info.suboffsets = &self.minusone
50
info.shape = &self.zero
52
info.format = self.format
53
info.itemsize = self.itemsize
60
Traceback (most recent call last):
62
ValueError: Buffer dtype mismatch, expected 'int' but got 'signed char'
65
Traceback (most recent call last):
67
ValueError: Buffer dtype mismatch, expected end but got 'float'
70
Traceback (most recent call last):
72
ValueError: Does not understand character buffer dtype format string ('$')
74
cdef object[int] buf = MockBuffer(fmt, sizeof(int))
80
cdef object[unsigned long] buf = MockBuffer(fmt, sizeof(unsigned long))
85
Traceback (most recent call last):
87
ValueError: Item size of buffer (1 byte) does not match size of 'float' (4 bytes)
90
cdef object[float] buf = MockBuffer("f", 1)
96
Traceback (most recent call last):
98
ValueError: Buffer dtype mismatch, expected 'Python object' but got 'int'
100
cdef object[object] buf = MockBuffer(fmt, sizeof(void*))
103
cdef struct ComplexFloat:
107
ctypedef struct Char3Int:
113
ctypedef struct LongString:
116
cdef struct CharIntCFloat:
122
cdef struct UnpackedStruct1:
129
ctypedef struct UnpackedStruct2:
133
ctypedef struct UnpackedStruct3:
138
cdef struct UnpackedStruct4:
149
>>> char3int("c1i1i1i")
153
>>> char3int("c@i@2i")
155
Extra pad bytes (assuming int size is 4 or more)
156
>>> char3int("cxiii")
157
>>> char3int("c3xiii")
158
>>> char3int("cxxxiii")
160
Standard alignment (assuming int size is 4)
161
>>> char3int("=c3xiii")
162
>>> char3int("=ciii")
163
Traceback (most recent call last):
165
ValueError: Buffer dtype mismatch; next field is at offset 1 but 4 expected
167
>>> char3int("=cxxx@iii")
171
Traceback (most recent call last):
173
ValueError: Buffer dtype mismatch, expected 'int' but got end in 'Char3Int.d'
175
cdef object obj = MockBuffer(fmt, sizeof(Char3Int))
176
cdef object[Char3Int, ndim=1] buf = obj
181
>>> long_string("90198s")
183
cdef object obj = MockBuffer(fmt, sizeof(LongString))
184
cdef object[LongString, ndim=1] buf = obj
187
def unpacked_struct(fmt):
190
>>> unpacked_struct("ciZffciii")
191
>>> unpacked_struct("@ci3fc3i")
192
>>> unpacked_struct("@ciZffci2i")
193
>>> unpacked_struct("ciZffT{ciii}")
194
>>> unpacked_struct("cT{ifffc2i}i")
195
>>> unpacked_struct("ciZffc3T{i}")
196
>>> unpacked_struct("T{c}T{T{iZffT{ci}}}2T{T{i}}")
199
assert (sizeof(UnpackedStruct1) == sizeof(UnpackedStruct2)
200
== sizeof(UnpackedStruct3) == sizeof(UnpackedStruct4))
201
cdef object obj = MockBuffer(fmt, sizeof(UnpackedStruct1))
202
cdef object[UnpackedStruct1, ndim=1] buf1 = obj
203
cdef object[UnpackedStruct2, ndim=1] buf2 = obj
204
cdef object[UnpackedStruct3, ndim=1] buf3 = obj
205
cdef object[UnpackedStruct4, ndim=1] buf4 = obj
207
cdef struct ComplexTest:
210
def complex_test(fmt):
212
>>> complex_test("ZfZfZf")
213
>>> complex_test("3Zf")
214
>>> complex_test("6f")
215
>>> complex_test("3T{Zf}")
217
>>> complex_test("fZfZff")
218
Traceback (most recent call last):
220
ValueError: Buffer dtype mismatch, expected 'float' but got 'complex float' in 'ComplexFloat.imag'
223
cdef object obj = MockBuffer(fmt, sizeof(ComplexTest))
224
cdef object[ComplexTest] buf1 = obj
227
def alignment_string(fmt, exc=None):
229
>>> alignment_string("@i")
230
>>> alignment_string("%si" % current_endian)
231
>>> alignment_string("%si" % other_endian, "X-endian buffer not supported on X-endian compiler")
232
>>> alignment_string("=i")
236
buf = MockBuffer(fmt, sizeof(int))
237
except ValueError, e:
238
msg = unicode(e).replace("Big", "X").replace("Little", "X").replace("big", "X").replace("little", "X")
241
print " is not equal to"
248
def int_and_long_are_same():
250
>>> int_and_long_are_same()
252
cdef object[int] intarr
253
cdef object[long] longarr
254
if sizeof(int) == sizeof(long):
255
intarr = MockBuffer("l", sizeof(int))
256
longarr = MockBuffer("i", sizeof(int))
258
cdef struct MixedComplex:
262
def mixed_complex_struct():
264
Triggering a specific execution path for this case.
266
>>> mixed_complex_struct()
267
Traceback (most recent call last):
269
ValueError: Buffer dtype mismatch, expected 'double' but got 'complex double' in 'MixedComplex.real'
271
cdef object[MixedComplex] buf = MockBuffer("Zd",
272
sizeof(MixedComplex))
275
cdef packed struct PackedSubStruct:
279
cdef struct UnpackedSubStruct:
283
cdef packed struct PackedStruct:
288
cdef struct PartiallyPackedStruct:
293
cdef packed struct PartiallyPackedStruct2:
295
UnpackedSubStruct sub
299
def packed_struct(fmt):
301
Assuming int is four bytes:
303
>>> packed_struct("^cici")
304
>>> packed_struct("=cici")
306
However aligned access won't work:
308
>>> packed_struct("^c@i^ci")
309
Traceback (most recent call last):
311
ValueError: Buffer dtype mismatch; next field is at offset 4 but 1 expected
313
>>> packed_struct("@cici")
314
Traceback (most recent call last):
316
ValueError: Buffer dtype mismatch; next field is at offset 4 but 1 expected
319
cdef object[PackedStruct] buf = MockBuffer(fmt, sizeof(PackedStruct))
321
def partially_packed_struct(fmt):
323
Assuming int is four bytes:
325
>>> partially_packed_struct("^c@i^ci")
326
>>> partially_packed_struct("@ci^ci")
327
>>> partially_packed_struct("^c@i=ci")
328
>>> partially_packed_struct("@ci=ci")
329
>>> partially_packed_struct("ci^ci")
330
>>> partially_packed_struct("ci=ci")
332
Incorrectly aligned accesses won't work:
334
>>> partially_packed_struct("^cici")
335
Traceback (most recent call last):
337
ValueError: Buffer dtype mismatch; next field is at offset 1 but 4 expected
339
>>> partially_packed_struct("=cibi")
340
Traceback (most recent call last):
342
ValueError: Buffer dtype mismatch; next field is at offset 1 but 4 expected
345
cdef object[PartiallyPackedStruct] buf = MockBuffer(
346
fmt, sizeof(PartiallyPackedStruct))
348
def partially_packed_struct_2(fmt):
350
Assuming int is four bytes:
352
>>> partially_packed_struct_2("^ccxxxici")
353
>>> partially_packed_struct_2("^ccxxxi^ci")
354
>>> partially_packed_struct_2("c=cxxxi^ci")
355
>>> partially_packed_struct_2("c^cxxxi^ci")
356
>>> partially_packed_struct_2("c^cxxxi=ci")
357
>>> partially_packed_struct_2("ccxxx^i@c^i")
359
Incorrectly aligned accesses won't work:
361
>>> partially_packed_struct_2("ccxxxici")
362
Traceback (most recent call last):
364
ValueError: Buffer dtype mismatch; next field is at offset 8 but 5 expected
366
>>> partially_packed_struct_2("ccici")
367
Traceback (most recent call last):
369
ValueError: Buffer dtype mismatch; next field is at offset 4 but 5 expected
372
cdef object[PartiallyPackedStruct2] buf = MockBuffer(
373
fmt, sizeof(PartiallyPackedStruct2))
376
cdef packed struct PackedStructWithCharArrays:
383
def packed_struct_with_strings(fmt):
385
>>> packed_struct_with_strings("T{f:a:i:b:5s:c:3s:d:}")
387
cdef object[PackedStructWithCharArrays] buf = MockBuffer(
388
fmt, sizeof(PackedStructWithCharArrays))
391
ctypedef struct PackedStructWithArrays:
396
ctypedef struct UnpackedStructWithArrays:
407
ctypedef struct PackedStructWithNDArrays:
414
def packed_struct_with_arrays(fmt):
416
>>> packed_struct_with_arrays("T{(16)d:a:(16)d:b:d:c:}")
419
cdef object[PackedStructWithArrays] buf = MockBuffer(
420
fmt, sizeof(PackedStructWithArrays))
423
def unpacked_struct_with_arrays(fmt):
425
>>> if struct.calcsize('P') == 8: # 64 bit
426
... unpacked_struct_with_arrays("T{i:a:(8)f:b:f:c:Q:d:(5)i:e:i:f:i:g:xxxx(4)d:h:i:i:}")
427
... elif struct.calcsize('P') == 4: # 32 bit
428
... unpacked_struct_with_arrays("T{i:a:(8)f:b:f:c:Q:d:(5)i:e:i:f:i:g:(4)d:h:i:i:}")
431
cdef object[UnpackedStructWithArrays] buf = MockBuffer(
432
fmt, sizeof(UnpackedStructWithArrays))
435
def packed_struct_with_ndarrays(fmt):
437
>>> packed_struct_with_ndarrays("T{d:a:(2,2)d:b:f:c:f:d:}")
440
cdef object[PackedStructWithNDArrays] buf = MockBuffer(
441
fmt, sizeof(PackedStructWithNDArrays))
445
# TODO: Incomplete structs