Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(34)

Delta Between Two Patch Sets: Objects/bytearrayobject.c

Issue 24467: bytearray pop and remove Buffer Over-read
Left Patch Set: Created 4 years, 7 months ago
Right Patch Set: Created 4 years, 7 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « Lib/test/test_bytes.py ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
1 /* PyByteArray (bytearray) implementation */ 1 /* PyByteArray (bytearray) implementation */
2 2
3 #define PY_SSIZE_T_CLEAN 3 #define PY_SSIZE_T_CLEAN
4 #include "Python.h" 4 #include "Python.h"
5 #include "structmember.h" 5 #include "structmember.h"
6 #include "bytes_methods.h" 6 #include "bytes_methods.h"
7 #include "bytesobject.h"
8 #include "pystrhex.h"
9
10 /*[clinic input]
11 class bytearray "PyByteArrayObject *" "&PyByteArray_Type"
12 [clinic start generated code]*/
13 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=5535b77c37a119e0]*/
7 14
8 char _PyByteArray_empty_string[] = ""; 15 char _PyByteArray_empty_string[] = "";
9 16
10 void 17 void
11 PyByteArray_Fini(void) 18 PyByteArray_Fini(void)
12 { 19 {
13 } 20 }
14 21
15 int 22 int
16 PyByteArray_Init(void) 23 PyByteArray_Init(void)
(...skipping 30 matching lines...) Expand all
47 return 0; 54 return 0;
48 } 55 }
49 56
50 *value = face_value; 57 *value = face_value;
51 return 1; 58 return 1;
52 } 59 }
53 60
54 static int 61 static int
55 bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) 62 bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)
56 { 63 {
57 int ret;
58 void *ptr; 64 void *ptr;
59 if (view == NULL) { 65 if (view == NULL) {
60 obj->ob_exports++; 66 PyErr_SetString(PyExc_BufferError,
61 return 0; 67 "bytearray_getbuffer: view==NULL argument is obsolete");
68 return -1;
62 } 69 }
63 ptr = (void *) PyByteArray_AS_STRING(obj); 70 ptr = (void *) PyByteArray_AS_STRING(obj);
64 ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); 71 /* cannot fail if view != NULL and readonly == 0 */
65 if (ret >= 0) { 72 (void)PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags);
66 obj->ob_exports++; 73 obj->ob_exports++;
67 } 74 return 0;
68 return ret;
69 } 75 }
70 76
71 static void 77 static void
72 bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) 78 bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)
73 { 79 {
74 obj->ob_exports--; 80 obj->ob_exports--;
75 }
76
77 static Py_ssize_t
78 _getbuffer(PyObject *obj, Py_buffer *view)
79 {
80 PyBufferProcs *buffer = Py_TYPE(obj)->tp_as_buffer;
81
82 if (buffer == NULL || buffer->bf_getbuffer == NULL)
83 {
84 PyErr_Format(PyExc_TypeError,
85 "Type %.100s doesn't support the buffer API",
86 Py_TYPE(obj)->tp_name);
87 return -1;
88 }
89
90 if (buffer->bf_getbuffer(obj, view, PyBUF_SIMPLE) < 0)
91 return -1;
92 return view->len;
93 } 81 }
94 82
95 static int 83 static int
96 _canresize(PyByteArrayObject *self) 84 _canresize(PyByteArrayObject *self)
97 { 85 {
98 if (self->ob_exports > 0) { 86 if (self->ob_exports > 0) {
99 PyErr_SetString(PyExc_BufferError, 87 PyErr_SetString(PyExc_BufferError,
100 "Existing exports of data: object cannot be re-sized"); 88 "Existing exports of data: object cannot be re-sized");
101 return 0; 89 return 0;
102 } 90 }
103 return 1; 91 return 1;
104 } 92 }
93
94 #include "clinic/bytearrayobject.c.h"
105 95
106 /* Direct API functions */ 96 /* Direct API functions */
107 97
108 PyObject * 98 PyObject *
109 PyByteArray_FromObject(PyObject *input) 99 PyByteArray_FromObject(PyObject *input)
110 { 100 {
111 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, 101 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
112 input, NULL); 102 input, NULL);
113 } 103 }
114 104
(...skipping 28 matching lines...) Expand all
143 if (new->ob_bytes == NULL) { 133 if (new->ob_bytes == NULL) {
144 Py_DECREF(new); 134 Py_DECREF(new);
145 return PyErr_NoMemory(); 135 return PyErr_NoMemory();
146 } 136 }
147 if (bytes != NULL && size > 0) 137 if (bytes != NULL && size > 0)
148 memcpy(new->ob_bytes, bytes, size); 138 memcpy(new->ob_bytes, bytes, size);
149 new->ob_bytes[size] = '\0'; /* Trailing null byte */ 139 new->ob_bytes[size] = '\0'; /* Trailing null byte */
150 } 140 }
151 Py_SIZE(new) = size; 141 Py_SIZE(new) = size;
152 new->ob_alloc = alloc; 142 new->ob_alloc = alloc;
143 new->ob_start = new->ob_bytes;
153 new->ob_exports = 0; 144 new->ob_exports = 0;
154 145
155 return (PyObject *)new; 146 return (PyObject *)new;
156 } 147 }
157 148
158 Py_ssize_t 149 Py_ssize_t
159 PyByteArray_Size(PyObject *self) 150 PyByteArray_Size(PyObject *self)
160 { 151 {
161 assert(self != NULL); 152 assert(self != NULL);
162 assert(PyByteArray_Check(self)); 153 assert(PyByteArray_Check(self));
163 154
164 return PyByteArray_GET_SIZE(self); 155 return PyByteArray_GET_SIZE(self);
165 } 156 }
166 157
167 char * 158 char *
168 PyByteArray_AsString(PyObject *self) 159 PyByteArray_AsString(PyObject *self)
169 { 160 {
170 assert(self != NULL); 161 assert(self != NULL);
171 assert(PyByteArray_Check(self)); 162 assert(PyByteArray_Check(self));
172 163
173 return PyByteArray_AS_STRING(self); 164 return PyByteArray_AS_STRING(self);
174 } 165 }
175 166
176 int 167 int
177 PyByteArray_Resize(PyObject *self, Py_ssize_t size) 168 PyByteArray_Resize(PyObject *self, Py_ssize_t requested_size)
178 { 169 {
179 void *sval; 170 void *sval;
180 Py_ssize_t alloc = ((PyByteArrayObject *)self)->ob_alloc; 171 PyByteArrayObject *obj = ((PyByteArrayObject *)self);
172 /* All computations are done unsigned to avoid integer overflows
173 (see issue #22335). */
174 size_t alloc = (size_t) obj->ob_alloc;
175 size_t logical_offset = (size_t) (obj->ob_start - obj->ob_bytes);
176 size_t size = (size_t) requested_size;
181 177
182 assert(self != NULL); 178 assert(self != NULL);
183 assert(PyByteArray_Check(self)); 179 assert(PyByteArray_Check(self));
184 assert(size >= 0); 180 assert(logical_offset <= alloc);
185 181 assert(requested_size >= 0);
186 if (size == Py_SIZE(self)) { 182
183 if (requested_size == Py_SIZE(self)) {
187 return 0; 184 return 0;
188 } 185 }
189 if (!_canresize((PyByteArrayObject *)self)) { 186 if (!_canresize(obj)) {
190 return -1; 187 return -1;
191 } 188 }
192 189
193 if (size < alloc / 2) { 190 if (size + logical_offset + 1 <= alloc) {
194 /* Major downsize; resize down to exact size */ 191 /* Current buffer is large enough to host the requested size,
195 alloc = size + 1; 192 decide on a strategy. */
196 } 193 if (size < alloc / 2) {
197 else if (size < alloc) { 194 /* Major downsize; resize down to exact size */
198 /* Within allocated size; quick exit */ 195 alloc = size + 1;
199 Py_SIZE(self) = size; 196 }
200 ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null */ 197 else {
201 return 0; 198 /* Minor downsize; quick exit */
202 } 199 Py_SIZE(self) = size;
203 else if (size <= alloc * 1.125) { 200 PyByteArray_AS_STRING(self)[size] = '\0'; /* Trailing null */
204 /* Moderate upsize; overallocate similar to list_resize() */ 201 return 0;
205 alloc = size + (size >> 3) + (size < 9 ? 3 : 6); 202 }
206 } 203 }
207 else { 204 else {
208 /* Major upsize; resize up to exact size */ 205 /* Need growing, decide on a strategy */
209 alloc = size + 1; 206 if (size <= alloc * 1.125) {
210 } 207 /* Moderate upsize; overallocate similar to list_resize() */
211 208 alloc = size + (size >> 3) + (size < 9 ? 3 : 6);
212 sval = PyObject_Realloc(((PyByteArrayObject *)self)->ob_bytes, alloc); 209 }
213 if (sval == NULL) { 210 else {
211 /* Major upsize; resize up to exact size */
212 alloc = size + 1;
213 }
214 }
215 if (alloc > PY_SSIZE_T_MAX) {
214 PyErr_NoMemory(); 216 PyErr_NoMemory();
215 return -1; 217 return -1;
216 } 218 }
217 219
218 ((PyByteArrayObject *)self)->ob_bytes = sval; 220 if (logical_offset > 0) {
221 sval = PyObject_Malloc(alloc);
222 if (sval == NULL) {
223 PyErr_NoMemory();
224 return -1;
225 }
226 memcpy(sval, PyByteArray_AS_STRING(self),
227 Py_MIN(requested_size, Py_SIZE(self)));
228 PyObject_Free(obj->ob_bytes);
229 }
230 else {
231 sval = PyObject_Realloc(obj->ob_bytes, alloc);
232 if (sval == NULL) {
233 PyErr_NoMemory();
234 return -1;
235 }
236 }
237
238 obj->ob_bytes = obj->ob_start = sval;
219 Py_SIZE(self) = size; 239 Py_SIZE(self) = size;
220 ((PyByteArrayObject *)self)->ob_alloc = alloc; 240 obj->ob_alloc = alloc;
221 ((PyByteArrayObject *)self)->ob_bytes[size] = '\0'; /* Trailing null byte */ 241 obj->ob_bytes[size] = '\0'; /* Trailing null byte */
222 242
223 return 0; 243 return 0;
224 } 244 }
225 245
226 PyObject * 246 PyObject *
227 PyByteArray_Concat(PyObject *a, PyObject *b) 247 PyByteArray_Concat(PyObject *a, PyObject *b)
228 { 248 {
229 Py_ssize_t size; 249 Py_ssize_t size;
230 Py_buffer va, vb; 250 Py_buffer va, vb;
231 PyByteArrayObject *result = NULL; 251 PyByteArrayObject *result = NULL;
232 252
233 va.len = -1; 253 va.len = -1;
234 vb.len = -1; 254 vb.len = -1;
235 if (_getbuffer(a, &va) < 0 || 255 if (PyObject_GetBuffer(a, &va, PyBUF_SIMPLE) != 0 ||
236 _getbuffer(b, &vb) < 0) { 256 PyObject_GetBuffer(b, &vb, PyBUF_SIMPLE) != 0) {
237 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", 257 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
238 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name); 258 Py_TYPE(a)->tp_name, Py_TYPE(b)->tp_name);
239 goto done; 259 goto done;
240 } 260 }
241 261
242 size = va.len + vb.len; 262 size = va.len + vb.len;
243 if (size < 0) { 263 if (size < 0) {
244 PyErr_NoMemory(); 264 PyErr_NoMemory();
245 goto done; 265 goto done;
246 } 266 }
247 267
248 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size); 268 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, size);
249 if (result != NULL) { 269 if (result != NULL) {
250 memcpy(result->ob_bytes, va.buf, va.len); 270 memcpy(result->ob_bytes, va.buf, va.len);
251 memcpy(result->ob_bytes + va.len, vb.buf, vb.len); 271 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
252 } 272 }
253 273
254 done: 274 done:
255 if (va.len != -1) 275 if (va.len != -1)
256 PyBuffer_Release(&va); 276 PyBuffer_Release(&va);
257 if (vb.len != -1) 277 if (vb.len != -1)
258 PyBuffer_Release(&vb); 278 PyBuffer_Release(&vb);
259 return (PyObject *)result; 279 return (PyObject *)result;
260 } 280 }
261 281
282 static PyObject *
283 bytearray_format(PyByteArrayObject *self, PyObject *args)
284 {
285 PyObject *bytes_in, *bytes_out, *res;
286 char *bytestring;
287
288 if (self == NULL || !PyByteArray_Check(self) || args == NULL) {
289 PyErr_BadInternalCall();
290 return NULL;
291 }
292 bytestring = PyByteArray_AS_STRING(self);
293 bytes_in = PyBytes_FromString(bytestring);
294 if (bytes_in == NULL)
295 return NULL;
296 bytes_out = _PyBytes_Format(bytes_in, args);
297 Py_DECREF(bytes_in);
298 if (bytes_out == NULL)
299 return NULL;
300 res = PyByteArray_FromObject(bytes_out);
301 Py_DECREF(bytes_out);
302 if (res == NULL)
303 return NULL;
304 return res;
305 }
306
262 /* Functions stuffed into the type object */ 307 /* Functions stuffed into the type object */
263 308
264 static Py_ssize_t 309 static Py_ssize_t
265 bytearray_length(PyByteArrayObject *self) 310 bytearray_length(PyByteArrayObject *self)
266 { 311 {
267 return Py_SIZE(self); 312 return Py_SIZE(self);
268 } 313 }
269 314
270 static PyObject * 315 static PyObject *
271 bytearray_iconcat(PyByteArrayObject *self, PyObject *other) 316 bytearray_iconcat(PyByteArrayObject *self, PyObject *other)
272 { 317 {
273 Py_ssize_t mysize; 318 Py_ssize_t mysize;
274 Py_ssize_t size; 319 Py_ssize_t size;
275 Py_buffer vo; 320 Py_buffer vo;
276 321
277 if (_getbuffer(other, &vo) < 0) { 322 if (PyObject_GetBuffer(other, &vo, PyBUF_SIMPLE) != 0) {
278 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s", 323 PyErr_Format(PyExc_TypeError, "can't concat %.100s to %.100s",
279 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name); 324 Py_TYPE(other)->tp_name, Py_TYPE(self)->tp_name);
280 return NULL; 325 return NULL;
281 } 326 }
282 327
283 mysize = Py_SIZE(self); 328 mysize = Py_SIZE(self);
284 size = mysize + vo.len; 329 size = mysize + vo.len;
285 if (size < 0) { 330 if (size < 0) {
286 PyBuffer_Release(&vo); 331 PyBuffer_Release(&vo);
287 return PyErr_NoMemory(); 332 return PyErr_NoMemory();
288 } 333 }
289 if (size < self->ob_alloc) { 334 if (PyByteArray_Resize((PyObject *)self, size) < 0) {
290 Py_SIZE(self) = size;
291 self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */
292 }
293 else if (PyByteArray_Resize((PyObject *)self, size) < 0) {
294 PyBuffer_Release(&vo); 335 PyBuffer_Release(&vo);
295 return NULL; 336 return NULL;
296 } 337 }
297 memcpy(self->ob_bytes + mysize, vo.buf, vo.len); 338 memcpy(PyByteArray_AS_STRING(self) + mysize, vo.buf, vo.len);
298 PyBuffer_Release(&vo); 339 PyBuffer_Release(&vo);
299 Py_INCREF(self); 340 Py_INCREF(self);
300 return (PyObject *)self; 341 return (PyObject *)self;
301 } 342 }
302 343
303 static PyObject * 344 static PyObject *
304 bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) 345 bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count)
305 { 346 {
306 PyByteArrayObject *result; 347 PyByteArrayObject *result;
307 Py_ssize_t mysize; 348 Py_ssize_t mysize;
(...skipping 16 matching lines...) Expand all
324 } 365 }
325 } 366 }
326 return (PyObject *)result; 367 return (PyObject *)result;
327 } 368 }
328 369
329 static PyObject * 370 static PyObject *
330 bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) 371 bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count)
331 { 372 {
332 Py_ssize_t mysize; 373 Py_ssize_t mysize;
333 Py_ssize_t size; 374 Py_ssize_t size;
375 char *buf;
334 376
335 if (count < 0) 377 if (count < 0)
336 count = 0; 378 count = 0;
337 mysize = Py_SIZE(self); 379 mysize = Py_SIZE(self);
338 if (count > 0 && mysize > PY_SSIZE_T_MAX / count) 380 if (count > 0 && mysize > PY_SSIZE_T_MAX / count)
339 return PyErr_NoMemory(); 381 return PyErr_NoMemory();
340 size = mysize * count; 382 size = mysize * count;
341 if (size < self->ob_alloc) { 383 if (PyByteArray_Resize((PyObject *)self, size) < 0)
342 Py_SIZE(self) = size; 384 return NULL;
343 self->ob_bytes[Py_SIZE(self)] = '\0'; /* Trailing null byte */ 385
344 } 386 buf = PyByteArray_AS_STRING(self);
345 else if (PyByteArray_Resize((PyObject *)self, size) < 0)
346 return NULL;
347
348 if (mysize == 1) 387 if (mysize == 1)
349 memset(self->ob_bytes, self->ob_bytes[0], size); 388 memset(buf, buf[0], size);
350 else { 389 else {
351 Py_ssize_t i; 390 Py_ssize_t i;
352 for (i = 1; i < count; i++) 391 for (i = 1; i < count; i++)
353 memcpy(self->ob_bytes + i*mysize, self->ob_bytes, mysize); 392 memcpy(buf + i*mysize, buf, mysize);
354 } 393 }
355 394
356 Py_INCREF(self); 395 Py_INCREF(self);
357 return (PyObject *)self; 396 return (PyObject *)self;
358 } 397 }
359 398
360 static PyObject * 399 static PyObject *
361 bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) 400 bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i)
362 { 401 {
363 if (i < 0) 402 if (i < 0)
364 i += Py_SIZE(self); 403 i += Py_SIZE(self);
365 if (i < 0 || i >= Py_SIZE(self)) { 404 if (i < 0 || i >= Py_SIZE(self)) {
366 PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); 405 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
367 return NULL; 406 return NULL;
368 } 407 }
369 return PyLong_FromLong((unsigned char)(self->ob_bytes[i])); 408 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
370 } 409 }
371 410
372 static PyObject * 411 static PyObject *
373 bytearray_subscript(PyByteArrayObject *self, PyObject *index) 412 bytearray_subscript(PyByteArrayObject *self, PyObject *index)
374 { 413 {
375 if (PyIndex_Check(index)) { 414 if (PyIndex_Check(index)) {
376 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); 415 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
377 416
378 if (i == -1 && PyErr_Occurred()) 417 if (i == -1 && PyErr_Occurred())
379 return NULL; 418 return NULL;
380 419
381 if (i < 0) 420 if (i < 0)
382 i += PyByteArray_GET_SIZE(self); 421 i += PyByteArray_GET_SIZE(self);
383 422
384 if (i < 0 || i >= Py_SIZE(self)) { 423 if (i < 0 || i >= Py_SIZE(self)) {
385 PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); 424 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
386 return NULL; 425 return NULL;
387 } 426 }
388 return PyLong_FromLong((unsigned char)(self->ob_bytes[i])); 427 return PyLong_FromLong((unsigned char)(PyByteArray_AS_STRING(self)[i]));
389 } 428 }
390 else if (PySlice_Check(index)) { 429 else if (PySlice_Check(index)) {
391 Py_ssize_t start, stop, step, slicelength, cur, i; 430 Py_ssize_t start, stop, step, slicelength, cur, i;
392 if (PySlice_GetIndicesEx(index, 431 if (PySlice_GetIndicesEx(index,
393 PyByteArray_GET_SIZE(self), 432 PyByteArray_GET_SIZE(self),
394 &start, &stop, &step, &slicelength) < 0) { 433 &start, &stop, &step, &slicelength) < 0) {
395 return NULL; 434 return NULL;
396 } 435 }
397 436
398 if (slicelength <= 0) 437 if (slicelength <= 0)
399 return PyByteArray_FromStringAndSize("", 0); 438 return PyByteArray_FromStringAndSize("", 0);
400 else if (step == 1) { 439 else if (step == 1) {
401 return PyByteArray_FromStringAndSize(self->ob_bytes + start, 440 return PyByteArray_FromStringAndSize(
402 slicelength); 441 PyByteArray_AS_STRING(self) + start, slicelength);
403 } 442 }
404 else { 443 else {
405 char *source_buf = PyByteArray_AS_STRING(self); 444 char *source_buf = PyByteArray_AS_STRING(self);
406 char *result_buf; 445 char *result_buf;
407 PyObject *result; 446 PyObject *result;
408 447
409 result = PyByteArray_FromStringAndSize(NULL, slicelength); 448 result = PyByteArray_FromStringAndSize(NULL, slicelength);
410 if (result == NULL) 449 if (result == NULL)
411 return NULL; 450 return NULL;
412 451
413 result_buf = PyByteArray_AS_STRING(result); 452 result_buf = PyByteArray_AS_STRING(result);
414 for (cur = start, i = 0; i < slicelength; 453 for (cur = start, i = 0; i < slicelength;
415 cur += step, i++) { 454 cur += step, i++) {
416 result_buf[i] = source_buf[cur]; 455 result_buf[i] = source_buf[cur];
417 } 456 }
418 return result; 457 return result;
419 } 458 }
420 } 459 }
421 else { 460 else {
422 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers"); 461 PyErr_Format(PyExc_TypeError,
423 return NULL; 462 "bytearray indices must be integers or slices, not %.200s",
424 } 463 Py_TYPE(index)->tp_name);
464 return NULL;
465 }
466 }
467
468 static int
469 bytearray_setslice_linear(PyByteArrayObject *self,
470 Py_ssize_t lo, Py_ssize_t hi,
471 char *bytes, Py_ssize_t bytes_len)
472 {
473 Py_ssize_t avail = hi - lo;
474 char *buf = PyByteArray_AS_STRING(self);
475 Py_ssize_t growth = bytes_len - avail;
476 int res = 0;
477 assert(avail >= 0);
478
479 if (growth < 0) {
480 if (!_canresize(self))
481 return -1;
482
483 if (lo == 0) {
484 /* Shrink the buffer by advancing its logical start */
485 self->ob_start -= growth;
486 /*
487 0 lo hi old_size
488 | |<----avail----->|<-----tail------>|
489 | |<-bytes_len->|<-----tail------>|
490 0 new_lo new_hi new_size
491 */
492 }
493 else {
494 /*
495 0 lo hi old_size
496 | |<----avail----->|<-----tomove------>|
497 | |<-bytes_len->|<-----tomove------>|
498 0 lo new_hi new_size
499 */
500 memmove(buf + lo + bytes_len, buf + hi,
501 Py_SIZE(self) - hi);
502 }
503 if (PyByteArray_Resize((PyObject *)self,
504 Py_SIZE(self) + growth) < 0) {
505 /* Issue #19578: Handling the memory allocation failure here is
506 tricky here because the bytearray object has already been
507 modified. Depending on growth and lo, the behaviour is
508 different.
509
510 If growth < 0 and lo != 0, the operation is completed, but a
511 MemoryError is still raised and the memory block is not
512 shrinked. Otherwise, the bytearray is restored in its previous
513 state and a MemoryError is raised. */
514 if (lo == 0) {
515 self->ob_start += growth;
516 return -1;
517 }
518 /* memmove() removed bytes, the bytearray object cannot be
519 restored in its previous state. */
520 Py_SIZE(self) += growth;
521 res = -1;
522 }
523 buf = PyByteArray_AS_STRING(self);
524 }
525 else if (growth > 0) {
526 if (Py_SIZE(self) > (Py_ssize_t)PY_SSIZE_T_MAX - growth) {
527 PyErr_NoMemory();
528 return -1;
529 }
530
531 if (PyByteArray_Resize((PyObject *)self,
532 Py_SIZE(self) + growth) < 0) {
533 return -1;
534 }
535 buf = PyByteArray_AS_STRING(self);
536 /* Make the place for the additional bytes */
537 /*
538 0 lo hi old_size
539 | |<-avail->|<-----tomove------>|
540 | |<---bytes_len-->|<-----tomove------>|
541 0 lo new_hi new_size
542 */
543 memmove(buf + lo + bytes_len, buf + hi,
544 Py_SIZE(self) - lo - bytes_len);
545 }
546
547 if (bytes_len > 0)
548 memcpy(buf + lo, bytes, bytes_len);
549 return res;
425 } 550 }
426 551
427 static int 552 static int
428 bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, 553 bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,
429 PyObject *values) 554 PyObject *values)
430 { 555 {
431 Py_ssize_t avail, needed; 556 Py_ssize_t needed;
432 void *bytes; 557 void *bytes;
433 Py_buffer vbytes; 558 Py_buffer vbytes;
434 int res = 0; 559 int res = 0;
435 560
436 vbytes.len = -1; 561 vbytes.len = -1;
437 if (values == (PyObject *)self) { 562 if (values == (PyObject *)self) {
438 /* Make a copy and call this function recursively */ 563 /* Make a copy and call this function recursively */
439 int err; 564 int err;
440 values = PyByteArray_FromObject(values); 565 values = PyByteArray_FromObject(values);
441 if (values == NULL) 566 if (values == NULL)
442 return -1; 567 return -1;
443 err = bytearray_setslice(self, lo, hi, values); 568 err = bytearray_setslice(self, lo, hi, values);
444 Py_DECREF(values); 569 Py_DECREF(values);
445 return err; 570 return err;
446 } 571 }
447 if (values == NULL) { 572 if (values == NULL) {
448 /* del b[lo:hi] */ 573 /* del b[lo:hi] */
449 bytes = NULL; 574 bytes = NULL;
450 needed = 0; 575 needed = 0;
451 } 576 }
452 else { 577 else {
453 if (_getbuffer(values, &vbytes) < 0) { 578 if (PyObject_GetBuffer(values, &vbytes, PyBUF_SIMPLE) != 0) {
454 PyErr_Format(PyExc_TypeError, 579 PyErr_Format(PyExc_TypeError,
455 "can't set bytearray slice from %.100s", 580 "can't set bytearray slice from %.100s",
456 Py_TYPE(values)->tp_name); 581 Py_TYPE(values)->tp_name);
457 return -1; 582 return -1;
458 } 583 }
459 needed = vbytes.len; 584 needed = vbytes.len;
460 bytes = vbytes.buf; 585 bytes = vbytes.buf;
461 } 586 }
462 587
463 if (lo < 0) 588 if (lo < 0)
464 lo = 0; 589 lo = 0;
465 if (hi < lo) 590 if (hi < lo)
466 hi = lo; 591 hi = lo;
467 if (hi > Py_SIZE(self)) 592 if (hi > Py_SIZE(self))
468 hi = Py_SIZE(self); 593 hi = Py_SIZE(self);
469 594
470 avail = hi - lo; 595 res = bytearray_setslice_linear(self, lo, hi, bytes, needed);
471 if (avail < 0)
472 lo = hi = avail = 0;
473
474 if (avail != needed) {
475 if (avail > needed) {
476 if (!_canresize(self)) {
477 res = -1;
478 goto finish;
479 }
480 /*
481 0 lo hi old_size
482 | |<----avail----->|<-----tomove------>|
483 | |<-needed->|<-----tomove------>|
484 0 lo new_hi new_size
485 */
486 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
487 Py_SIZE(self) - hi);
488 }
489 /* XXX(nnorwitz): need to verify this can't overflow! */
490 if (PyByteArray_Resize((PyObject *)self,
491 Py_SIZE(self) + needed - avail) < 0) {
492 res = -1;
493 goto finish;
494 }
495 if (avail < needed) {
496 /*
497 0 lo hi old_size
498 | |<-avail->|<-----tomove------>|
499 | |<----needed---->|<-----tomove------>|
500 0 lo new_hi new_size
501 */
502 memmove(self->ob_bytes + lo + needed, self->ob_bytes + hi,
503 Py_SIZE(self) - lo - needed);
504 }
505 }
506
507 if (needed > 0)
508 memcpy(self->ob_bytes + lo, bytes, needed);
509
510
511 finish:
512 if (vbytes.len != -1) 596 if (vbytes.len != -1)
513 PyBuffer_Release(&vbytes); 597 PyBuffer_Release(&vbytes);
514 return res; 598 return res;
515 } 599 }
516 600
517 static int 601 static int
518 bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) 602 bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)
519 { 603 {
520 int ival; 604 int ival;
521 605
522 if (i < 0) 606 if (i < 0)
523 i += Py_SIZE(self); 607 i += Py_SIZE(self);
524 608
525 if (i < 0 || i >= Py_SIZE(self)) { 609 if (i < 0 || i >= Py_SIZE(self)) {
526 PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); 610 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
527 return -1; 611 return -1;
528 } 612 }
529 613
530 if (value == NULL) 614 if (value == NULL)
531 return bytearray_setslice(self, i, i+1, NULL); 615 return bytearray_setslice(self, i, i+1, NULL);
532 616
533 if (!_getbytevalue(value, &ival)) 617 if (!_getbytevalue(value, &ival))
534 return -1; 618 return -1;
535 619
536 self->ob_bytes[i] = ival; 620 PyByteArray_AS_STRING(self)[i] = ival;
537 return 0; 621 return 0;
538 } 622 }
539 623
540 static int 624 static int
541 bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu es) 625 bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *valu es)
542 { 626 {
543 Py_ssize_t start, stop, step, slicelen, needed; 627 Py_ssize_t start, stop, step, slicelen, needed;
544 char *bytes; 628 char *buf, *bytes;
629 buf = PyByteArray_AS_STRING(self);
545 630
546 if (PyIndex_Check(index)) { 631 if (PyIndex_Check(index)) {
547 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError); 632 Py_ssize_t i = PyNumber_AsSsize_t(index, PyExc_IndexError);
548 633
549 if (i == -1 && PyErr_Occurred()) 634 if (i == -1 && PyErr_Occurred())
550 return -1; 635 return -1;
551 636
552 if (i < 0) 637 if (i < 0)
553 i += PyByteArray_GET_SIZE(self); 638 i += PyByteArray_GET_SIZE(self);
554 639
555 if (i < 0 || i >= Py_SIZE(self)) { 640 if (i < 0 || i >= Py_SIZE(self)) {
556 PyErr_SetString(PyExc_IndexError, "bytearray index out of range"); 641 PyErr_SetString(PyExc_IndexError, "bytearray index out of range");
557 return -1; 642 return -1;
558 } 643 }
559 644
560 if (values == NULL) { 645 if (values == NULL) {
561 /* Fall through to slice assignment */ 646 /* Fall through to slice assignment */
562 start = i; 647 start = i;
563 stop = i + 1; 648 stop = i + 1;
564 step = 1; 649 step = 1;
565 slicelen = 1; 650 slicelen = 1;
566 } 651 }
567 else { 652 else {
568 int ival; 653 int ival;
569 if (!_getbytevalue(values, &ival)) 654 if (!_getbytevalue(values, &ival))
570 return -1; 655 return -1;
571 self->ob_bytes[i] = (char)ival; 656 buf[i] = (char)ival;
572 return 0; 657 return 0;
573 } 658 }
574 } 659 }
575 else if (PySlice_Check(index)) { 660 else if (PySlice_Check(index)) {
576 if (PySlice_GetIndicesEx(index, 661 if (PySlice_GetIndicesEx(index,
577 PyByteArray_GET_SIZE(self), 662 PyByteArray_GET_SIZE(self),
578 &start, &stop, &step, &slicelen) < 0) { 663 &start, &stop, &step, &slicelen) < 0) {
579 return -1; 664 return -1;
580 } 665 }
581 } 666 }
582 else { 667 else {
583 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integer"); 668 PyErr_Format(PyExc_TypeError,
669 "bytearray indices must be integers or slices, not %.200s",
670 Py_TYPE(index)->tp_name);
584 return -1; 671 return -1;
585 } 672 }
586 673
587 if (values == NULL) { 674 if (values == NULL) {
588 bytes = NULL; 675 bytes = NULL;
589 needed = 0; 676 needed = 0;
590 } 677 }
591 else if (values == (PyObject *)self || !PyByteArray_Check(values)) { 678 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
592 int err; 679 int err;
593 if (PyNumber_Check(values) || PyUnicode_Check(values)) { 680 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
594 PyErr_SetString(PyExc_TypeError, 681 PyErr_SetString(PyExc_TypeError,
595 "can assign only bytes, buffers, or iterables " 682 "can assign only bytes, buffers, or iterables "
596 "of ints in range(0, 256)"); 683 "of ints in range(0, 256)");
597 return -1; 684 return -1;
598 } 685 }
599 /* Make a copy and call this function recursively */ 686 /* Make a copy and call this function recursively */
600 values = PyByteArray_FromObject(values); 687 values = PyByteArray_FromObject(values);
601 if (values == NULL) 688 if (values == NULL)
602 return -1; 689 return -1;
603 err = bytearray_ass_subscript(self, index, values); 690 err = bytearray_ass_subscript(self, index, values);
604 Py_DECREF(values); 691 Py_DECREF(values);
605 return err; 692 return err;
606 } 693 }
607 else { 694 else {
608 assert(PyByteArray_Check(values)); 695 assert(PyByteArray_Check(values));
609 bytes = ((PyByteArrayObject *)values)->ob_bytes; 696 bytes = PyByteArray_AS_STRING(values);
610 needed = Py_SIZE(values); 697 needed = Py_SIZE(values);
611 } 698 }
612 /* Make sure b[5:2] = ... inserts before 5, not before 2. */ 699 /* Make sure b[5:2] = ... inserts before 5, not before 2. */
613 if ((step < 0 && start < stop) || 700 if ((step < 0 && start < stop) ||
614 (step > 0 && start > stop)) 701 (step > 0 && start > stop))
615 stop = start; 702 stop = start;
616 if (step == 1) { 703 if (step == 1) {
617 if (slicelen != needed) { 704 return bytearray_setslice_linear(self, start, stop, bytes, needed);
618 if (!_canresize(self))
619 return -1;
620 if (slicelen > needed) {
621 /*
622 0 start stop old_size
623 | |<---slicelen--->|<-----tomove------>|
624 | |<-needed->|<-----tomove------>|
625 0 lo new_hi new_size
626 */
627 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,
628 Py_SIZE(self) - stop);
629 }
630 if (PyByteArray_Resize((PyObject *)self,
631 Py_SIZE(self) + needed - slicelen) < 0)
632 return -1;
633 if (slicelen < needed) {
634 /*
635 0 lo hi old_size
636 | |<-avail->|<-----tomove------>|
637 | |<----needed---->|<-----tomove------>|
638 0 lo new_hi new_size
639 */
640 memmove(self->ob_bytes + start + needed, self->ob_bytes + stop,
641 Py_SIZE(self) - start - needed);
642 }
643 }
644
645 if (needed > 0)
646 memcpy(self->ob_bytes + start, bytes, needed);
647
648 return 0;
649 } 705 }
650 else { 706 else {
651 if (needed == 0) { 707 if (needed == 0) {
652 /* Delete slice */ 708 /* Delete slice */
653 size_t cur; 709 size_t cur;
654 Py_ssize_t i; 710 Py_ssize_t i;
655 711
656 if (!_canresize(self)) 712 if (!_canresize(self))
657 return -1; 713 return -1;
658 714
659 if (slicelen == 0) 715 if (slicelen == 0)
660 /* Nothing to do here. */ 716 /* Nothing to do here. */
661 return 0; 717 return 0;
662 718
663 if (step < 0) { 719 if (step < 0) {
664 stop = start + 1; 720 stop = start + 1;
665 start = stop + step * (slicelen - 1) - 1; 721 start = stop + step * (slicelen - 1) - 1;
666 step = -step; 722 step = -step;
667 } 723 }
668 for (cur = start, i = 0; 724 for (cur = start, i = 0;
669 i < slicelen; cur += step, i++) { 725 i < slicelen; cur += step, i++) {
670 Py_ssize_t lim = step - 1; 726 Py_ssize_t lim = step - 1;
671 727
672 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self)) 728 if (cur + step >= (size_t)PyByteArray_GET_SIZE(self))
673 lim = PyByteArray_GET_SIZE(self) - cur - 1; 729 lim = PyByteArray_GET_SIZE(self) - cur - 1;
674 730
675 memmove(self->ob_bytes + cur - i, 731 memmove(buf + cur - i,
676 self->ob_bytes + cur + 1, lim); 732 buf + cur + 1, lim);
677 } 733 }
678 /* Move the tail of the bytes, in one chunk */ 734 /* Move the tail of the bytes, in one chunk */
679 cur = start + (size_t)slicelen*step; 735 cur = start + (size_t)slicelen*step;
680 if (cur < (size_t)PyByteArray_GET_SIZE(self)) { 736 if (cur < (size_t)PyByteArray_GET_SIZE(self)) {
681 memmove(self->ob_bytes + cur - slicelen, 737 memmove(buf + cur - slicelen,
682 self->ob_bytes + cur, 738 buf + cur,
683 PyByteArray_GET_SIZE(self) - cur); 739 PyByteArray_GET_SIZE(self) - cur);
684 } 740 }
685 if (PyByteArray_Resize((PyObject *)self, 741 if (PyByteArray_Resize((PyObject *)self,
686 PyByteArray_GET_SIZE(self) - slicelen) < 0) 742 PyByteArray_GET_SIZE(self) - slicelen) < 0)
687 return -1; 743 return -1;
688 744
689 return 0; 745 return 0;
690 } 746 }
691 else { 747 else {
692 /* Assign slice */ 748 /* Assign slice */
693 Py_ssize_t i; 749 Py_ssize_t i;
694 size_t cur; 750 size_t cur;
695 751
696 if (needed != slicelen) { 752 if (needed != slicelen) {
697 PyErr_Format(PyExc_ValueError, 753 PyErr_Format(PyExc_ValueError,
698 "attempt to assign bytes of size %zd " 754 "attempt to assign bytes of size %zd "
699 "to extended slice of size %zd", 755 "to extended slice of size %zd",
700 needed, slicelen); 756 needed, slicelen);
701 return -1; 757 return -1;
702 } 758 }
703 for (cur = start, i = 0; i < slicelen; cur += step, i++) 759 for (cur = start, i = 0; i < slicelen; cur += step, i++)
704 self->ob_bytes[cur] = bytes[i]; 760 buf[cur] = bytes[i];
705 return 0; 761 return 0;
706 } 762 }
707 } 763 }
708 } 764 }
709 765
710 static int 766 static int
711 bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) 767 bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)
712 { 768 {
713 static char *kwlist[] = {"source", "encoding", "errors", 0}; 769 static char *kwlist[] = {"source", "encoding", "errors", 0};
714 PyObject *arg = NULL; 770 PyObject *arg = NULL;
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after
774 PyErr_Clear(); 830 PyErr_Clear();
775 } 831 }
776 else if (count < 0) { 832 else if (count < 0) {
777 PyErr_SetString(PyExc_ValueError, "negative count"); 833 PyErr_SetString(PyExc_ValueError, "negative count");
778 return -1; 834 return -1;
779 } 835 }
780 else { 836 else {
781 if (count > 0) { 837 if (count > 0) {
782 if (PyByteArray_Resize((PyObject *)self, count)) 838 if (PyByteArray_Resize((PyObject *)self, count))
783 return -1; 839 return -1;
784 memset(self->ob_bytes, 0, count); 840 memset(PyByteArray_AS_STRING(self), 0, count);
785 } 841 }
786 return 0; 842 return 0;
787 } 843 }
788 844
789 /* Use the buffer API */ 845 /* Use the buffer API */
790 if (PyObject_CheckBuffer(arg)) { 846 if (PyObject_CheckBuffer(arg)) {
791 Py_ssize_t size; 847 Py_ssize_t size;
792 Py_buffer view; 848 Py_buffer view;
793 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0) 849 if (PyObject_GetBuffer(arg, &view, PyBUF_FULL_RO) < 0)
794 return -1; 850 return -1;
795 size = view.len; 851 size = view.len;
796 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail; 852 if (PyByteArray_Resize((PyObject *)self, size) < 0) goto fail;
797 if (PyBuffer_ToContiguous(self->ob_bytes, &view, size, 'C') < 0) 853 if (PyBuffer_ToContiguous(PyByteArray_AS_STRING(self),
854 &view, size, 'C') < 0)
798 goto fail; 855 goto fail;
799 PyBuffer_Release(&view); 856 PyBuffer_Release(&view);
800 return 0; 857 return 0;
801 fail: 858 fail:
802 PyBuffer_Release(&view); 859 PyBuffer_Release(&view);
803 return -1; 860 return -1;
804 } 861 }
805 862
806 /* XXX Optimize this if the arguments is a list, tuple */ 863 /* XXX Optimize this if the arguments is a list, tuple */
807 864
(...skipping 19 matching lines...) Expand all
827 break; 884 break;
828 } 885 }
829 886
830 /* Interpret it as an int (__index__) */ 887 /* Interpret it as an int (__index__) */
831 rc = _getbytevalue(item, &value); 888 rc = _getbytevalue(item, &value);
832 Py_DECREF(item); 889 Py_DECREF(item);
833 if (!rc) 890 if (!rc)
834 goto error; 891 goto error;
835 892
836 /* Append the byte */ 893 /* Append the byte */
837 if (Py_SIZE(self) < self->ob_alloc) 894 if (Py_SIZE(self) + 1 < self->ob_alloc) {
838 Py_SIZE(self)++; 895 Py_SIZE(self)++;
896 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
897 }
839 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) 898 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
840 goto error; 899 goto error;
841 self->ob_bytes[Py_SIZE(self)-1] = value; 900 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
842 } 901 }
843 902
844 /* Clean up and return success */ 903 /* Clean up and return success */
845 Py_DECREF(it); 904 Py_DECREF(it);
846 return 0; 905 return 0;
847 906
848 error: 907 error:
849 /* Error handling when it != NULL */ 908 /* Error handling when it != NULL */
850 Py_DECREF(it); 909 Py_DECREF(it);
851 return -1; 910 return -1;
852 } 911 }
853 912
854 /* Mostly copied from string_repr, but without the 913 /* Mostly copied from string_repr, but without the
855 "smart quote" functionality. */ 914 "smart quote" functionality. */
856 static PyObject * 915 static PyObject *
857 bytearray_repr(PyByteArrayObject *self) 916 bytearray_repr(PyByteArrayObject *self)
858 { 917 {
859 const char *quote_prefix = "bytearray(b"; 918 const char *quote_prefix = "bytearray(b";
860 const char *quote_postfix = ")"; 919 const char *quote_postfix = ")";
861 Py_ssize_t length = Py_SIZE(self); 920 Py_ssize_t length = Py_SIZE(self);
862 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */ 921 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
863 size_t newsize; 922 size_t newsize;
864 PyObject *v; 923 PyObject *v;
865 register Py_ssize_t i; 924 Py_ssize_t i;
866 register char c; 925 char *bytes;
867 register char *p; 926 char c;
927 char *p;
868 int quote; 928 int quote;
869 char *test, *start; 929 char *test, *start;
870 char *buffer; 930 char *buffer;
871 931
872 if (length > (PY_SSIZE_T_MAX - 15) / 4) { 932 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
873 PyErr_SetString(PyExc_OverflowError, 933 PyErr_SetString(PyExc_OverflowError,
874 "bytearray object is too large to make repr"); 934 "bytearray object is too large to make repr");
875 return NULL; 935 return NULL;
876 } 936 }
877 937
(...skipping 14 matching lines...) Expand all
892 } 952 }
893 else if (*test == '\'') 953 else if (*test == '\'')
894 quote = '"'; 954 quote = '"';
895 } 955 }
896 956
897 p = buffer; 957 p = buffer;
898 while (*quote_prefix) 958 while (*quote_prefix)
899 *p++ = *quote_prefix++; 959 *p++ = *quote_prefix++;
900 *p++ = quote; 960 *p++ = quote;
901 961
962 bytes = PyByteArray_AS_STRING(self);
902 for (i = 0; i < length; i++) { 963 for (i = 0; i < length; i++) {
903 /* There's at least enough room for a hex escape 964 /* There's at least enough room for a hex escape
904 and a closing quote. */ 965 and a closing quote. */
905 assert(newsize - (p - buffer) >= 5); 966 assert(newsize - (p - buffer) >= 5);
906 c = self->ob_bytes[i]; 967 c = bytes[i];
907 if (c == '\'' || c == '\\') 968 if (c == '\'' || c == '\\')
908 *p++ = '\\', *p++ = c; 969 *p++ = '\\', *p++ = c;
909 else if (c == '\t') 970 else if (c == '\t')
910 *p++ = '\\', *p++ = 't'; 971 *p++ = '\\', *p++ = 't';
911 else if (c == '\n') 972 else if (c == '\n')
912 *p++ = '\\', *p++ = 'n'; 973 *p++ = '\\', *p++ = 'n';
913 else if (c == '\r') 974 else if (c == '\r')
914 *p++ = '\\', *p++ = 'r'; 975 *p++ = '\\', *p++ = 'r';
915 else if (c == 0) 976 else if (c == 0)
916 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; 977 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
(...skipping 28 matching lines...) Expand all
945 return bytearray_repr((PyByteArrayObject*)op); 1006 return bytearray_repr((PyByteArrayObject*)op);
946 } 1007 }
947 1008
948 static PyObject * 1009 static PyObject *
949 bytearray_richcompare(PyObject *self, PyObject *other, int op) 1010 bytearray_richcompare(PyObject *self, PyObject *other, int op)
950 { 1011 {
951 Py_ssize_t self_size, other_size; 1012 Py_ssize_t self_size, other_size;
952 Py_buffer self_bytes, other_bytes; 1013 Py_buffer self_bytes, other_bytes;
953 PyObject *res; 1014 PyObject *res;
954 Py_ssize_t minsize; 1015 Py_ssize_t minsize;
955 int cmp; 1016 int cmp, rc;
956 1017
957 /* Bytes can be compared to anything that supports the (binary) 1018 /* Bytes can be compared to anything that supports the (binary)
958 buffer API. Except that a comparison with Unicode is always an 1019 buffer API. Except that a comparison with Unicode is always an
959 error, even if the comparison is for equality. */ 1020 error, even if the comparison is for equality. */
960 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || 1021 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
961 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) { 1022 if (!rc)
1023 rc = PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type);
1024 if (rc < 0)
1025 return NULL;
1026 if (rc) {
962 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { 1027 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
963 if (PyErr_WarnEx(PyExc_BytesWarning, 1028 if (PyErr_WarnEx(PyExc_BytesWarning,
964 "Comparison between bytearray and string", 1)) 1029 "Comparison between bytearray and string", 1))
965 return NULL; 1030 return NULL;
966 } 1031 }
967 1032
968 Py_RETURN_NOTIMPLEMENTED; 1033 Py_RETURN_NOTIMPLEMENTED;
969 } 1034 }
970 1035
971 self_size = _getbuffer(self, &self_bytes); 1036 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
972 if (self_size < 0) {
973 PyErr_Clear(); 1037 PyErr_Clear();
974 Py_RETURN_NOTIMPLEMENTED; 1038 Py_RETURN_NOTIMPLEMENTED;
975 } 1039 }
976 1040 self_size = self_bytes.len;
977 other_size = _getbuffer(other, &other_bytes); 1041
978 if (other_size < 0) { 1042 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
979 PyErr_Clear(); 1043 PyErr_Clear();
980 PyBuffer_Release(&self_bytes); 1044 PyBuffer_Release(&self_bytes);
981 Py_RETURN_NOTIMPLEMENTED; 1045 Py_RETURN_NOTIMPLEMENTED;
982 } 1046 }
1047 other_size = other_bytes.len;
983 1048
984 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { 1049 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
985 /* Shortcut: if the lengths differ, the objects differ */ 1050 /* Shortcut: if the lengths differ, the objects differ */
986 cmp = (op == Py_NE); 1051 cmp = (op == Py_NE);
987 } 1052 }
988 else { 1053 else {
989 minsize = self_size; 1054 minsize = self_size;
990 if (other_size < minsize) 1055 if (other_size < minsize)
991 minsize = other_size; 1056 minsize = other_size;
992 1057
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
1031 Py_TYPE(self)->tp_free((PyObject *)self); 1096 Py_TYPE(self)->tp_free((PyObject *)self);
1032 } 1097 }
1033 1098
1034 1099
1035 /* -------------------------------------------------------------------- */ 1100 /* -------------------------------------------------------------------- */
1036 /* Methods */ 1101 /* Methods */
1037 1102
1038 #define FASTSEARCH fastsearch 1103 #define FASTSEARCH fastsearch
1039 #define STRINGLIB(F) stringlib_##F 1104 #define STRINGLIB(F) stringlib_##F
1040 #define STRINGLIB_CHAR char 1105 #define STRINGLIB_CHAR char
1106 #define STRINGLIB_SIZEOF_CHAR 1
1041 #define STRINGLIB_LEN PyByteArray_GET_SIZE 1107 #define STRINGLIB_LEN PyByteArray_GET_SIZE
1042 #define STRINGLIB_STR PyByteArray_AS_STRING 1108 #define STRINGLIB_STR PyByteArray_AS_STRING
1043 #define STRINGLIB_NEW PyByteArray_FromStringAndSize 1109 #define STRINGLIB_NEW PyByteArray_FromStringAndSize
1044 #define STRINGLIB_ISSPACE Py_ISSPACE 1110 #define STRINGLIB_ISSPACE Py_ISSPACE
1045 #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) 1111 #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
1046 #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact 1112 #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1047 #define STRINGLIB_MUTABLE 1 1113 #define STRINGLIB_MUTABLE 1
1048 1114
1049 #include "stringlib/fastsearch.h" 1115 #include "stringlib/fastsearch.h"
1050 #include "stringlib/count.h" 1116 #include "stringlib/count.h"
1051 #include "stringlib/find.h" 1117 #include "stringlib/find.h"
1118 #include "stringlib/join.h"
1052 #include "stringlib/partition.h" 1119 #include "stringlib/partition.h"
1053 #include "stringlib/split.h" 1120 #include "stringlib/split.h"
1054 #include "stringlib/ctype.h" 1121 #include "stringlib/ctype.h"
1055 #include "stringlib/transmogrify.h" 1122 #include "stringlib/transmogrify.h"
1056 1123
1057 1124
1058 /* The following Py_LOCAL_INLINE and Py_LOCAL functions 1125 /* The following Py_LOCAL_INLINE and Py_LOCAL functions
1059 were copied from the old char* style string object. */ 1126 were copied from the old char* style string object. */
1060 1127
1061 /* helper macro to fixup start/end slice values */ 1128 /* helper macro to fixup start/end slice values */
(...skipping 11 matching lines...) Expand all
1073 start = 0; \ 1140 start = 0; \
1074 } 1141 }
1075 1142
1076 Py_LOCAL_INLINE(Py_ssize_t) 1143 Py_LOCAL_INLINE(Py_ssize_t)
1077 bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir) 1144 bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
1078 { 1145 {
1079 PyObject *subobj; 1146 PyObject *subobj;
1080 char byte; 1147 char byte;
1081 Py_buffer subbuf; 1148 Py_buffer subbuf;
1082 const char *sub; 1149 const char *sub;
1083 Py_ssize_t sub_len; 1150 Py_ssize_t len, sub_len;
1084 Py_ssize_t start=0, end=PY_SSIZE_T_MAX; 1151 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1085 Py_ssize_t res; 1152 Py_ssize_t res;
1086 1153
1087 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex", 1154 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1088 args, &subobj, &byte, &start, &end)) 1155 args, &subobj, &byte, &start, &end))
1089 return -2; 1156 return -2;
1090 1157
1091 if (subobj) { 1158 if (subobj) {
1092 if (_getbuffer(subobj, &subbuf) < 0) 1159 if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
1093 return -2; 1160 return -2;
1094 1161
1095 sub = subbuf.buf; 1162 sub = subbuf.buf;
1096 sub_len = subbuf.len; 1163 sub_len = subbuf.len;
1097 } 1164 }
1098 else { 1165 else {
1099 sub = &byte; 1166 sub = &byte;
1100 sub_len = 1; 1167 sub_len = 1;
1101 } 1168 }
1102 1169 len = PyByteArray_GET_SIZE(self);
1103 if (dir > 0) 1170
1104 res = stringlib_find_slice( 1171 ADJUST_INDICES(start, end, len);
1105 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 1172 if (end - start < sub_len)
1106 sub, sub_len, start, end); 1173 res = -1;
1107 else 1174 /* Issue #23573: FIXME, windows has no memrchr() */
1108 res = stringlib_rfind_slice( 1175 else if (sub_len == 1 && dir > 0) {
1109 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 1176 unsigned char needle = *sub;
1110 sub, sub_len, start, end); 1177 res = stringlib_fastsearch_memchr_1char(
1178 PyByteArray_AS_STRING(self) + start, end - start,
1179 needle, needle, FAST_SEARCH);
1180 if (res >= 0)
1181 res += start;
1182 }
1183 else {
1184 if (dir > 0)
1185 res = stringlib_find_slice(
1186 PyByteArray_AS_STRING(self), len,
1187 sub, sub_len, start, end);
1188 else
1189 res = stringlib_rfind_slice(
1190 PyByteArray_AS_STRING(self), len,
1191 sub, sub_len, start, end);
1192 }
1111 1193
1112 if (subobj) 1194 if (subobj)
1113 PyBuffer_Release(&subbuf); 1195 PyBuffer_Release(&subbuf);
1114 1196
1115 return res; 1197 return res;
1116 } 1198 }
1117 1199
1118 PyDoc_STRVAR(find__doc__, 1200 PyDoc_STRVAR(find__doc__,
1119 "B.find(sub[, start[, end]]) -> int\n\ 1201 "B.find(sub[, start[, end]]) -> int\n\
1120 \n\ 1202 \n\
(...skipping 29 matching lines...) Expand all
1150 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; 1232 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
1151 1233
1152 Py_buffer vsub; 1234 Py_buffer vsub;
1153 PyObject *count_obj; 1235 PyObject *count_obj;
1154 1236
1155 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte, 1237 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1156 &start, &end)) 1238 &start, &end))
1157 return NULL; 1239 return NULL;
1158 1240
1159 if (sub_obj) { 1241 if (sub_obj) {
1160 if (_getbuffer(sub_obj, &vsub) < 0) 1242 if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
1161 return NULL; 1243 return NULL;
1162 1244
1163 sub = vsub.buf; 1245 sub = vsub.buf;
1164 sub_len = vsub.len; 1246 sub_len = vsub.len;
1165 } 1247 }
1166 else { 1248 else {
1167 sub = &byte; 1249 sub = &byte;
1168 sub_len = 1; 1250 sub_len = 1;
1169 } 1251 }
1170 1252
1171 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self)); 1253 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
1172 1254
1173 count_obj = PyLong_FromSsize_t( 1255 count_obj = PyLong_FromSsize_t(
1174 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) 1256 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
1175 ); 1257 );
1176 1258
1177 if (sub_obj) 1259 if (sub_obj)
1178 PyBuffer_Release(&vsub); 1260 PyBuffer_Release(&vsub);
1179 1261
1180 return count_obj; 1262 return count_obj;
1181 } 1263 }
1182 1264
1183 PyDoc_STRVAR(clear__doc__, 1265 /*[clinic input]
1184 "B.clear() -> None\n\ 1266 bytearray.clear
1185 \n\ 1267
1186 Remove all items from B."); 1268 self: self(type="PyByteArrayObject *")
1187 1269
1188 static PyObject * 1270 Remove all items from the bytearray.
1189 bytearray_clear(PyByteArrayObject *self) 1271 [clinic start generated code]*/
1272
1273 static PyObject *
1274 bytearray_clear_impl(PyByteArrayObject *self)
1275 /*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/
1190 { 1276 {
1191 if (PyByteArray_Resize((PyObject *)self, 0) < 0) 1277 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1192 return NULL; 1278 return NULL;
1193 Py_RETURN_NONE; 1279 Py_RETURN_NONE;
1194 } 1280 }
1195 1281
1196 PyDoc_STRVAR(copy__doc__, 1282 /*[clinic input]
1197 "B.copy() -> bytearray\n\ 1283 bytearray.copy
1198 \n\ 1284
1199 Return a copy of B."); 1285 self: self(type="PyByteArrayObject *")
1200 1286
1201 static PyObject * 1287 Return a copy of B.
1202 bytearray_copy(PyByteArrayObject *self) 1288 [clinic start generated code]*/
1289
1290 static PyObject *
1291 bytearray_copy_impl(PyByteArrayObject *self)
1292 /*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/
1203 { 1293 {
1204 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self) , 1294 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self) ,
1205 PyByteArray_GET_SIZE(self)); 1295 PyByteArray_GET_SIZE(self));
1206 } 1296 }
1207 1297
1208 PyDoc_STRVAR(index__doc__, 1298 PyDoc_STRVAR(index__doc__,
1209 "B.index(sub[, start[, end]]) -> int\n\ 1299 "B.index(sub[, start[, end]]) -> int\n\
1210 \n\ 1300 \n\
1211 Like B.find() but raise ValueError when the subsection is not found."); 1301 Like B.find() but raise ValueError when the subsection is not found.");
1212 1302
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1265 1355
1266 1356
1267 static int 1357 static int
1268 bytearray_contains(PyObject *self, PyObject *arg) 1358 bytearray_contains(PyObject *self, PyObject *arg)
1269 { 1359 {
1270 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); 1360 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1271 if (ival == -1 && PyErr_Occurred()) { 1361 if (ival == -1 && PyErr_Occurred()) {
1272 Py_buffer varg; 1362 Py_buffer varg;
1273 Py_ssize_t pos; 1363 Py_ssize_t pos;
1274 PyErr_Clear(); 1364 PyErr_Clear();
1275 if (_getbuffer(arg, &varg) < 0) 1365 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0)
1276 return -1; 1366 return -1;
1277 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self), 1367 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1278 varg.buf, varg.len, 0); 1368 varg.buf, varg.len, 0);
1279 PyBuffer_Release(&varg); 1369 PyBuffer_Release(&varg);
1280 return pos >= 0; 1370 return pos >= 0;
1281 } 1371 }
1282 if (ival < 0 || ival >= 256) { 1372 if (ival < 0 || ival >= 256) {
1283 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); 1373 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1284 return -1; 1374 return -1;
1285 } 1375 }
(...skipping 10 matching lines...) Expand all
1296 _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start , 1386 _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start ,
1297 Py_ssize_t end, int direction) 1387 Py_ssize_t end, int direction)
1298 { 1388 {
1299 Py_ssize_t len = PyByteArray_GET_SIZE(self); 1389 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1300 const char* str; 1390 const char* str;
1301 Py_buffer vsubstr; 1391 Py_buffer vsubstr;
1302 int rv = 0; 1392 int rv = 0;
1303 1393
1304 str = PyByteArray_AS_STRING(self); 1394 str = PyByteArray_AS_STRING(self);
1305 1395
1306 if (_getbuffer(substr, &vsubstr) < 0) 1396 if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0)
1307 return -1; 1397 return -1;
1308 1398
1309 ADJUST_INDICES(start, end, len); 1399 ADJUST_INDICES(start, end, len);
1310 1400
1311 if (direction < 0) { 1401 if (direction < 0) {
1312 /* startswith */ 1402 /* startswith */
1313 if (start+vsubstr.len > len) { 1403 if (start+vsubstr.len > len) {
1314 goto done; 1404 goto done;
1315 } 1405 }
1316 } else { 1406 } else {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1411 if (PyErr_ExceptionMatches(PyExc_TypeError)) 1501 if (PyErr_ExceptionMatches(PyExc_TypeError))
1412 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " 1502 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1413 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); 1503 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
1414 return NULL; 1504 return NULL;
1415 } 1505 }
1416 else 1506 else
1417 return PyBool_FromLong(result); 1507 return PyBool_FromLong(result);
1418 } 1508 }
1419 1509
1420 1510
1421 PyDoc_STRVAR(translate__doc__, 1511 /*[clinic input]
1422 "B.translate(table[, deletechars]) -> bytearray\n\ 1512 bytearray.translate
1423 \n\ 1513
1424 Return a copy of B, where all characters occurring in the\n\ 1514 self: self(type="PyByteArrayObject *")
1425 optional argument deletechars are removed, and the remaining\n\ 1515 table: object
1426 characters have been mapped through the given translation\n\ 1516 Translation table, which must be a bytes object of length 256.
1427 table, which must be a bytes object of length 256."); 1517 [
1428 1518 deletechars: object
1429 static PyObject * 1519 ]
1430 bytearray_translate(PyByteArrayObject *self, PyObject *args) 1520 /
1431 { 1521
1432 register char *input, *output; 1522 Return a copy with each character mapped by the given translation table.
1433 register const char *table; 1523
1434 register Py_ssize_t i, c; 1524 All characters occurring in the optional argument deletechars are removed.
1525 The remaining characters are mapped through the given translation table.
1526 [clinic start generated code]*/
1527
1528 static PyObject *
1529 bytearray_translate_impl(PyByteArrayObject *self, PyObject *table,
1530 int group_right_1, PyObject *deletechars)
1531 /*[clinic end generated code: output=2bebc86a9a1ff083 input=b749ad85f4860824]*/
1532 {
1533 char *input, *output;
1534 const char *table_chars;
1535 Py_ssize_t i, c;
1435 PyObject *input_obj = (PyObject*)self; 1536 PyObject *input_obj = (PyObject*)self;
1436 const char *output_start; 1537 const char *output_start;
1437 Py_ssize_t inlen; 1538 Py_ssize_t inlen;
1438 PyObject *result = NULL; 1539 PyObject *result = NULL;
1439 int trans_table[256]; 1540 int trans_table[256];
1440 PyObject *tableobj = NULL, *delobj = NULL;
1441 Py_buffer vtable, vdel; 1541 Py_buffer vtable, vdel;
1442 1542
1443 if (!PyArg_UnpackTuple(args, "translate", 1, 2, 1543 if (table == Py_None) {
1444 &tableobj, &delobj)) 1544 table_chars = NULL;
1445 return NULL;
1446
1447 if (tableobj == Py_None) {
1448 table = NULL; 1545 table = NULL;
1449 tableobj = NULL; 1546 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
1450 } else if (_getbuffer(tableobj, &vtable) < 0) {
1451 return NULL; 1547 return NULL;
1452 } else { 1548 } else {
1453 if (vtable.len != 256) { 1549 if (vtable.len != 256) {
1454 PyErr_SetString(PyExc_ValueError, 1550 PyErr_SetString(PyExc_ValueError,
1455 "translation table must be 256 characters long"); 1551 "translation table must be 256 characters long");
1456 PyBuffer_Release(&vtable); 1552 PyBuffer_Release(&vtable);
1457 return NULL; 1553 return NULL;
1458 } 1554 }
1459 table = (const char*)vtable.buf; 1555 table_chars = (const char*)vtable.buf;
1460 } 1556 }
1461 1557
1462 if (delobj != NULL) { 1558 if (deletechars != NULL) {
1463 if (_getbuffer(delobj, &vdel) < 0) { 1559 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
1464 if (tableobj != NULL) 1560 if (table != NULL)
1465 PyBuffer_Release(&vtable); 1561 PyBuffer_Release(&vtable);
1466 return NULL; 1562 return NULL;
1467 } 1563 }
1468 } 1564 }
1469 else { 1565 else {
1470 vdel.buf = NULL; 1566 vdel.buf = NULL;
1471 vdel.len = 0; 1567 vdel.len = 0;
1472 } 1568 }
1473 1569
1474 inlen = PyByteArray_GET_SIZE(input_obj); 1570 inlen = PyByteArray_GET_SIZE(input_obj);
1475 result = PyByteArray_FromStringAndSize((char *)NULL, inlen); 1571 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1476 if (result == NULL) 1572 if (result == NULL)
1477 goto done; 1573 goto done;
1478 output_start = output = PyByteArray_AsString(result); 1574 output_start = output = PyByteArray_AsString(result);
1479 input = PyByteArray_AS_STRING(input_obj); 1575 input = PyByteArray_AS_STRING(input_obj);
1480 1576
1481 if (vdel.len == 0 && table != NULL) { 1577 if (vdel.len == 0 && table_chars != NULL) {
1482 /* If no deletions are required, use faster code */ 1578 /* If no deletions are required, use faster code */
1483 for (i = inlen; --i >= 0; ) { 1579 for (i = inlen; --i >= 0; ) {
1484 c = Py_CHARMASK(*input++); 1580 c = Py_CHARMASK(*input++);
1485 *output++ = table[c]; 1581 *output++ = table_chars[c];
1486 } 1582 }
1487 goto done; 1583 goto done;
1488 } 1584 }
1489 1585
1490 if (table == NULL) { 1586 if (table_chars == NULL) {
1491 for (i = 0; i < 256; i++) 1587 for (i = 0; i < 256; i++)
1492 trans_table[i] = Py_CHARMASK(i); 1588 trans_table[i] = Py_CHARMASK(i);
1493 } else { 1589 } else {
1494 for (i = 0; i < 256; i++) 1590 for (i = 0; i < 256; i++)
1495 trans_table[i] = Py_CHARMASK(table[i]); 1591 trans_table[i] = Py_CHARMASK(table_chars[i]);
1496 } 1592 }
1497 1593
1498 for (i = 0; i < vdel.len; i++) 1594 for (i = 0; i < vdel.len; i++)
1499 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; 1595 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1500 1596
1501 for (i = inlen; --i >= 0; ) { 1597 for (i = inlen; --i >= 0; ) {
1502 c = Py_CHARMASK(*input++); 1598 c = Py_CHARMASK(*input++);
1503 if (trans_table[c] != -1) 1599 if (trans_table[c] != -1)
1504 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) 1600 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1505 continue; 1601 continue;
1506 } 1602 }
1507 /* Fix the size of the resulting string */ 1603 /* Fix the size of the resulting string */
1508 if (inlen > 0) 1604 if (inlen > 0)
1509 if (PyByteArray_Resize(result, output - output_start) < 0) { 1605 if (PyByteArray_Resize(result, output - output_start) < 0) {
1510 Py_CLEAR(result); 1606 Py_CLEAR(result);
1511 goto done; 1607 goto done;
1512 } 1608 }
1513 1609
1514 done: 1610 done:
1515 if (tableobj != NULL) 1611 if (table != NULL)
1516 PyBuffer_Release(&vtable); 1612 PyBuffer_Release(&vtable);
1517 if (delobj != NULL) 1613 if (deletechars != NULL)
1518 PyBuffer_Release(&vdel); 1614 PyBuffer_Release(&vdel);
1519 return result; 1615 return result;
1520 } 1616 }
1521 1617
1522 1618
1523 static PyObject * 1619 /*[clinic input]
1524 bytearray_maketrans(PyObject *null, PyObject *args) 1620
1525 { 1621 @staticmethod
1526 return _Py_bytes_maketrans(args); 1622 bytearray.maketrans
1623
1624 frm: Py_buffer
1625 to: Py_buffer
1626 /
1627
1628 Return a translation table useable for the bytes or bytearray translate method.
1629
1630 The returned table will be one where each byte in frm is mapped to the byte at
1631 the same position in to.
1632
1633 The bytes objects frm and to must be of the same length.
1634 [clinic start generated code]*/
1635
1636 static PyObject *
1637 bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to)
1638 /*[clinic end generated code: output=1df267d99f56b15e input=5925a81d2fbbf151]*/
1639 {
1640 return _Py_bytes_maketrans(frm, to);
1527 } 1641 }
1528 1642
1529 1643
1530 /* find and count characters and substrings */ 1644 /* find and count characters and substrings */
1531 1645
1532 #define findchar(target, target_len, c) \ 1646 #define findchar(target, target_len, c) \
1533 ((char *)memchr((const void *)(target), c, target_len)) 1647 ((char *)memchr((const void *)(target), c, target_len))
1534 1648
1535 1649
1536 /* Bytes ops must return a string, create a copy */ 1650 /* Bytes ops must return a string, create a copy */
(...skipping 470 matching lines...) Expand 10 before | Expand all | Expand 10 after
2007 if (from_len == 1) { 2121 if (from_len == 1) {
2008 return replace_single_character(self, from_s[0], 2122 return replace_single_character(self, from_s[0],
2009 to_s, to_len, maxcount); 2123 to_s, to_len, maxcount);
2010 } else { 2124 } else {
2011 /* len('from')>=2, len('to')>=1 */ 2125 /* len('from')>=2, len('to')>=1 */
2012 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount) ; 2126 return replace_substring(self, from_s, from_len, to_s, to_len, maxcount) ;
2013 } 2127 }
2014 } 2128 }
2015 2129
2016 2130
2017 PyDoc_STRVAR(replace__doc__, 2131 /*[clinic input]
2018 "B.replace(old, new[, count]) -> bytearray\n\ 2132 bytearray.replace
2019 \n\ 2133
2020 Return a copy of B with all occurrences of subsection\n\ 2134 old: Py_buffer
2021 old replaced by new. If the optional argument count is\n\ 2135 new: Py_buffer
2022 given, only the first count occurrences are replaced."); 2136 count: Py_ssize_t = -1
2023 2137 Maximum number of occurrences to replace.
2024 static PyObject * 2138 -1 (the default value) means replace all occurrences.
2025 bytearray_replace(PyByteArrayObject *self, PyObject *args) 2139 /
2026 { 2140
2027 Py_ssize_t count = -1; 2141 Return a copy with all occurrences of substring old replaced by new.
2028 PyObject *from, *to, *res; 2142
2029 Py_buffer vfrom, vto; 2143 If the optional argument count is given, only the first count occurrences are
2030 2144 replaced.
2031 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) 2145 [clinic start generated code]*/
2032 return NULL; 2146
2033 2147 static PyObject *
2034 if (_getbuffer(from, &vfrom) < 0) 2148 bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
2035 return NULL; 2149 Py_buffer *new, Py_ssize_t count)
2036 if (_getbuffer(to, &vto) < 0) { 2150 /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
2037 PyBuffer_Release(&vfrom); 2151 {
2038 return NULL; 2152 return (PyObject *)replace((PyByteArrayObject *) self,
2039 } 2153 old->buf, old->len,
2040 2154 new->buf, new->len, count);
2041 res = (PyObject *)replace((PyByteArrayObject *) self, 2155 }
2042 vfrom.buf, vfrom.len, 2156
2043 vto.buf, vto.len, count); 2157 /*[clinic input]
2044 2158 bytearray.split
2045 PyBuffer_Release(&vfrom); 2159
2046 PyBuffer_Release(&vto); 2160 sep: object = None
2047 return res; 2161 The delimiter according which to split the bytearray.
2048 } 2162 None (the default value) means split on ASCII whitespace characters
2049 2163 (space, tab, return, newline, formfeed, vertical tab).
2050 PyDoc_STRVAR(split__doc__, 2164 maxsplit: Py_ssize_t = -1
2051 "B.split(sep=None, maxsplit=-1) -> list of bytearrays\n\ 2165 Maximum number of splits to do.
2052 \n\ 2166 -1 (the default value) means no limit.
2053 Return a list of the sections in B, using sep as the delimiter.\n\ 2167
2054 If sep is not given, B is split on ASCII whitespace characters\n\ 2168 Return a list of the sections in the bytearray, using sep as the delimiter.
2055 (space, tab, return, newline, formfeed, vertical tab).\n\ 2169 [clinic start generated code]*/
2056 If maxsplit is given, at most maxsplit splits are done."); 2170
2057 2171 static PyObject *
2058 static PyObject * 2172 bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
2059 bytearray_split(PyByteArrayObject *self, PyObject *args, PyObject *kwds) 2173 Py_ssize_t maxsplit)
2060 { 2174 /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
2061 static char *kwlist[] = {"sep", "maxsplit", 0}; 2175 {
2062 Py_ssize_t len = PyByteArray_GET_SIZE(self), n; 2176 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2063 Py_ssize_t maxsplit = -1;
2064 const char *s = PyByteArray_AS_STRING(self), *sub; 2177 const char *s = PyByteArray_AS_STRING(self), *sub;
2065 PyObject *list, *subobj = Py_None; 2178 PyObject *list;
2066 Py_buffer vsub; 2179 Py_buffer vsub;
2067 2180
2068 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split",
2069 kwlist, &subobj, &maxsplit))
2070 return NULL;
2071 if (maxsplit < 0) 2181 if (maxsplit < 0)
2072 maxsplit = PY_SSIZE_T_MAX; 2182 maxsplit = PY_SSIZE_T_MAX;
2073 2183
2074 if (subobj == Py_None) 2184 if (sep == Py_None)
2075 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); 2185 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
2076 2186
2077 if (_getbuffer(subobj, &vsub) < 0) 2187 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
2078 return NULL; 2188 return NULL;
2079 sub = vsub.buf; 2189 sub = vsub.buf;
2080 n = vsub.len; 2190 n = vsub.len;
2081 2191
2082 list = stringlib_split( 2192 list = stringlib_split(
2083 (PyObject*) self, s, len, sub, n, maxsplit 2193 (PyObject*) self, s, len, sub, n, maxsplit
2084 ); 2194 );
2085 PyBuffer_Release(&vsub); 2195 PyBuffer_Release(&vsub);
2086 return list; 2196 return list;
2087 } 2197 }
2088 2198
2089 PyDoc_STRVAR(partition__doc__, 2199 /*[clinic input]
2090 "B.partition(sep) -> (head, sep, tail)\n\ 2200 bytearray.partition
2091 \n\ 2201
2092 Search for the separator sep in B, and return the part before it,\n\ 2202 self: self(type="PyByteArrayObject *")
2093 the separator itself, and the part after it. If the separator is not\n\ 2203 sep: object
2094 found, returns B and two empty bytearray objects."); 2204 /
2095 2205
2096 static PyObject * 2206 Partition the bytearray into three parts using the given separator.
2097 bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj) 2207
2208 This will search for the separator sep in the bytearray. If the separator is
2209 found, returns a 3-tuple containing the part before the separator, the
2210 separator itself, and the part after it.
2211
2212 If the separator is not found, returns a 3-tuple containing the original
2213 bytearray object and two empty bytearray objects.
2214 [clinic start generated code]*/
2215
2216 static PyObject *
2217 bytearray_partition(PyByteArrayObject *self, PyObject *sep)
2218 /*[clinic end generated code: output=45d2525ddd35f957 input=7d7fe37b1696d506]*/
2098 { 2219 {
2099 PyObject *bytesep, *result; 2220 PyObject *bytesep, *result;
2100 2221
2101 bytesep = PyByteArray_FromObject(sep_obj); 2222 bytesep = PyByteArray_FromObject(sep);
2102 if (! bytesep) 2223 if (! bytesep)
2103 return NULL; 2224 return NULL;
2104 2225
2105 result = stringlib_partition( 2226 result = stringlib_partition(
2106 (PyObject*) self, 2227 (PyObject*) self,
2107 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 2228 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2108 bytesep, 2229 bytesep,
2109 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) 2230 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2110 ); 2231 );
2111 2232
2112 Py_DECREF(bytesep); 2233 Py_DECREF(bytesep);
2113 return result; 2234 return result;
2114 } 2235 }
2115 2236
2116 PyDoc_STRVAR(rpartition__doc__, 2237 /*[clinic input]
2117 "B.rpartition(sep) -> (head, sep, tail)\n\ 2238 bytearray.rpartition
2118 \n\ 2239
2119 Search for the separator sep in B, starting at the end of B,\n\ 2240 self: self(type="PyByteArrayObject *")
2120 and return the part before it, the separator itself, and the\n\ 2241 sep: object
2121 part after it. If the separator is not found, returns two empty\n\ 2242 /
2122 bytearray objects and B."); 2243
2123 2244 Partition the bytes into three parts using the given separator.
2124 static PyObject * 2245
2125 bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj) 2246 This will search for the separator sep in the bytearray, starting and the end.
2247 If the separator is found, returns a 3-tuple containing the part before the
2248 separator, the separator itself, and the part after it.
2249
2250 If the separator is not found, returns a 3-tuple containing two empty bytearray
2251 objects and the original bytearray object.
2252 [clinic start generated code]*/
2253
2254 static PyObject *
2255 bytearray_rpartition(PyByteArrayObject *self, PyObject *sep)
2256 /*[clinic end generated code: output=440de3c9426115e8 input=9b8cd540c1b75853]*/
2126 { 2257 {
2127 PyObject *bytesep, *result; 2258 PyObject *bytesep, *result;
2128 2259
2129 bytesep = PyByteArray_FromObject(sep_obj); 2260 bytesep = PyByteArray_FromObject(sep);
2130 if (! bytesep) 2261 if (! bytesep)
2131 return NULL; 2262 return NULL;
2132 2263
2133 result = stringlib_rpartition( 2264 result = stringlib_rpartition(
2134 (PyObject*) self, 2265 (PyObject*) self,
2135 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 2266 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2136 bytesep, 2267 bytesep,
2137 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) 2268 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2138 ); 2269 );
2139 2270
2140 Py_DECREF(bytesep); 2271 Py_DECREF(bytesep);
2141 return result; 2272 return result;
2142 } 2273 }
2143 2274
2144 PyDoc_STRVAR(rsplit__doc__, 2275 /*[clinic input]
2145 "B.rsplit(sep=None, maxsplit=-1) -> list of bytearrays\n\ 2276 bytearray.rsplit = bytearray.split
2146 \n\ 2277
2147 Return a list of the sections in B, using sep as the delimiter,\n\ 2278 Return a list of the sections in the bytearray, using sep as the delimiter.
2148 starting at the end of B and working to the front.\n\ 2279
2149 If sep is not given, B is split on ASCII whitespace characters\n\ 2280 Splitting is done starting at the end of the bytearray and working to the front.
2150 (space, tab, return, newline, formfeed, vertical tab).\n\ 2281 [clinic start generated code]*/
2151 If maxsplit is given, at most maxsplit splits are done."); 2282
2152 2283 static PyObject *
2153 static PyObject * 2284 bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
2154 bytearray_rsplit(PyByteArrayObject *self, PyObject *args, PyObject *kwds) 2285 Py_ssize_t maxsplit)
2155 { 2286 /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
2156 static char *kwlist[] = {"sep", "maxsplit", 0}; 2287 {
2157 Py_ssize_t len = PyByteArray_GET_SIZE(self), n; 2288 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2158 Py_ssize_t maxsplit = -1;
2159 const char *s = PyByteArray_AS_STRING(self), *sub; 2289 const char *s = PyByteArray_AS_STRING(self), *sub;
2160 PyObject *list, *subobj = Py_None; 2290 PyObject *list;
2161 Py_buffer vsub; 2291 Py_buffer vsub;
2162 2292
2163 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit",
2164 kwlist, &subobj, &maxsplit))
2165 return NULL;
2166 if (maxsplit < 0) 2293 if (maxsplit < 0)
2167 maxsplit = PY_SSIZE_T_MAX; 2294 maxsplit = PY_SSIZE_T_MAX;
2168 2295
2169 if (subobj == Py_None) 2296 if (sep == Py_None)
2170 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); 2297 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
2171 2298
2172 if (_getbuffer(subobj, &vsub) < 0) 2299 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
2173 return NULL; 2300 return NULL;
2174 sub = vsub.buf; 2301 sub = vsub.buf;
2175 n = vsub.len; 2302 n = vsub.len;
2176 2303
2177 list = stringlib_rsplit( 2304 list = stringlib_rsplit(
2178 (PyObject*) self, s, len, sub, n, maxsplit 2305 (PyObject*) self, s, len, sub, n, maxsplit
2179 ); 2306 );
2180 PyBuffer_Release(&vsub); 2307 PyBuffer_Release(&vsub);
2181 return list; 2308 return list;
2182 } 2309 }
2183 2310
2184 PyDoc_STRVAR(reverse__doc__, 2311 /*[clinic input]
2185 "B.reverse() -> None\n\ 2312 bytearray.reverse
2186 \n\ 2313
2187 Reverse the order of the values in B in place."); 2314 self: self(type="PyByteArrayObject *")
2188 static PyObject * 2315
2189 bytearray_reverse(PyByteArrayObject *self, PyObject *unused) 2316 Reverse the order of the values in B in place.
2317 [clinic start generated code]*/
2318
2319 static PyObject *
2320 bytearray_reverse_impl(PyByteArrayObject *self)
2321 /*[clinic end generated code: output=9f7616f29ab309d3 input=7933a499b8597bd1]*/
2190 { 2322 {
2191 char swap, *head, *tail; 2323 char swap, *head, *tail;
2192 Py_ssize_t i, j, n = Py_SIZE(self); 2324 Py_ssize_t i, j, n = Py_SIZE(self);
2193 2325
2194 j = n / 2; 2326 j = n / 2;
2195 head = self->ob_bytes; 2327 head = PyByteArray_AS_STRING(self);
2196 tail = head + n - 1; 2328 tail = head + n - 1;
2197 for (i = 0; i < j; i++) { 2329 for (i = 0; i < j; i++) {
2198 swap = *head; 2330 swap = *head;
2199 *head++ = *tail; 2331 *head++ = *tail;
2200 *tail-- = swap; 2332 *tail-- = swap;
2201 } 2333 }
2202 2334
2203 Py_RETURN_NONE; 2335 Py_RETURN_NONE;
2204 } 2336 }
2205 2337
2206 PyDoc_STRVAR(insert__doc__, 2338
2207 "B.insert(index, int) -> None\n\ 2339 /*[python input]
2208 \n\ 2340 class bytesvalue_converter(CConverter):
2209 Insert a single item into the bytearray before the given index."); 2341 type = 'int'
2210 static PyObject * 2342 converter = '_getbytevalue'
2211 bytearray_insert(PyByteArrayObject *self, PyObject *args) 2343 [python start generated code]*/
2212 { 2344 /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
2213 PyObject *value; 2345
2214 int ival; 2346
2215 Py_ssize_t where, n = Py_SIZE(self); 2347 /*[clinic input]
2216 2348 bytearray.insert
2217 if (!PyArg_ParseTuple(args, "nO:insert", &where, &value)) 2349
2218 return NULL; 2350 self: self(type="PyByteArrayObject *")
2351 index: Py_ssize_t
2352 The index where the value is to be inserted.
2353 item: bytesvalue
2354 The item to be inserted.
2355 /
2356
2357 Insert a single item into the bytearray before the given index.
2358 [clinic start generated code]*/
2359
2360 static PyObject *
2361 bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item)
2362 /*[clinic end generated code: output=76c775a70e7b07b7 input=833766836ba30e1e]*/
2363 {
2364 Py_ssize_t n = Py_SIZE(self);
2365 char *buf;
2219 2366
2220 if (n == PY_SSIZE_T_MAX) { 2367 if (n == PY_SSIZE_T_MAX) {
2221 PyErr_SetString(PyExc_OverflowError, 2368 PyErr_SetString(PyExc_OverflowError,
2222 "cannot add more objects to bytearray"); 2369 "cannot add more objects to bytearray");
2223 return NULL; 2370 return NULL;
2224 } 2371 }
2225 if (!_getbytevalue(value, &ival))
2226 return NULL;
2227 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) 2372 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2228 return NULL; 2373 return NULL;
2229 2374 buf = PyByteArray_AS_STRING(self);
2230 if (where < 0) { 2375
2231 where += n; 2376 if (index < 0) {
2232 if (where < 0) 2377 index += n;
2233 where = 0; 2378 if (index < 0)
2234 } 2379 index = 0;
2235 if (where > n) 2380 }
2236 where = n; 2381 if (index > n)
2237 memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where); 2382 index = n;
2238 self->ob_bytes[where] = ival; 2383 memmove(buf + index + 1, buf + index, n - index);
2384 buf[index] = item;
2239 2385
2240 Py_RETURN_NONE; 2386 Py_RETURN_NONE;
2241 } 2387 }
2242 2388
2243 PyDoc_STRVAR(append__doc__, 2389 /*[clinic input]
2244 "B.append(int) -> None\n\ 2390 bytearray.append
2245 \n\ 2391
2246 Append a single item to the end of B."); 2392 self: self(type="PyByteArrayObject *")
2247 static PyObject * 2393 item: bytesvalue
2248 bytearray_append(PyByteArrayObject *self, PyObject *arg) 2394 The item to be appended.
2249 { 2395 /
2250 int value; 2396
2397 Append a single item to the end of the bytearray.
2398 [clinic start generated code]*/
2399
2400 static PyObject *
2401 bytearray_append_impl(PyByteArrayObject *self, int item)
2402 /*[clinic end generated code: output=a154e19ed1886cb6 input=ae56ea87380407cc]*/
2403 {
2251 Py_ssize_t n = Py_SIZE(self); 2404 Py_ssize_t n = Py_SIZE(self);
2252 2405
2253 if (! _getbytevalue(arg, &value))
2254 return NULL;
2255 if (n == PY_SSIZE_T_MAX) { 2406 if (n == PY_SSIZE_T_MAX) {
2256 PyErr_SetString(PyExc_OverflowError, 2407 PyErr_SetString(PyExc_OverflowError,
2257 "cannot add more objects to bytearray"); 2408 "cannot add more objects to bytearray");
2258 return NULL; 2409 return NULL;
2259 } 2410 }
2260 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) 2411 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2261 return NULL; 2412 return NULL;
2262 2413
2263 self->ob_bytes[n] = value; 2414 PyByteArray_AS_STRING(self)[n] = item;
2264 2415
2265 Py_RETURN_NONE; 2416 Py_RETURN_NONE;
2266 } 2417 }
2267 2418
2268 PyDoc_STRVAR(extend__doc__, 2419 /*[clinic input]
2269 "B.extend(iterable_of_ints) -> None\n\ 2420 bytearray.extend
2270 \n\ 2421
2271 Append all the elements from the iterator or sequence to the\n\ 2422 self: self(type="PyByteArrayObject *")
2272 end of B."); 2423 iterable_of_ints: object
2273 static PyObject * 2424 The iterable of items to append.
2274 bytearray_extend(PyByteArrayObject *self, PyObject *arg) 2425 /
2426
2427 Append all the items from the iterator or sequence to the end of the bytearray.
2428 [clinic start generated code]*/
2429
2430 static PyObject *
2431 bytearray_extend(PyByteArrayObject *self, PyObject *iterable_of_ints)
2432 /*[clinic end generated code: output=98155dbe249170b1 input=ce83a5d75b70d850]*/
2275 { 2433 {
2276 PyObject *it, *item, *bytearray_obj; 2434 PyObject *it, *item, *bytearray_obj;
2277 Py_ssize_t buf_size = 0, len = 0; 2435 Py_ssize_t buf_size = 0, len = 0;
2278 int value; 2436 int value;
2279 char *buf; 2437 char *buf;
2280 2438
2281 /* bytearray_setslice code only accepts something supporting PEP 3118. */ 2439 /* bytearray_setslice code only accepts something supporting PEP 3118. */
2282 if (PyObject_CheckBuffer(arg)) { 2440 if (PyObject_CheckBuffer(iterable_of_ints)) {
2283 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1) 2441 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), iterable_of_i nts) == -1)
2284 return NULL; 2442 return NULL;
2285 2443
2286 Py_RETURN_NONE; 2444 Py_RETURN_NONE;
2287 } 2445 }
2288 2446
2289 it = PyObject_GetIter(arg); 2447 it = PyObject_GetIter(iterable_of_ints);
2290 if (it == NULL) 2448 if (it == NULL)
2291 return NULL; 2449 return NULL;
2292 2450
2293 /* Try to determine the length of the argument. 32 is arbitrary. */ 2451 /* Try to determine the length of the argument. 32 is arbitrary. */
2294 buf_size = _PyObject_LengthHint(arg, 32); 2452 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
2295 if (buf_size == -1) { 2453 if (buf_size == -1) {
2296 Py_DECREF(it); 2454 Py_DECREF(it);
2297 return NULL; 2455 return NULL;
2298 } 2456 }
2299 2457
2300 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); 2458 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
2301 if (bytearray_obj == NULL) { 2459 if (bytearray_obj == NULL) {
2302 Py_DECREF(it); 2460 Py_DECREF(it);
2303 return NULL; 2461 return NULL;
2304 } 2462 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2336 2494
2337 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { 2495 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) {
2338 Py_DECREF(bytearray_obj); 2496 Py_DECREF(bytearray_obj);
2339 return NULL; 2497 return NULL;
2340 } 2498 }
2341 Py_DECREF(bytearray_obj); 2499 Py_DECREF(bytearray_obj);
2342 2500
2343 Py_RETURN_NONE; 2501 Py_RETURN_NONE;
2344 } 2502 }
2345 2503
2346 PyDoc_STRVAR(pop__doc__, 2504 /*[clinic input]
2347 "B.pop([index]) -> int\n\ 2505 bytearray.pop
2348 \n\ 2506
2349 Remove and return a single item from B. If no index\n\ 2507 self: self(type="PyByteArrayObject *")
2350 argument is given, will pop the last value."); 2508 index: Py_ssize_t = -1
2351 static PyObject * 2509 The index from where to remove the item.
2352 bytearray_pop(PyByteArrayObject *self, PyObject *args) 2510 -1 (the default value) means remove the last item.
2511 /
2512
2513 Remove and return a single item from B.
2514
2515 If no index argument is given, will pop the last item.
2516 [clinic start generated code]*/
2517
2518 static PyObject *
2519 bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index)
2520 /*[clinic end generated code: output=e0ccd401f8021da8 input=0797e6c0ca9d5a85]*/
2353 { 2521 {
2354 int value; 2522 int value;
2355 Py_ssize_t where = -1, n = Py_SIZE(self); 2523 Py_ssize_t n = Py_SIZE(self);
2356 2524 char *buf;
2357 if (!PyArg_ParseTuple(args, "|n:pop", &where))
2358 return NULL;
2359 2525
2360 if (n == 0) { 2526 if (n == 0) {
2361 PyErr_SetString(PyExc_IndexError, 2527 PyErr_SetString(PyExc_IndexError,
2362 "pop from empty bytearray"); 2528 "pop from empty bytearray");
2363 return NULL; 2529 return NULL;
2364 } 2530 }
2365 if (where < 0) 2531 if (index < 0)
2366 where += n; 2532 index += Py_SIZE(self);
2367 if (where < 0 || where >= n) { 2533 if (index < 0 || index >= Py_SIZE(self)) {
2368 PyErr_SetString(PyExc_IndexError, "pop index out of range"); 2534 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2369 return NULL; 2535 return NULL;
2370 } 2536 }
2371 if (!_canresize(self)) 2537 if (!_canresize(self))
2372 return NULL; 2538 return NULL;
2373 2539
2374 value = self->ob_bytes[where]; 2540 buf = PyByteArray_AS_STRING(self);
2375 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where - 1); 2541 value = buf[index];
2542 memmove(buf + index, buf + index + 1, n - index);
2376 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2543 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2377 return NULL; 2544 return NULL;
2378 2545
2379 return PyLong_FromLong((unsigned char)value); 2546 return PyLong_FromLong((unsigned char)value);
2380 } 2547 }
2381 2548
2382 PyDoc_STRVAR(remove__doc__, 2549 /*[clinic input]
2383 "B.remove(int) -> None\n\ 2550 bytearray.remove
2384 \n\ 2551
2385 Remove the first occurrence of a value in B."); 2552 self: self(type="PyByteArrayObject *")
2386 static PyObject * 2553 value: bytesvalue
2387 bytearray_remove(PyByteArrayObject *self, PyObject *arg) 2554 The value to remove.
2388 { 2555 /
2389 int value; 2556
2557 Remove the first occurrence of a value in the bytearray.
2558 [clinic start generated code]*/
2559
2560 static PyObject *
2561 bytearray_remove_impl(PyByteArrayObject *self, int value)
2562 /*[clinic end generated code: output=d659e37866709c13 input=47560b11fd856c24]*/
2563 {
2390 Py_ssize_t where, n = Py_SIZE(self); 2564 Py_ssize_t where, n = Py_SIZE(self);
2391 2565 char *buf = PyByteArray_AS_STRING(self);
2392 if (! _getbytevalue(arg, &value))
2393 return NULL;
2394 2566
2395 for (where = 0; where < n; where++) { 2567 for (where = 0; where < n; where++) {
2396 if (self->ob_bytes[where] == value) 2568 if (buf[where] == value)
2397 break; 2569 break;
2398 } 2570 }
2399 if (where == n) { 2571 if (where == n) {
2400 PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); 2572 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
2401 return NULL; 2573 return NULL;
2402 } 2574 }
2403 if (!_canresize(self)) 2575 if (!_canresize(self))
2404 return NULL; 2576 return NULL;
2405 2577
2406 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where - 1); 2578 memmove(buf + where, buf + where + 1, n - where);
2407 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2579 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2408 return NULL; 2580 return NULL;
2409 2581
2410 Py_RETURN_NONE; 2582 Py_RETURN_NONE;
2411 } 2583 }
2412 2584
2413 /* XXX These two helpers could be optimized if argsize == 1 */ 2585 /* XXX These two helpers could be optimized if argsize == 1 */
2414 2586
2415 static Py_ssize_t 2587 static Py_ssize_t
2416 lstrip_helper(unsigned char *myptr, Py_ssize_t mysize, 2588 lstrip_helper(char *myptr, Py_ssize_t mysize,
2417 void *argptr, Py_ssize_t argsize) 2589 void *argptr, Py_ssize_t argsize)
2418 { 2590 {
2419 Py_ssize_t i = 0; 2591 Py_ssize_t i = 0;
2420 while (i < mysize && memchr(argptr, myptr[i], argsize)) 2592 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
2421 i++; 2593 i++;
2422 return i; 2594 return i;
2423 } 2595 }
2424 2596
2425 static Py_ssize_t 2597 static Py_ssize_t
2426 rstrip_helper(unsigned char *myptr, Py_ssize_t mysize, 2598 rstrip_helper(char *myptr, Py_ssize_t mysize,
2427 void *argptr, Py_ssize_t argsize) 2599 void *argptr, Py_ssize_t argsize)
2428 { 2600 {
2429 Py_ssize_t i = mysize - 1; 2601 Py_ssize_t i = mysize - 1;
2430 while (i >= 0 && memchr(argptr, myptr[i], argsize)) 2602 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
2431 i--; 2603 i--;
2432 return i + 1; 2604 return i + 1;
2433 } 2605 }
2434 2606
2435 PyDoc_STRVAR(strip__doc__, 2607 /*[clinic input]
2436 "B.strip([bytes]) -> bytearray\n\ 2608 bytearray.strip
2437 \n\ 2609
2438 Strip leading and trailing bytes contained in the argument\n\ 2610 bytes: object = None
2439 and return the result as a new bytearray.\n\ 2611 /
2440 If the argument is omitted, strip ASCII whitespace."); 2612
2441 static PyObject * 2613 Strip leading and trailing bytes contained in the argument.
2442 bytearray_strip(PyByteArrayObject *self, PyObject *args) 2614
2443 { 2615 If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2444 Py_ssize_t left, right, mysize, argsize; 2616 [clinic start generated code]*/
2445 void *myptr, *argptr; 2617
2446 PyObject *arg = Py_None; 2618 static PyObject *
2447 Py_buffer varg; 2619 bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
2448 if (!PyArg_ParseTuple(args, "|O:strip", &arg)) 2620 /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
2449 return NULL; 2621 {
2450 if (arg == Py_None) { 2622 Py_ssize_t left, right, mysize, byteslen;
2451 argptr = "\t\n\r\f\v "; 2623 char *myptr, *bytesptr;
2452 argsize = 6; 2624 Py_buffer vbytes;
2625
2626 if (bytes == Py_None) {
2627 bytesptr = "\t\n\r\f\v ";
2628 byteslen = 6;
2453 } 2629 }
2454 else { 2630 else {
2455 if (_getbuffer(arg, &varg) < 0) 2631 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2456 return NULL; 2632 return NULL;
2457 argptr = varg.buf; 2633 bytesptr = (char *) vbytes.buf;
2458 argsize = varg.len; 2634 byteslen = vbytes.len;
2459 } 2635 }
2460 myptr = self->ob_bytes; 2636 myptr = PyByteArray_AS_STRING(self);
2461 mysize = Py_SIZE(self); 2637 mysize = Py_SIZE(self);
2462 left = lstrip_helper(myptr, mysize, argptr, argsize); 2638 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
2463 if (left == mysize) 2639 if (left == mysize)
2464 right = left; 2640 right = left;
2465 else 2641 else
2466 right = rstrip_helper(myptr, mysize, argptr, argsize); 2642 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2467 if (arg != Py_None) 2643 if (bytes != Py_None)
2468 PyBuffer_Release(&varg); 2644 PyBuffer_Release(&vbytes);
2469 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); 2645 return PyByteArray_FromStringAndSize(myptr + left, right - left);
2470 } 2646 }
2471 2647
2472 PyDoc_STRVAR(lstrip__doc__, 2648 /*[clinic input]
2473 "B.lstrip([bytes]) -> bytearray\n\ 2649 bytearray.lstrip
2474 \n\ 2650
2475 Strip leading bytes contained in the argument\n\ 2651 bytes: object = None
2476 and return the result as a new bytearray.\n\ 2652 /
2477 If the argument is omitted, strip leading ASCII whitespace."); 2653
2478 static PyObject * 2654 Strip leading bytes contained in the argument.
2479 bytearray_lstrip(PyByteArrayObject *self, PyObject *args) 2655
2480 { 2656 If the argument is omitted or None, strip leading ASCII whitespace.
2481 Py_ssize_t left, right, mysize, argsize; 2657 [clinic start generated code]*/
2482 void *myptr, *argptr; 2658
2483 PyObject *arg = Py_None; 2659 static PyObject *
2484 Py_buffer varg; 2660 bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
2485 if (!PyArg_ParseTuple(args, "|O:lstrip", &arg)) 2661 /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
2486 return NULL; 2662 {
2487 if (arg == Py_None) { 2663 Py_ssize_t left, right, mysize, byteslen;
2488 argptr = "\t\n\r\f\v "; 2664 char *myptr, *bytesptr;
2489 argsize = 6; 2665 Py_buffer vbytes;
2666
2667 if (bytes == Py_None) {
2668 bytesptr = "\t\n\r\f\v ";
2669 byteslen = 6;
2490 } 2670 }
2491 else { 2671 else {
2492 if (_getbuffer(arg, &varg) < 0) 2672 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2493 return NULL; 2673 return NULL;
2494 argptr = varg.buf; 2674 bytesptr = (char *) vbytes.buf;
2495 argsize = varg.len; 2675 byteslen = vbytes.len;
2496 } 2676 }
2497 myptr = self->ob_bytes; 2677 myptr = PyByteArray_AS_STRING(self);
2498 mysize = Py_SIZE(self); 2678 mysize = Py_SIZE(self);
2499 left = lstrip_helper(myptr, mysize, argptr, argsize); 2679 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
2500 right = mysize; 2680 right = mysize;
2501 if (arg != Py_None) 2681 if (bytes != Py_None)
2502 PyBuffer_Release(&varg); 2682 PyBuffer_Release(&vbytes);
2503 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); 2683 return PyByteArray_FromStringAndSize(myptr + left, right - left);
2504 } 2684 }
2505 2685
2506 PyDoc_STRVAR(rstrip__doc__, 2686 /*[clinic input]
2507 "B.rstrip([bytes]) -> bytearray\n\ 2687 bytearray.rstrip
2508 \n\ 2688
2509 Strip trailing bytes contained in the argument\n\ 2689 bytes: object = None
2510 and return the result as a new bytearray.\n\ 2690 /
2511 If the argument is omitted, strip trailing ASCII whitespace."); 2691
2512 static PyObject * 2692 Strip trailing bytes contained in the argument.
2513 bytearray_rstrip(PyByteArrayObject *self, PyObject *args) 2693
2514 { 2694 If the argument is omitted or None, strip trailing ASCII whitespace.
2515 Py_ssize_t right, mysize, argsize; 2695 [clinic start generated code]*/
2516 void *myptr, *argptr; 2696
2517 PyObject *arg = Py_None; 2697 static PyObject *
2518 Py_buffer varg; 2698 bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
2519 if (!PyArg_ParseTuple(args, "|O:rstrip", &arg)) 2699 /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
2520 return NULL; 2700 {
2521 if (arg == Py_None) { 2701 Py_ssize_t right, mysize, byteslen;
2522 argptr = "\t\n\r\f\v "; 2702 char *myptr, *bytesptr;
2523 argsize = 6; 2703 Py_buffer vbytes;
2704
2705 if (bytes == Py_None) {
2706 bytesptr = "\t\n\r\f\v ";
2707 byteslen = 6;
2524 } 2708 }
2525 else { 2709 else {
2526 if (_getbuffer(arg, &varg) < 0) 2710 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2527 return NULL; 2711 return NULL;
2528 argptr = varg.buf; 2712 bytesptr = (char *) vbytes.buf;
2529 argsize = varg.len; 2713 byteslen = vbytes.len;
2530 } 2714 }
2531 myptr = self->ob_bytes; 2715 myptr = PyByteArray_AS_STRING(self);
2532 mysize = Py_SIZE(self); 2716 mysize = Py_SIZE(self);
2533 right = rstrip_helper(myptr, mysize, argptr, argsize); 2717 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2534 if (arg != Py_None) 2718 if (bytes != Py_None)
2535 PyBuffer_Release(&varg); 2719 PyBuffer_Release(&vbytes);
2536 return PyByteArray_FromStringAndSize(self->ob_bytes, right); 2720 return PyByteArray_FromStringAndSize(myptr, right);
2537 } 2721 }
2538 2722
2539 PyDoc_STRVAR(decode_doc, 2723 /*[clinic input]
2540 "B.decode(encoding='utf-8', errors='strict') -> str\n\ 2724 bytearray.decode
2541 \n\ 2725
2542 Decode B using the codec registered for encoding. Default encoding\n\ 2726 encoding: str(c_default="NULL") = 'utf-8'
2543 is 'utf-8'. errors may be given to set a different error\n\ 2727 The encoding with which to decode the bytearray.
2544 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ 2728 errors: str(c_default="NULL") = 'strict'
2545 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ 2729 The error handling scheme to use for the handling of decoding errors.
2546 as well as any other name registered with codecs.register_error that is\n\ 2730 The default is 'strict' meaning that decoding errors raise a
2547 able to handle UnicodeDecodeErrors."); 2731 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2548 2732 as well as any other name registered with codecs.register_error that
2549 static PyObject * 2733 can handle UnicodeDecodeErrors.
2550 bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs) 2734
2551 { 2735 Decode the bytearray using the codec registered for encoding.
2552 const char *encoding = NULL; 2736 [clinic start generated code]*/
2553 const char *errors = NULL; 2737
2554 static char *kwlist[] = {"encoding", "errors", 0}; 2738 static PyObject *
2555 2739 bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
2556 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encodi ng, &errors)) 2740 const char *errors)
2557 return NULL; 2741 /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
2742 {
2558 if (encoding == NULL) 2743 if (encoding == NULL)
2559 encoding = PyUnicode_GetDefaultEncoding(); 2744 encoding = PyUnicode_GetDefaultEncoding();
2560 return PyUnicode_FromEncodedObject(self, encoding, errors); 2745 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
2561 } 2746 }
2562 2747
2563 PyDoc_STRVAR(alloc_doc, 2748 PyDoc_STRVAR(alloc_doc,
2564 "B.__alloc__() -> int\n\ 2749 "B.__alloc__() -> int\n\
2565 \n\ 2750 \n\
2566 Return the number of bytes actually allocated."); 2751 Return the number of bytes actually allocated.");
2567 2752
2568 static PyObject * 2753 static PyObject *
2569 bytearray_alloc(PyByteArrayObject *self) 2754 bytearray_alloc(PyByteArrayObject *self)
2570 { 2755 {
2571 return PyLong_FromSsize_t(self->ob_alloc); 2756 return PyLong_FromSsize_t(self->ob_alloc);
2572 } 2757 }
2573 2758
2574 PyDoc_STRVAR(join_doc, 2759 /*[clinic input]
2575 "B.join(iterable_of_bytes) -> bytearray\n\ 2760 bytearray.join
2576 \n\ 2761
2577 Concatenate any number of bytes/bytearray objects, with B\n\ 2762 iterable_of_bytes: object
2578 in between each pair, and return the result as a new bytearray."); 2763 /
2579 2764
2580 static PyObject * 2765 Concatenate any number of bytes/bytearray objects.
2581 bytearray_join(PyByteArrayObject *self, PyObject *it) 2766
2582 { 2767 The bytearray whose method is called is inserted in between each pair.
2583 PyObject *seq; 2768
2584 Py_ssize_t mysize = Py_SIZE(self); 2769 The result is returned as a new bytearray object.
2585 Py_ssize_t i; 2770 [clinic start generated code]*/
2586 Py_ssize_t n; 2771
2587 PyObject **items; 2772 static PyObject *
2588 Py_ssize_t totalsize = 0; 2773 bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
2589 PyObject *result; 2774 /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
2590 char *dest; 2775 {
2591 2776 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
2592 seq = PySequence_Fast(it, "can only join an iterable"); 2777 }
2593 if (seq == NULL) 2778
2594 return NULL; 2779 /*[clinic input]
2595 n = PySequence_Fast_GET_SIZE(seq); 2780 bytearray.splitlines
2596 items = PySequence_Fast_ITEMS(seq); 2781
2597 2782 keepends: int(c_default="0") = False
2598 /* Compute the total size, and check that they are all bytes */ 2783
2599 /* XXX Shouldn't we use _getbuffer() on these items instead? */ 2784 Return a list of the lines in the bytearray, breaking at line boundaries.
2600 for (i = 0; i < n; i++) { 2785
2601 PyObject *obj = items[i]; 2786 Line breaks are not included in the resulting list unless keepends is given and
2602 if (!PyByteArray_Check(obj) && !PyBytes_Check(obj)) { 2787 true.
2603 PyErr_Format(PyExc_TypeError, 2788 [clinic start generated code]*/
2604 "can only join an iterable of bytes " 2789
2605 "(item %ld has type '%.100s')", 2790 static PyObject *
2606 /* XXX %ld isn't right on Win64 */ 2791 bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
2607 (long)i, Py_TYPE(obj)->tp_name); 2792 /*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
2608 goto error; 2793 {
2609 }
2610 if (i > 0)
2611 totalsize += mysize;
2612 totalsize += Py_SIZE(obj);
2613 if (totalsize < 0) {
2614 PyErr_NoMemory();
2615 goto error;
2616 }
2617 }
2618
2619 /* Allocate the result, and copy the bytes */
2620 result = PyByteArray_FromStringAndSize(NULL, totalsize);
2621 if (result == NULL)
2622 goto error;
2623 dest = PyByteArray_AS_STRING(result);
2624 for (i = 0; i < n; i++) {
2625 PyObject *obj = items[i];
2626 Py_ssize_t size = Py_SIZE(obj);
2627 char *buf;
2628 if (PyByteArray_Check(obj))
2629 buf = PyByteArray_AS_STRING(obj);
2630 else
2631 buf = PyBytes_AS_STRING(obj);
2632 if (i) {
2633 memcpy(dest, self->ob_bytes, mysize);
2634 dest += mysize;
2635 }
2636 memcpy(dest, buf, size);
2637 dest += size;
2638 }
2639
2640 /* Done */
2641 Py_DECREF(seq);
2642 return result;
2643
2644 /* Error handling */
2645 error:
2646 Py_DECREF(seq);
2647 return NULL;
2648 }
2649
2650 PyDoc_STRVAR(splitlines__doc__,
2651 "B.splitlines([keepends]) -> list of lines\n\
2652 \n\
2653 Return a list of the lines in B, breaking at line boundaries.\n\
2654 Line breaks are not included in the resulting list unless keepends\n\
2655 is given and true.");
2656
2657 static PyObject*
2658 bytearray_splitlines(PyObject *self, PyObject *args, PyObject *kwds)
2659 {
2660 static char *kwlist[] = {"keepends", 0};
2661 int keepends = 0;
2662
2663 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines",
2664 kwlist, &keepends))
2665 return NULL;
2666
2667 return stringlib_splitlines( 2794 return stringlib_splitlines(
2668 (PyObject*) self, PyByteArray_AS_STRING(self), 2795 (PyObject*) self, PyByteArray_AS_STRING(self),
2669 PyByteArray_GET_SIZE(self), keepends 2796 PyByteArray_GET_SIZE(self), keepends
2670 ); 2797 );
2671 } 2798 }
2672
2673 PyDoc_STRVAR(fromhex_doc,
2674 "bytearray.fromhex(string) -> bytearray (static method)\n\
2675 \n\
2676 Create a bytearray object from a string of hexadecimal numbers.\n\
2677 Spaces between two numbers are accepted.\n\
2678 Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
2679 2799
2680 static int 2800 static int
2681 hex_digit_to_int(Py_UCS4 c) 2801 hex_digit_to_int(Py_UCS4 c)
2682 { 2802 {
2683 if (c >= 128) 2803 if (c >= 128)
2684 return -1; 2804 return -1;
2685 if (Py_ISDIGIT(c)) 2805 if (Py_ISDIGIT(c))
2686 return c - '0'; 2806 return c - '0';
2687 else { 2807 else {
2688 if (Py_ISUPPER(c)) 2808 if (Py_ISUPPER(c))
2689 c = Py_TOLOWER(c); 2809 c = Py_TOLOWER(c);
2690 if (c >= 'a' && c <= 'f') 2810 if (c >= 'a' && c <= 'f')
2691 return c - 'a' + 10; 2811 return c - 'a' + 10;
2692 } 2812 }
2693 return -1; 2813 return -1;
2694 } 2814 }
2695 2815
2696 static PyObject * 2816 /*[clinic input]
2697 bytearray_fromhex(PyObject *cls, PyObject *args) 2817 @classmethod
2698 { 2818 bytearray.fromhex
2699 PyObject *newbytes, *hexobj; 2819
2820 cls: self(type="PyObject*")
2821 string: unicode
2822 /
2823
2824 Create a bytearray object from a string of hexadecimal numbers.
2825
2826 Spaces between two numbers are accepted.
2827 Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef')
2828 [clinic start generated code]*/
2829
2830 static PyObject *
2831 bytearray_fromhex_impl(PyObject*cls, PyObject *string)
2832 /*[clinic end generated code: output=df3da60129b3700c input=907bbd2d34d9367a]*/
2833 {
2834 PyObject *newbytes;
2700 char *buf; 2835 char *buf;
2701 Py_ssize_t hexlen, byteslen, i, j; 2836 Py_ssize_t hexlen, byteslen, i, j;
2702 int top, bot; 2837 int top, bot;
2703 void *data; 2838 void *data;
2704 unsigned int kind; 2839 unsigned int kind;
2705 2840
2706 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) 2841 assert(PyUnicode_Check(string));
2707 return NULL; 2842 if (PyUnicode_READY(string))
2708 assert(PyUnicode_Check(hexobj)); 2843 return NULL;
2709 if (PyUnicode_READY(hexobj)) 2844 kind = PyUnicode_KIND(string);
2710 return NULL; 2845 data = PyUnicode_DATA(string);
2711 kind = PyUnicode_KIND(hexobj); 2846 hexlen = PyUnicode_GET_LENGTH(string);
2712 data = PyUnicode_DATA(hexobj);
2713 hexlen = PyUnicode_GET_LENGTH(hexobj);
2714 2847
2715 byteslen = hexlen/2; /* This overestimates if there are spaces */ 2848 byteslen = hexlen/2; /* This overestimates if there are spaces */
2716 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); 2849 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
2717 if (!newbytes) 2850 if (!newbytes)
2718 return NULL; 2851 return NULL;
2719 buf = PyByteArray_AS_STRING(newbytes); 2852 buf = PyByteArray_AS_STRING(newbytes);
2720 for (i = j = 0; i < hexlen; i += 2) { 2853 for (i = j = 0; i < hexlen; i += 2) {
2721 /* skip over spaces in the input */ 2854 /* skip over spaces in the input */
2722 while (PyUnicode_READ(kind, data, i) == ' ') 2855 while (PyUnicode_READ(kind, data, i) == ' ')
2723 i++; 2856 i++;
(...skipping 11 matching lines...) Expand all
2735 } 2868 }
2736 if (PyByteArray_Resize(newbytes, j) < 0) 2869 if (PyByteArray_Resize(newbytes, j) < 0)
2737 goto error; 2870 goto error;
2738 return newbytes; 2871 return newbytes;
2739 2872
2740 error: 2873 error:
2741 Py_DECREF(newbytes); 2874 Py_DECREF(newbytes);
2742 return NULL; 2875 return NULL;
2743 } 2876 }
2744 2877
2878 PyDoc_STRVAR(hex__doc__,
2879 "B.hex() -> string\n\
2880 \n\
2881 Create a string of hexadecimal numbers from a bytearray object.\n\
2882 Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2883
2884 static PyObject *
2885 bytearray_hex(PyBytesObject *self)
2886 {
2887 char* argbuf = PyByteArray_AS_STRING(self);
2888 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2889 return _Py_strhex(argbuf, arglen);
2890 }
2745 2891
2746 static PyObject * 2892 static PyObject *
2747 _common_reduce(PyByteArrayObject *self, int proto) 2893 _common_reduce(PyByteArrayObject *self, int proto)
2748 { 2894 {
2749 PyObject *dict; 2895 PyObject *dict;
2750 _Py_IDENTIFIER(__dict__); 2896 _Py_IDENTIFIER(__dict__);
2897 char *buf;
2751 2898
2752 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); 2899 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
2753 if (dict == NULL) { 2900 if (dict == NULL) {
2754 PyErr_Clear(); 2901 PyErr_Clear();
2755 dict = Py_None; 2902 dict = Py_None;
2756 Py_INCREF(dict); 2903 Py_INCREF(dict);
2757 } 2904 }
2758 2905
2906 buf = PyByteArray_AS_STRING(self);
2759 if (proto < 3) { 2907 if (proto < 3) {
2760 /* use str based reduction for backwards compatibility with Python 2.x * / 2908 /* use str based reduction for backwards compatibility with Python 2.x * /
2761 PyObject *latin1; 2909 PyObject *latin1;
2762 if (self->ob_bytes) 2910 if (Py_SIZE(self))
2763 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, Py_SIZE(self), NULL) ; 2911 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
2764 else 2912 else
2765 latin1 = PyUnicode_FromString(""); 2913 latin1 = PyUnicode_FromString("");
2766 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict) ; 2914 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict) ;
2767 } 2915 }
2768 else { 2916 else {
2769 /* use more efficient byte based reduction */ 2917 /* use more efficient byte based reduction */
2770 if (self->ob_bytes) { 2918 if (Py_SIZE(self)) {
2771 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), self->ob_bytes, Py_S IZE(self), dict); 2919 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
2772 } 2920 }
2773 else { 2921 else {
2774 return Py_BuildValue("(O()N)", Py_TYPE(self), dict); 2922 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2775 } 2923 }
2776 } 2924 }
2777 } 2925 }
2778 2926
2779 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); 2927 /*[clinic input]
2780 2928 bytearray.__reduce__ as bytearray_reduce
2781 static PyObject * 2929
2782 bytearray_reduce(PyByteArrayObject *self) 2930 self: self(type="PyByteArrayObject *")
2931
2932 Return state information for pickling.
2933 [clinic start generated code]*/
2934
2935 static PyObject *
2936 bytearray_reduce_impl(PyByteArrayObject *self)
2937 /*[clinic end generated code: output=52bf304086464cab input=fbb07de4d102a03a]*/
2783 { 2938 {
2784 return _common_reduce(self, 2); 2939 return _common_reduce(self, 2);
2785 } 2940 }
2786 2941
2787 PyDoc_STRVAR(reduce_ex_doc, "Return state information for pickling."); 2942 /*[clinic input]
2788 2943 bytearray.__reduce_ex__ as bytearray_reduce_ex
2789 static PyObject * 2944
2790 bytearray_reduce_ex(PyByteArrayObject *self, PyObject *args) 2945 self: self(type="PyByteArrayObject *")
2791 { 2946 proto: int = 0
2792 int proto = 0; 2947 /
2793 2948
2794 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) 2949 Return state information for pickling.
2795 return NULL; 2950 [clinic start generated code]*/
2796 2951
2952 static PyObject *
2953 bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
2954 /*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/
2955 {
2797 return _common_reduce(self, proto); 2956 return _common_reduce(self, proto);
2798 } 2957 }
2799 2958
2800 PyDoc_STRVAR(sizeof_doc, 2959 /*[clinic input]
2801 "B.__sizeof__() -> int\n\ 2960 bytearray.__sizeof__ as bytearray_sizeof
2802 \n\ 2961
2803 Returns the size of B in memory, in bytes"); 2962 self: self(type="PyByteArrayObject *")
2804 static PyObject * 2963
2805 bytearray_sizeof(PyByteArrayObject *self) 2964 Returns the size of the bytearray object in memory, in bytes.
2965 [clinic start generated code]*/
2966
2967 static PyObject *
2968 bytearray_sizeof_impl(PyByteArrayObject *self)
2969 /*[clinic end generated code: output=738abdd17951c427 input=6b23d305362b462b]*/
2806 { 2970 {
2807 Py_ssize_t res; 2971 Py_ssize_t res;
2808 2972
2809 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char); 2973 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
2810 return PyLong_FromSsize_t(res); 2974 return PyLong_FromSsize_t(res);
2811 } 2975 }
2812 2976
2813 static PySequenceMethods bytearray_as_sequence = { 2977 static PySequenceMethods bytearray_as_sequence = {
2814 (lenfunc)bytearray_length, /* sq_length */ 2978 (lenfunc)bytearray_length, /* sq_length */
2815 (binaryfunc)PyByteArray_Concat, /* sq_concat */ 2979 (binaryfunc)PyByteArray_Concat, /* sq_concat */
(...skipping 14 matching lines...) Expand all
2830 }; 2994 };
2831 2995
2832 static PyBufferProcs bytearray_as_buffer = { 2996 static PyBufferProcs bytearray_as_buffer = {
2833 (getbufferproc)bytearray_getbuffer, 2997 (getbufferproc)bytearray_getbuffer,
2834 (releasebufferproc)bytearray_releasebuffer, 2998 (releasebufferproc)bytearray_releasebuffer,
2835 }; 2999 };
2836 3000
2837 static PyMethodDef 3001 static PyMethodDef
2838 bytearray_methods[] = { 3002 bytearray_methods[] = {
2839 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, 3003 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
2840 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, reduce_doc}, 3004 BYTEARRAY_REDUCE_METHODDEF
2841 {"__reduce_ex__", (PyCFunction)bytearray_reduce_ex, METH_VARARGS, reduce_ex_ doc}, 3005 BYTEARRAY_REDUCE_EX_METHODDEF
2842 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, sizeof_doc}, 3006 BYTEARRAY_SIZEOF_METHODDEF
2843 {"append", (PyCFunction)bytearray_append, METH_O, append__doc__}, 3007 BYTEARRAY_APPEND_METHODDEF
2844 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, 3008 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2845 _Py_capitalize__doc__}, 3009 _Py_capitalize__doc__},
2846 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, 3010 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
2847 {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, clear__doc__}, 3011 BYTEARRAY_CLEAR_METHODDEF
2848 {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, copy__doc__}, 3012 BYTEARRAY_COPY_METHODDEF
2849 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, 3013 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
2850 {"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, deco de_doc}, 3014 BYTEARRAY_DECODE_METHODDEF
2851 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__} , 3015 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__} ,
2852 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, 3016 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWOR DS,
2853 expandtabs__doc__}, 3017 expandtabs__doc__},
2854 {"extend", (PyCFunction)bytearray_extend, METH_O, extend__doc__}, 3018 BYTEARRAY_EXTEND_METHODDEF
2855 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, 3019 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
2856 {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS, 3020 BYTEARRAY_FROMHEX_METHODDEF
2857 fromhex_doc}, 3021 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2858 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, 3022 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
2859 {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, insert__doc__}, 3023 BYTEARRAY_INSERT_METHODDEF
2860 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, 3024 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2861 _Py_isalnum__doc__}, 3025 _Py_isalnum__doc__},
2862 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, 3026 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2863 _Py_isalpha__doc__}, 3027 _Py_isalpha__doc__},
2864 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, 3028 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2865 _Py_isdigit__doc__}, 3029 _Py_isdigit__doc__},
2866 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, 3030 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2867 _Py_islower__doc__}, 3031 _Py_islower__doc__},
2868 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, 3032 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2869 _Py_isspace__doc__}, 3033 _Py_isspace__doc__},
2870 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, 3034 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2871 _Py_istitle__doc__}, 3035 _Py_istitle__doc__},
2872 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, 3036 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2873 _Py_isupper__doc__}, 3037 _Py_isupper__doc__},
2874 {"join", (PyCFunction)bytearray_join, METH_O, join_doc}, 3038 BYTEARRAY_JOIN_METHODDEF
2875 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, 3039 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
2876 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, 3040 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
2877 {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, lstrip__doc__}, 3041 BYTEARRAY_LSTRIP_METHODDEF
2878 {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC, 3042 BYTEARRAY_MAKETRANS_METHODDEF
2879 _Py_maketrans__doc__}, 3043 BYTEARRAY_PARTITION_METHODDEF
2880 {"partition", (PyCFunction)bytearray_partition, METH_O, partition__doc__}, 3044 BYTEARRAY_POP_METHODDEF
2881 {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, pop__doc__}, 3045 BYTEARRAY_REMOVE_METHODDEF
2882 {"remove", (PyCFunction)bytearray_remove, METH_O, remove__doc__}, 3046 BYTEARRAY_REPLACE_METHODDEF
2883 {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, replace__doc__}, 3047 BYTEARRAY_REVERSE_METHODDEF
2884 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, reverse__doc__},
2885 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, 3048 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
2886 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, 3049 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
2887 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, 3050 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
2888 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__} , 3051 BYTEARRAY_RPARTITION_METHODDEF
2889 {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS | METH_KEYWORDS, rspl it__doc__}, 3052 BYTEARRAY_RSPLIT_METHODDEF
2890 {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__}, 3053 BYTEARRAY_RSTRIP_METHODDEF
2891 {"split", (PyCFunction)bytearray_split, METH_VARARGS | METH_KEYWORDS, split_ _doc__}, 3054 BYTEARRAY_SPLIT_METHODDEF
2892 {"splitlines", (PyCFunction)bytearray_splitlines, 3055 BYTEARRAY_SPLITLINES_METHODDEF
2893 METH_VARARGS | METH_KEYWORDS, splitlines__doc__},
2894 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , 3056 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
2895 startswith__doc__}, 3057 startswith__doc__},
2896 {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__}, 3058 BYTEARRAY_STRIP_METHODDEF
2897 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, 3059 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2898 _Py_swapcase__doc__}, 3060 _Py_swapcase__doc__},
2899 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, 3061 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
2900 {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, 3062 BYTEARRAY_TRANSLATE_METHODDEF
2901 translate__doc__},
2902 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, 3063 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2903 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, 3064 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
2904 {NULL} 3065 {NULL}
3066 };
3067
3068 static PyObject *
3069 bytearray_mod(PyObject *v, PyObject *w)
3070 {
3071 if (!PyByteArray_Check(v))
3072 Py_RETURN_NOTIMPLEMENTED;
3073 return bytearray_format((PyByteArrayObject *)v, w);
3074 }
3075
3076 static PyNumberMethods bytearray_as_number = {
3077 0, /*nb_add*/
3078 0, /*nb_subtract*/
3079 0, /*nb_multiply*/
3080 bytearray_mod, /*nb_remainder*/
2905 }; 3081 };
2906 3082
2907 PyDoc_STRVAR(bytearray_doc, 3083 PyDoc_STRVAR(bytearray_doc,
2908 "bytearray(iterable_of_ints) -> bytearray\n\ 3084 "bytearray(iterable_of_ints) -> bytearray\n\
2909 bytearray(string, encoding[, errors]) -> bytearray\n\ 3085 bytearray(string, encoding[, errors]) -> bytearray\n\
2910 bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ 3086 bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2911 bytearray(int) -> bytes array of size given by the parameter initialized with nu ll bytes\n\ 3087 bytearray(int) -> bytes array of size given by the parameter initialized with nu ll bytes\n\
2912 bytearray() -> empty bytes array\n\ 3088 bytearray() -> empty bytes array\n\
2913 \n\ 3089 \n\
2914 Construct an mutable bytearray object from:\n\ 3090 Construct an mutable bytearray object from:\n\
(...skipping 10 matching lines...) Expand all
2925 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3101 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2926 "bytearray", 3102 "bytearray",
2927 sizeof(PyByteArrayObject), 3103 sizeof(PyByteArrayObject),
2928 0, 3104 0,
2929 (destructor)bytearray_dealloc, /* tp_dealloc */ 3105 (destructor)bytearray_dealloc, /* tp_dealloc */
2930 0, /* tp_print */ 3106 0, /* tp_print */
2931 0, /* tp_getattr */ 3107 0, /* tp_getattr */
2932 0, /* tp_setattr */ 3108 0, /* tp_setattr */
2933 0, /* tp_reserved */ 3109 0, /* tp_reserved */
2934 (reprfunc)bytearray_repr, /* tp_repr */ 3110 (reprfunc)bytearray_repr, /* tp_repr */
2935 0, /* tp_as_number */ 3111 &bytearray_as_number, /* tp_as_number */
2936 &bytearray_as_sequence, /* tp_as_sequence */ 3112 &bytearray_as_sequence, /* tp_as_sequence */
2937 &bytearray_as_mapping, /* tp_as_mapping */ 3113 &bytearray_as_mapping, /* tp_as_mapping */
2938 0, /* tp_hash */ 3114 0, /* tp_hash */
2939 0, /* tp_call */ 3115 0, /* tp_call */
2940 bytearray_str, /* tp_str */ 3116 bytearray_str, /* tp_str */
2941 PyObject_GenericGetAttr, /* tp_getattro */ 3117 PyObject_GenericGetAttr, /* tp_getattro */
2942 0, /* tp_setattro */ 3118 0, /* tp_setattro */
2943 &bytearray_as_buffer, /* tp_as_buffer */ 3119 &bytearray_as_buffer, /* tp_as_buffer */
2944 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ 3120 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2945 bytearray_doc, /* tp_doc */ 3121 bytearray_doc, /* tp_doc */
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
2993 PyObject *item; 3169 PyObject *item;
2994 3170
2995 assert(it != NULL); 3171 assert(it != NULL);
2996 seq = it->it_seq; 3172 seq = it->it_seq;
2997 if (seq == NULL) 3173 if (seq == NULL)
2998 return NULL; 3174 return NULL;
2999 assert(PyByteArray_Check(seq)); 3175 assert(PyByteArray_Check(seq));
3000 3176
3001 if (it->it_index < PyByteArray_GET_SIZE(seq)) { 3177 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
3002 item = PyLong_FromLong( 3178 item = PyLong_FromLong(
3003 (unsigned char)seq->ob_bytes[it->it_index]); 3179 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
3004 if (item != NULL) 3180 if (item != NULL)
3005 ++it->it_index; 3181 ++it->it_index;
3006 return item; 3182 return item;
3007 } 3183 }
3008 3184
3009 Py_DECREF(seq); 3185 Py_DECREF(seq);
3010 it->it_seq = NULL; 3186 it->it_seq = NULL;
3011 return NULL; 3187 return NULL;
3012 } 3188 }
3013 3189
(...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after
3052 } 3228 }
3053 Py_RETURN_NONE; 3229 Py_RETURN_NONE;
3054 } 3230 }
3055 3231
3056 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); 3232 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3057 3233
3058 static PyMethodDef bytearrayiter_methods[] = { 3234 static PyMethodDef bytearrayiter_methods[] = {
3059 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, 3235 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
3060 length_hint_doc}, 3236 length_hint_doc},
3061 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, 3237 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
3062 reduce_doc}, 3238 bytearray_reduce__doc__},
3063 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, 3239 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3064 setstate_doc}, 3240 setstate_doc},
3065 {NULL, NULL} /* sentinel */ 3241 {NULL, NULL} /* sentinel */
3066 }; 3242 };
3067 3243
3068 PyTypeObject PyByteArrayIter_Type = { 3244 PyTypeObject PyByteArrayIter_Type = {
3069 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3245 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3070 "bytearray_iterator", /* tp_name */ 3246 "bytearray_iterator", /* tp_name */
3071 sizeof(bytesiterobject), /* tp_basicsize */ 3247 sizeof(bytesiterobject), /* tp_basicsize */
3072 0, /* tp_itemsize */ 3248 0, /* tp_itemsize */
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
3109 } 3285 }
3110 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); 3286 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3111 if (it == NULL) 3287 if (it == NULL)
3112 return NULL; 3288 return NULL;
3113 it->it_index = 0; 3289 it->it_index = 0;
3114 Py_INCREF(seq); 3290 Py_INCREF(seq);
3115 it->it_seq = (PyByteArrayObject *)seq; 3291 it->it_seq = (PyByteArrayObject *)seq;
3116 _PyObject_GC_TRACK(it); 3292 _PyObject_GC_TRACK(it);
3117 return (PyObject *)it; 3293 return (PyObject *)it;
3118 } 3294 }
LEFTRIGHT

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+