1 | n/a | /* PyByteArray (bytearray) implementation */ |
---|
2 | n/a | |
---|
3 | n/a | #define PY_SSIZE_T_CLEAN |
---|
4 | n/a | #include "Python.h" |
---|
5 | n/a | #include "structmember.h" |
---|
6 | n/a | #include "bytes_methods.h" |
---|
7 | n/a | #include "bytesobject.h" |
---|
8 | n/a | #include "pystrhex.h" |
---|
9 | n/a | |
---|
10 | n/a | /*[clinic input] |
---|
11 | n/a | class bytearray "PyByteArrayObject *" "&PyByteArray_Type" |
---|
12 | n/a | [clinic start generated code]*/ |
---|
13 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/ |
---|
14 | n/a | |
---|
15 | n/a | char _PyByteArray_empty_string[] = ""; |
---|
16 | n/a | |
---|
17 | n/a | void |
---|
18 | n/a | PyByteArray_Fini(void) |
---|
19 | n/a | { |
---|
20 | n/a | } |
---|
21 | n/a | |
---|
22 | n/a | int |
---|
23 | n/a | PyByteArray_Init(void) |
---|
24 | n/a | { |
---|
25 | n/a | return 1; |
---|
26 | n/a | } |
---|
27 | n/a | |
---|
28 | n/a | /* end nullbytes support */ |
---|
29 | n/a | |
---|
30 | n/a | /* Helpers */ |
---|
31 | n/a | |
---|
32 | n/a | static int |
---|
33 | n/a | _getbytevalue(PyObject* arg, int *value) |
---|
34 | n/a | { |
---|
35 | n/a | long face_value; |
---|
36 | n/a | |
---|
37 | n/a | if (PyLong_Check(arg)) { |
---|
38 | n/a | face_value = PyLong_AsLong(arg); |
---|
39 | n/a | } else { |
---|
40 | n/a | PyObject *index = PyNumber_Index(arg); |
---|
41 | n/a | if (index == NULL) { |
---|
42 | n/a | PyErr_Format(PyExc_TypeError, "an integer is required"); |
---|
43 | n/a | *value = -1; |
---|
44 | n/a | return 0; |
---|
45 | n/a | } |
---|
46 | n/a | face_value = PyLong_AsLong(index); |
---|
47 | n/a | Py_DECREF(index); |
---|
48 | n/a | } |
---|
49 | n/a | |
---|
50 | n/a | if (face_value < 0 || face_value >= 256) { |
---|
51 | n/a | /* this includes the OverflowError in case the long is too large */ |
---|
52 | n/a | PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); |
---|
53 | n/a | *value = -1; |
---|
54 | n/a | return 0; |
---|
55 | n/a | } |
---|
56 | n/a | |
---|
57 | n/a | *value = face_value; |
---|
58 | n/a | return 1; |
---|
59 | n/a | } |
---|
60 | n/a | |
---|
61 | n/a | static int |
---|
62 | n/a | bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) |
---|
63 | n/a | { |
---|
64 | n/a | void *ptr; |
---|
65 | n/a | if (view == NULL) { |
---|
66 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
67 | n/a | "bytearray_getbuffer: view==NULL argument is obsolete"); |
---|
68 | n/a | return -1; |
---|
69 | n/a | } |
---|
70 | n/a | ptr = (void *) PyByteArray_AS_STRING(obj); |
---|
71 | n/a | /* cannot fail if view != NULL and readonly == 0 */ |
---|
72 | n/a | (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); |
---|
73 | n/a | obj->ob_exports++; |
---|
74 | n/a | return 0; |
---|
75 | n/a | } |
---|
76 | n/a | |
---|
77 | n/a | static void |
---|
78 | n/a | bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) |
---|
79 | n/a | { |
---|
80 | n/a | obj->ob_exports--; |
---|
81 | n/a | } |
---|
82 | n/a | |
---|
83 | n/a | static int |
---|
84 | n/a | _canresize(PyByteArrayObject *self) |
---|
85 | n/a | { |
---|
86 | n/a | if (self->ob_exports > 0) { |
---|
87 | n/a | PyErr_SetString(PyExc_BufferError, |
---|
88 | n/a | "Existing exports of data: object cannot be re-sized"); |
---|
89 | n/a | return 0; |
---|
90 | n/a | } |
---|
91 | n/a | return 1; |
---|
92 | n/a | } |
---|
93 | n/a | |
---|
94 | n/a | #include "clinic/bytearrayobject.c.h" |
---|
95 | n/a | |
---|
96 | n/a | /* Direct API functions */ |
---|
97 | n/a | |
---|
98 | n/a | PyObject * |
---|
99 | n/a | PyByteArray_FromObject(PyObject *input) |
---|
100 | n/a | { |
---|
101 | n/a | return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, |
---|
102 | n/a | input, NULL); |
---|
103 | n/a | } |
---|
104 | n/a | |
---|
105 | n/a | PyObject * |
---|
106 | n/a | PyByteArray_FromStringAndSize(const char *bytes, Py_ssize_t size) |
---|
107 | n/a | { |
---|
108 | n/a | PyByteArrayObject *new; |
---|
109 | n/a | Py_ssize_t alloc; |
---|
110 | n/a | |
---|
111 | n/a | if (size < 0) { |
---|
112 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
113 | n/a | "Negative size passed to PyByteArray_FromStringAndSize"); |
---|
114 | n/a | return NULL; |
---|
115 | n/a | } |
---|
116 | n/a | |
---|
117 | n/a | /* Prevent buffer overflow when setting alloc to size+1. */ |
---|
118 | n/a | if (size == PY_SSIZE_T_MAX) { |
---|
119 | n/a | return PyErr_NoMemory(); |
---|
120 | n/a | } |
---|
121 | n/a | |
---|
122 | n/a | new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); |
---|
123 | n/a | if (new == NULL) |
---|
124 | n/a | return NULL; |
---|
125 | n/a | |
---|
126 | n/a | if (size == 0) { |
---|
127 | n/a | new->ob_bytes = NULL; |
---|
128 | n/a | alloc = 0; |
---|
129 | n/a | } |
---|
130 | n/a | else { |
---|
131 | n/a | alloc = size + 1; |
---|
132 | n/a | new->ob_bytes = PyObject_Malloc(alloc); |
---|
133 | n/a | if (new->ob_bytes == NULL) { |
---|
134 | n/a | Py_DECREF(new); |
---|
135 | n/a | return PyErr_NoMemory(); |
---|
136 | n/a | } |
---|
137 | n/a | if (bytes != NULL && size > 0) |
---|
138 | n/a | memcpy(new->ob_bytes, bytes, size); |
---|
139 | n/a | new->ob_bytes[size] = '\0'; /* Trailing null byte */ |
---|
140 | n/a | } |
---|
141 | n/a | Py_SIZE(new) = size; |
---|
142 | n/a | new->ob_alloc = alloc; |
---|
143 | n/a | new->ob_start = new->ob_bytes; |
---|
144 | n/a | new->ob_exports = 0; |
---|
145 | n/a | |
---|
146 | n/a | return (PyObject *)new; |
---|
147 | n/a | } |
---|
148 | n/a | |
---|
149 | n/a | Py_ssize_t |
---|
150 | n/a | PyByteArray_Size(PyObject *self) |
---|
151 | n/a | { |
---|
152 | n/a | assert(self != NULL); |
---|
153 | n/a | assert(PyByteArray_Check(self)); |
---|
154 | n/a | |
---|
155 | n/a | return PyByteArray_GET_SIZE(self); |
---|
156 | n/a | } |
---|
157 | n/a | |
---|
158 | n/a | char * |
---|
159 | n/a | PyByteArray_AsString(PyObject *self) |
---|
160 | n/a | { |
---|
161 | n/a | assert(self != NULL); |
---|
162 | n/a | assert(PyByteArray_Check(self)); |
---|
163 | n/a | |
---|
164 | n/a | return PyByteArray_AS_STRING(self); |
---|
165 | n/a | } |
---|
166 | n/a | |
---|
167 | n/a | int |
---|
168 | n/a | PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size) |
---|
169 | n/a | { |
---|
170 | n/a | void *sval; |
---|
171 | n/a | PyByteArrayObject *obj = ((PyByteArrayObject *)self); |
---|
172 | n/a | /* All computations are done unsigned to avoid integer overflows |
---|
173 | n/a | (see issue #22335). */ |
---|
174 | n/a | size_t alloc = (size_t) obj->ob_alloc; |
---|
175 | n/a | size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes); |
---|
176 | n/a | size_t size = (size_t) requested_size; |
---|
177 | n/a | |
---|
178 | n/a | assert(self != NULL); |
---|
179 | n/a | assert(PyByteArray_Check(self)); |
---|
180 | n/a | assert(logical_offset <= alloc); |
---|
181 | n/a | assert(requested_size >= 0); |
---|
182 | n/a | |
---|
183 | n/a | if (requested_size == Py_SIZE(self)) { |
---|
184 | n/a | return 0; |
---|
185 | n/a | } |
---|
186 | n/a | if (!_canresize(obj)) { |
---|
187 | n/a | return -1; |
---|
188 | n/a | } |
---|
189 | n/a | |
---|
190 | n/a | if (size + logical_offset + 1 <= alloc) { |
---|
191 | n/a | /* Current buffer is large enough to host the requested size, |
---|
192 | n/a | decide on a strategy. */ |
---|
193 | n/a | if (size < alloc / 2) { |
---|
194 | n/a | /* Major downsize; resize down to exact size */ |
---|
195 | n/a | alloc = size + 1; |
---|
196 | n/a | } |
---|
197 | n/a | else { |
---|
198 | n/a | /* Minor downsize; quick exit */ |
---|
199 | n/a | Py_SIZE(self) = size; |
---|
200 | n/a | PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */ |
---|
201 | n/a | return 0; |
---|
202 | n/a | } |
---|
203 | n/a | } |
---|
204 | n/a | else { |
---|
205 | n/a | /* Need growing, decide on a strategy */ |
---|
206 | n/a | if (size <= alloc * 1.125) { |
---|
207 | n/a | /* Moderate upsize; overallocate similar to list_resize() */ |
---|
208 | n/a | alloc = size + (size >> 3) + (size < 9 ? 3 : 6); |
---|
209 | n/a | } |
---|
210 | n/a | else { |
---|
211 | n/a | /* Major upsize; resize up to exact size */ |
---|
212 | n/a | alloc = size + 1; |
---|
213 | n/a | } |
---|
214 | n/a | } |
---|
215 | n/a | if (alloc > PY_SSIZE_T_MAX) { |
---|
216 | n/a | PyErr_NoMemory(); |
---|
217 | n/a | return -1; |
---|
218 | n/a | } |
---|
219 | n/a | |
---|
220 | n/a | if (logical_offset > 0) { |
---|
221 | n/a | sval = PyObject_Malloc(alloc); |
---|
222 | n/a | if (sval == NULL) { |
---|
223 | n/a | PyErr_NoMemory(); |
---|
224 | n/a | return -1; |
---|
225 | n/a | } |
---|
226 | n/a | memcpy(sval, PyByteArray_AS_STRING(self), |
---|
227 | n/a | Py_MIN(requested_size, Py_SIZE(self))); |
---|
228 | n/a | PyObject_Free(obj->ob_bytes); |
---|
229 | n/a | } |
---|
230 | n/a | else { |
---|
231 | n/a | sval = PyObject_Realloc(obj->ob_bytes, alloc); |
---|
232 | n/a | if (sval == NULL) { |
---|
233 | n/a | PyErr_NoMemory(); |
---|
234 | n/a | return -1; |
---|
235 | n/a | } |
---|
236 | n/a | } |
---|
237 | n/a | |
---|
238 | n/a | obj->ob_bytes = obj->ob_start = sval; |
---|
239 | n/a | Py_SIZE(self) = size; |
---|
240 | n/a | obj->ob_alloc = alloc; |
---|
241 | n/a | obj->ob_bytes[size] = '\0'; /* Trailing null byte */ |
---|
242 | n/a | |
---|
243 | n/a | return 0; |
---|
244 | n/a | } |
---|
245 | n/a | |
---|
246 | n/a | PyObject * |
---|
247 | n/a | PyByteArray_Concat(PyObject *a, PyObject *b) |
---|
248 | n/a | { |
---|
249 | n/a | Py_buffer va, vb; |
---|
250 | n/a | PyByteArrayObject *result = NULL; |
---|
251 | n/a | |
---|
252 | n/a | va.len = -1; |
---|
253 | n/a | vb.len = -1; |
---|
254 | n/a | if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 || |
---|
255 | n/a | PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) { |
---|
256 | n/a | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
---|
257 | n/a | Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); |
---|
258 | n/a | goto done; |
---|
259 | n/a | } |
---|
260 | n/a | |
---|
261 | n/a | if (va.len > PY_SSIZE_T_MAX - vb.len) { |
---|
262 | n/a | PyErr_NoMemory(); |
---|
263 | n/a | goto done; |
---|
264 | n/a | } |
---|
265 | n/a | |
---|
266 | n/a | result = (PyByteArrayObject *) \ |
---|
267 | n/a | PyByteArray_FromStringAndSize(NULL, va.len + vb.len); |
---|
268 | n/a | if (result != NULL) { |
---|
269 | n/a | memcpy(result->ob_bytes, va.buf, va.len); |
---|
270 | n/a | memcpy(result->ob_bytes + va.len, vb.buf, vb.len); |
---|
271 | n/a | } |
---|
272 | n/a | |
---|
273 | n/a | done: |
---|
274 | n/a | if (va.len != -1) |
---|
275 | n/a | PyBuffer_Release(&va); |
---|
276 | n/a | if (vb.len != -1) |
---|
277 | n/a | PyBuffer_Release(&vb); |
---|
278 | n/a | return (PyObject *)result; |
---|
279 | n/a | } |
---|
280 | n/a | |
---|
281 | n/a | /* Functions stuffed into the type object */ |
---|
282 | n/a | |
---|
283 | n/a | static Py_ssize_t |
---|
284 | n/a | bytearray_length(PyByteArrayObject *self) |
---|
285 | n/a | { |
---|
286 | n/a | return Py_SIZE(self); |
---|
287 | n/a | } |
---|
288 | n/a | |
---|
289 | n/a | static PyObject * |
---|
290 | n/a | bytearray_iconcat(PyByteArrayObject *self, PyObject *other) |
---|
291 | n/a | { |
---|
292 | n/a | Py_ssize_t size; |
---|
293 | n/a | Py_buffer vo; |
---|
294 | n/a | |
---|
295 | n/a | if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) { |
---|
296 | n/a | PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", |
---|
297 | n/a | Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); |
---|
298 | n/a | return NULL; |
---|
299 | n/a | } |
---|
300 | n/a | |
---|
301 | n/a | size = Py_SIZE(self); |
---|
302 | n/a | if (size > PY_SSIZE_T_MAX - vo.len) { |
---|
303 | n/a | PyBuffer_Release(&vo); |
---|
304 | n/a | return PyErr_NoMemory(); |
---|
305 | n/a | } |
---|
306 | n/a | if (PyByteArray_Resize((PyObject *)self, size + vo.len) < 0) { |
---|
307 | n/a | PyBuffer_Release(&vo); |
---|
308 | n/a | return NULL; |
---|
309 | n/a | } |
---|
310 | n/a | memcpy(PyByteArray_AS_STRING(self) + size, vo.buf, vo.len); |
---|
311 | n/a | PyBuffer_Release(&vo); |
---|
312 | n/a | Py_INCREF(self); |
---|
313 | n/a | return (PyObject *)self; |
---|
314 | n/a | } |
---|
315 | n/a | |
---|
316 | n/a | static PyObject * |
---|
317 | n/a | bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) |
---|
318 | n/a | { |
---|
319 | n/a | PyByteArrayObject *result; |
---|
320 | n/a | Py_ssize_t mysize; |
---|
321 | n/a | Py_ssize_t size; |
---|
322 | n/a | |
---|
323 | n/a | if (count < 0) |
---|
324 | n/a | count = 0; |
---|
325 | n/a | mysize = Py_SIZE(self); |
---|
326 | n/a | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
---|
327 | n/a | return PyErr_NoMemory(); |
---|
328 | n/a | size = mysize * count; |
---|
329 | n/a | result = (PyByteArrayObject *)PyByteArray_FromStringAndSize(NULL, size); |
---|
330 | n/a | if (result != NULL && size != 0) { |
---|
331 | n/a | if (mysize == 1) |
---|
332 | n/a | memset(result->ob_bytes, self->ob_bytes[0], size); |
---|
333 | n/a | else { |
---|
334 | n/a | Py_ssize_t i; |
---|
335 | n/a | for (i = 0; i < count; i++) |
---|
336 | n/a | memcpy(result->ob_bytes + i*mysize, self->ob_bytes, mysize); |
---|
337 | n/a | } |
---|
338 | n/a | } |
---|
339 | n/a | return (PyObject *)result; |
---|
340 | n/a | } |
---|
341 | n/a | |
---|
342 | n/a | static PyObject * |
---|
343 | n/a | bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) |
---|
344 | n/a | { |
---|
345 | n/a | Py_ssize_t mysize; |
---|
346 | n/a | Py_ssize_t size; |
---|
347 | n/a | char *buf; |
---|
348 | n/a | |
---|
349 | n/a | if (count < 0) |
---|
350 | n/a | count = 0; |
---|
351 | n/a | mysize = Py_SIZE(self); |
---|
352 | n/a | if (count > 0 && mysize > PY_SSIZE_T_MAX / count) |
---|
353 | n/a | return PyErr_NoMemory(); |
---|
354 | n/a | size = mysize * count; |
---|
355 | n/a | if (PyByteArray_Resize((PyObject *)self, size) < 0) |
---|
356 | n/a | return NULL; |
---|
357 | n/a | |
---|
358 | n/a | buf = PyByteArray_AS_STRING(self); |
---|
359 | n/a | if (mysize == 1) |
---|
360 | n/a | memset(buf, buf[0], size); |
---|
361 | n/a | else { |
---|
362 | n/a | Py_ssize_t i; |
---|
363 | n/a | for (i = 1; i < count; i++) |
---|
364 | n/a | memcpy(buf + i*mysize, buf, mysize); |
---|
365 | n/a | } |
---|
366 | n/a | |
---|
367 | n/a | Py_INCREF(self); |
---|
368 | n/a | return (PyObject *)self; |
---|
369 | n/a | } |
---|
370 | n/a | |
---|
371 | n/a | static PyObject * |
---|
372 | n/a | bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) |
---|
373 | n/a | { |
---|
374 | n/a | if (i < 0) |
---|
375 | n/a | i += Py_SIZE(self); |
---|
376 | n/a | if (i < 0 || i >= Py_SIZE(self)) { |
---|
377 | n/a | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
---|
378 | n/a | return NULL; |
---|
379 | n/a | } |
---|
380 | n/a | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
---|
381 | n/a | } |
---|
382 | n/a | |
---|
383 | n/a | static PyObject * |
---|
384 | n/a | bytearray_subscript(PyByteArrayObject *self, PyObject *index) |
---|
385 | n/a | { |
---|
386 | n/a | if (PyIndex_Check(index)) { |
---|
387 | n/a | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
---|
388 | n/a | |
---|
389 | n/a | if (i == -1 && PyErr_Occurred()) |
---|
390 | n/a | return NULL; |
---|
391 | n/a | |
---|
392 | n/a | if (i < 0) |
---|
393 | n/a | i += PyByteArray_GET_SIZE(self); |
---|
394 | n/a | |
---|
395 | n/a | if (i < 0 || i >= Py_SIZE(self)) { |
---|
396 | n/a | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
---|
397 | n/a | return NULL; |
---|
398 | n/a | } |
---|
399 | n/a | return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i])); |
---|
400 | n/a | } |
---|
401 | n/a | else if (PySlice_Check(index)) { |
---|
402 | n/a | Py_ssize_t start, stop, step, slicelength, cur, i; |
---|
403 | n/a | if (PySlice_GetIndicesEx(index, |
---|
404 | n/a | PyByteArray_GET_SIZE(self), |
---|
405 | n/a | &start, &stop, &step, &slicelength) < 0) { |
---|
406 | n/a | return NULL; |
---|
407 | n/a | } |
---|
408 | n/a | |
---|
409 | n/a | if (slicelength <= 0) |
---|
410 | n/a | return PyByteArray_FromStringAndSize("", 0); |
---|
411 | n/a | else if (step == 1) { |
---|
412 | n/a | return PyByteArray_FromStringAndSize( |
---|
413 | n/a | PyByteArray_AS_STRING(self) + start, slicelength); |
---|
414 | n/a | } |
---|
415 | n/a | else { |
---|
416 | n/a | char *source_buf = PyByteArray_AS_STRING(self); |
---|
417 | n/a | char *result_buf; |
---|
418 | n/a | PyObject *result; |
---|
419 | n/a | |
---|
420 | n/a | result = PyByteArray_FromStringAndSize(NULL, slicelength); |
---|
421 | n/a | if (result == NULL) |
---|
422 | n/a | return NULL; |
---|
423 | n/a | |
---|
424 | n/a | result_buf = PyByteArray_AS_STRING(result); |
---|
425 | n/a | for (cur = start, i = 0; i < slicelength; |
---|
426 | n/a | cur += step, i++) { |
---|
427 | n/a | result_buf[i] = source_buf[cur]; |
---|
428 | n/a | } |
---|
429 | n/a | return result; |
---|
430 | n/a | } |
---|
431 | n/a | } |
---|
432 | n/a | else { |
---|
433 | n/a | PyErr_Format(PyExc_TypeError, |
---|
434 | n/a | "bytearray indices must be integers or slices, not %.200s", |
---|
435 | n/a | Py_TYPE(index)->tp_name); |
---|
436 | n/a | return NULL; |
---|
437 | n/a | } |
---|
438 | n/a | } |
---|
439 | n/a | |
---|
440 | n/a | static int |
---|
441 | n/a | bytearray_setslice_linear(PyByteArrayObject *self, |
---|
442 | n/a | Py_ssize_t lo, Py_ssize_t hi, |
---|
443 | n/a | char *bytes, Py_ssize_t bytes_len) |
---|
444 | n/a | { |
---|
445 | n/a | Py_ssize_t avail = hi - lo; |
---|
446 | n/a | char *buf = PyByteArray_AS_STRING(self); |
---|
447 | n/a | Py_ssize_t growth = bytes_len - avail; |
---|
448 | n/a | int res = 0; |
---|
449 | n/a | assert(avail >= 0); |
---|
450 | n/a | |
---|
451 | n/a | if (growth < 0) { |
---|
452 | n/a | if (!_canresize(self)) |
---|
453 | n/a | return -1; |
---|
454 | n/a | |
---|
455 | n/a | if (lo == 0) { |
---|
456 | n/a | /* Shrink the buffer by advancing its logical start */ |
---|
457 | n/a | self->ob_start -= growth; |
---|
458 | n/a | /* |
---|
459 | n/a | 0 lo hi old_size |
---|
460 | n/a | | |<----avail----->|<-----tail------>| |
---|
461 | n/a | | |<-bytes_len->|<-----tail------>| |
---|
462 | n/a | 0 new_lo new_hi new_size |
---|
463 | n/a | */ |
---|
464 | n/a | } |
---|
465 | n/a | else { |
---|
466 | n/a | /* |
---|
467 | n/a | 0 lo hi old_size |
---|
468 | n/a | | |<----avail----->|<-----tomove------>| |
---|
469 | n/a | | |<-bytes_len->|<-----tomove------>| |
---|
470 | n/a | 0 lo new_hi new_size |
---|
471 | n/a | */ |
---|
472 | n/a | memmove(buf + lo + bytes_len, buf + hi, |
---|
473 | n/a | Py_SIZE(self) - hi); |
---|
474 | n/a | } |
---|
475 | n/a | if (PyByteArray_Resize((PyObject *)self, |
---|
476 | n/a | Py_SIZE(self) + growth) < 0) { |
---|
477 | n/a | /* Issue #19578: Handling the memory allocation failure here is |
---|
478 | n/a | tricky here because the bytearray object has already been |
---|
479 | n/a | modified. Depending on growth and lo, the behaviour is |
---|
480 | n/a | different. |
---|
481 | n/a | |
---|
482 | n/a | If growth < 0 and lo != 0, the operation is completed, but a |
---|
483 | n/a | MemoryError is still raised and the memory block is not |
---|
484 | n/a | shrunk. Otherwise, the bytearray is restored in its previous |
---|
485 | n/a | state and a MemoryError is raised. */ |
---|
486 | n/a | if (lo == 0) { |
---|
487 | n/a | self->ob_start += growth; |
---|
488 | n/a | return -1; |
---|
489 | n/a | } |
---|
490 | n/a | /* memmove() removed bytes, the bytearray object cannot be |
---|
491 | n/a | restored in its previous state. */ |
---|
492 | n/a | Py_SIZE(self) += growth; |
---|
493 | n/a | res = -1; |
---|
494 | n/a | } |
---|
495 | n/a | buf = PyByteArray_AS_STRING(self); |
---|
496 | n/a | } |
---|
497 | n/a | else if (growth > 0) { |
---|
498 | n/a | if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) { |
---|
499 | n/a | PyErr_NoMemory(); |
---|
500 | n/a | return -1; |
---|
501 | n/a | } |
---|
502 | n/a | |
---|
503 | n/a | if (PyByteArray_Resize((PyObject *)self, |
---|
504 | n/a | Py_SIZE(self) + growth) < 0) { |
---|
505 | n/a | return -1; |
---|
506 | n/a | } |
---|
507 | n/a | buf = PyByteArray_AS_STRING(self); |
---|
508 | n/a | /* Make the place for the additional bytes */ |
---|
509 | n/a | /* |
---|
510 | n/a | 0 lo hi old_size |
---|
511 | n/a | | |<-avail->|<-----tomove------>| |
---|
512 | n/a | | |<---bytes_len-->|<-----tomove------>| |
---|
513 | n/a | 0 lo new_hi new_size |
---|
514 | n/a | */ |
---|
515 | n/a | memmove(buf + lo + bytes_len, buf + hi, |
---|
516 | n/a | Py_SIZE(self) - lo - bytes_len); |
---|
517 | n/a | } |
---|
518 | n/a | |
---|
519 | n/a | if (bytes_len > 0) |
---|
520 | n/a | memcpy(buf + lo, bytes, bytes_len); |
---|
521 | n/a | return res; |
---|
522 | n/a | } |
---|
523 | n/a | |
---|
524 | n/a | static int |
---|
525 | n/a | bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, |
---|
526 | n/a | PyObject *values) |
---|
527 | n/a | { |
---|
528 | n/a | Py_ssize_t needed; |
---|
529 | n/a | void *bytes; |
---|
530 | n/a | Py_buffer vbytes; |
---|
531 | n/a | int res = 0; |
---|
532 | n/a | |
---|
533 | n/a | vbytes.len = -1; |
---|
534 | n/a | if (values == (PyObject *)self) { |
---|
535 | n/a | /* Make a copy and call this function recursively */ |
---|
536 | n/a | int err; |
---|
537 | n/a | values = PyByteArray_FromObject(values); |
---|
538 | n/a | if (values == NULL) |
---|
539 | n/a | return -1; |
---|
540 | n/a | err = bytearray_setslice(self, lo, hi, values); |
---|
541 | n/a | Py_DECREF(values); |
---|
542 | n/a | return err; |
---|
543 | n/a | } |
---|
544 | n/a | if (values == NULL) { |
---|
545 | n/a | /* del b[lo:hi] */ |
---|
546 | n/a | bytes = NULL; |
---|
547 | n/a | needed = 0; |
---|
548 | n/a | } |
---|
549 | n/a | else { |
---|
550 | n/a | if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) { |
---|
551 | n/a | PyErr_Format(PyExc_TypeError, |
---|
552 | n/a | "can't set bytearray slice from %.100s", |
---|
553 | n/a | Py_TYPE(values)->tp_name); |
---|
554 | n/a | return -1; |
---|
555 | n/a | } |
---|
556 | n/a | needed = vbytes.len; |
---|
557 | n/a | bytes = vbytes.buf; |
---|
558 | n/a | } |
---|
559 | n/a | |
---|
560 | n/a | if (lo < 0) |
---|
561 | n/a | lo = 0; |
---|
562 | n/a | if (hi < lo) |
---|
563 | n/a | hi = lo; |
---|
564 | n/a | if (hi > Py_SIZE(self)) |
---|
565 | n/a | hi = Py_SIZE(self); |
---|
566 | n/a | |
---|
567 | n/a | res = bytearray_setslice_linear(self, lo, hi, bytes, needed); |
---|
568 | n/a | if (vbytes.len != -1) |
---|
569 | n/a | PyBuffer_Release(&vbytes); |
---|
570 | n/a | return res; |
---|
571 | n/a | } |
---|
572 | n/a | |
---|
573 | n/a | static int |
---|
574 | n/a | bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) |
---|
575 | n/a | { |
---|
576 | n/a | int ival; |
---|
577 | n/a | |
---|
578 | n/a | if (i < 0) |
---|
579 | n/a | i += Py_SIZE(self); |
---|
580 | n/a | |
---|
581 | n/a | if (i < 0 || i >= Py_SIZE(self)) { |
---|
582 | n/a | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
---|
583 | n/a | return -1; |
---|
584 | n/a | } |
---|
585 | n/a | |
---|
586 | n/a | if (value == NULL) |
---|
587 | n/a | return bytearray_setslice(self, i, i+1, NULL); |
---|
588 | n/a | |
---|
589 | n/a | if (!_getbytevalue(value, &ival)) |
---|
590 | n/a | return -1; |
---|
591 | n/a | |
---|
592 | n/a | PyByteArray_AS_STRING(self)[i] = ival; |
---|
593 | n/a | return 0; |
---|
594 | n/a | } |
---|
595 | n/a | |
---|
596 | n/a | static int |
---|
597 | n/a | bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) |
---|
598 | n/a | { |
---|
599 | n/a | Py_ssize_t start, stop, step, slicelen, needed; |
---|
600 | n/a | char *buf, *bytes; |
---|
601 | n/a | buf = PyByteArray_AS_STRING(self); |
---|
602 | n/a | |
---|
603 | n/a | if (PyIndex_Check(index)) { |
---|
604 | n/a | Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); |
---|
605 | n/a | |
---|
606 | n/a | if (i == -1 && PyErr_Occurred()) |
---|
607 | n/a | return -1; |
---|
608 | n/a | |
---|
609 | n/a | if (i < 0) |
---|
610 | n/a | i += PyByteArray_GET_SIZE(self); |
---|
611 | n/a | |
---|
612 | n/a | if (i < 0 || i >= Py_SIZE(self)) { |
---|
613 | n/a | PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); |
---|
614 | n/a | return -1; |
---|
615 | n/a | } |
---|
616 | n/a | |
---|
617 | n/a | if (values == NULL) { |
---|
618 | n/a | /* Fall through to slice assignment */ |
---|
619 | n/a | start = i; |
---|
620 | n/a | stop = i + 1; |
---|
621 | n/a | step = 1; |
---|
622 | n/a | slicelen = 1; |
---|
623 | n/a | } |
---|
624 | n/a | else { |
---|
625 | n/a | int ival; |
---|
626 | n/a | if (!_getbytevalue(values, &ival)) |
---|
627 | n/a | return -1; |
---|
628 | n/a | buf[i] = (char)ival; |
---|
629 | n/a | return 0; |
---|
630 | n/a | } |
---|
631 | n/a | } |
---|
632 | n/a | else if (PySlice_Check(index)) { |
---|
633 | n/a | if (PySlice_GetIndicesEx(index, |
---|
634 | n/a | PyByteArray_GET_SIZE(self), |
---|
635 | n/a | &start, &stop, &step, &slicelen) < 0) { |
---|
636 | n/a | return -1; |
---|
637 | n/a | } |
---|
638 | n/a | } |
---|
639 | n/a | else { |
---|
640 | n/a | PyErr_Format(PyExc_TypeError, |
---|
641 | n/a | "bytearray indices must be integers or slices, not %.200s", |
---|
642 | n/a | Py_TYPE(index)->tp_name); |
---|
643 | n/a | return -1; |
---|
644 | n/a | } |
---|
645 | n/a | |
---|
646 | n/a | if (values == NULL) { |
---|
647 | n/a | bytes = NULL; |
---|
648 | n/a | needed = 0; |
---|
649 | n/a | } |
---|
650 | n/a | else if (values == (PyObject *)self || !PyByteArray_Check(values)) { |
---|
651 | n/a | int err; |
---|
652 | n/a | if (PyNumber_Check(values) || PyUnicode_Check(values)) { |
---|
653 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
654 | n/a | "can assign only bytes, buffers, or iterables " |
---|
655 | n/a | "of ints in range(0, 256)"); |
---|
656 | n/a | return -1; |
---|
657 | n/a | } |
---|
658 | n/a | /* Make a copy and call this function recursively */ |
---|
659 | n/a | values = PyByteArray_FromObject(values); |
---|
660 | n/a | if (values == NULL) |
---|
661 | n/a | return -1; |
---|
662 | n/a | err = bytearray_ass_subscript(self, index, values); |
---|
663 | n/a | Py_DECREF(values); |
---|
664 | n/a | return err; |
---|
665 | n/a | } |
---|
666 | n/a | else { |
---|
667 | n/a | assert(PyByteArray_Check(values)); |
---|
668 | n/a | bytes = PyByteArray_AS_STRING(values); |
---|
669 | n/a | needed = Py_SIZE(values); |
---|
670 | n/a | } |
---|
671 | n/a | /* Make sure b[5:2] = ... inserts before 5, not before 2. */ |
---|
672 | n/a | if ((step < 0 && start < stop) || |
---|
673 | n/a | (step > 0 && start > stop)) |
---|
674 | n/a | stop = start; |
---|
675 | n/a | if (step == 1) { |
---|
676 | n/a | return bytearray_setslice_linear(self, start, stop, bytes, needed); |
---|
677 | n/a | } |
---|
678 | n/a | else { |
---|
679 | n/a | if (needed == 0) { |
---|
680 | n/a | /* Delete slice */ |
---|
681 | n/a | size_t cur; |
---|
682 | n/a | Py_ssize_t i; |
---|
683 | n/a | |
---|
684 | n/a | if (!_canresize(self)) |
---|
685 | n/a | return -1; |
---|
686 | n/a | |
---|
687 | n/a | if (slicelen == 0) |
---|
688 | n/a | /* Nothing to do here. */ |
---|
689 | n/a | return 0; |
---|
690 | n/a | |
---|
691 | n/a | if (step < 0) { |
---|
692 | n/a | stop = start + 1; |
---|
693 | n/a | start = stop + step * (slicelen - 1) - 1; |
---|
694 | n/a | step = -step; |
---|
695 | n/a | } |
---|
696 | n/a | for (cur = start, i = 0; |
---|
697 | n/a | i < slicelen; cur += step, i++) { |
---|
698 | n/a | Py_ssize_t lim = step - 1; |
---|
699 | n/a | |
---|
700 | n/a | if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) |
---|
701 | n/a | lim = PyByteArray_GET_SIZE(self) - cur - 1; |
---|
702 | n/a | |
---|
703 | n/a | memmove(buf + cur - i, |
---|
704 | n/a | buf + cur + 1, lim); |
---|
705 | n/a | } |
---|
706 | n/a | /* Move the tail of the bytes, in one chunk */ |
---|
707 | n/a | cur = start + (size_t)slicelen*step; |
---|
708 | n/a | if (cur < (size_t)PyByteArray_GET_SIZE(self)) { |
---|
709 | n/a | memmove(buf + cur - slicelen, |
---|
710 | n/a | buf + cur, |
---|
711 | n/a | PyByteArray_GET_SIZE(self) - cur); |
---|
712 | n/a | } |
---|
713 | n/a | if (PyByteArray_Resize((PyObject *)self, |
---|
714 | n/a | PyByteArray_GET_SIZE(self) - slicelen) < 0) |
---|
715 | n/a | return -1; |
---|
716 | n/a | |
---|
717 | n/a | return 0; |
---|
718 | n/a | } |
---|
719 | n/a | else { |
---|
720 | n/a | /* Assign slice */ |
---|
721 | n/a | Py_ssize_t i; |
---|
722 | n/a | size_t cur; |
---|
723 | n/a | |
---|
724 | n/a | if (needed != slicelen) { |
---|
725 | n/a | PyErr_Format(PyExc_ValueError, |
---|
726 | n/a | "attempt to assign bytes of size %zd " |
---|
727 | n/a | "to extended slice of size %zd", |
---|
728 | n/a | needed, slicelen); |
---|
729 | n/a | return -1; |
---|
730 | n/a | } |
---|
731 | n/a | for (cur = start, i = 0; i < slicelen; cur += step, i++) |
---|
732 | n/a | buf[cur] = bytes[i]; |
---|
733 | n/a | return 0; |
---|
734 | n/a | } |
---|
735 | n/a | } |
---|
736 | n/a | } |
---|
737 | n/a | |
---|
738 | n/a | static int |
---|
739 | n/a | bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) |
---|
740 | n/a | { |
---|
741 | n/a | static char *kwlist[] = {"source", "encoding", "errors", 0}; |
---|
742 | n/a | PyObject *arg = NULL; |
---|
743 | n/a | const char *encoding = NULL; |
---|
744 | n/a | const char *errors = NULL; |
---|
745 | n/a | Py_ssize_t count; |
---|
746 | n/a | PyObject *it; |
---|
747 | n/a | PyObject *(*iternext)(PyObject *); |
---|
748 | n/a | |
---|
749 | n/a | if (Py_SIZE(self) != 0) { |
---|
750 | n/a | /* Empty previous contents (yes, do this first of all!) */ |
---|
751 | n/a | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
---|
752 | n/a | return -1; |
---|
753 | n/a | } |
---|
754 | n/a | |
---|
755 | n/a | /* Parse arguments */ |
---|
756 | n/a | if (!PyArg_ParseTupleAndKeywords(args, kwds, "|Oss:bytearray", kwlist, |
---|
757 | n/a | &arg, &encoding, &errors)) |
---|
758 | n/a | return -1; |
---|
759 | n/a | |
---|
760 | n/a | /* Make a quick exit if no first argument */ |
---|
761 | n/a | if (arg == NULL) { |
---|
762 | n/a | if (encoding != NULL || errors != NULL) { |
---|
763 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
764 | n/a | "encoding or errors without sequence argument"); |
---|
765 | n/a | return -1; |
---|
766 | n/a | } |
---|
767 | n/a | return 0; |
---|
768 | n/a | } |
---|
769 | n/a | |
---|
770 | n/a | if (PyUnicode_Check(arg)) { |
---|
771 | n/a | /* Encode via the codec registry */ |
---|
772 | n/a | PyObject *encoded, *new; |
---|
773 | n/a | if (encoding == NULL) { |
---|
774 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
775 | n/a | "string argument without an encoding"); |
---|
776 | n/a | return -1; |
---|
777 | n/a | } |
---|
778 | n/a | encoded = PyUnicode_AsEncodedString(arg, encoding, errors); |
---|
779 | n/a | if (encoded == NULL) |
---|
780 | n/a | return -1; |
---|
781 | n/a | assert(PyBytes_Check(encoded)); |
---|
782 | n/a | new = bytearray_iconcat(self, encoded); |
---|
783 | n/a | Py_DECREF(encoded); |
---|
784 | n/a | if (new == NULL) |
---|
785 | n/a | return -1; |
---|
786 | n/a | Py_DECREF(new); |
---|
787 | n/a | return 0; |
---|
788 | n/a | } |
---|
789 | n/a | |
---|
790 | n/a | /* If it's not unicode, there can't be encoding or errors */ |
---|
791 | n/a | if (encoding != NULL || errors != NULL) { |
---|
792 | n/a | PyErr_SetString(PyExc_TypeError, |
---|
793 | n/a | "encoding or errors without a string argument"); |
---|
794 | n/a | return -1; |
---|
795 | n/a | } |
---|
796 | n/a | |
---|
797 | n/a | /* Is it an int? */ |
---|
798 | n/a | if (PyIndex_Check(arg)) { |
---|
799 | n/a | count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); |
---|
800 | n/a | if (count == -1 && PyErr_Occurred()) { |
---|
801 | n/a | if (PyErr_ExceptionMatches(PyExc_OverflowError)) |
---|
802 | n/a | return -1; |
---|
803 | n/a | PyErr_Clear(); /* fall through */ |
---|
804 | n/a | } |
---|
805 | n/a | else { |
---|
806 | n/a | if (count < 0) { |
---|
807 | n/a | PyErr_SetString(PyExc_ValueError, "negative count"); |
---|
808 | n/a | return -1; |
---|
809 | n/a | } |
---|
810 | n/a | if (count > 0) { |
---|
811 | n/a | if (PyByteArray_Resize((PyObject *)self, count)) |
---|
812 | n/a | return -1; |
---|
813 | n/a | memset(PyByteArray_AS_STRING(self), 0, count); |
---|
814 | n/a | } |
---|
815 | n/a | return 0; |
---|
816 | n/a | } |
---|
817 | n/a | } |
---|
818 | n/a | |
---|
819 | n/a | /* Use the buffer API */ |
---|
820 | n/a | if (PyObject_CheckBuffer(arg)) { |
---|
821 | n/a | Py_ssize_t size; |
---|
822 | n/a | Py_buffer view; |
---|
823 | n/a | if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) |
---|
824 | n/a | return -1; |
---|
825 | n/a | size = view.len; |
---|
826 | n/a | if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; |
---|
827 | n/a | if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self), |
---|
828 | n/a | &view, size, 'C') < 0) |
---|
829 | n/a | goto fail; |
---|
830 | n/a | PyBuffer_Release(&view); |
---|
831 | n/a | return 0; |
---|
832 | n/a | fail: |
---|
833 | n/a | PyBuffer_Release(&view); |
---|
834 | n/a | return -1; |
---|
835 | n/a | } |
---|
836 | n/a | |
---|
837 | n/a | /* XXX Optimize this if the arguments is a list, tuple */ |
---|
838 | n/a | |
---|
839 | n/a | /* Get the iterator */ |
---|
840 | n/a | it = PyObject_GetIter(arg); |
---|
841 | n/a | if (it == NULL) |
---|
842 | n/a | return -1; |
---|
843 | n/a | iternext = *Py_TYPE(it)->tp_iternext; |
---|
844 | n/a | |
---|
845 | n/a | /* Run the iterator to exhaustion */ |
---|
846 | n/a | for (;;) { |
---|
847 | n/a | PyObject *item; |
---|
848 | n/a | int rc, value; |
---|
849 | n/a | |
---|
850 | n/a | /* Get the next item */ |
---|
851 | n/a | item = iternext(it); |
---|
852 | n/a | if (item == NULL) { |
---|
853 | n/a | if (PyErr_Occurred()) { |
---|
854 | n/a | if (!PyErr_ExceptionMatches(PyExc_StopIteration)) |
---|
855 | n/a | goto error; |
---|
856 | n/a | PyErr_Clear(); |
---|
857 | n/a | } |
---|
858 | n/a | break; |
---|
859 | n/a | } |
---|
860 | n/a | |
---|
861 | n/a | /* Interpret it as an int (__index__) */ |
---|
862 | n/a | rc = _getbytevalue(item, &value); |
---|
863 | n/a | Py_DECREF(item); |
---|
864 | n/a | if (!rc) |
---|
865 | n/a | goto error; |
---|
866 | n/a | |
---|
867 | n/a | /* Append the byte */ |
---|
868 | n/a | if (Py_SIZE(self) + 1 < self->ob_alloc) { |
---|
869 | n/a | Py_SIZE(self)++; |
---|
870 | n/a | PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0'; |
---|
871 | n/a | } |
---|
872 | n/a | else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) |
---|
873 | n/a | goto error; |
---|
874 | n/a | PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; |
---|
875 | n/a | } |
---|
876 | n/a | |
---|
877 | n/a | /* Clean up and return success */ |
---|
878 | n/a | Py_DECREF(it); |
---|
879 | n/a | return 0; |
---|
880 | n/a | |
---|
881 | n/a | error: |
---|
882 | n/a | /* Error handling when it != NULL */ |
---|
883 | n/a | Py_DECREF(it); |
---|
884 | n/a | return -1; |
---|
885 | n/a | } |
---|
886 | n/a | |
---|
887 | n/a | /* Mostly copied from string_repr, but without the |
---|
888 | n/a | "smart quote" functionality. */ |
---|
889 | n/a | static PyObject * |
---|
890 | n/a | bytearray_repr(PyByteArrayObject *self) |
---|
891 | n/a | { |
---|
892 | n/a | const char *quote_prefix = "bytearray(b"; |
---|
893 | n/a | const char *quote_postfix = ")"; |
---|
894 | n/a | Py_ssize_t length = Py_SIZE(self); |
---|
895 | n/a | /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ |
---|
896 | n/a | size_t newsize; |
---|
897 | n/a | PyObject *v; |
---|
898 | n/a | Py_ssize_t i; |
---|
899 | n/a | char *bytes; |
---|
900 | n/a | char c; |
---|
901 | n/a | char *p; |
---|
902 | n/a | int quote; |
---|
903 | n/a | char *test, *start; |
---|
904 | n/a | char *buffer; |
---|
905 | n/a | |
---|
906 | n/a | if (length > (PY_SSIZE_T_MAX - 15) / 4) { |
---|
907 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
908 | n/a | "bytearray object is too large to make repr"); |
---|
909 | n/a | return NULL; |
---|
910 | n/a | } |
---|
911 | n/a | |
---|
912 | n/a | newsize = 15 + length * 4; |
---|
913 | n/a | buffer = PyObject_Malloc(newsize); |
---|
914 | n/a | if (buffer == NULL) { |
---|
915 | n/a | PyErr_NoMemory(); |
---|
916 | n/a | return NULL; |
---|
917 | n/a | } |
---|
918 | n/a | |
---|
919 | n/a | /* Figure out which quote to use; single is preferred */ |
---|
920 | n/a | quote = '\''; |
---|
921 | n/a | start = PyByteArray_AS_STRING(self); |
---|
922 | n/a | for (test = start; test < start+length; ++test) { |
---|
923 | n/a | if (*test == '"') { |
---|
924 | n/a | quote = '\''; /* back to single */ |
---|
925 | n/a | break; |
---|
926 | n/a | } |
---|
927 | n/a | else if (*test == '\'') |
---|
928 | n/a | quote = '"'; |
---|
929 | n/a | } |
---|
930 | n/a | |
---|
931 | n/a | p = buffer; |
---|
932 | n/a | while (*quote_prefix) |
---|
933 | n/a | *p++ = *quote_prefix++; |
---|
934 | n/a | *p++ = quote; |
---|
935 | n/a | |
---|
936 | n/a | bytes = PyByteArray_AS_STRING(self); |
---|
937 | n/a | for (i = 0; i < length; i++) { |
---|
938 | n/a | /* There's at least enough room for a hex escape |
---|
939 | n/a | and a closing quote. */ |
---|
940 | n/a | assert(newsize - (p - buffer) >= 5); |
---|
941 | n/a | c = bytes[i]; |
---|
942 | n/a | if (c == '\'' || c == '\\') |
---|
943 | n/a | *p++ = '\\', *p++ = c; |
---|
944 | n/a | else if (c == '\t') |
---|
945 | n/a | *p++ = '\\', *p++ = 't'; |
---|
946 | n/a | else if (c == '\n') |
---|
947 | n/a | *p++ = '\\', *p++ = 'n'; |
---|
948 | n/a | else if (c == '\r') |
---|
949 | n/a | *p++ = '\\', *p++ = 'r'; |
---|
950 | n/a | else if (c == 0) |
---|
951 | n/a | *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; |
---|
952 | n/a | else if (c < ' ' || c >= 0x7f) { |
---|
953 | n/a | *p++ = '\\'; |
---|
954 | n/a | *p++ = 'x'; |
---|
955 | n/a | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
---|
956 | n/a | *p++ = Py_hexdigits[c & 0xf]; |
---|
957 | n/a | } |
---|
958 | n/a | else |
---|
959 | n/a | *p++ = c; |
---|
960 | n/a | } |
---|
961 | n/a | assert(newsize - (p - buffer) >= 1); |
---|
962 | n/a | *p++ = quote; |
---|
963 | n/a | while (*quote_postfix) { |
---|
964 | n/a | *p++ = *quote_postfix++; |
---|
965 | n/a | } |
---|
966 | n/a | |
---|
967 | n/a | v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL); |
---|
968 | n/a | PyObject_Free(buffer); |
---|
969 | n/a | return v; |
---|
970 | n/a | } |
---|
971 | n/a | |
---|
972 | n/a | static PyObject * |
---|
973 | n/a | bytearray_str(PyObject *op) |
---|
974 | n/a | { |
---|
975 | n/a | if (Py_BytesWarningFlag) { |
---|
976 | n/a | if (PyErr_WarnEx(PyExc_BytesWarning, |
---|
977 | n/a | "str() on a bytearray instance", 1)) |
---|
978 | n/a | return NULL; |
---|
979 | n/a | } |
---|
980 | n/a | return bytearray_repr((PyByteArrayObject*)op); |
---|
981 | n/a | } |
---|
982 | n/a | |
---|
983 | n/a | static PyObject * |
---|
984 | n/a | bytearray_richcompare(PyObject *self, PyObject *other, int op) |
---|
985 | n/a | { |
---|
986 | n/a | Py_ssize_t self_size, other_size; |
---|
987 | n/a | Py_buffer self_bytes, other_bytes; |
---|
988 | n/a | PyObject *res; |
---|
989 | n/a | Py_ssize_t minsize; |
---|
990 | n/a | int cmp, rc; |
---|
991 | n/a | |
---|
992 | n/a | /* Bytes can be compared to anything that supports the (binary) |
---|
993 | n/a | buffer API. Except that a comparison with Unicode is always an |
---|
994 | n/a | error, even if the comparison is for equality. */ |
---|
995 | n/a | rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type); |
---|
996 | n/a | if (!rc) |
---|
997 | n/a | rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type); |
---|
998 | n/a | if (rc < 0) |
---|
999 | n/a | return NULL; |
---|
1000 | n/a | if (rc) { |
---|
1001 | n/a | if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { |
---|
1002 | n/a | if (PyErr_WarnEx(PyExc_BytesWarning, |
---|
1003 | n/a | "Comparison between bytearray and string", 1)) |
---|
1004 | n/a | return NULL; |
---|
1005 | n/a | } |
---|
1006 | n/a | |
---|
1007 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
1008 | n/a | } |
---|
1009 | n/a | |
---|
1010 | n/a | if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) { |
---|
1011 | n/a | PyErr_Clear(); |
---|
1012 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
1013 | n/a | } |
---|
1014 | n/a | self_size = self_bytes.len; |
---|
1015 | n/a | |
---|
1016 | n/a | if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) { |
---|
1017 | n/a | PyErr_Clear(); |
---|
1018 | n/a | PyBuffer_Release(&self_bytes); |
---|
1019 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
1020 | n/a | } |
---|
1021 | n/a | other_size = other_bytes.len; |
---|
1022 | n/a | |
---|
1023 | n/a | if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { |
---|
1024 | n/a | /* Shortcut: if the lengths differ, the objects differ */ |
---|
1025 | n/a | cmp = (op == Py_NE); |
---|
1026 | n/a | } |
---|
1027 | n/a | else { |
---|
1028 | n/a | minsize = self_size; |
---|
1029 | n/a | if (other_size < minsize) |
---|
1030 | n/a | minsize = other_size; |
---|
1031 | n/a | |
---|
1032 | n/a | cmp = memcmp(self_bytes.buf, other_bytes.buf, minsize); |
---|
1033 | n/a | /* In ISO C, memcmp() guarantees to use unsigned bytes! */ |
---|
1034 | n/a | |
---|
1035 | n/a | if (cmp == 0) { |
---|
1036 | n/a | if (self_size < other_size) |
---|
1037 | n/a | cmp = -1; |
---|
1038 | n/a | else if (self_size > other_size) |
---|
1039 | n/a | cmp = 1; |
---|
1040 | n/a | } |
---|
1041 | n/a | |
---|
1042 | n/a | switch (op) { |
---|
1043 | n/a | case Py_LT: cmp = cmp < 0; break; |
---|
1044 | n/a | case Py_LE: cmp = cmp <= 0; break; |
---|
1045 | n/a | case Py_EQ: cmp = cmp == 0; break; |
---|
1046 | n/a | case Py_NE: cmp = cmp != 0; break; |
---|
1047 | n/a | case Py_GT: cmp = cmp > 0; break; |
---|
1048 | n/a | case Py_GE: cmp = cmp >= 0; break; |
---|
1049 | n/a | } |
---|
1050 | n/a | } |
---|
1051 | n/a | |
---|
1052 | n/a | res = cmp ? Py_True : Py_False; |
---|
1053 | n/a | PyBuffer_Release(&self_bytes); |
---|
1054 | n/a | PyBuffer_Release(&other_bytes); |
---|
1055 | n/a | Py_INCREF(res); |
---|
1056 | n/a | return res; |
---|
1057 | n/a | } |
---|
1058 | n/a | |
---|
1059 | n/a | static void |
---|
1060 | n/a | bytearray_dealloc(PyByteArrayObject *self) |
---|
1061 | n/a | { |
---|
1062 | n/a | if (self->ob_exports > 0) { |
---|
1063 | n/a | PyErr_SetString(PyExc_SystemError, |
---|
1064 | n/a | "deallocated bytearray object has exported buffers"); |
---|
1065 | n/a | PyErr_Print(); |
---|
1066 | n/a | } |
---|
1067 | n/a | if (self->ob_bytes != 0) { |
---|
1068 | n/a | PyObject_Free(self->ob_bytes); |
---|
1069 | n/a | } |
---|
1070 | n/a | Py_TYPE(self)->tp_free((PyObject *)self); |
---|
1071 | n/a | } |
---|
1072 | n/a | |
---|
1073 | n/a | |
---|
1074 | n/a | /* -------------------------------------------------------------------- */ |
---|
1075 | n/a | /* Methods */ |
---|
1076 | n/a | |
---|
1077 | n/a | #define FASTSEARCH fastsearch |
---|
1078 | n/a | #define STRINGLIB(F) stringlib_##F |
---|
1079 | n/a | #define STRINGLIB_CHAR char |
---|
1080 | n/a | #define STRINGLIB_SIZEOF_CHAR 1 |
---|
1081 | n/a | #define STRINGLIB_LEN PyByteArray_GET_SIZE |
---|
1082 | n/a | #define STRINGLIB_STR PyByteArray_AS_STRING |
---|
1083 | n/a | #define STRINGLIB_NEW PyByteArray_FromStringAndSize |
---|
1084 | n/a | #define STRINGLIB_ISSPACE Py_ISSPACE |
---|
1085 | n/a | #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) |
---|
1086 | n/a | #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact |
---|
1087 | n/a | #define STRINGLIB_MUTABLE 1 |
---|
1088 | n/a | |
---|
1089 | n/a | #include "stringlib/fastsearch.h" |
---|
1090 | n/a | #include "stringlib/count.h" |
---|
1091 | n/a | #include "stringlib/find.h" |
---|
1092 | n/a | #include "stringlib/join.h" |
---|
1093 | n/a | #include "stringlib/partition.h" |
---|
1094 | n/a | #include "stringlib/split.h" |
---|
1095 | n/a | #include "stringlib/ctype.h" |
---|
1096 | n/a | #include "stringlib/transmogrify.h" |
---|
1097 | n/a | |
---|
1098 | n/a | |
---|
1099 | n/a | static PyObject * |
---|
1100 | n/a | bytearray_find(PyByteArrayObject *self, PyObject *args) |
---|
1101 | n/a | { |
---|
1102 | n/a | return _Py_bytes_find(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
---|
1103 | n/a | } |
---|
1104 | n/a | |
---|
1105 | n/a | static PyObject * |
---|
1106 | n/a | bytearray_count(PyByteArrayObject *self, PyObject *args) |
---|
1107 | n/a | { |
---|
1108 | n/a | return _Py_bytes_count(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
---|
1109 | n/a | } |
---|
1110 | n/a | |
---|
1111 | n/a | /*[clinic input] |
---|
1112 | n/a | bytearray.clear |
---|
1113 | n/a | |
---|
1114 | n/a | Remove all items from the bytearray. |
---|
1115 | n/a | [clinic start generated code]*/ |
---|
1116 | n/a | |
---|
1117 | n/a | static PyObject * |
---|
1118 | n/a | bytearray_clear_impl(PyByteArrayObject *self) |
---|
1119 | n/a | /*[clinic end generated code: output=85c2fe6aede0956c input=ed6edae9de447ac4]*/ |
---|
1120 | n/a | { |
---|
1121 | n/a | if (PyByteArray_Resize((PyObject *)self, 0) < 0) |
---|
1122 | n/a | return NULL; |
---|
1123 | n/a | Py_RETURN_NONE; |
---|
1124 | n/a | } |
---|
1125 | n/a | |
---|
1126 | n/a | /*[clinic input] |
---|
1127 | n/a | bytearray.copy |
---|
1128 | n/a | |
---|
1129 | n/a | Return a copy of B. |
---|
1130 | n/a | [clinic start generated code]*/ |
---|
1131 | n/a | |
---|
1132 | n/a | static PyObject * |
---|
1133 | n/a | bytearray_copy_impl(PyByteArrayObject *self) |
---|
1134 | n/a | /*[clinic end generated code: output=68cfbcfed484c132 input=6597b0c01bccaa9e]*/ |
---|
1135 | n/a | { |
---|
1136 | n/a | return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self), |
---|
1137 | n/a | PyByteArray_GET_SIZE(self)); |
---|
1138 | n/a | } |
---|
1139 | n/a | |
---|
1140 | n/a | static PyObject * |
---|
1141 | n/a | bytearray_index(PyByteArrayObject *self, PyObject *args) |
---|
1142 | n/a | { |
---|
1143 | n/a | return _Py_bytes_index(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
---|
1144 | n/a | } |
---|
1145 | n/a | |
---|
1146 | n/a | static PyObject * |
---|
1147 | n/a | bytearray_rfind(PyByteArrayObject *self, PyObject *args) |
---|
1148 | n/a | { |
---|
1149 | n/a | return _Py_bytes_rfind(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
---|
1150 | n/a | } |
---|
1151 | n/a | |
---|
1152 | n/a | static PyObject * |
---|
1153 | n/a | bytearray_rindex(PyByteArrayObject *self, PyObject *args) |
---|
1154 | n/a | { |
---|
1155 | n/a | return _Py_bytes_rindex(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
---|
1156 | n/a | } |
---|
1157 | n/a | |
---|
1158 | n/a | static int |
---|
1159 | n/a | bytearray_contains(PyObject *self, PyObject *arg) |
---|
1160 | n/a | { |
---|
1161 | n/a | return _Py_bytes_contains(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), arg); |
---|
1162 | n/a | } |
---|
1163 | n/a | |
---|
1164 | n/a | static PyObject * |
---|
1165 | n/a | bytearray_startswith(PyByteArrayObject *self, PyObject *args) |
---|
1166 | n/a | { |
---|
1167 | n/a | return _Py_bytes_startswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
---|
1168 | n/a | } |
---|
1169 | n/a | |
---|
1170 | n/a | static PyObject * |
---|
1171 | n/a | bytearray_endswith(PyByteArrayObject *self, PyObject *args) |
---|
1172 | n/a | { |
---|
1173 | n/a | return _Py_bytes_endswith(PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), args); |
---|
1174 | n/a | } |
---|
1175 | n/a | |
---|
1176 | n/a | |
---|
1177 | n/a | /*[clinic input] |
---|
1178 | n/a | bytearray.translate |
---|
1179 | n/a | |
---|
1180 | n/a | table: object |
---|
1181 | n/a | Translation table, which must be a bytes object of length 256. |
---|
1182 | n/a | / |
---|
1183 | n/a | delete as deletechars: object(c_default="NULL") = b'' |
---|
1184 | n/a | |
---|
1185 | n/a | Return a copy with each character mapped by the given translation table. |
---|
1186 | n/a | |
---|
1187 | n/a | All characters occurring in the optional argument delete are removed. |
---|
1188 | n/a | The remaining characters are mapped through the given translation table. |
---|
1189 | n/a | [clinic start generated code]*/ |
---|
1190 | n/a | |
---|
1191 | n/a | static PyObject * |
---|
1192 | n/a | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
---|
1193 | n/a | PyObject *deletechars) |
---|
1194 | n/a | /*[clinic end generated code: output=b6a8f01c2a74e446 input=cfff956d4d127a9b]*/ |
---|
1195 | n/a | { |
---|
1196 | n/a | char *input, *output; |
---|
1197 | n/a | const char *table_chars; |
---|
1198 | n/a | Py_ssize_t i, c; |
---|
1199 | n/a | PyObject *input_obj = (PyObject*)self; |
---|
1200 | n/a | const char *output_start; |
---|
1201 | n/a | Py_ssize_t inlen; |
---|
1202 | n/a | PyObject *result = NULL; |
---|
1203 | n/a | int trans_table[256]; |
---|
1204 | n/a | Py_buffer vtable, vdel; |
---|
1205 | n/a | |
---|
1206 | n/a | if (table == Py_None) { |
---|
1207 | n/a | table_chars = NULL; |
---|
1208 | n/a | table = NULL; |
---|
1209 | n/a | } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) { |
---|
1210 | n/a | return NULL; |
---|
1211 | n/a | } else { |
---|
1212 | n/a | if (vtable.len != 256) { |
---|
1213 | n/a | PyErr_SetString(PyExc_ValueError, |
---|
1214 | n/a | "translation table must be 256 characters long"); |
---|
1215 | n/a | PyBuffer_Release(&vtable); |
---|
1216 | n/a | return NULL; |
---|
1217 | n/a | } |
---|
1218 | n/a | table_chars = (const char*)vtable.buf; |
---|
1219 | n/a | } |
---|
1220 | n/a | |
---|
1221 | n/a | if (deletechars != NULL) { |
---|
1222 | n/a | if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) { |
---|
1223 | n/a | if (table != NULL) |
---|
1224 | n/a | PyBuffer_Release(&vtable); |
---|
1225 | n/a | return NULL; |
---|
1226 | n/a | } |
---|
1227 | n/a | } |
---|
1228 | n/a | else { |
---|
1229 | n/a | vdel.buf = NULL; |
---|
1230 | n/a | vdel.len = 0; |
---|
1231 | n/a | } |
---|
1232 | n/a | |
---|
1233 | n/a | inlen = PyByteArray_GET_SIZE(input_obj); |
---|
1234 | n/a | result = PyByteArray_FromStringAndSize((char *)NULL, inlen); |
---|
1235 | n/a | if (result == NULL) |
---|
1236 | n/a | goto done; |
---|
1237 | n/a | output_start = output = PyByteArray_AS_STRING(result); |
---|
1238 | n/a | input = PyByteArray_AS_STRING(input_obj); |
---|
1239 | n/a | |
---|
1240 | n/a | if (vdel.len == 0 && table_chars != NULL) { |
---|
1241 | n/a | /* If no deletions are required, use faster code */ |
---|
1242 | n/a | for (i = inlen; --i >= 0; ) { |
---|
1243 | n/a | c = Py_CHARMASK(*input++); |
---|
1244 | n/a | *output++ = table_chars[c]; |
---|
1245 | n/a | } |
---|
1246 | n/a | goto done; |
---|
1247 | n/a | } |
---|
1248 | n/a | |
---|
1249 | n/a | if (table_chars == NULL) { |
---|
1250 | n/a | for (i = 0; i < 256; i++) |
---|
1251 | n/a | trans_table[i] = Py_CHARMASK(i); |
---|
1252 | n/a | } else { |
---|
1253 | n/a | for (i = 0; i < 256; i++) |
---|
1254 | n/a | trans_table[i] = Py_CHARMASK(table_chars[i]); |
---|
1255 | n/a | } |
---|
1256 | n/a | |
---|
1257 | n/a | for (i = 0; i < vdel.len; i++) |
---|
1258 | n/a | trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; |
---|
1259 | n/a | |
---|
1260 | n/a | for (i = inlen; --i >= 0; ) { |
---|
1261 | n/a | c = Py_CHARMASK(*input++); |
---|
1262 | n/a | if (trans_table[c] != -1) |
---|
1263 | n/a | *output++ = (char)trans_table[c]; |
---|
1264 | n/a | } |
---|
1265 | n/a | /* Fix the size of the resulting string */ |
---|
1266 | n/a | if (inlen > 0) |
---|
1267 | n/a | if (PyByteArray_Resize(result, output - output_start) < 0) { |
---|
1268 | n/a | Py_CLEAR(result); |
---|
1269 | n/a | goto done; |
---|
1270 | n/a | } |
---|
1271 | n/a | |
---|
1272 | n/a | done: |
---|
1273 | n/a | if (table != NULL) |
---|
1274 | n/a | PyBuffer_Release(&vtable); |
---|
1275 | n/a | if (deletechars != NULL) |
---|
1276 | n/a | PyBuffer_Release(&vdel); |
---|
1277 | n/a | return result; |
---|
1278 | n/a | } |
---|
1279 | n/a | |
---|
1280 | n/a | |
---|
1281 | n/a | /*[clinic input] |
---|
1282 | n/a | |
---|
1283 | n/a | @staticmethod |
---|
1284 | n/a | bytearray.maketrans |
---|
1285 | n/a | |
---|
1286 | n/a | frm: Py_buffer |
---|
1287 | n/a | to: Py_buffer |
---|
1288 | n/a | / |
---|
1289 | n/a | |
---|
1290 | n/a | Return a translation table useable for the bytes or bytearray translate method. |
---|
1291 | n/a | |
---|
1292 | n/a | The returned table will be one where each byte in frm is mapped to the byte at |
---|
1293 | n/a | the same position in to. |
---|
1294 | n/a | |
---|
1295 | n/a | The bytes objects frm and to must be of the same length. |
---|
1296 | n/a | [clinic start generated code]*/ |
---|
1297 | n/a | |
---|
1298 | n/a | static PyObject * |
---|
1299 | n/a | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to) |
---|
1300 | n/a | /*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/ |
---|
1301 | n/a | { |
---|
1302 | n/a | return _Py_bytes_maketrans(frm, to); |
---|
1303 | n/a | } |
---|
1304 | n/a | |
---|
1305 | n/a | |
---|
1306 | n/a | /*[clinic input] |
---|
1307 | n/a | bytearray.replace |
---|
1308 | n/a | |
---|
1309 | n/a | old: Py_buffer |
---|
1310 | n/a | new: Py_buffer |
---|
1311 | n/a | count: Py_ssize_t = -1 |
---|
1312 | n/a | Maximum number of occurrences to replace. |
---|
1313 | n/a | -1 (the default value) means replace all occurrences. |
---|
1314 | n/a | / |
---|
1315 | n/a | |
---|
1316 | n/a | Return a copy with all occurrences of substring old replaced by new. |
---|
1317 | n/a | |
---|
1318 | n/a | If the optional argument count is given, only the first count occurrences are |
---|
1319 | n/a | replaced. |
---|
1320 | n/a | [clinic start generated code]*/ |
---|
1321 | n/a | |
---|
1322 | n/a | static PyObject * |
---|
1323 | n/a | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
---|
1324 | n/a | Py_buffer *new, Py_ssize_t count) |
---|
1325 | n/a | /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/ |
---|
1326 | n/a | { |
---|
1327 | n/a | return stringlib_replace((PyObject *)self, |
---|
1328 | n/a | (const char *)old->buf, old->len, |
---|
1329 | n/a | (const char *)new->buf, new->len, count); |
---|
1330 | n/a | } |
---|
1331 | n/a | |
---|
1332 | n/a | /*[clinic input] |
---|
1333 | n/a | bytearray.split |
---|
1334 | n/a | |
---|
1335 | n/a | sep: object = None |
---|
1336 | n/a | The delimiter according which to split the bytearray. |
---|
1337 | n/a | None (the default value) means split on ASCII whitespace characters |
---|
1338 | n/a | (space, tab, return, newline, formfeed, vertical tab). |
---|
1339 | n/a | maxsplit: Py_ssize_t = -1 |
---|
1340 | n/a | Maximum number of splits to do. |
---|
1341 | n/a | -1 (the default value) means no limit. |
---|
1342 | n/a | |
---|
1343 | n/a | Return a list of the sections in the bytearray, using sep as the delimiter. |
---|
1344 | n/a | [clinic start generated code]*/ |
---|
1345 | n/a | |
---|
1346 | n/a | static PyObject * |
---|
1347 | n/a | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
---|
1348 | n/a | Py_ssize_t maxsplit) |
---|
1349 | n/a | /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/ |
---|
1350 | n/a | { |
---|
1351 | n/a | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
---|
1352 | n/a | const char *s = PyByteArray_AS_STRING(self), *sub; |
---|
1353 | n/a | PyObject *list; |
---|
1354 | n/a | Py_buffer vsub; |
---|
1355 | n/a | |
---|
1356 | n/a | if (maxsplit < 0) |
---|
1357 | n/a | maxsplit = PY_SSIZE_T_MAX; |
---|
1358 | n/a | |
---|
1359 | n/a | if (sep == Py_None) |
---|
1360 | n/a | return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); |
---|
1361 | n/a | |
---|
1362 | n/a | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
---|
1363 | n/a | return NULL; |
---|
1364 | n/a | sub = vsub.buf; |
---|
1365 | n/a | n = vsub.len; |
---|
1366 | n/a | |
---|
1367 | n/a | list = stringlib_split( |
---|
1368 | n/a | (PyObject*) self, s, len, sub, n, maxsplit |
---|
1369 | n/a | ); |
---|
1370 | n/a | PyBuffer_Release(&vsub); |
---|
1371 | n/a | return list; |
---|
1372 | n/a | } |
---|
1373 | n/a | |
---|
1374 | n/a | /*[clinic input] |
---|
1375 | n/a | bytearray.partition |
---|
1376 | n/a | |
---|
1377 | n/a | sep: object |
---|
1378 | n/a | / |
---|
1379 | n/a | |
---|
1380 | n/a | Partition the bytearray into three parts using the given separator. |
---|
1381 | n/a | |
---|
1382 | n/a | This will search for the separator sep in the bytearray. If the separator is |
---|
1383 | n/a | found, returns a 3-tuple containing the part before the separator, the |
---|
1384 | n/a | separator itself, and the part after it. |
---|
1385 | n/a | |
---|
1386 | n/a | If the separator is not found, returns a 3-tuple containing the original |
---|
1387 | n/a | bytearray object and two empty bytearray objects. |
---|
1388 | n/a | [clinic start generated code]*/ |
---|
1389 | n/a | |
---|
1390 | n/a | static PyObject * |
---|
1391 | n/a | bytearray_partition(PyByteArrayObject *self, PyObject *sep) |
---|
1392 | n/a | /*[clinic end generated code: output=45d2525ddd35f957 input=86f89223892b70b5]*/ |
---|
1393 | n/a | { |
---|
1394 | n/a | PyObject *bytesep, *result; |
---|
1395 | n/a | |
---|
1396 | n/a | bytesep = PyByteArray_FromObject(sep); |
---|
1397 | n/a | if (! bytesep) |
---|
1398 | n/a | return NULL; |
---|
1399 | n/a | |
---|
1400 | n/a | result = stringlib_partition( |
---|
1401 | n/a | (PyObject*) self, |
---|
1402 | n/a | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
---|
1403 | n/a | bytesep, |
---|
1404 | n/a | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
---|
1405 | n/a | ); |
---|
1406 | n/a | |
---|
1407 | n/a | Py_DECREF(bytesep); |
---|
1408 | n/a | return result; |
---|
1409 | n/a | } |
---|
1410 | n/a | |
---|
1411 | n/a | /*[clinic input] |
---|
1412 | n/a | bytearray.rpartition |
---|
1413 | n/a | |
---|
1414 | n/a | sep: object |
---|
1415 | n/a | / |
---|
1416 | n/a | |
---|
1417 | n/a | Partition the bytes into three parts using the given separator. |
---|
1418 | n/a | |
---|
1419 | n/a | This will search for the separator sep in the bytearray, starting and the end. |
---|
1420 | n/a | If the separator is found, returns a 3-tuple containing the part before the |
---|
1421 | n/a | separator, the separator itself, and the part after it. |
---|
1422 | n/a | |
---|
1423 | n/a | If the separator is not found, returns a 3-tuple containing two empty bytearray |
---|
1424 | n/a | objects and the original bytearray object. |
---|
1425 | n/a | [clinic start generated code]*/ |
---|
1426 | n/a | |
---|
1427 | n/a | static PyObject * |
---|
1428 | n/a | bytearray_rpartition(PyByteArrayObject *self, PyObject *sep) |
---|
1429 | n/a | /*[clinic end generated code: output=440de3c9426115e8 input=5f4094f2de87c8f3]*/ |
---|
1430 | n/a | { |
---|
1431 | n/a | PyObject *bytesep, *result; |
---|
1432 | n/a | |
---|
1433 | n/a | bytesep = PyByteArray_FromObject(sep); |
---|
1434 | n/a | if (! bytesep) |
---|
1435 | n/a | return NULL; |
---|
1436 | n/a | |
---|
1437 | n/a | result = stringlib_rpartition( |
---|
1438 | n/a | (PyObject*) self, |
---|
1439 | n/a | PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), |
---|
1440 | n/a | bytesep, |
---|
1441 | n/a | PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) |
---|
1442 | n/a | ); |
---|
1443 | n/a | |
---|
1444 | n/a | Py_DECREF(bytesep); |
---|
1445 | n/a | return result; |
---|
1446 | n/a | } |
---|
1447 | n/a | |
---|
1448 | n/a | /*[clinic input] |
---|
1449 | n/a | bytearray.rsplit = bytearray.split |
---|
1450 | n/a | |
---|
1451 | n/a | Return a list of the sections in the bytearray, using sep as the delimiter. |
---|
1452 | n/a | |
---|
1453 | n/a | Splitting is done starting at the end of the bytearray and working to the front. |
---|
1454 | n/a | [clinic start generated code]*/ |
---|
1455 | n/a | |
---|
1456 | n/a | static PyObject * |
---|
1457 | n/a | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
---|
1458 | n/a | Py_ssize_t maxsplit) |
---|
1459 | n/a | /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/ |
---|
1460 | n/a | { |
---|
1461 | n/a | Py_ssize_t len = PyByteArray_GET_SIZE(self), n; |
---|
1462 | n/a | const char *s = PyByteArray_AS_STRING(self), *sub; |
---|
1463 | n/a | PyObject *list; |
---|
1464 | n/a | Py_buffer vsub; |
---|
1465 | n/a | |
---|
1466 | n/a | if (maxsplit < 0) |
---|
1467 | n/a | maxsplit = PY_SSIZE_T_MAX; |
---|
1468 | n/a | |
---|
1469 | n/a | if (sep == Py_None) |
---|
1470 | n/a | return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); |
---|
1471 | n/a | |
---|
1472 | n/a | if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0) |
---|
1473 | n/a | return NULL; |
---|
1474 | n/a | sub = vsub.buf; |
---|
1475 | n/a | n = vsub.len; |
---|
1476 | n/a | |
---|
1477 | n/a | list = stringlib_rsplit( |
---|
1478 | n/a | (PyObject*) self, s, len, sub, n, maxsplit |
---|
1479 | n/a | ); |
---|
1480 | n/a | PyBuffer_Release(&vsub); |
---|
1481 | n/a | return list; |
---|
1482 | n/a | } |
---|
1483 | n/a | |
---|
1484 | n/a | /*[clinic input] |
---|
1485 | n/a | bytearray.reverse |
---|
1486 | n/a | |
---|
1487 | n/a | Reverse the order of the values in B in place. |
---|
1488 | n/a | [clinic start generated code]*/ |
---|
1489 | n/a | |
---|
1490 | n/a | static PyObject * |
---|
1491 | n/a | bytearray_reverse_impl(PyByteArrayObject *self) |
---|
1492 | n/a | /*[clinic end generated code: output=9f7616f29ab309d3 input=543356319fc78557]*/ |
---|
1493 | n/a | { |
---|
1494 | n/a | char swap, *head, *tail; |
---|
1495 | n/a | Py_ssize_t i, j, n = Py_SIZE(self); |
---|
1496 | n/a | |
---|
1497 | n/a | j = n / 2; |
---|
1498 | n/a | head = PyByteArray_AS_STRING(self); |
---|
1499 | n/a | tail = head + n - 1; |
---|
1500 | n/a | for (i = 0; i < j; i++) { |
---|
1501 | n/a | swap = *head; |
---|
1502 | n/a | *head++ = *tail; |
---|
1503 | n/a | *tail-- = swap; |
---|
1504 | n/a | } |
---|
1505 | n/a | |
---|
1506 | n/a | Py_RETURN_NONE; |
---|
1507 | n/a | } |
---|
1508 | n/a | |
---|
1509 | n/a | |
---|
1510 | n/a | /*[python input] |
---|
1511 | n/a | class bytesvalue_converter(CConverter): |
---|
1512 | n/a | type = 'int' |
---|
1513 | n/a | converter = '_getbytevalue' |
---|
1514 | n/a | [python start generated code]*/ |
---|
1515 | n/a | /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/ |
---|
1516 | n/a | |
---|
1517 | n/a | |
---|
1518 | n/a | /*[clinic input] |
---|
1519 | n/a | bytearray.insert |
---|
1520 | n/a | |
---|
1521 | n/a | index: Py_ssize_t |
---|
1522 | n/a | The index where the value is to be inserted. |
---|
1523 | n/a | item: bytesvalue |
---|
1524 | n/a | The item to be inserted. |
---|
1525 | n/a | / |
---|
1526 | n/a | |
---|
1527 | n/a | Insert a single item into the bytearray before the given index. |
---|
1528 | n/a | [clinic start generated code]*/ |
---|
1529 | n/a | |
---|
1530 | n/a | static PyObject * |
---|
1531 | n/a | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item) |
---|
1532 | n/a | /*[clinic end generated code: output=76c775a70e7b07b7 input=b2b5d07e9de6c070]*/ |
---|
1533 | n/a | { |
---|
1534 | n/a | Py_ssize_t n = Py_SIZE(self); |
---|
1535 | n/a | char *buf; |
---|
1536 | n/a | |
---|
1537 | n/a | if (n == PY_SSIZE_T_MAX) { |
---|
1538 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
1539 | n/a | "cannot add more objects to bytearray"); |
---|
1540 | n/a | return NULL; |
---|
1541 | n/a | } |
---|
1542 | n/a | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
---|
1543 | n/a | return NULL; |
---|
1544 | n/a | buf = PyByteArray_AS_STRING(self); |
---|
1545 | n/a | |
---|
1546 | n/a | if (index < 0) { |
---|
1547 | n/a | index += n; |
---|
1548 | n/a | if (index < 0) |
---|
1549 | n/a | index = 0; |
---|
1550 | n/a | } |
---|
1551 | n/a | if (index > n) |
---|
1552 | n/a | index = n; |
---|
1553 | n/a | memmove(buf + index + 1, buf + index, n - index); |
---|
1554 | n/a | buf[index] = item; |
---|
1555 | n/a | |
---|
1556 | n/a | Py_RETURN_NONE; |
---|
1557 | n/a | } |
---|
1558 | n/a | |
---|
1559 | n/a | /*[clinic input] |
---|
1560 | n/a | bytearray.append |
---|
1561 | n/a | |
---|
1562 | n/a | item: bytesvalue |
---|
1563 | n/a | The item to be appended. |
---|
1564 | n/a | / |
---|
1565 | n/a | |
---|
1566 | n/a | Append a single item to the end of the bytearray. |
---|
1567 | n/a | [clinic start generated code]*/ |
---|
1568 | n/a | |
---|
1569 | n/a | static PyObject * |
---|
1570 | n/a | bytearray_append_impl(PyByteArrayObject *self, int item) |
---|
1571 | n/a | /*[clinic end generated code: output=a154e19ed1886cb6 input=20d6bec3d1340593]*/ |
---|
1572 | n/a | { |
---|
1573 | n/a | Py_ssize_t n = Py_SIZE(self); |
---|
1574 | n/a | |
---|
1575 | n/a | if (n == PY_SSIZE_T_MAX) { |
---|
1576 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
1577 | n/a | "cannot add more objects to bytearray"); |
---|
1578 | n/a | return NULL; |
---|
1579 | n/a | } |
---|
1580 | n/a | if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) |
---|
1581 | n/a | return NULL; |
---|
1582 | n/a | |
---|
1583 | n/a | PyByteArray_AS_STRING(self)[n] = item; |
---|
1584 | n/a | |
---|
1585 | n/a | Py_RETURN_NONE; |
---|
1586 | n/a | } |
---|
1587 | n/a | |
---|
1588 | n/a | /*[clinic input] |
---|
1589 | n/a | bytearray.extend |
---|
1590 | n/a | |
---|
1591 | n/a | iterable_of_ints: object |
---|
1592 | n/a | The iterable of items to append. |
---|
1593 | n/a | / |
---|
1594 | n/a | |
---|
1595 | n/a | Append all the items from the iterator or sequence to the end of the bytearray. |
---|
1596 | n/a | [clinic start generated code]*/ |
---|
1597 | n/a | |
---|
1598 | n/a | static PyObject * |
---|
1599 | n/a | bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints) |
---|
1600 | n/a | /*[clinic end generated code: output=98155dbe249170b1 input=c617b3a93249ba28]*/ |
---|
1601 | n/a | { |
---|
1602 | n/a | PyObject *it, *item, *bytearray_obj; |
---|
1603 | n/a | Py_ssize_t buf_size = 0, len = 0; |
---|
1604 | n/a | int value; |
---|
1605 | n/a | char *buf; |
---|
1606 | n/a | |
---|
1607 | n/a | /* bytearray_setslice code only accepts something supporting PEP 3118. */ |
---|
1608 | n/a | if (PyObject_CheckBuffer(iterable_of_ints)) { |
---|
1609 | n/a | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_ints) == -1) |
---|
1610 | n/a | return NULL; |
---|
1611 | n/a | |
---|
1612 | n/a | Py_RETURN_NONE; |
---|
1613 | n/a | } |
---|
1614 | n/a | |
---|
1615 | n/a | it = PyObject_GetIter(iterable_of_ints); |
---|
1616 | n/a | if (it == NULL) |
---|
1617 | n/a | return NULL; |
---|
1618 | n/a | |
---|
1619 | n/a | /* Try to determine the length of the argument. 32 is arbitrary. */ |
---|
1620 | n/a | buf_size = PyObject_LengthHint(iterable_of_ints, 32); |
---|
1621 | n/a | if (buf_size == -1) { |
---|
1622 | n/a | Py_DECREF(it); |
---|
1623 | n/a | return NULL; |
---|
1624 | n/a | } |
---|
1625 | n/a | |
---|
1626 | n/a | bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); |
---|
1627 | n/a | if (bytearray_obj == NULL) { |
---|
1628 | n/a | Py_DECREF(it); |
---|
1629 | n/a | return NULL; |
---|
1630 | n/a | } |
---|
1631 | n/a | buf = PyByteArray_AS_STRING(bytearray_obj); |
---|
1632 | n/a | |
---|
1633 | n/a | while ((item = PyIter_Next(it)) != NULL) { |
---|
1634 | n/a | if (! _getbytevalue(item, &value)) { |
---|
1635 | n/a | Py_DECREF(item); |
---|
1636 | n/a | Py_DECREF(it); |
---|
1637 | n/a | Py_DECREF(bytearray_obj); |
---|
1638 | n/a | return NULL; |
---|
1639 | n/a | } |
---|
1640 | n/a | buf[len++] = value; |
---|
1641 | n/a | Py_DECREF(item); |
---|
1642 | n/a | |
---|
1643 | n/a | if (len >= buf_size) { |
---|
1644 | n/a | Py_ssize_t addition; |
---|
1645 | n/a | if (len == PY_SSIZE_T_MAX) { |
---|
1646 | n/a | Py_DECREF(it); |
---|
1647 | n/a | Py_DECREF(bytearray_obj); |
---|
1648 | n/a | return PyErr_NoMemory(); |
---|
1649 | n/a | } |
---|
1650 | n/a | addition = len >> 1; |
---|
1651 | n/a | if (addition > PY_SSIZE_T_MAX - len - 1) |
---|
1652 | n/a | buf_size = PY_SSIZE_T_MAX; |
---|
1653 | n/a | else |
---|
1654 | n/a | buf_size = len + addition + 1; |
---|
1655 | n/a | if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { |
---|
1656 | n/a | Py_DECREF(it); |
---|
1657 | n/a | Py_DECREF(bytearray_obj); |
---|
1658 | n/a | return NULL; |
---|
1659 | n/a | } |
---|
1660 | n/a | /* Recompute the `buf' pointer, since the resizing operation may |
---|
1661 | n/a | have invalidated it. */ |
---|
1662 | n/a | buf = PyByteArray_AS_STRING(bytearray_obj); |
---|
1663 | n/a | } |
---|
1664 | n/a | } |
---|
1665 | n/a | Py_DECREF(it); |
---|
1666 | n/a | |
---|
1667 | n/a | /* Resize down to exact size. */ |
---|
1668 | n/a | if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { |
---|
1669 | n/a | Py_DECREF(bytearray_obj); |
---|
1670 | n/a | return NULL; |
---|
1671 | n/a | } |
---|
1672 | n/a | |
---|
1673 | n/a | if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { |
---|
1674 | n/a | Py_DECREF(bytearray_obj); |
---|
1675 | n/a | return NULL; |
---|
1676 | n/a | } |
---|
1677 | n/a | Py_DECREF(bytearray_obj); |
---|
1678 | n/a | |
---|
1679 | n/a | Py_RETURN_NONE; |
---|
1680 | n/a | } |
---|
1681 | n/a | |
---|
1682 | n/a | /*[clinic input] |
---|
1683 | n/a | bytearray.pop |
---|
1684 | n/a | |
---|
1685 | n/a | index: Py_ssize_t = -1 |
---|
1686 | n/a | The index from where to remove the item. |
---|
1687 | n/a | -1 (the default value) means remove the last item. |
---|
1688 | n/a | / |
---|
1689 | n/a | |
---|
1690 | n/a | Remove and return a single item from B. |
---|
1691 | n/a | |
---|
1692 | n/a | If no index argument is given, will pop the last item. |
---|
1693 | n/a | [clinic start generated code]*/ |
---|
1694 | n/a | |
---|
1695 | n/a | static PyObject * |
---|
1696 | n/a | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index) |
---|
1697 | n/a | /*[clinic end generated code: output=e0ccd401f8021da8 input=3591df2d06c0d237]*/ |
---|
1698 | n/a | { |
---|
1699 | n/a | int value; |
---|
1700 | n/a | Py_ssize_t n = Py_SIZE(self); |
---|
1701 | n/a | char *buf; |
---|
1702 | n/a | |
---|
1703 | n/a | if (n == 0) { |
---|
1704 | n/a | PyErr_SetString(PyExc_IndexError, |
---|
1705 | n/a | "pop from empty bytearray"); |
---|
1706 | n/a | return NULL; |
---|
1707 | n/a | } |
---|
1708 | n/a | if (index < 0) |
---|
1709 | n/a | index += Py_SIZE(self); |
---|
1710 | n/a | if (index < 0 || index >= Py_SIZE(self)) { |
---|
1711 | n/a | PyErr_SetString(PyExc_IndexError, "pop index out of range"); |
---|
1712 | n/a | return NULL; |
---|
1713 | n/a | } |
---|
1714 | n/a | if (!_canresize(self)) |
---|
1715 | n/a | return NULL; |
---|
1716 | n/a | |
---|
1717 | n/a | buf = PyByteArray_AS_STRING(self); |
---|
1718 | n/a | value = buf[index]; |
---|
1719 | n/a | memmove(buf + index, buf + index + 1, n - index); |
---|
1720 | n/a | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
---|
1721 | n/a | return NULL; |
---|
1722 | n/a | |
---|
1723 | n/a | return PyLong_FromLong((unsigned char)value); |
---|
1724 | n/a | } |
---|
1725 | n/a | |
---|
1726 | n/a | /*[clinic input] |
---|
1727 | n/a | bytearray.remove |
---|
1728 | n/a | |
---|
1729 | n/a | value: bytesvalue |
---|
1730 | n/a | The value to remove. |
---|
1731 | n/a | / |
---|
1732 | n/a | |
---|
1733 | n/a | Remove the first occurrence of a value in the bytearray. |
---|
1734 | n/a | [clinic start generated code]*/ |
---|
1735 | n/a | |
---|
1736 | n/a | static PyObject * |
---|
1737 | n/a | bytearray_remove_impl(PyByteArrayObject *self, int value) |
---|
1738 | n/a | /*[clinic end generated code: output=d659e37866709c13 input=121831240cd51ddf]*/ |
---|
1739 | n/a | { |
---|
1740 | n/a | Py_ssize_t where, n = Py_SIZE(self); |
---|
1741 | n/a | char *buf = PyByteArray_AS_STRING(self); |
---|
1742 | n/a | |
---|
1743 | n/a | where = stringlib_find_char(buf, n, value); |
---|
1744 | n/a | if (where < 0) { |
---|
1745 | n/a | PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); |
---|
1746 | n/a | return NULL; |
---|
1747 | n/a | } |
---|
1748 | n/a | if (!_canresize(self)) |
---|
1749 | n/a | return NULL; |
---|
1750 | n/a | |
---|
1751 | n/a | memmove(buf + where, buf + where + 1, n - where); |
---|
1752 | n/a | if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) |
---|
1753 | n/a | return NULL; |
---|
1754 | n/a | |
---|
1755 | n/a | Py_RETURN_NONE; |
---|
1756 | n/a | } |
---|
1757 | n/a | |
---|
1758 | n/a | /* XXX These two helpers could be optimized if argsize == 1 */ |
---|
1759 | n/a | |
---|
1760 | n/a | static Py_ssize_t |
---|
1761 | n/a | lstrip_helper(const char *myptr, Py_ssize_t mysize, |
---|
1762 | n/a | const void *argptr, Py_ssize_t argsize) |
---|
1763 | n/a | { |
---|
1764 | n/a | Py_ssize_t i = 0; |
---|
1765 | n/a | while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) |
---|
1766 | n/a | i++; |
---|
1767 | n/a | return i; |
---|
1768 | n/a | } |
---|
1769 | n/a | |
---|
1770 | n/a | static Py_ssize_t |
---|
1771 | n/a | rstrip_helper(const char *myptr, Py_ssize_t mysize, |
---|
1772 | n/a | const void *argptr, Py_ssize_t argsize) |
---|
1773 | n/a | { |
---|
1774 | n/a | Py_ssize_t i = mysize - 1; |
---|
1775 | n/a | while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) |
---|
1776 | n/a | i--; |
---|
1777 | n/a | return i + 1; |
---|
1778 | n/a | } |
---|
1779 | n/a | |
---|
1780 | n/a | /*[clinic input] |
---|
1781 | n/a | bytearray.strip |
---|
1782 | n/a | |
---|
1783 | n/a | bytes: object = None |
---|
1784 | n/a | / |
---|
1785 | n/a | |
---|
1786 | n/a | Strip leading and trailing bytes contained in the argument. |
---|
1787 | n/a | |
---|
1788 | n/a | If the argument is omitted or None, strip leading and trailing ASCII whitespace. |
---|
1789 | n/a | [clinic start generated code]*/ |
---|
1790 | n/a | |
---|
1791 | n/a | static PyObject * |
---|
1792 | n/a | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes) |
---|
1793 | n/a | /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/ |
---|
1794 | n/a | { |
---|
1795 | n/a | Py_ssize_t left, right, mysize, byteslen; |
---|
1796 | n/a | char *myptr, *bytesptr; |
---|
1797 | n/a | Py_buffer vbytes; |
---|
1798 | n/a | |
---|
1799 | n/a | if (bytes == Py_None) { |
---|
1800 | n/a | bytesptr = "\t\n\r\f\v "; |
---|
1801 | n/a | byteslen = 6; |
---|
1802 | n/a | } |
---|
1803 | n/a | else { |
---|
1804 | n/a | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
---|
1805 | n/a | return NULL; |
---|
1806 | n/a | bytesptr = (char *) vbytes.buf; |
---|
1807 | n/a | byteslen = vbytes.len; |
---|
1808 | n/a | } |
---|
1809 | n/a | myptr = PyByteArray_AS_STRING(self); |
---|
1810 | n/a | mysize = Py_SIZE(self); |
---|
1811 | n/a | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
---|
1812 | n/a | if (left == mysize) |
---|
1813 | n/a | right = left; |
---|
1814 | n/a | else |
---|
1815 | n/a | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
---|
1816 | n/a | if (bytes != Py_None) |
---|
1817 | n/a | PyBuffer_Release(&vbytes); |
---|
1818 | n/a | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
---|
1819 | n/a | } |
---|
1820 | n/a | |
---|
1821 | n/a | /*[clinic input] |
---|
1822 | n/a | bytearray.lstrip |
---|
1823 | n/a | |
---|
1824 | n/a | bytes: object = None |
---|
1825 | n/a | / |
---|
1826 | n/a | |
---|
1827 | n/a | Strip leading bytes contained in the argument. |
---|
1828 | n/a | |
---|
1829 | n/a | If the argument is omitted or None, strip leading ASCII whitespace. |
---|
1830 | n/a | [clinic start generated code]*/ |
---|
1831 | n/a | |
---|
1832 | n/a | static PyObject * |
---|
1833 | n/a | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
---|
1834 | n/a | /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/ |
---|
1835 | n/a | { |
---|
1836 | n/a | Py_ssize_t left, right, mysize, byteslen; |
---|
1837 | n/a | char *myptr, *bytesptr; |
---|
1838 | n/a | Py_buffer vbytes; |
---|
1839 | n/a | |
---|
1840 | n/a | if (bytes == Py_None) { |
---|
1841 | n/a | bytesptr = "\t\n\r\f\v "; |
---|
1842 | n/a | byteslen = 6; |
---|
1843 | n/a | } |
---|
1844 | n/a | else { |
---|
1845 | n/a | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
---|
1846 | n/a | return NULL; |
---|
1847 | n/a | bytesptr = (char *) vbytes.buf; |
---|
1848 | n/a | byteslen = vbytes.len; |
---|
1849 | n/a | } |
---|
1850 | n/a | myptr = PyByteArray_AS_STRING(self); |
---|
1851 | n/a | mysize = Py_SIZE(self); |
---|
1852 | n/a | left = lstrip_helper(myptr, mysize, bytesptr, byteslen); |
---|
1853 | n/a | right = mysize; |
---|
1854 | n/a | if (bytes != Py_None) |
---|
1855 | n/a | PyBuffer_Release(&vbytes); |
---|
1856 | n/a | return PyByteArray_FromStringAndSize(myptr + left, right - left); |
---|
1857 | n/a | } |
---|
1858 | n/a | |
---|
1859 | n/a | /*[clinic input] |
---|
1860 | n/a | bytearray.rstrip |
---|
1861 | n/a | |
---|
1862 | n/a | bytes: object = None |
---|
1863 | n/a | / |
---|
1864 | n/a | |
---|
1865 | n/a | Strip trailing bytes contained in the argument. |
---|
1866 | n/a | |
---|
1867 | n/a | If the argument is omitted or None, strip trailing ASCII whitespace. |
---|
1868 | n/a | [clinic start generated code]*/ |
---|
1869 | n/a | |
---|
1870 | n/a | static PyObject * |
---|
1871 | n/a | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes) |
---|
1872 | n/a | /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/ |
---|
1873 | n/a | { |
---|
1874 | n/a | Py_ssize_t right, mysize, byteslen; |
---|
1875 | n/a | char *myptr, *bytesptr; |
---|
1876 | n/a | Py_buffer vbytes; |
---|
1877 | n/a | |
---|
1878 | n/a | if (bytes == Py_None) { |
---|
1879 | n/a | bytesptr = "\t\n\r\f\v "; |
---|
1880 | n/a | byteslen = 6; |
---|
1881 | n/a | } |
---|
1882 | n/a | else { |
---|
1883 | n/a | if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0) |
---|
1884 | n/a | return NULL; |
---|
1885 | n/a | bytesptr = (char *) vbytes.buf; |
---|
1886 | n/a | byteslen = vbytes.len; |
---|
1887 | n/a | } |
---|
1888 | n/a | myptr = PyByteArray_AS_STRING(self); |
---|
1889 | n/a | mysize = Py_SIZE(self); |
---|
1890 | n/a | right = rstrip_helper(myptr, mysize, bytesptr, byteslen); |
---|
1891 | n/a | if (bytes != Py_None) |
---|
1892 | n/a | PyBuffer_Release(&vbytes); |
---|
1893 | n/a | return PyByteArray_FromStringAndSize(myptr, right); |
---|
1894 | n/a | } |
---|
1895 | n/a | |
---|
1896 | n/a | /*[clinic input] |
---|
1897 | n/a | bytearray.decode |
---|
1898 | n/a | |
---|
1899 | n/a | encoding: str(c_default="NULL") = 'utf-8' |
---|
1900 | n/a | The encoding with which to decode the bytearray. |
---|
1901 | n/a | errors: str(c_default="NULL") = 'strict' |
---|
1902 | n/a | The error handling scheme to use for the handling of decoding errors. |
---|
1903 | n/a | The default is 'strict' meaning that decoding errors raise a |
---|
1904 | n/a | UnicodeDecodeError. Other possible values are 'ignore' and 'replace' |
---|
1905 | n/a | as well as any other name registered with codecs.register_error that |
---|
1906 | n/a | can handle UnicodeDecodeErrors. |
---|
1907 | n/a | |
---|
1908 | n/a | Decode the bytearray using the codec registered for encoding. |
---|
1909 | n/a | [clinic start generated code]*/ |
---|
1910 | n/a | |
---|
1911 | n/a | static PyObject * |
---|
1912 | n/a | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
---|
1913 | n/a | const char *errors) |
---|
1914 | n/a | /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/ |
---|
1915 | n/a | { |
---|
1916 | n/a | if (encoding == NULL) |
---|
1917 | n/a | encoding = PyUnicode_GetDefaultEncoding(); |
---|
1918 | n/a | return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors); |
---|
1919 | n/a | } |
---|
1920 | n/a | |
---|
1921 | n/a | PyDoc_STRVAR(alloc_doc, |
---|
1922 | n/a | "B.__alloc__() -> int\n\ |
---|
1923 | n/a | \n\ |
---|
1924 | n/a | Return the number of bytes actually allocated."); |
---|
1925 | n/a | |
---|
1926 | n/a | static PyObject * |
---|
1927 | n/a | bytearray_alloc(PyByteArrayObject *self) |
---|
1928 | n/a | { |
---|
1929 | n/a | return PyLong_FromSsize_t(self->ob_alloc); |
---|
1930 | n/a | } |
---|
1931 | n/a | |
---|
1932 | n/a | /*[clinic input] |
---|
1933 | n/a | bytearray.join |
---|
1934 | n/a | |
---|
1935 | n/a | iterable_of_bytes: object |
---|
1936 | n/a | / |
---|
1937 | n/a | |
---|
1938 | n/a | Concatenate any number of bytes/bytearray objects. |
---|
1939 | n/a | |
---|
1940 | n/a | The bytearray whose method is called is inserted in between each pair. |
---|
1941 | n/a | |
---|
1942 | n/a | The result is returned as a new bytearray object. |
---|
1943 | n/a | [clinic start generated code]*/ |
---|
1944 | n/a | |
---|
1945 | n/a | static PyObject * |
---|
1946 | n/a | bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes) |
---|
1947 | n/a | /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/ |
---|
1948 | n/a | { |
---|
1949 | n/a | return stringlib_bytes_join((PyObject*)self, iterable_of_bytes); |
---|
1950 | n/a | } |
---|
1951 | n/a | |
---|
1952 | n/a | /*[clinic input] |
---|
1953 | n/a | bytearray.splitlines |
---|
1954 | n/a | |
---|
1955 | n/a | keepends: int(c_default="0") = False |
---|
1956 | n/a | |
---|
1957 | n/a | Return a list of the lines in the bytearray, breaking at line boundaries. |
---|
1958 | n/a | |
---|
1959 | n/a | Line breaks are not included in the resulting list unless keepends is given and |
---|
1960 | n/a | true. |
---|
1961 | n/a | [clinic start generated code]*/ |
---|
1962 | n/a | |
---|
1963 | n/a | static PyObject * |
---|
1964 | n/a | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends) |
---|
1965 | n/a | /*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/ |
---|
1966 | n/a | { |
---|
1967 | n/a | return stringlib_splitlines( |
---|
1968 | n/a | (PyObject*) self, PyByteArray_AS_STRING(self), |
---|
1969 | n/a | PyByteArray_GET_SIZE(self), keepends |
---|
1970 | n/a | ); |
---|
1971 | n/a | } |
---|
1972 | n/a | |
---|
1973 | n/a | /*[clinic input] |
---|
1974 | n/a | @classmethod |
---|
1975 | n/a | bytearray.fromhex |
---|
1976 | n/a | |
---|
1977 | n/a | string: unicode |
---|
1978 | n/a | / |
---|
1979 | n/a | |
---|
1980 | n/a | Create a bytearray object from a string of hexadecimal numbers. |
---|
1981 | n/a | |
---|
1982 | n/a | Spaces between two numbers are accepted. |
---|
1983 | n/a | Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef') |
---|
1984 | n/a | [clinic start generated code]*/ |
---|
1985 | n/a | |
---|
1986 | n/a | static PyObject * |
---|
1987 | n/a | bytearray_fromhex_impl(PyTypeObject *type, PyObject *string) |
---|
1988 | n/a | /*[clinic end generated code: output=8f0f0b6d30fb3ba0 input=f033a16d1fb21f48]*/ |
---|
1989 | n/a | { |
---|
1990 | n/a | PyObject *result = _PyBytes_FromHex(string, type == &PyByteArray_Type); |
---|
1991 | n/a | if (type != &PyByteArray_Type && result != NULL) { |
---|
1992 | n/a | Py_SETREF(result, PyObject_CallFunctionObjArgs((PyObject *)type, |
---|
1993 | n/a | result, NULL)); |
---|
1994 | n/a | } |
---|
1995 | n/a | return result; |
---|
1996 | n/a | } |
---|
1997 | n/a | |
---|
1998 | n/a | PyDoc_STRVAR(hex__doc__, |
---|
1999 | n/a | "B.hex() -> string\n\ |
---|
2000 | n/a | \n\ |
---|
2001 | n/a | Create a string of hexadecimal numbers from a bytearray object.\n\ |
---|
2002 | n/a | Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'."); |
---|
2003 | n/a | |
---|
2004 | n/a | static PyObject * |
---|
2005 | n/a | bytearray_hex(PyBytesObject *self) |
---|
2006 | n/a | { |
---|
2007 | n/a | char* argbuf = PyByteArray_AS_STRING(self); |
---|
2008 | n/a | Py_ssize_t arglen = PyByteArray_GET_SIZE(self); |
---|
2009 | n/a | return _Py_strhex(argbuf, arglen); |
---|
2010 | n/a | } |
---|
2011 | n/a | |
---|
2012 | n/a | static PyObject * |
---|
2013 | n/a | _common_reduce(PyByteArrayObject *self, int proto) |
---|
2014 | n/a | { |
---|
2015 | n/a | PyObject *dict; |
---|
2016 | n/a | _Py_IDENTIFIER(__dict__); |
---|
2017 | n/a | char *buf; |
---|
2018 | n/a | |
---|
2019 | n/a | dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); |
---|
2020 | n/a | if (dict == NULL) { |
---|
2021 | n/a | PyErr_Clear(); |
---|
2022 | n/a | dict = Py_None; |
---|
2023 | n/a | Py_INCREF(dict); |
---|
2024 | n/a | } |
---|
2025 | n/a | |
---|
2026 | n/a | buf = PyByteArray_AS_STRING(self); |
---|
2027 | n/a | if (proto < 3) { |
---|
2028 | n/a | /* use str based reduction for backwards compatibility with Python 2.x */ |
---|
2029 | n/a | PyObject *latin1; |
---|
2030 | n/a | if (Py_SIZE(self)) |
---|
2031 | n/a | latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL); |
---|
2032 | n/a | else |
---|
2033 | n/a | latin1 = PyUnicode_FromString(""); |
---|
2034 | n/a | return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); |
---|
2035 | n/a | } |
---|
2036 | n/a | else { |
---|
2037 | n/a | /* use more efficient byte based reduction */ |
---|
2038 | n/a | if (Py_SIZE(self)) { |
---|
2039 | n/a | return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict); |
---|
2040 | n/a | } |
---|
2041 | n/a | else { |
---|
2042 | n/a | return Py_BuildValue("(O()N)", Py_TYPE(self), dict); |
---|
2043 | n/a | } |
---|
2044 | n/a | } |
---|
2045 | n/a | } |
---|
2046 | n/a | |
---|
2047 | n/a | /*[clinic input] |
---|
2048 | n/a | bytearray.__reduce__ as bytearray_reduce |
---|
2049 | n/a | |
---|
2050 | n/a | Return state information for pickling. |
---|
2051 | n/a | [clinic start generated code]*/ |
---|
2052 | n/a | |
---|
2053 | n/a | static PyObject * |
---|
2054 | n/a | bytearray_reduce_impl(PyByteArrayObject *self) |
---|
2055 | n/a | /*[clinic end generated code: output=52bf304086464cab input=44b5737ada62dd3f]*/ |
---|
2056 | n/a | { |
---|
2057 | n/a | return _common_reduce(self, 2); |
---|
2058 | n/a | } |
---|
2059 | n/a | |
---|
2060 | n/a | /*[clinic input] |
---|
2061 | n/a | bytearray.__reduce_ex__ as bytearray_reduce_ex |
---|
2062 | n/a | |
---|
2063 | n/a | proto: int = 0 |
---|
2064 | n/a | / |
---|
2065 | n/a | |
---|
2066 | n/a | Return state information for pickling. |
---|
2067 | n/a | [clinic start generated code]*/ |
---|
2068 | n/a | |
---|
2069 | n/a | static PyObject * |
---|
2070 | n/a | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto) |
---|
2071 | n/a | /*[clinic end generated code: output=52eac33377197520 input=f129bc1a1aa151ee]*/ |
---|
2072 | n/a | { |
---|
2073 | n/a | return _common_reduce(self, proto); |
---|
2074 | n/a | } |
---|
2075 | n/a | |
---|
2076 | n/a | /*[clinic input] |
---|
2077 | n/a | bytearray.__sizeof__ as bytearray_sizeof |
---|
2078 | n/a | |
---|
2079 | n/a | Returns the size of the bytearray object in memory, in bytes. |
---|
2080 | n/a | [clinic start generated code]*/ |
---|
2081 | n/a | |
---|
2082 | n/a | static PyObject * |
---|
2083 | n/a | bytearray_sizeof_impl(PyByteArrayObject *self) |
---|
2084 | n/a | /*[clinic end generated code: output=738abdd17951c427 input=e27320fd98a4bc5a]*/ |
---|
2085 | n/a | { |
---|
2086 | n/a | Py_ssize_t res; |
---|
2087 | n/a | |
---|
2088 | n/a | res = _PyObject_SIZE(Py_TYPE(self)) + self->ob_alloc * sizeof(char); |
---|
2089 | n/a | return PyLong_FromSsize_t(res); |
---|
2090 | n/a | } |
---|
2091 | n/a | |
---|
2092 | n/a | static PySequenceMethods bytearray_as_sequence = { |
---|
2093 | n/a | (lenfunc)bytearray_length, /* sq_length */ |
---|
2094 | n/a | (binaryfunc)PyByteArray_Concat, /* sq_concat */ |
---|
2095 | n/a | (ssizeargfunc)bytearray_repeat, /* sq_repeat */ |
---|
2096 | n/a | (ssizeargfunc)bytearray_getitem, /* sq_item */ |
---|
2097 | n/a | 0, /* sq_slice */ |
---|
2098 | n/a | (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ |
---|
2099 | n/a | 0, /* sq_ass_slice */ |
---|
2100 | n/a | (objobjproc)bytearray_contains, /* sq_contains */ |
---|
2101 | n/a | (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ |
---|
2102 | n/a | (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ |
---|
2103 | n/a | }; |
---|
2104 | n/a | |
---|
2105 | n/a | static PyMappingMethods bytearray_as_mapping = { |
---|
2106 | n/a | (lenfunc)bytearray_length, |
---|
2107 | n/a | (binaryfunc)bytearray_subscript, |
---|
2108 | n/a | (objobjargproc)bytearray_ass_subscript, |
---|
2109 | n/a | }; |
---|
2110 | n/a | |
---|
2111 | n/a | static PyBufferProcs bytearray_as_buffer = { |
---|
2112 | n/a | (getbufferproc)bytearray_getbuffer, |
---|
2113 | n/a | (releasebufferproc)bytearray_releasebuffer, |
---|
2114 | n/a | }; |
---|
2115 | n/a | |
---|
2116 | n/a | static PyMethodDef |
---|
2117 | n/a | bytearray_methods[] = { |
---|
2118 | n/a | {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, |
---|
2119 | n/a | BYTEARRAY_REDUCE_METHODDEF |
---|
2120 | n/a | BYTEARRAY_REDUCE_EX_METHODDEF |
---|
2121 | n/a | BYTEARRAY_SIZEOF_METHODDEF |
---|
2122 | n/a | BYTEARRAY_APPEND_METHODDEF |
---|
2123 | n/a | {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, |
---|
2124 | n/a | _Py_capitalize__doc__}, |
---|
2125 | n/a | {"center", (PyCFunction)stringlib_center, METH_VARARGS, _Py_center__doc__}, |
---|
2126 | n/a | BYTEARRAY_CLEAR_METHODDEF |
---|
2127 | n/a | BYTEARRAY_COPY_METHODDEF |
---|
2128 | n/a | {"count", (PyCFunction)bytearray_count, METH_VARARGS, |
---|
2129 | n/a | _Py_count__doc__}, |
---|
2130 | n/a | BYTEARRAY_DECODE_METHODDEF |
---|
2131 | n/a | {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, |
---|
2132 | n/a | _Py_endswith__doc__}, |
---|
2133 | n/a | {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWORDS, |
---|
2134 | n/a | _Py_expandtabs__doc__}, |
---|
2135 | n/a | BYTEARRAY_EXTEND_METHODDEF |
---|
2136 | n/a | {"find", (PyCFunction)bytearray_find, METH_VARARGS, |
---|
2137 | n/a | _Py_find__doc__}, |
---|
2138 | n/a | BYTEARRAY_FROMHEX_METHODDEF |
---|
2139 | n/a | {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__}, |
---|
2140 | n/a | {"index", (PyCFunction)bytearray_index, METH_VARARGS, _Py_index__doc__}, |
---|
2141 | n/a | BYTEARRAY_INSERT_METHODDEF |
---|
2142 | n/a | {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, |
---|
2143 | n/a | _Py_isalnum__doc__}, |
---|
2144 | n/a | {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, |
---|
2145 | n/a | _Py_isalpha__doc__}, |
---|
2146 | n/a | {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, |
---|
2147 | n/a | _Py_isdigit__doc__}, |
---|
2148 | n/a | {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, |
---|
2149 | n/a | _Py_islower__doc__}, |
---|
2150 | n/a | {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, |
---|
2151 | n/a | _Py_isspace__doc__}, |
---|
2152 | n/a | {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, |
---|
2153 | n/a | _Py_istitle__doc__}, |
---|
2154 | n/a | {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, |
---|
2155 | n/a | _Py_isupper__doc__}, |
---|
2156 | n/a | BYTEARRAY_JOIN_METHODDEF |
---|
2157 | n/a | {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, _Py_ljust__doc__}, |
---|
2158 | n/a | {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, |
---|
2159 | n/a | BYTEARRAY_LSTRIP_METHODDEF |
---|
2160 | n/a | BYTEARRAY_MAKETRANS_METHODDEF |
---|
2161 | n/a | BYTEARRAY_PARTITION_METHODDEF |
---|
2162 | n/a | BYTEARRAY_POP_METHODDEF |
---|
2163 | n/a | BYTEARRAY_REMOVE_METHODDEF |
---|
2164 | n/a | BYTEARRAY_REPLACE_METHODDEF |
---|
2165 | n/a | BYTEARRAY_REVERSE_METHODDEF |
---|
2166 | n/a | {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, _Py_rfind__doc__}, |
---|
2167 | n/a | {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, _Py_rindex__doc__}, |
---|
2168 | n/a | {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, _Py_rjust__doc__}, |
---|
2169 | n/a | BYTEARRAY_RPARTITION_METHODDEF |
---|
2170 | n/a | BYTEARRAY_RSPLIT_METHODDEF |
---|
2171 | n/a | BYTEARRAY_RSTRIP_METHODDEF |
---|
2172 | n/a | BYTEARRAY_SPLIT_METHODDEF |
---|
2173 | n/a | BYTEARRAY_SPLITLINES_METHODDEF |
---|
2174 | n/a | {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , |
---|
2175 | n/a | _Py_startswith__doc__}, |
---|
2176 | n/a | BYTEARRAY_STRIP_METHODDEF |
---|
2177 | n/a | {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, |
---|
2178 | n/a | _Py_swapcase__doc__}, |
---|
2179 | n/a | {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, |
---|
2180 | n/a | BYTEARRAY_TRANSLATE_METHODDEF |
---|
2181 | n/a | {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, |
---|
2182 | n/a | {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, _Py_zfill__doc__}, |
---|
2183 | n/a | {NULL} |
---|
2184 | n/a | }; |
---|
2185 | n/a | |
---|
2186 | n/a | static PyObject * |
---|
2187 | n/a | bytearray_mod(PyObject *v, PyObject *w) |
---|
2188 | n/a | { |
---|
2189 | n/a | if (!PyByteArray_Check(v)) |
---|
2190 | n/a | Py_RETURN_NOTIMPLEMENTED; |
---|
2191 | n/a | return _PyBytes_FormatEx(PyByteArray_AS_STRING(v), PyByteArray_GET_SIZE(v), w, 1); |
---|
2192 | n/a | } |
---|
2193 | n/a | |
---|
2194 | n/a | static PyNumberMethods bytearray_as_number = { |
---|
2195 | n/a | 0, /*nb_add*/ |
---|
2196 | n/a | 0, /*nb_subtract*/ |
---|
2197 | n/a | 0, /*nb_multiply*/ |
---|
2198 | n/a | bytearray_mod, /*nb_remainder*/ |
---|
2199 | n/a | }; |
---|
2200 | n/a | |
---|
2201 | n/a | PyDoc_STRVAR(bytearray_doc, |
---|
2202 | n/a | "bytearray(iterable_of_ints) -> bytearray\n\ |
---|
2203 | n/a | bytearray(string, encoding[, errors]) -> bytearray\n\ |
---|
2204 | n/a | bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ |
---|
2205 | n/a | bytearray(int) -> bytes array of size given by the parameter initialized with null bytes\n\ |
---|
2206 | n/a | bytearray() -> empty bytes array\n\ |
---|
2207 | n/a | \n\ |
---|
2208 | n/a | Construct a mutable bytearray object from:\n\ |
---|
2209 | n/a | - an iterable yielding integers in range(256)\n\ |
---|
2210 | n/a | - a text string encoded using the specified encoding\n\ |
---|
2211 | n/a | - a bytes or a buffer object\n\ |
---|
2212 | n/a | - any object implementing the buffer API.\n\ |
---|
2213 | n/a | - an integer"); |
---|
2214 | n/a | |
---|
2215 | n/a | |
---|
2216 | n/a | static PyObject *bytearray_iter(PyObject *seq); |
---|
2217 | n/a | |
---|
2218 | n/a | PyTypeObject PyByteArray_Type = { |
---|
2219 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
2220 | n/a | "bytearray", |
---|
2221 | n/a | sizeof(PyByteArrayObject), |
---|
2222 | n/a | 0, |
---|
2223 | n/a | (destructor)bytearray_dealloc, /* tp_dealloc */ |
---|
2224 | n/a | 0, /* tp_print */ |
---|
2225 | n/a | 0, /* tp_getattr */ |
---|
2226 | n/a | 0, /* tp_setattr */ |
---|
2227 | n/a | 0, /* tp_reserved */ |
---|
2228 | n/a | (reprfunc)bytearray_repr, /* tp_repr */ |
---|
2229 | n/a | &bytearray_as_number, /* tp_as_number */ |
---|
2230 | n/a | &bytearray_as_sequence, /* tp_as_sequence */ |
---|
2231 | n/a | &bytearray_as_mapping, /* tp_as_mapping */ |
---|
2232 | n/a | 0, /* tp_hash */ |
---|
2233 | n/a | 0, /* tp_call */ |
---|
2234 | n/a | bytearray_str, /* tp_str */ |
---|
2235 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
2236 | n/a | 0, /* tp_setattro */ |
---|
2237 | n/a | &bytearray_as_buffer, /* tp_as_buffer */ |
---|
2238 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ |
---|
2239 | n/a | bytearray_doc, /* tp_doc */ |
---|
2240 | n/a | 0, /* tp_traverse */ |
---|
2241 | n/a | 0, /* tp_clear */ |
---|
2242 | n/a | (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ |
---|
2243 | n/a | 0, /* tp_weaklistoffset */ |
---|
2244 | n/a | bytearray_iter, /* tp_iter */ |
---|
2245 | n/a | 0, /* tp_iternext */ |
---|
2246 | n/a | bytearray_methods, /* tp_methods */ |
---|
2247 | n/a | 0, /* tp_members */ |
---|
2248 | n/a | 0, /* tp_getset */ |
---|
2249 | n/a | 0, /* tp_base */ |
---|
2250 | n/a | 0, /* tp_dict */ |
---|
2251 | n/a | 0, /* tp_descr_get */ |
---|
2252 | n/a | 0, /* tp_descr_set */ |
---|
2253 | n/a | 0, /* tp_dictoffset */ |
---|
2254 | n/a | (initproc)bytearray_init, /* tp_init */ |
---|
2255 | n/a | PyType_GenericAlloc, /* tp_alloc */ |
---|
2256 | n/a | PyType_GenericNew, /* tp_new */ |
---|
2257 | n/a | PyObject_Del, /* tp_free */ |
---|
2258 | n/a | }; |
---|
2259 | n/a | |
---|
2260 | n/a | /*********************** Bytes Iterator ****************************/ |
---|
2261 | n/a | |
---|
2262 | n/a | typedef struct { |
---|
2263 | n/a | PyObject_HEAD |
---|
2264 | n/a | Py_ssize_t it_index; |
---|
2265 | n/a | PyByteArrayObject *it_seq; /* Set to NULL when iterator is exhausted */ |
---|
2266 | n/a | } bytesiterobject; |
---|
2267 | n/a | |
---|
2268 | n/a | static void |
---|
2269 | n/a | bytearrayiter_dealloc(bytesiterobject *it) |
---|
2270 | n/a | { |
---|
2271 | n/a | _PyObject_GC_UNTRACK(it); |
---|
2272 | n/a | Py_XDECREF(it->it_seq); |
---|
2273 | n/a | PyObject_GC_Del(it); |
---|
2274 | n/a | } |
---|
2275 | n/a | |
---|
2276 | n/a | static int |
---|
2277 | n/a | bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) |
---|
2278 | n/a | { |
---|
2279 | n/a | Py_VISIT(it->it_seq); |
---|
2280 | n/a | return 0; |
---|
2281 | n/a | } |
---|
2282 | n/a | |
---|
2283 | n/a | static PyObject * |
---|
2284 | n/a | bytearrayiter_next(bytesiterobject *it) |
---|
2285 | n/a | { |
---|
2286 | n/a | PyByteArrayObject *seq; |
---|
2287 | n/a | PyObject *item; |
---|
2288 | n/a | |
---|
2289 | n/a | assert(it != NULL); |
---|
2290 | n/a | seq = it->it_seq; |
---|
2291 | n/a | if (seq == NULL) |
---|
2292 | n/a | return NULL; |
---|
2293 | n/a | assert(PyByteArray_Check(seq)); |
---|
2294 | n/a | |
---|
2295 | n/a | if (it->it_index < PyByteArray_GET_SIZE(seq)) { |
---|
2296 | n/a | item = PyLong_FromLong( |
---|
2297 | n/a | (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]); |
---|
2298 | n/a | if (item != NULL) |
---|
2299 | n/a | ++it->it_index; |
---|
2300 | n/a | return item; |
---|
2301 | n/a | } |
---|
2302 | n/a | |
---|
2303 | n/a | it->it_seq = NULL; |
---|
2304 | n/a | Py_DECREF(seq); |
---|
2305 | n/a | return NULL; |
---|
2306 | n/a | } |
---|
2307 | n/a | |
---|
2308 | n/a | static PyObject * |
---|
2309 | n/a | bytearrayiter_length_hint(bytesiterobject *it) |
---|
2310 | n/a | { |
---|
2311 | n/a | Py_ssize_t len = 0; |
---|
2312 | n/a | if (it->it_seq) { |
---|
2313 | n/a | len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; |
---|
2314 | n/a | if (len < 0) { |
---|
2315 | n/a | len = 0; |
---|
2316 | n/a | } |
---|
2317 | n/a | } |
---|
2318 | n/a | return PyLong_FromSsize_t(len); |
---|
2319 | n/a | } |
---|
2320 | n/a | |
---|
2321 | n/a | PyDoc_STRVAR(length_hint_doc, |
---|
2322 | n/a | "Private method returning an estimate of len(list(it))."); |
---|
2323 | n/a | |
---|
2324 | n/a | static PyObject * |
---|
2325 | n/a | bytearrayiter_reduce(bytesiterobject *it) |
---|
2326 | n/a | { |
---|
2327 | n/a | if (it->it_seq != NULL) { |
---|
2328 | n/a | return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"), |
---|
2329 | n/a | it->it_seq, it->it_index); |
---|
2330 | n/a | } else { |
---|
2331 | n/a | return Py_BuildValue("N(())", _PyObject_GetBuiltin("iter")); |
---|
2332 | n/a | } |
---|
2333 | n/a | } |
---|
2334 | n/a | |
---|
2335 | n/a | static PyObject * |
---|
2336 | n/a | bytearrayiter_setstate(bytesiterobject *it, PyObject *state) |
---|
2337 | n/a | { |
---|
2338 | n/a | Py_ssize_t index = PyLong_AsSsize_t(state); |
---|
2339 | n/a | if (index == -1 && PyErr_Occurred()) |
---|
2340 | n/a | return NULL; |
---|
2341 | n/a | if (it->it_seq != NULL) { |
---|
2342 | n/a | if (index < 0) |
---|
2343 | n/a | index = 0; |
---|
2344 | n/a | else if (index > PyByteArray_GET_SIZE(it->it_seq)) |
---|
2345 | n/a | index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */ |
---|
2346 | n/a | it->it_index = index; |
---|
2347 | n/a | } |
---|
2348 | n/a | Py_RETURN_NONE; |
---|
2349 | n/a | } |
---|
2350 | n/a | |
---|
2351 | n/a | PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); |
---|
2352 | n/a | |
---|
2353 | n/a | static PyMethodDef bytearrayiter_methods[] = { |
---|
2354 | n/a | {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, |
---|
2355 | n/a | length_hint_doc}, |
---|
2356 | n/a | {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, |
---|
2357 | n/a | bytearray_reduce__doc__}, |
---|
2358 | n/a | {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, |
---|
2359 | n/a | setstate_doc}, |
---|
2360 | n/a | {NULL, NULL} /* sentinel */ |
---|
2361 | n/a | }; |
---|
2362 | n/a | |
---|
2363 | n/a | PyTypeObject PyByteArrayIter_Type = { |
---|
2364 | n/a | PyVarObject_HEAD_INIT(&PyType_Type, 0) |
---|
2365 | n/a | "bytearray_iterator", /* tp_name */ |
---|
2366 | n/a | sizeof(bytesiterobject), /* tp_basicsize */ |
---|
2367 | n/a | 0, /* tp_itemsize */ |
---|
2368 | n/a | /* methods */ |
---|
2369 | n/a | (destructor)bytearrayiter_dealloc, /* tp_dealloc */ |
---|
2370 | n/a | 0, /* tp_print */ |
---|
2371 | n/a | 0, /* tp_getattr */ |
---|
2372 | n/a | 0, /* tp_setattr */ |
---|
2373 | n/a | 0, /* tp_reserved */ |
---|
2374 | n/a | 0, /* tp_repr */ |
---|
2375 | n/a | 0, /* tp_as_number */ |
---|
2376 | n/a | 0, /* tp_as_sequence */ |
---|
2377 | n/a | 0, /* tp_as_mapping */ |
---|
2378 | n/a | 0, /* tp_hash */ |
---|
2379 | n/a | 0, /* tp_call */ |
---|
2380 | n/a | 0, /* tp_str */ |
---|
2381 | n/a | PyObject_GenericGetAttr, /* tp_getattro */ |
---|
2382 | n/a | 0, /* tp_setattro */ |
---|
2383 | n/a | 0, /* tp_as_buffer */ |
---|
2384 | n/a | Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ |
---|
2385 | n/a | 0, /* tp_doc */ |
---|
2386 | n/a | (traverseproc)bytearrayiter_traverse, /* tp_traverse */ |
---|
2387 | n/a | 0, /* tp_clear */ |
---|
2388 | n/a | 0, /* tp_richcompare */ |
---|
2389 | n/a | 0, /* tp_weaklistoffset */ |
---|
2390 | n/a | PyObject_SelfIter, /* tp_iter */ |
---|
2391 | n/a | (iternextfunc)bytearrayiter_next, /* tp_iternext */ |
---|
2392 | n/a | bytearrayiter_methods, /* tp_methods */ |
---|
2393 | n/a | 0, |
---|
2394 | n/a | }; |
---|
2395 | n/a | |
---|
2396 | n/a | static PyObject * |
---|
2397 | n/a | bytearray_iter(PyObject *seq) |
---|
2398 | n/a | { |
---|
2399 | n/a | bytesiterobject *it; |
---|
2400 | n/a | |
---|
2401 | n/a | if (!PyByteArray_Check(seq)) { |
---|
2402 | n/a | PyErr_BadInternalCall(); |
---|
2403 | n/a | return NULL; |
---|
2404 | n/a | } |
---|
2405 | n/a | it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); |
---|
2406 | n/a | if (it == NULL) |
---|
2407 | n/a | return NULL; |
---|
2408 | n/a | it->it_index = 0; |
---|
2409 | n/a | Py_INCREF(seq); |
---|
2410 | n/a | it->it_seq = (PyByteArrayObject *)seq; |
---|
2411 | n/a | _PyObject_GC_TRACK(it); |
---|
2412 | n/a | return (PyObject *)it; |
---|
2413 | n/a | } |
---|