cython

Форк
0
/
bytearraymethods.pyx 
293 строки · 8.2 Кб
1

2
cimport cython
3

4
b_a = bytearray(b'a')
5
b_b = bytearray(b'b')
6

7

8
'''   # disabled for now, enable when we consider it worth the code overhead
9

10
@cython.test_assert_path_exists(
11
    "//PythonCapiCallNode")
12
@cython.test_fail_if_path_exists(
13
    "//SimpleCallNode")
14
def bytearray_startswith(bytearray s, sub, start=None, stop=None):
15
    """
16
    >>> bytearray_startswith(b_a, b_a)
17
    True
18
    >>> bytearray_startswith(b_a+b_b, b_a)
19
    True
20
    >>> bytearray_startswith(b_a, b_b)
21
    False
22
    >>> bytearray_startswith(b_a+b_b, b_b)
23
    False
24
    >>> bytearray_startswith(b_a, (b_a, b_b))
25
    True
26
    >>> bytearray_startswith(b_a, b_a, 1)
27
    False
28
    >>> bytearray_startswith(b_a, b_a, 0, 0)
29
    False
30
    """
31

32
    if start is None:
33
      return s.startswith(sub)
34
    elif stop is None:
35
      return s.startswith(sub, start)
36
    else:
37
      return s.startswith(sub, start, stop)
38

39

40
@cython.test_assert_path_exists(
41
    "//PythonCapiCallNode")
42
@cython.test_fail_if_path_exists(
43
    "//SimpleCallNode")
44
def bytearray_endswith(bytearray s, sub, start=None, stop=None):
45
    """
46
    >>> bytearray_endswith(b_a, b_a)
47
    True
48
    >>> bytearray_endswith(b_b+b_a, b_a)
49
    True
50
    >>> bytearray_endswith(b_a, b_b)
51
    False
52
    >>> bytearray_endswith(b_b+b_a, b_b)
53
    False
54
    >>> bytearray_endswith(b_a, (b_a, b_b))
55
    True
56
    >>> bytearray_endswith(b_a, b_a, 1)
57
    False
58
    >>> bytearray_endswith(b_a, b_a, 0, 0)
59
    False
60
    """
61

62
    if start is None:
63
      return s.endswith(sub)
64
    elif stop is None:
65
      return s.endswith(sub, start)
66
    else:
67
      return s.endswith(sub, start, stop)
68
'''
69

70

71
@cython.test_assert_path_exists(
72
    "//PythonCapiCallNode")
73
@cython.test_fail_if_path_exists(
74
    "//SimpleCallNode")
75
def bytearray_decode(bytearray s, start=None, stop=None):
76
    """
77
    >>> s = b_a+b_b+b_a+b_a+b_b
78
    >>> print(bytearray_decode(s))
79
    abaab
80

81
    >>> print(bytearray_decode(s, 2))
82
    aab
83
    >>> print(bytearray_decode(s, -3))
84
    aab
85

86
    >>> print(bytearray_decode(s, None, 4))
87
    abaa
88
    >>> print(bytearray_decode(s, None, 400))
89
    abaab
90
    >>> print(bytearray_decode(s, None, -2))
91
    aba
92
    >>> print(bytearray_decode(s, None, -4))
93
    a
94
    >>> print(bytearray_decode(s, None, -5))
95
    <BLANKLINE>
96
    >>> print(bytearray_decode(s, None, -200))
97
    <BLANKLINE>
98

99
    >>> print(bytearray_decode(s, 2, 5))
100
    aab
101
    >>> print(bytearray_decode(s, 2, 500))
102
    aab
103
    >>> print(bytearray_decode(s, 2, -1))
104
    aa
105
    >>> print(bytearray_decode(s, 2, -3))
106
    <BLANKLINE>
107
    >>> print(bytearray_decode(s, 2, -300))
108
    <BLANKLINE>
109
    >>> print(bytearray_decode(s, -3, -1))
110
    aa
111
    >>> print(bytearray_decode(s, -300, 300))
112
    abaab
113
    >>> print(bytearray_decode(s, -300, -4))
114
    a
115
    >>> print(bytearray_decode(s, -300, -5))
116
    <BLANKLINE>
117
    >>> print(bytearray_decode(s, -300, -6))
118
    <BLANKLINE>
119
    >>> print(bytearray_decode(s, -300, -500))
120
    <BLANKLINE>
121

122
    >>> s[:'test']                       # doctest: +ELLIPSIS
123
    Traceback (most recent call last):
124
    TypeError:...
125
    >>> print(bytearray_decode(s, 'test'))   # doctest: +ELLIPSIS
126
    Traceback (most recent call last):
127
    TypeError:...
128
    >>> print(bytearray_decode(s, None, 'test'))    # doctest: +ELLIPSIS
129
    Traceback (most recent call last):
130
    TypeError:...
131
    >>> print(bytearray_decode(s, 'test', 'test'))  # doctest: +ELLIPSIS
132
    Traceback (most recent call last):
133
    TypeError:...
134

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
147
    """
