8
''' # disabled for now, enable when we consider it worth the code overhead
10
@cython.test_assert_path_exists(
11
"//PythonCapiCallNode")
12
@cython.test_fail_if_path_exists(
14
def bytearray_startswith(bytearray s, sub, start=None, stop=None):
16
>>> bytearray_startswith(b_a, b_a)
18
>>> bytearray_startswith(b_a+b_b, b_a)
20
>>> bytearray_startswith(b_a, b_b)
22
>>> bytearray_startswith(b_a+b_b, b_b)
24
>>> bytearray_startswith(b_a, (b_a, b_b))
26
>>> bytearray_startswith(b_a, b_a, 1)
28
>>> bytearray_startswith(b_a, b_a, 0, 0)
33
return s.startswith(sub)
35
return s.startswith(sub, start)
37
return s.startswith(sub, start, stop)
40
@cython.test_assert_path_exists(
41
"//PythonCapiCallNode")
42
@cython.test_fail_if_path_exists(
44
def bytearray_endswith(bytearray s, sub, start=None, stop=None):
46
>>> bytearray_endswith(b_a, b_a)
48
>>> bytearray_endswith(b_b+b_a, b_a)
50
>>> bytearray_endswith(b_a, b_b)
52
>>> bytearray_endswith(b_b+b_a, b_b)
54
>>> bytearray_endswith(b_a, (b_a, b_b))
56
>>> bytearray_endswith(b_a, b_a, 1)
58
>>> bytearray_endswith(b_a, b_a, 0, 0)
63
return s.endswith(sub)
65
return s.endswith(sub, start)
67
return s.endswith(sub, start, stop)
71
@cython.test_assert_path_exists(
72
"//PythonCapiCallNode")
73
@cython.test_fail_if_path_exists(
75
def bytearray_decode(bytearray s, start=None, stop=None):
77
>>> s = b_a+b_b+b_a+b_a+b_b
78
>>> print(bytearray_decode(s))
81
>>> print(bytearray_decode(s, 2))
83
>>> print(bytearray_decode(s, -3))
86
>>> print(bytearray_decode(s, None, 4))
88
>>> print(bytearray_decode(s, None, 400))
90
>>> print(bytearray_decode(s, None, -2))
92
>>> print(bytearray_decode(s, None, -4))
94
>>> print(bytearray_decode(s, None, -5))
96
>>> print(bytearray_decode(s, None, -200))
99
>>> print(bytearray_decode(s, 2, 5))
101
>>> print(bytearray_decode(s, 2, 500))
103
>>> print(bytearray_decode(s, 2, -1))
105
>>> print(bytearray_decode(s, 2, -3))
107
>>> print(bytearray_decode(s, 2, -300))
109
>>> print(bytearray_decode(s, -3, -1))
111
>>> print(bytearray_decode(s, -300, 300))
113
>>> print(bytearray_decode(s, -300, -4))
115
>>> print(bytearray_decode(s, -300, -5))
117
>>> print(bytearray_decode(s, -300, -6))
119
>>> print(bytearray_decode(s, -300, -500))
122
>>> s[:'test'] # doctest: +ELLIPSIS
123
Traceback (most recent call last):
125
>>> print(bytearray_decode(s, 'test')) # doctest: +ELLIPSIS
126
Traceback (most recent call last):
128
>>> print(bytearray_decode(s, None, 'test')) # doctest: +ELLIPSIS
129
Traceback (most recent call last):
131
>>> print(bytearray_decode(s, 'test', 'test')) # doctest: +ELLIPSIS
132
Traceback (most recent call last):
135
>>> print(bytearray_decode(None))
136
Traceback (most recent call last):
137
AttributeError: 'NoneType' object has no attribute 'decode'
138
>>> print(bytearray_decode(None, 1))
139
Traceback (most recent call last):
140
TypeError: 'NoneType' object is not subscriptable
141
>>> print(bytearray_decode(None, None, 1))
142
Traceback (most recent call last):
143
TypeError: 'NoneType' object is not subscriptable
144
>>> print(bytearray_decode(None, 0, 1))
145
Traceback (most recent call last):
146
TypeError: 'NoneType' object is not subscriptable
150
return s.decode('utf8')
152
return s[:stop].decode('utf8')
154
return s[start:].decode('utf8')
156
return s[start:stop].decode('utf8')
159
@cython.test_assert_path_exists(
160
"//PythonCapiCallNode")
161
@cython.test_fail_if_path_exists(
163
def bytearray_decode_unbound_method(bytearray s, start=None, stop=None):
165
>>> s = b_a+b_b+b_a+b_a+b_b
166
>>> print(bytearray_decode_unbound_method(s))
168
>>> print(bytearray_decode_unbound_method(s, 1))
170
>>> print(bytearray_decode_unbound_method(s, None, 3))
172
>>> print(bytearray_decode_unbound_method(s, 1, 4))
175
>>> print(bytearray_decode_unbound_method(None))
176
Traceback (most recent call last):
177
TypeError: descriptor 'decode' requires a 'bytearray' object but received a 'NoneType'
178
>>> print(bytearray_decode_unbound_method(None, 1))
179
Traceback (most recent call last):
180
TypeError: 'NoneType' object is not subscriptable
181
>>> print(bytearray_decode_unbound_method(None, None, 1))
182
Traceback (most recent call last):
183
TypeError: 'NoneType' object is not subscriptable
184
>>> print(bytearray_decode_unbound_method(None, 0, 1))
185
Traceback (most recent call last):
186
TypeError: 'NoneType' object is not subscriptable
190
return bytearray.decode(s, 'utf8')
192
return bytearray.decode(s[:stop], 'utf8')
194
return bytearray.decode(s[start:], 'utf8')
196
return bytearray.decode(s[start:stop], 'utf8')
198
@cython.test_fail_if_path_exists('//SimpleCallNode')
199
@cython.test_assert_path_exists('//PythonCapiCallNode')
200
def bytearray_append(bytearray b, signed char c, int i, object o):
202
>>> b = bytearray(b'abc')
203
>>> b = bytearray_append(b, ord('x'), ord('y'), ord('z'))
204
>>> print(b.decode('ascii'))
207
>>> b = bytearray(b'abc')
208
>>> b = bytearray_append(b, ord('x'), ord('y'), 0)
209
>>> print(b.decode('ascii')[:-1])
214
>>> b = bytearray(b'abc')
215
>>> b = bytearray_append(b, ord('x'), ord('y'), ord('z'))
216
>>> print(b.decode('ascii'))
219
>>> b = bytearray(b'abc')
220
>>> b = bytearray_append(b, ord('x'), ord('y'), ord('\\xc3'))
221
>>> print(b[:-1].decode('ascii'))
223
>>> print('%x' % b[-1])
226
>>> b = bytearray(b'abc')
228
... b = bytearray_append(b, ord('x'), ord('y'), b'zz')
229
... except (TypeError, ValueError): pass # (Py3, Py2)
230
... else: print("FAIL")
231
>>> print(b.decode('ascii'))
234
>>> b = bytearray(b'abc')
235
>>> b = bytearray_append(b, -1, ord('y'), ord('z')) # doctest: +ELLIPSIS
236
Traceback (most recent call last):
238
>>> print(b.decode('ascii'))
241
>>> b = bytearray(b'abc')
242
>>> b = bytearray_append(b, ord('x'), -1, ord('z')) # doctest: +ELLIPSIS
243
Traceback (most recent call last):
245
>>> print(b.decode('ascii'))
248
>>> b = bytearray(b'abc')
249
>>> b = bytearray_append(b, ord('x'), 256, ord('z')) # doctest: +ELLIPSIS
250
Traceback (most recent call last):
252
>>> print(b.decode('ascii'))
255
>>> b = bytearray(b'abc')
256
>>> b = bytearray_append(b, ord('x'), ord('y'), -1) # doctest: +ELLIPSIS
257
Traceback (most recent call last):
259
>>> print(b.decode('ascii'))
262
>>> b = bytearray(b'abc')
263
>>> b = bytearray_append(b, ord('x'), ord('y'), 256) # doctest: +ELLIPSIS
264
Traceback (most recent call last):
266
>>> print(b.decode('ascii'))
269
assert b.append('X') is None
277
cdef class BytearraySubtype(bytearray):
279
>>> b = BytearraySubtype(b'abc')
280
>>> b._append(ord('x'))
281
>>> b.append(ord('y'))
282
>>> print(b.decode('ascii'))
285
def _append(self, x):
288
def fromhex(bytearray b):
290
https://github.com/cython/cython/issues/5051
291
Optimization of bound method calls was breaking classmethods
292
>>> fromhex(bytearray(b""))
294
assert b.fromhex('2Ef0 F1f2 ') == b'.\xf0\xf1\xf2'