8
cpdef bytearray coerce_to_charptr(char* b):
10
>>> b = bytearray(b'abc')
11
>>> coerced = coerce_to_charptr(b)
12
>>> coerced == b or (coerced, b)
14
>>> isinstance(coerced, bytearray) or type(coerced)
19
def coerce_to_charptrs(bytearray b):
21
>>> b = bytearray(b'abc')
22
>>> coerce_to_charptrs(b)
26
cdef unsigned char* ucs = b
27
cdef signed char* scs = b
28
return b == <bytearray>cs == <bytearray> ucs == <bytearray>scs
30
cpdef bytearray coerce_charptr_slice(char* b):
32
>>> b = bytearray(b'abc')
33
>>> coerced = coerce_charptr_slice(b)
34
>>> coerced == b[:2] or (coerced, b)
36
>>> isinstance(coerced, bytearray) or type(coerced)
42
def infer_concatenation_types(bytearray b):
44
>>> b = bytearray(b'a\\xFEc')
45
>>> b2, c, d, e, tb, tc, td, te = infer_concatenation_types(b)
47
('bytearray object', 'bytearray object', 'bytearray object', 'bytearray object')
49
(bytearray(b'a\\xfec'), bytearray(b'a\\xfeca\\xfec'), bytearray(b'a\\xfeca\\xfec'), bytearray(b'a\\xfeca\\xfec'))
59
return b, c, d, e, cython.typeof(b), cython.typeof(c), cython.typeof(d), cython.typeof(e)
62
def infer_index_types(bytearray b):
64
>>> b = bytearray(b'a\\xFEc')
65
>>> print(infer_index_types(b))
66
(254, 254, 254, 'unsigned char', 'unsigned char', 'unsigned char', 'int')
69
with cython.wraparound(False):
71
with cython.boundscheck(False):
73
return c, d, e, cython.typeof(c), cython.typeof(d), cython.typeof(e), cython.typeof(b[1])
76
def infer_slice_types(bytearray b):
78
>>> b = bytearray(b'abc')
79
>>> print(infer_slice_types(b))
80
(bytearray(b'bc'), bytearray(b'bc'), bytearray(b'bc'), 'bytearray object', 'bytearray object', 'bytearray object', 'bytearray object')
83
with cython.boundscheck(False):
85
with cython.boundscheck(False), cython.wraparound(False):
87
return c, d, e, cython.typeof(c), cython.typeof(d), cython.typeof(e), cython.typeof(b[1:])
90
def assign_to_index(bytearray b, value):
92
>>> b = bytearray(b'0abcdefg')
93
>>> assign_to_index(b, 1)
94
bytearray(b'xyzee\\x01h')
96
bytearray(b'xyzee\\x01h')
98
>>> assign_to_index(bytearray(b'0ABCDEFG'), 40)
101
>>> assign_to_index(bytearray(b'0abcdefg'), -1)
102
Traceback (most recent call last):
103
OverflowError: can't convert negative value to unsigned char
105
>>> assign_to_index(bytearray(b'0abcdef\\x00'), 255)
106
bytearray(b'xyzee\\xff\\xff')
107
>>> assign_to_index(bytearray(b'0abcdef\\x01'), 255)
108
Traceback (most recent call last):
109
OverflowError: value too large to convert to unsigned char
110
>>> assign_to_index(bytearray(b'0abcdef\\x00'), 256)
111
Traceback (most recent call last):
112
OverflowError: value too large to convert to unsigned char
128
assert False, "IndexError not raised"
131
b[int(str(len(b)))] = 1 # test non-int-index assignment
135
assert False, "IndexError not raised"
140
def check_bounds(int cvalue):
144
>>> check_bounds(255)
146
>>> check_bounds(256)
147
Traceback (most recent call last):
148
ValueError: byte must be in range(0, 256)
150
Traceback (most recent call last):
151
ValueError: byte must be in range(0, 256)
160
assert False, "ValueError not raised"
167
assert False, "ValueError not raised"
173
def nogil_assignment(bytearray x, int value):
175
>>> b = bytearray(b'abc')
176
>>> nogil_assignment(b, ord('y'))