148
    if start is None:
149
        if stop is None:
150
            return s.decode('utf8')
151
        else:
152
            return s[:stop].decode('utf8')
153
    elif stop is None:
154
        return s[start:].decode('utf8')
155
    else:
156
        return s[start:stop].decode('utf8')
157

158

159
@cython.test_assert_path_exists(
160
    "//PythonCapiCallNode")
161
@cython.test_fail_if_path_exists(
162
    "//SimpleCallNode")
163
def bytearray_decode_unbound_method(bytearray s, start=None, stop=None):
164
    """
165
    >>> s = b_a+b_b+b_a+b_a+b_b
166
    >>> print(bytearray_decode_unbound_method(s))
167
    abaab
168
    >>> print(bytearray_decode_unbound_method(s, 1))
169
    baab
170
    >>> print(bytearray_decode_unbound_method(s, None, 3))
171
    aba
172
    >>> print(bytearray_decode_unbound_method(s, 1, 4))
173
    baa
174

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
187
    """
188
    if start is None:
189
        if stop is None:
190
            return bytearray.decode(s, 'utf8')
191
        else:
192
            return bytearray.decode(s[:stop], 'utf8')
193
    elif stop is None:
194
        return bytearray.decode(s[start:], 'utf8')
195
    else:
196
        return bytearray.decode(s[start:stop], 'utf8')
197

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):
201
    """
202
    >>> b = bytearray(b'abc')
203
    >>> b = bytearray_append(b, ord('x'), ord('y'), ord('z'))
204
    >>> print(b.decode('ascii'))
205
    abcX@xyz
206

207
    >>> b = bytearray(b'abc')
208
    >>> b = bytearray_append(b, ord('x'), ord('y'), 0)
209
    >>> print(b.decode('ascii')[:-1])
210
    abcX@xy
211
    >>> b[-1]
212
    0
213

214
    >>> b = bytearray(b'abc')
215
    >>> b = bytearray_append(b, ord('x'), ord('y'), ord('z'))
216
    >>> print(b.decode('ascii'))
217
    abcX@xyz
218

219
    >>> b = bytearray(b'abc')
220
    >>> b = bytearray_append(b, ord('x'), ord('y'), ord('\\xc3'))
221
    >>> print(b[:-1].decode('ascii'))
222
    abcX@xy
223
    >>> print('%x' % b[-1])
224
    c3
225

226
    >>> b = bytearray(b'abc')
227
    >>> try:
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'))
232
    abcX@xy
233

234
    >>> b = bytearray(b'abc')
235
    >>> b = bytearray_append(b, -1, ord('y'), ord('z'))  # doctest: +ELLIPSIS
236
    Traceback (most recent call last):
237
    ValueError: ...
238
    >>> print(b.decode('ascii'))
239
    abcX@
240

241
    >>> b = bytearray(b'abc')
242
    >>> b = bytearray_append(b, ord('x'), -1, ord('z'))  # doctest: +ELLIPSIS
243
    Traceback (most recent call last):
244
    ValueError: ...
245
    >>> print(b.decode('ascii'))
246
    abcX@x
247

248
    >>> b = bytearray(b'abc')
249
    >>> b = bytearray_append(b, ord('x'), 256, ord('z'))  # doctest: +ELLIPSIS
250
    Traceback (most recent call last):
251
    ValueError: ...
252
    >>> print(b.decode('ascii'))
253
    abcX@x
254

255
    >>> b = bytearray(b'abc')
256
    >>> b = bytearray_append(b, ord('x'), ord('y'), -1)  # doctest: +ELLIPSIS
257
    Traceback (most recent call last):
258
    ValueError: ...
259
    >>> print(b.decode('ascii'))
260
    abcX@xy
261

262
    >>> b = bytearray(b'abc')
263
    >>> b = bytearray_append(b, ord('x'), ord('y'), 256)  # doctest: +ELLIPSIS
264
    Traceback (most recent call last):
265
    ValueError: ...
266
    >>> print(b.decode('ascii'))
267
    abcX@xy
268
    """
269
    assert b.append('X') is None
270
    b.append(64)
271
    b.append(c)
272
    b.append(i)
273
    b.append(o)
274
    return b
275

276

277
cdef class BytearraySubtype(bytearray):
278
    """
279
    >>> b = BytearraySubtype(b'abc')
280
    >>> b._append(ord('x'))
281
    >>> b.append(ord('y'))
282
    >>> print(b.decode('ascii'))
283
    abcxy
284
    """
285
    def _append(self, x):
286
        self.append(x)
287

288
def fromhex(bytearray b):
289
    """
290
    https://github.com/cython/cython/issues/5051
291
    Optimization of bound method calls was breaking classmethods
292
    >>> fromhex(bytearray(b""))
293
    """
294
    assert b.fromhex('2Ef0 F1f2  ') == b'.\xf0\xf1\xf2'
295

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.