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

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 }
105 93
94 #include "clinic/bytearrayobject.c.h"
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
115 PyObject * 105 PyObject *
(...skipping 16 matching lines...) Expand all
132 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type); 122 new = PyObject_New(PyByteArrayObject, &PyByteArray_Type);
133 if (new == NULL) 123 if (new == NULL)
134 return NULL; 124 return NULL;
135 125
136 if (size == 0) { 126 if (size == 0) {
137 new->ob_bytes = NULL; 127 new->ob_bytes = NULL;
138 alloc = 0; 128 alloc = 0;
139 } 129 }
140 else { 130 else {
141 alloc = size + 1; 131 alloc = size + 1;
142 new->ob_bytes = PyMem_Malloc(alloc); 132 new->ob_bytes = PyObject_Malloc(alloc);
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 = PyMem_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),
798 goto fail; 854 &view, size, 'C') < 0)
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
808 /* Get the iterator */ 865 /* Get the iterator */
(...skipping 18 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 static const char *hexdigits = "0123456789abcdef";
860 const char *quote_prefix = "bytearray(b"; 918 const char *quote_prefix = "bytearray(b";
861 const char *quote_postfix = ")"; 919 const char *quote_postfix = ")";
862 Py_ssize_t length = Py_SIZE(self); 920 Py_ssize_t length = Py_SIZE(self);
863 /* 14 == strlen(quote_prefix) + 2 + strlen(quote_postfix) */ 921 /* 15 == strlen(quote_prefix) + 2 + strlen(quote_postfix) + 1 */
864 size_t newsize; 922 size_t newsize;
865 PyObject *v; 923 PyObject *v;
866 if (length > (PY_SSIZE_T_MAX - 14) / 4) { 924 Py_ssize_t i;
925 char *bytes;
926 char c;
927 char *p;
928 int quote;
929 char *test, *start;
930 char *buffer;
931
932 if (length > (PY_SSIZE_T_MAX - 15) / 4) {
867 PyErr_SetString(PyExc_OverflowError, 933 PyErr_SetString(PyExc_OverflowError,
868 "bytearray object is too large to make repr"); 934 "bytearray object is too large to make repr");
869 return NULL; 935 return NULL;
870 } 936 }
871 newsize = 14 + 4 * length; 937
872 v = PyUnicode_FromUnicode(NULL, newsize); 938 newsize = 15 + length * 4;
873 if (v == NULL) { 939 buffer = PyObject_Malloc(newsize);
874 return NULL; 940 if (buffer == NULL) {
875 } 941 PyErr_NoMemory();
876 else { 942 return NULL;
877 register Py_ssize_t i; 943 }
878 register Py_UNICODE c; 944
879 register Py_UNICODE *p; 945 /* Figure out which quote to use; single is preferred */
880 int quote; 946 quote = '\'';
881 947 start = PyByteArray_AS_STRING(self);
882 /* Figure out which quote to use; single is preferred */ 948 for (test = start; test < start+length; ++test) {
883 quote = '\''; 949 if (*test == '"') {
884 { 950 quote = '\''; /* back to single */
885 char *test, *start; 951 break;
886 start = PyByteArray_AS_STRING(self); 952 }
887 for (test = start; test < start+length; ++test) { 953 else if (*test == '\'')
888 if (*test == '"') { 954 quote = '"';
889 quote = '\''; /* back to single */ 955 }
890 goto decided; 956
891 } 957 p = buffer;
892 else if (*test == '\'') 958 while (*quote_prefix)
893 quote = '"'; 959 *p++ = *quote_prefix++;
894 } 960 *p++ = quote;
895 decided: 961
896 ; 962 bytes = PyByteArray_AS_STRING(self);
897 } 963 for (i = 0; i < length; i++) {
898 964 /* There's at least enough room for a hex escape
899 p = PyUnicode_AS_UNICODE(v); 965 and a closing quote. */
900 while (*quote_prefix) 966 assert(newsize - (p - buffer) >= 5);
901 *p++ = *quote_prefix++; 967 c = bytes[i];
902 *p++ = quote; 968 if (c == '\'' || c == '\\')
903 969 *p++ = '\\', *p++ = c;
904 for (i = 0; i < length; i++) { 970 else if (c == '\t')
905 /* There's at least enough room for a hex escape 971 *p++ = '\\', *p++ = 't';
906 and a closing quote. */ 972 else if (c == '\n')
907 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 5); 973 *p++ = '\\', *p++ = 'n';
908 c = self->ob_bytes[i]; 974 else if (c == '\r')
909 if (c == '\'' || c == '\\') 975 *p++ = '\\', *p++ = 'r';
910 *p++ = '\\', *p++ = c; 976 else if (c == 0)
911 else if (c == '\t') 977 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0';
912 *p++ = '\\', *p++ = 't'; 978 else if (c < ' ' || c >= 0x7f) {
913 else if (c == '\n') 979 *p++ = '\\';
914 *p++ = '\\', *p++ = 'n'; 980 *p++ = 'x';
915 else if (c == '\r') 981 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
916 *p++ = '\\', *p++ = 'r'; 982 *p++ = Py_hexdigits[c & 0xf];
917 else if (c == 0) 983 }
918 *p++ = '\\', *p++ = 'x', *p++ = '0', *p++ = '0'; 984 else
919 else if (c < ' ' || c >= 0x7f) { 985 *p++ = c;
920 *p++ = '\\'; 986 }
921 *p++ = 'x'; 987 assert(newsize - (p - buffer) >= 1);
922 *p++ = hexdigits[(c & 0xf0) >> 4]; 988 *p++ = quote;
923 *p++ = hexdigits[c & 0xf]; 989 while (*quote_postfix) {
924 } 990 *p++ = *quote_postfix++;
925 else 991 }
926 *p++ = c; 992
927 } 993 v = PyUnicode_DecodeASCII(buffer, p - buffer, NULL);
928 assert(newsize - (p - PyUnicode_AS_UNICODE(v)) >= 1); 994 PyObject_Free(buffer);
929 *p++ = quote; 995 return v;
930 while (*quote_postfix) {
931 *p++ = *quote_postfix++;
932 }
933 *p = '\0';
934 if (PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {
935 Py_DECREF(v);
936 return NULL;
937 }
938 return v;
939 }
940 } 996 }
941 997
942 static PyObject * 998 static PyObject *
943 bytearray_str(PyObject *op) 999 bytearray_str(PyObject *op)
944 { 1000 {
945 if (Py_BytesWarningFlag) { 1001 if (Py_BytesWarningFlag) {
946 if (PyErr_WarnEx(PyExc_BytesWarning, 1002 if (PyErr_WarnEx(PyExc_BytesWarning,
947 "str() on a bytearray instance", 1)) 1003 "str() on a bytearray instance", 1))
948 return NULL; 1004 return NULL;
949 } 1005 }
950 return bytearray_repr((PyByteArrayObject*)op); 1006 return bytearray_repr((PyByteArrayObject*)op);
951 } 1007 }
952 1008
953 static PyObject * 1009 static PyObject *
954 bytearray_richcompare(PyObject *self, PyObject *other, int op) 1010 bytearray_richcompare(PyObject *self, PyObject *other, int op)
955 { 1011 {
956 Py_ssize_t self_size, other_size; 1012 Py_ssize_t self_size, other_size;
957 Py_buffer self_bytes, other_bytes; 1013 Py_buffer self_bytes, other_bytes;
958 PyObject *res; 1014 PyObject *res;
959 Py_ssize_t minsize; 1015 Py_ssize_t minsize;
960 int cmp; 1016 int cmp, rc;
961 1017
962 /* Bytes can be compared to anything that supports the (binary) 1018 /* Bytes can be compared to anything that supports the (binary)
963 buffer API. Except that a comparison with Unicode is always an 1019 buffer API. Except that a comparison with Unicode is always an
964 error, even if the comparison is for equality. */ 1020 error, even if the comparison is for equality. */
965 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || 1021 rc = PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type);
966 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) {
967 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) { 1027 if (Py_BytesWarningFlag && (op == Py_EQ || op == Py_NE)) {
968 if (PyErr_WarnEx(PyExc_BytesWarning, 1028 if (PyErr_WarnEx(PyExc_BytesWarning,
969 "Comparison between bytearray and string", 1)) 1029 "Comparison between bytearray and string", 1))
970 return NULL; 1030 return NULL;
971 } 1031 }
972 1032
973 Py_INCREF(Py_NotImplemented); 1033 Py_RETURN_NOTIMPLEMENTED;
974 return Py_NotImplemented; 1034 }
975 } 1035
976 1036 if (PyObject_GetBuffer(self, &self_bytes, PyBUF_SIMPLE) != 0) {
977 self_size = _getbuffer(self, &self_bytes);
978 if (self_size < 0) {
979 PyErr_Clear(); 1037 PyErr_Clear();
980 Py_INCREF(Py_NotImplemented); 1038 Py_RETURN_NOTIMPLEMENTED;
981 return Py_NotImplemented; 1039 }
982 } 1040 self_size = self_bytes.len;
983 1041
984 other_size = _getbuffer(other, &other_bytes); 1042 if (PyObject_GetBuffer(other, &other_bytes, PyBUF_SIMPLE) != 0) {
985 if (other_size < 0) {
986 PyErr_Clear(); 1043 PyErr_Clear();
987 PyBuffer_Release(&self_bytes); 1044 PyBuffer_Release(&self_bytes);
988 Py_INCREF(Py_NotImplemented); 1045 Py_RETURN_NOTIMPLEMENTED;
989 return Py_NotImplemented; 1046 }
990 } 1047 other_size = other_bytes.len;
991 1048
992 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) { 1049 if (self_size != other_size && (op == Py_EQ || op == Py_NE)) {
993 /* Shortcut: if the lengths differ, the objects differ */ 1050 /* Shortcut: if the lengths differ, the objects differ */
994 cmp = (op == Py_NE); 1051 cmp = (op == Py_NE);
995 } 1052 }
996 else { 1053 else {
997 minsize = self_size; 1054 minsize = self_size;
998 if (other_size < minsize) 1055 if (other_size < minsize)
999 minsize = other_size; 1056 minsize = other_size;
1000 1057
(...skipping 26 matching lines...) Expand all
1027 1084
1028 static void 1085 static void
1029 bytearray_dealloc(PyByteArrayObject *self) 1086 bytearray_dealloc(PyByteArrayObject *self)
1030 { 1087 {
1031 if (self->ob_exports > 0) { 1088 if (self->ob_exports > 0) {
1032 PyErr_SetString(PyExc_SystemError, 1089 PyErr_SetString(PyExc_SystemError,
1033 "deallocated bytearray object has exported buffers"); 1090 "deallocated bytearray object has exported buffers");
1034 PyErr_Print(); 1091 PyErr_Print();
1035 } 1092 }
1036 if (self->ob_bytes != 0) { 1093 if (self->ob_bytes != 0) {
1037 PyMem_Free(self->ob_bytes); 1094 PyObject_Free(self->ob_bytes);
1038 } 1095 }
1039 Py_TYPE(self)->tp_free((PyObject *)self); 1096 Py_TYPE(self)->tp_free((PyObject *)self);
1040 } 1097 }
1041 1098
1042 1099
1043 /* -------------------------------------------------------------------- */ 1100 /* -------------------------------------------------------------------- */
1044 /* Methods */ 1101 /* Methods */
1045 1102
1103 #define FASTSEARCH fastsearch
1104 #define STRINGLIB(F) stringlib_##F
1046 #define STRINGLIB_CHAR char 1105 #define STRINGLIB_CHAR char
1106 #define STRINGLIB_SIZEOF_CHAR 1
1047 #define STRINGLIB_LEN PyByteArray_GET_SIZE 1107 #define STRINGLIB_LEN PyByteArray_GET_SIZE
1048 #define STRINGLIB_STR PyByteArray_AS_STRING 1108 #define STRINGLIB_STR PyByteArray_AS_STRING
1049 #define STRINGLIB_NEW PyByteArray_FromStringAndSize 1109 #define STRINGLIB_NEW PyByteArray_FromStringAndSize
1050 #define STRINGLIB_ISSPACE Py_ISSPACE 1110 #define STRINGLIB_ISSPACE Py_ISSPACE
1051 #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) 1111 #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r'))
1052 #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact 1112 #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact
1053 #define STRINGLIB_MUTABLE 1 1113 #define STRINGLIB_MUTABLE 1
1054 1114
1055 #include "stringlib/fastsearch.h" 1115 #include "stringlib/fastsearch.h"
1056 #include "stringlib/count.h" 1116 #include "stringlib/count.h"
1057 #include "stringlib/find.h" 1117 #include "stringlib/find.h"
1118 #include "stringlib/join.h"
1058 #include "stringlib/partition.h" 1119 #include "stringlib/partition.h"
1059 #include "stringlib/split.h" 1120 #include "stringlib/split.h"
1060 #include "stringlib/ctype.h" 1121 #include "stringlib/ctype.h"
1061 #include "stringlib/transmogrify.h" 1122 #include "stringlib/transmogrify.h"
1062 1123
1063 1124
1064 /* The following Py_LOCAL_INLINE and Py_LOCAL functions 1125 /* The following Py_LOCAL_INLINE and Py_LOCAL functions
1065 were copied from the old char* style string object. */ 1126 were copied from the old char* style string object. */
1066 1127
1067 /* helper macro to fixup start/end slice values */ 1128 /* helper macro to fixup start/end slice values */
1068 #define ADJUST_INDICES(start, end, len) \ 1129 #define ADJUST_INDICES(start, end, len) \
1069 if (end > len) \ 1130 if (end > len) \
1070 end = len; \ 1131 end = len; \
1071 else if (end < 0) { \ 1132 else if (end < 0) { \
1072 end += len; \ 1133 end += len; \
1073 if (end < 0) \ 1134 if (end < 0) \
1074 end = 0; \ 1135 end = 0; \
1075 } \ 1136 } \
1076 if (start < 0) { \ 1137 if (start < 0) { \
1077 start += len; \ 1138 start += len; \
1078 if (start < 0) \ 1139 if (start < 0) \
1079 start = 0; \ 1140 start = 0; \
1080 } 1141 }
1081 1142
1082 Py_LOCAL_INLINE(Py_ssize_t) 1143 Py_LOCAL_INLINE(Py_ssize_t)
1083 bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir) 1144 bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
1084 { 1145 {
1085 PyObject *subobj; 1146 PyObject *subobj;
1147 char byte;
1086 Py_buffer subbuf; 1148 Py_buffer subbuf;
1149 const char *sub;
1150 Py_ssize_t len, sub_len;
1087 Py_ssize_t start=0, end=PY_SSIZE_T_MAX; 1151 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1088 Py_ssize_t res; 1152 Py_ssize_t res;
1089 1153
1090 if (!stringlib_parse_args_finds("find/rfind/index/rindex", 1154 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1091 args, &subobj, &start, &end)) 1155 args, &subobj, &byte, &start, &end))
1092 return -2; 1156 return -2;
1093 if (_getbuffer(subobj, &subbuf) < 0) 1157
1094 return -2; 1158 if (subobj) {
1095 if (dir > 0) 1159 if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
1096 res = stringlib_find_slice( 1160 return -2;
1097 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 1161
1098 subbuf.buf, subbuf.len, start, end); 1162 sub = subbuf.buf;
1099 else 1163 sub_len = subbuf.len;
1100 res = stringlib_rfind_slice( 1164 }
1101 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 1165 else {
1102 subbuf.buf, subbuf.len, start, end); 1166 sub = &byte;
1103 PyBuffer_Release(&subbuf); 1167 sub_len = 1;
1168 }
1169 len = PyByteArray_GET_SIZE(self);
1170
1171 ADJUST_INDICES(start, end, len);
1172 if (end - start < sub_len)
1173 res = -1;
1174 /* Issue #23573: FIXME, windows has no memrchr() */
1175 else if (sub_len == 1 && dir > 0) {
1176 unsigned char needle = *sub;
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 }
1193
1194 if (subobj)
1195 PyBuffer_Release(&subbuf);
1196
1104 return res; 1197 return res;
1105 } 1198 }
1106 1199
1107 PyDoc_STRVAR(find__doc__, 1200 PyDoc_STRVAR(find__doc__,
1108 "B.find(sub[, start[, end]]) -> int\n\ 1201 "B.find(sub[, start[, end]]) -> int\n\
1109 \n\ 1202 \n\
1110 Return the lowest index in B where subsection sub is found,\n\ 1203 Return the lowest index in B where subsection sub is found,\n\
1111 such that sub is contained within B[start,end]. Optional\n\ 1204 such that sub is contained within B[start,end]. Optional\n\
1112 arguments start and end are interpreted as in slice notation.\n\ 1205 arguments start and end are interpreted as in slice notation.\n\
1113 \n\ 1206 \n\
(...skipping 12 matching lines...) Expand all
1126 "B.count(sub[, start[, end]]) -> int\n\ 1219 "B.count(sub[, start[, end]]) -> int\n\
1127 \n\ 1220 \n\
1128 Return the number of non-overlapping occurrences of subsection sub in\n\ 1221 Return the number of non-overlapping occurrences of subsection sub in\n\
1129 bytes B[start:end]. Optional arguments start and end are interpreted\n\ 1222 bytes B[start:end]. Optional arguments start and end are interpreted\n\
1130 as in slice notation."); 1223 as in slice notation.");
1131 1224
1132 static PyObject * 1225 static PyObject *
1133 bytearray_count(PyByteArrayObject *self, PyObject *args) 1226 bytearray_count(PyByteArrayObject *self, PyObject *args)
1134 { 1227 {
1135 PyObject *sub_obj; 1228 PyObject *sub_obj;
1136 const char *str = PyByteArray_AS_STRING(self); 1229 const char *str = PyByteArray_AS_STRING(self), *sub;
1230 Py_ssize_t sub_len;
1231 char byte;
1137 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX; 1232 Py_ssize_t start = 0, end = PY_SSIZE_T_MAX;
1233
1138 Py_buffer vsub; 1234 Py_buffer vsub;
1139 PyObject *count_obj; 1235 PyObject *count_obj;
1140 1236
1141 if (!stringlib_parse_args_finds("count", args, &sub_obj, &start, &end)) 1237 if (!stringlib_parse_args_finds_byte("count", args, &sub_obj, &byte,
1142 return NULL; 1238 &start, &end))
1143 1239 return NULL;
1144 if (_getbuffer(sub_obj, &vsub) < 0) 1240
1145 return NULL; 1241 if (sub_obj) {
1242 if (PyObject_GetBuffer(sub_obj, &vsub, PyBUF_SIMPLE) != 0)
1243 return NULL;
1244
1245 sub = vsub.buf;
1246 sub_len = vsub.len;
1247 }
1248 else {
1249 sub = &byte;
1250 sub_len = 1;
1251 }
1146 1252
1147 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self)); 1253 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self));
1148 1254
1149 count_obj = PyLong_FromSsize_t( 1255 count_obj = PyLong_FromSsize_t(
1150 stringlib_count(str + start, end - start, vsub.buf, vsub.len, PY_SSIZE_T _MAX) 1256 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
1151 ); 1257 );
1152 PyBuffer_Release(&vsub); 1258
1259 if (sub_obj)
1260 PyBuffer_Release(&vsub);
1261
1153 return count_obj; 1262 return count_obj;
1154 } 1263 }
1155 1264
1265 /*[clinic input]
1266 bytearray.clear
1267
1268 self: self(type="PyByteArrayObject *")
1269
1270 Remove all items from the bytearray.
1271 [clinic start generated code]*/
1272
1273 static PyObject *
1274 bytearray_clear_impl(PyByteArrayObject *self)
1275 /*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/
1276 {
1277 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1278 return NULL;
1279 Py_RETURN_NONE;
1280 }
1281
1282 /*[clinic input]
1283 bytearray.copy
1284
1285 self: self(type="PyByteArrayObject *")
1286
1287 Return a copy of B.
1288 [clinic start generated code]*/
1289
1290 static PyObject *
1291 bytearray_copy_impl(PyByteArrayObject *self)
1292 /*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/
1293 {
1294 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self) ,
1295 PyByteArray_GET_SIZE(self));
1296 }
1156 1297
1157 PyDoc_STRVAR(index__doc__, 1298 PyDoc_STRVAR(index__doc__,
1158 "B.index(sub[, start[, end]]) -> int\n\ 1299 "B.index(sub[, start[, end]]) -> int\n\
1159 \n\ 1300 \n\
1160 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.");
1161 1302
1162 static PyObject * 1303 static PyObject *
1163 bytearray_index(PyByteArrayObject *self, PyObject *args) 1304 bytearray_index(PyByteArrayObject *self, PyObject *args)
1164 { 1305 {
1165 Py_ssize_t result = bytearray_find_internal(self, args, +1); 1306 Py_ssize_t result = bytearray_find_internal(self, args, +1);
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after
1214 1355
1215 1356
1216 static int 1357 static int
1217 bytearray_contains(PyObject *self, PyObject *arg) 1358 bytearray_contains(PyObject *self, PyObject *arg)
1218 { 1359 {
1219 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); 1360 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError);
1220 if (ival == -1 && PyErr_Occurred()) { 1361 if (ival == -1 && PyErr_Occurred()) {
1221 Py_buffer varg; 1362 Py_buffer varg;
1222 Py_ssize_t pos; 1363 Py_ssize_t pos;
1223 PyErr_Clear(); 1364 PyErr_Clear();
1224 if (_getbuffer(arg, &varg) < 0) 1365 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0)
1225 return -1; 1366 return -1;
1226 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self), 1367 pos = stringlib_find(PyByteArray_AS_STRING(self), Py_SIZE(self),
1227 varg.buf, varg.len, 0); 1368 varg.buf, varg.len, 0);
1228 PyBuffer_Release(&varg); 1369 PyBuffer_Release(&varg);
1229 return pos >= 0; 1370 return pos >= 0;
1230 } 1371 }
1231 if (ival < 0 || ival >= 256) { 1372 if (ival < 0 || ival >= 256) {
1232 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)"); 1373 PyErr_SetString(PyExc_ValueError, "byte must be in range(0, 256)");
1233 return -1; 1374 return -1;
1234 } 1375 }
(...skipping 10 matching lines...) Expand all
1245 _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start , 1386 _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start ,
1246 Py_ssize_t end, int direction) 1387 Py_ssize_t end, int direction)
1247 { 1388 {
1248 Py_ssize_t len = PyByteArray_GET_SIZE(self); 1389 Py_ssize_t len = PyByteArray_GET_SIZE(self);
1249 const char* str; 1390 const char* str;
1250 Py_buffer vsubstr; 1391 Py_buffer vsubstr;
1251 int rv = 0; 1392 int rv = 0;
1252 1393
1253 str = PyByteArray_AS_STRING(self); 1394 str = PyByteArray_AS_STRING(self);
1254 1395
1255 if (_getbuffer(substr, &vsubstr) < 0) 1396 if (PyObject_GetBuffer(substr, &vsubstr, PyBUF_SIMPLE) != 0)
1256 return -1; 1397 return -1;
1257 1398
1258 ADJUST_INDICES(start, end, len); 1399 ADJUST_INDICES(start, end, len);
1259 1400
1260 if (direction < 0) { 1401 if (direction < 0) {
1261 /* startswith */ 1402 /* startswith */
1262 if (start+vsubstr.len > len) { 1403 if (start+vsubstr.len > len) {
1263 goto done; 1404 goto done;
1264 } 1405 }
1265 } else { 1406 } else {
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
1360 if (PyErr_ExceptionMatches(PyExc_TypeError)) 1501 if (PyErr_ExceptionMatches(PyExc_TypeError))
1361 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " 1502 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1362 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); 1503 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
1363 return NULL; 1504 return NULL;
1364 } 1505 }
1365 else 1506 else
1366 return PyBool_FromLong(result); 1507 return PyBool_FromLong(result);
1367 } 1508 }
1368 1509
1369 1510
1370 PyDoc_STRVAR(translate__doc__, 1511 /*[clinic input]
1371 "B.translate(table[, deletechars]) -> bytearray\n\ 1512 bytearray.translate
1372 \n\ 1513
1373 Return a copy of B, where all characters occurring in the\n\ 1514 self: self(type="PyByteArrayObject *")
1374 optional argument deletechars are removed, and the remaining\n\ 1515 table: object
1375 characters have been mapped through the given translation\n\ 1516 Translation table, which must be a bytes object of length 256.
1376 table, which must be a bytes object of length 256."); 1517 [
1377 1518 deletechars: object
1378 static PyObject * 1519 ]
1379 bytearray_translate(PyByteArrayObject *self, PyObject *args) 1520 /
1380 { 1521
1381 register char *input, *output; 1522 Return a copy with each character mapped by the given translation table.
1382 register const char *table; 1523
1383 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;
1384 PyObject *input_obj = (PyObject*)self; 1536 PyObject *input_obj = (PyObject*)self;
1385 const char *output_start; 1537 const char *output_start;
1386 Py_ssize_t inlen; 1538 Py_ssize_t inlen;
1387 PyObject *result = NULL; 1539 PyObject *result = NULL;
1388 int trans_table[256]; 1540 int trans_table[256];
1389 PyObject *tableobj = NULL, *delobj = NULL;
1390 Py_buffer vtable, vdel; 1541 Py_buffer vtable, vdel;
1391 1542
1392 if (!PyArg_UnpackTuple(args, "translate", 1, 2, 1543 if (table == Py_None) {
1393 &tableobj, &delobj)) 1544 table_chars = NULL;
1394 return NULL;
1395
1396 if (tableobj == Py_None) {
1397 table = NULL; 1545 table = NULL;
1398 tableobj = NULL; 1546 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
1399 } else if (_getbuffer(tableobj, &vtable) < 0) {
1400 return NULL; 1547 return NULL;
1401 } else { 1548 } else {
1402 if (vtable.len != 256) { 1549 if (vtable.len != 256) {
1403 PyErr_SetString(PyExc_ValueError, 1550 PyErr_SetString(PyExc_ValueError,
1404 "translation table must be 256 characters long"); 1551 "translation table must be 256 characters long");
1405 PyBuffer_Release(&vtable); 1552 PyBuffer_Release(&vtable);
1406 return NULL; 1553 return NULL;
1407 } 1554 }
1408 table = (const char*)vtable.buf; 1555 table_chars = (const char*)vtable.buf;
1409 } 1556 }
1410 1557
1411 if (delobj != NULL) { 1558 if (deletechars != NULL) {
1412 if (_getbuffer(delobj, &vdel) < 0) { 1559 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
1413 if (tableobj != NULL) 1560 if (table != NULL)
1414 PyBuffer_Release(&vtable); 1561 PyBuffer_Release(&vtable);
1415 return NULL; 1562 return NULL;
1416 } 1563 }
1417 } 1564 }
1418 else { 1565 else {
1419 vdel.buf = NULL; 1566 vdel.buf = NULL;
1420 vdel.len = 0; 1567 vdel.len = 0;
1421 } 1568 }
1422 1569
1423 inlen = PyByteArray_GET_SIZE(input_obj); 1570 inlen = PyByteArray_GET_SIZE(input_obj);
1424 result = PyByteArray_FromStringAndSize((char *)NULL, inlen); 1571 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1425 if (result == NULL) 1572 if (result == NULL)
1426 goto done; 1573 goto done;
1427 output_start = output = PyByteArray_AsString(result); 1574 output_start = output = PyByteArray_AsString(result);
1428 input = PyByteArray_AS_STRING(input_obj); 1575 input = PyByteArray_AS_STRING(input_obj);
1429 1576
1430 if (vdel.len == 0 && table != NULL) { 1577 if (vdel.len == 0 && table_chars != NULL) {
1431 /* If no deletions are required, use faster code */ 1578 /* If no deletions are required, use faster code */
1432 for (i = inlen; --i >= 0; ) { 1579 for (i = inlen; --i >= 0; ) {
1433 c = Py_CHARMASK(*input++); 1580 c = Py_CHARMASK(*input++);
1434 *output++ = table[c]; 1581 *output++ = table_chars[c];
1435 } 1582 }
1436 goto done; 1583 goto done;
1437 } 1584 }
1438 1585
1439 if (table == NULL) { 1586 if (table_chars == NULL) {
1440 for (i = 0; i < 256; i++) 1587 for (i = 0; i < 256; i++)
1441 trans_table[i] = Py_CHARMASK(i); 1588 trans_table[i] = Py_CHARMASK(i);
1442 } else { 1589 } else {
1443 for (i = 0; i < 256; i++) 1590 for (i = 0; i < 256; i++)
1444 trans_table[i] = Py_CHARMASK(table[i]); 1591 trans_table[i] = Py_CHARMASK(table_chars[i]);
1445 } 1592 }
1446 1593
1447 for (i = 0; i < vdel.len; i++) 1594 for (i = 0; i < vdel.len; i++)
1448 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; 1595 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1449 1596
1450 for (i = inlen; --i >= 0; ) { 1597 for (i = inlen; --i >= 0; ) {
1451 c = Py_CHARMASK(*input++); 1598 c = Py_CHARMASK(*input++);
1452 if (trans_table[c] != -1) 1599 if (trans_table[c] != -1)
1453 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) 1600 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1454 continue; 1601 continue;
1455 } 1602 }
1456 /* Fix the size of the resulting string */ 1603 /* Fix the size of the resulting string */
1457 if (inlen > 0) 1604 if (inlen > 0)
1458 PyByteArray_Resize(result, output - output_start); 1605 if (PyByteArray_Resize(result, output - output_start) < 0) {
1606 Py_CLEAR(result);
1607 goto done;
1608 }
1459 1609
1460 done: 1610 done:
1461 if (tableobj != NULL) 1611 if (table != NULL)
1462 PyBuffer_Release(&vtable); 1612 PyBuffer_Release(&vtable);
1463 if (delobj != NULL) 1613 if (deletechars != NULL)
1464 PyBuffer_Release(&vdel); 1614 PyBuffer_Release(&vdel);
1465 return result; 1615 return result;
1466 } 1616 }
1467 1617
1468 1618
1469 static PyObject * 1619 /*[clinic input]
1470 bytearray_maketrans(PyObject *null, PyObject *args) 1620
1471 { 1621 @staticmethod
1472 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);
1473 } 1641 }
1474 1642
1475 1643
1476 /* find and count characters and substrings */ 1644 /* find and count characters and substrings */
1477 1645
1478 #define findchar(target, target_len, c) \ 1646 #define findchar(target, target_len, c) \
1479 ((char *)memchr((const void *)(target), c, target_len)) 1647 ((char *)memchr((const void *)(target), c, target_len))
1480 1648
1481 1649
1482 /* 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
1953 if (from_len == 1) { 2121 if (from_len == 1) {
1954 return replace_single_character(self, from_s[0], 2122 return replace_single_character(self, from_s[0],
1955 to_s, to_len, maxcount); 2123 to_s, to_len, maxcount);
1956 } else { 2124 } else {
1957 /* len('from')>=2, len('to')>=1 */ 2125 /* len('from')>=2, len('to')>=1 */
1958 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) ;
1959 } 2127 }
1960 } 2128 }
1961 2129
1962 2130
1963 PyDoc_STRVAR(replace__doc__, 2131 /*[clinic input]
1964 "B.replace(old, new[, count]) -> bytearray\n\ 2132 bytearray.replace
1965 \n\ 2133
1966 Return a copy of B with all occurrences of subsection\n\ 2134 old: Py_buffer
1967 old replaced by new. If the optional argument count is\n\ 2135 new: Py_buffer
1968 given, only the first count occurrences are replaced."); 2136 count: Py_ssize_t = -1
1969 2137 Maximum number of occurrences to replace.
1970 static PyObject * 2138 -1 (the default value) means replace all occurrences.
1971 bytearray_replace(PyByteArrayObject *self, PyObject *args) 2139 /
1972 { 2140
1973 Py_ssize_t count = -1; 2141 Return a copy with all occurrences of substring old replaced by new.
1974 PyObject *from, *to, *res; 2142
1975 Py_buffer vfrom, vto; 2143 If the optional argument count is given, only the first count occurrences are
1976 2144 replaced.
1977 if (!PyArg_ParseTuple(args, "OO|n:replace", &from, &to, &count)) 2145 [clinic start generated code]*/
1978 return NULL; 2146
1979 2147 static PyObject *
1980 if (_getbuffer(from, &vfrom) < 0) 2148 bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
1981 return NULL; 2149 Py_buffer *new, Py_ssize_t count)
1982 if (_getbuffer(to, &vto) < 0) { 2150 /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
1983 PyBuffer_Release(&vfrom); 2151 {
1984 return NULL; 2152 return (PyObject *)replace((PyByteArrayObject *) self,
1985 } 2153 old->buf, old->len,
1986 2154 new->buf, new->len, count);
1987 res = (PyObject *)replace((PyByteArrayObject *) self, 2155 }
1988 vfrom.buf, vfrom.len, 2156
1989 vto.buf, vto.len, count); 2157 /*[clinic input]
1990 2158 bytearray.split
1991 PyBuffer_Release(&vfrom); 2159
1992 PyBuffer_Release(&vto); 2160 sep: object = None
1993 return res; 2161 The delimiter according which to split the bytearray.
1994 } 2162 None (the default value) means split on ASCII whitespace characters
1995 2163 (space, tab, return, newline, formfeed, vertical tab).
1996 PyDoc_STRVAR(split__doc__, 2164 maxsplit: Py_ssize_t = -1
1997 "B.split([sep[, maxsplit]]) -> list of bytearrays\n\ 2165 Maximum number of splits to do.
1998 \n\ 2166 -1 (the default value) means no limit.
1999 Return a list of the sections in B, using sep as the delimiter.\n\ 2167
2000 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.
2001 (space, tab, return, newline, formfeed, vertical tab).\n\ 2169 [clinic start generated code]*/
2002 If maxsplit is given, at most maxsplit splits are done."); 2170
2003 2171 static PyObject *
2004 static PyObject * 2172 bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
2005 bytearray_split(PyByteArrayObject *self, PyObject *args) 2173 Py_ssize_t maxsplit)
2174 /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
2006 { 2175 {
2007 Py_ssize_t len = PyByteArray_GET_SIZE(self), n; 2176 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2008 Py_ssize_t maxsplit = -1;
2009 const char *s = PyByteArray_AS_STRING(self), *sub; 2177 const char *s = PyByteArray_AS_STRING(self), *sub;
2010 PyObject *list, *subobj = Py_None; 2178 PyObject *list;
2011 Py_buffer vsub; 2179 Py_buffer vsub;
2012 2180
2013 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit))
2014 return NULL;
2015 if (maxsplit < 0) 2181 if (maxsplit < 0)
2016 maxsplit = PY_SSIZE_T_MAX; 2182 maxsplit = PY_SSIZE_T_MAX;
2017 2183
2018 if (subobj == Py_None) 2184 if (sep == Py_None)
2019 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); 2185 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
2020 2186
2021 if (_getbuffer(subobj, &vsub) < 0) 2187 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
2022 return NULL; 2188 return NULL;
2023 sub = vsub.buf; 2189 sub = vsub.buf;
2024 n = vsub.len; 2190 n = vsub.len;
2025 2191
2026 list = stringlib_split( 2192 list = stringlib_split(
2027 (PyObject*) self, s, len, sub, n, maxsplit 2193 (PyObject*) self, s, len, sub, n, maxsplit
2028 ); 2194 );
2029 PyBuffer_Release(&vsub); 2195 PyBuffer_Release(&vsub);
2030 return list; 2196 return list;
2031 } 2197 }
2032 2198
2033 PyDoc_STRVAR(partition__doc__, 2199 /*[clinic input]
2034 "B.partition(sep) -> (head, sep, tail)\n\ 2200 bytearray.partition
2035 \n\ 2201
2036 Search for the separator sep in B, and return the part before it,\n\ 2202 self: self(type="PyByteArrayObject *")
2037 the separator itself, and the part after it. If the separator is not\n\ 2203 sep: object
2038 found, returns B and two empty bytearray objects."); 2204 /
2039 2205
2040 static PyObject * 2206 Partition the bytearray into three parts using the given separator.
2041 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]*/
2042 { 2219 {
2043 PyObject *bytesep, *result; 2220 PyObject *bytesep, *result;
2044 2221
2045 bytesep = PyByteArray_FromObject(sep_obj); 2222 bytesep = PyByteArray_FromObject(sep);
2046 if (! bytesep) 2223 if (! bytesep)
2047 return NULL; 2224 return NULL;
2048 2225
2049 result = stringlib_partition( 2226 result = stringlib_partition(
2050 (PyObject*) self, 2227 (PyObject*) self,
2051 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 2228 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2052 bytesep, 2229 bytesep,
2053 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) 2230 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2054 ); 2231 );
2055 2232
2056 Py_DECREF(bytesep); 2233 Py_DECREF(bytesep);
2057 return result; 2234 return result;
2058 } 2235 }
2059 2236
2060 PyDoc_STRVAR(rpartition__doc__, 2237 /*[clinic input]
2061 "B.rpartition(sep) -> (head, sep, tail)\n\ 2238 bytearray.rpartition
2062 \n\ 2239
2063 Search for the separator sep in B, starting at the end of B,\n\ 2240 self: self(type="PyByteArrayObject *")
2064 and return the part before it, the separator itself, and the\n\ 2241 sep: object
2065 part after it. If the separator is not found, returns two empty\n\ 2242 /
2066 bytearray objects and B."); 2243
2067 2244 Partition the bytes into three parts using the given separator.
2068 static PyObject * 2245
2069 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]*/
2070 { 2257 {
2071 PyObject *bytesep, *result; 2258 PyObject *bytesep, *result;
2072 2259
2073 bytesep = PyByteArray_FromObject(sep_obj); 2260 bytesep = PyByteArray_FromObject(sep);
2074 if (! bytesep) 2261 if (! bytesep)
2075 return NULL; 2262 return NULL;
2076 2263
2077 result = stringlib_rpartition( 2264 result = stringlib_rpartition(
2078 (PyObject*) self, 2265 (PyObject*) self,
2079 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 2266 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2080 bytesep, 2267 bytesep,
2081 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) 2268 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2082 ); 2269 );
2083 2270
2084 Py_DECREF(bytesep); 2271 Py_DECREF(bytesep);
2085 return result; 2272 return result;
2086 } 2273 }
2087 2274
2088 PyDoc_STRVAR(rsplit__doc__, 2275 /*[clinic input]
2089 "B.rsplit(sep[, maxsplit]) -> list of bytearrays\n\ 2276 bytearray.rsplit = bytearray.split
2090 \n\ 2277
2091 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.
2092 starting at the end of B and working to the front.\n\ 2279
2093 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.
2094 (space, tab, return, newline, formfeed, vertical tab).\n\ 2281 [clinic start generated code]*/
2095 If maxsplit is given, at most maxsplit splits are done."); 2282
2096 2283 static PyObject *
2097 static PyObject * 2284 bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
2098 bytearray_rsplit(PyByteArrayObject *self, PyObject *args) 2285 Py_ssize_t maxsplit)
2286 /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
2099 { 2287 {
2100 Py_ssize_t len = PyByteArray_GET_SIZE(self), n; 2288 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2101 Py_ssize_t maxsplit = -1;
2102 const char *s = PyByteArray_AS_STRING(self), *sub; 2289 const char *s = PyByteArray_AS_STRING(self), *sub;
2103 PyObject *list, *subobj = Py_None; 2290 PyObject *list;
2104 Py_buffer vsub; 2291 Py_buffer vsub;
2105 2292
2106 if (!PyArg_ParseTuple(args, "|On:rsplit", &subobj, &maxsplit))
2107 return NULL;
2108 if (maxsplit < 0) 2293 if (maxsplit < 0)
2109 maxsplit = PY_SSIZE_T_MAX; 2294 maxsplit = PY_SSIZE_T_MAX;
2110 2295
2111 if (subobj == Py_None) 2296 if (sep == Py_None)
2112 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); 2297 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
2113 2298
2114 if (_getbuffer(subobj, &vsub) < 0) 2299 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
2115 return NULL; 2300 return NULL;
2116 sub = vsub.buf; 2301 sub = vsub.buf;
2117 n = vsub.len; 2302 n = vsub.len;
2118 2303
2119 list = stringlib_rsplit( 2304 list = stringlib_rsplit(
2120 (PyObject*) self, s, len, sub, n, maxsplit 2305 (PyObject*) self, s, len, sub, n, maxsplit
2121 ); 2306 );
2122 PyBuffer_Release(&vsub); 2307 PyBuffer_Release(&vsub);
2123 return list; 2308 return list;
2124 } 2309 }
2125 2310
2126 PyDoc_STRVAR(reverse__doc__, 2311 /*[clinic input]
2127 "B.reverse() -> None\n\ 2312 bytearray.reverse
2128 \n\ 2313
2129 Reverse the order of the values in B in place."); 2314 self: self(type="PyByteArrayObject *")
2130 static PyObject * 2315
2131 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]*/
2132 { 2322 {
2133 char swap, *head, *tail; 2323 char swap, *head, *tail;
2134 Py_ssize_t i, j, n = Py_SIZE(self); 2324 Py_ssize_t i, j, n = Py_SIZE(self);
2135 2325
2136 j = n / 2; 2326 j = n / 2;
2137 head = self->ob_bytes; 2327 head = PyByteArray_AS_STRING(self);
2138 tail = head + n - 1; 2328 tail = head + n - 1;
2139 for (i = 0; i < j; i++) { 2329 for (i = 0; i < j; i++) {
2140 swap = *head; 2330 swap = *head;
2141 *head++ = *tail; 2331 *head++ = *tail;
2142 *tail-- = swap; 2332 *tail-- = swap;
2143 } 2333 }
2144 2334
2145 Py_RETURN_NONE; 2335 Py_RETURN_NONE;
2146 } 2336 }
2147 2337
2148 PyDoc_STRVAR(insert__doc__, 2338
2149 "B.insert(index, int) -> None\n\ 2339 /*[python input]
2150 \n\ 2340 class bytesvalue_converter(CConverter):
2151 Insert a single item into the bytearray before the given index."); 2341 type = 'int'
2152 static PyObject * 2342 converter = '_getbytevalue'
2153 bytearray_insert(PyByteArrayObject *self, PyObject *args) 2343 [python start generated code]*/
2154 { 2344 /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
2155 PyObject *value; 2345
2156 int ival; 2346
2157 Py_ssize_t where, n = Py_SIZE(self); 2347 /*[clinic input]
2158 2348 bytearray.insert
2159 if (!PyArg_ParseTuple(args, "nO:insert", &where, &value)) 2349
2160 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;
2161 2366
2162 if (n == PY_SSIZE_T_MAX) { 2367 if (n == PY_SSIZE_T_MAX) {
2163 PyErr_SetString(PyExc_OverflowError, 2368 PyErr_SetString(PyExc_OverflowError,
2164 "cannot add more objects to bytearray"); 2369 "cannot add more objects to bytearray");
2165 return NULL; 2370 return NULL;
2166 } 2371 }
2167 if (!_getbytevalue(value, &ival))
2168 return NULL;
2169 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) 2372 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2170 return NULL; 2373 return NULL;
2171 2374 buf = PyByteArray_AS_STRING(self);
2172 if (where < 0) { 2375
2173 where += n; 2376 if (index < 0) {
2174 if (where < 0) 2377 index += n;
2175 where = 0; 2378 if (index < 0)
2176 } 2379 index = 0;
2177 if (where > n) 2380 }
2178 where = n; 2381 if (index > n)
2179 memmove(self->ob_bytes + where + 1, self->ob_bytes + where, n - where); 2382 index = n;
2180 self->ob_bytes[where] = ival; 2383 memmove(buf + index + 1, buf + index, n - index);
2384 buf[index] = item;
2181 2385
2182 Py_RETURN_NONE; 2386 Py_RETURN_NONE;
2183 } 2387 }
2184 2388
2185 PyDoc_STRVAR(append__doc__, 2389 /*[clinic input]
2186 "B.append(int) -> None\n\ 2390 bytearray.append
2187 \n\ 2391
2188 Append a single item to the end of B."); 2392 self: self(type="PyByteArrayObject *")
2189 static PyObject * 2393 item: bytesvalue
2190 bytearray_append(PyByteArrayObject *self, PyObject *arg) 2394 The item to be appended.
2191 { 2395 /
2192 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 {
2193 Py_ssize_t n = Py_SIZE(self); 2404 Py_ssize_t n = Py_SIZE(self);
2194 2405
2195 if (! _getbytevalue(arg, &value))
2196 return NULL;
2197 if (n == PY_SSIZE_T_MAX) { 2406 if (n == PY_SSIZE_T_MAX) {
2198 PyErr_SetString(PyExc_OverflowError, 2407 PyErr_SetString(PyExc_OverflowError,
2199 "cannot add more objects to bytearray"); 2408 "cannot add more objects to bytearray");
2200 return NULL; 2409 return NULL;
2201 } 2410 }
2202 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) 2411 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2203 return NULL; 2412 return NULL;
2204 2413
2205 self->ob_bytes[n] = value; 2414 PyByteArray_AS_STRING(self)[n] = item;
2206 2415
2207 Py_RETURN_NONE; 2416 Py_RETURN_NONE;
2208 } 2417 }
2209 2418
2210 PyDoc_STRVAR(extend__doc__, 2419 /*[clinic input]
2211 "B.extend(iterable_of_ints) -> None\n\ 2420 bytearray.extend
2212 \n\ 2421
2213 Append all the elements from the iterator or sequence to the\n\ 2422 self: self(type="PyByteArrayObject *")
2214 end of B."); 2423 iterable_of_ints: object
2215 static PyObject * 2424 The iterable of items to append.
2216 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]*/
2217 { 2433 {
2218 PyObject *it, *item, *bytearray_obj; 2434 PyObject *it, *item, *bytearray_obj;
2219 Py_ssize_t buf_size = 0, len = 0; 2435 Py_ssize_t buf_size = 0, len = 0;
2220 int value; 2436 int value;
2221 char *buf; 2437 char *buf;
2222 2438
2223 /* bytearray_setslice code only accepts something supporting PEP 3118. */ 2439 /* bytearray_setslice code only accepts something supporting PEP 3118. */
2224 if (PyObject_CheckBuffer(arg)) { 2440 if (PyObject_CheckBuffer(iterable_of_ints)) {
2225 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)
2226 return NULL; 2442 return NULL;
2227 2443
2228 Py_RETURN_NONE; 2444 Py_RETURN_NONE;
2229 } 2445 }
2230 2446
2231 it = PyObject_GetIter(arg); 2447 it = PyObject_GetIter(iterable_of_ints);
2232 if (it == NULL) 2448 if (it == NULL)
2233 return NULL; 2449 return NULL;
2234 2450
2235 /* Try to determine the length of the argument. 32 is arbitrary. */ 2451 /* Try to determine the length of the argument. 32 is arbitrary. */
2236 buf_size = _PyObject_LengthHint(arg, 32); 2452 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
2237 if (buf_size == -1) { 2453 if (buf_size == -1) {
2238 Py_DECREF(it); 2454 Py_DECREF(it);
2239 return NULL; 2455 return NULL;
2240 } 2456 }
2241 2457
2242 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); 2458 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
2243 if (bytearray_obj == NULL) { 2459 if (bytearray_obj == NULL) {
2244 Py_DECREF(it); 2460 Py_DECREF(it);
2245 return NULL; 2461 return NULL;
2246 } 2462 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2278 2494
2279 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) {
2280 Py_DECREF(bytearray_obj); 2496 Py_DECREF(bytearray_obj);
2281 return NULL; 2497 return NULL;
2282 } 2498 }
2283 Py_DECREF(bytearray_obj); 2499 Py_DECREF(bytearray_obj);
2284 2500
2285 Py_RETURN_NONE; 2501 Py_RETURN_NONE;
2286 } 2502 }
2287 2503
2288 PyDoc_STRVAR(pop__doc__, 2504 /*[clinic input]
2289 "B.pop([index]) -> int\n\ 2505 bytearray.pop
2290 \n\ 2506
2291 Remove and return a single item from B. If no index\n\ 2507 self: self(type="PyByteArrayObject *")
2292 argument is given, will pop the last value."); 2508 index: Py_ssize_t = -1
2293 static PyObject * 2509 The index from where to remove the item.
2294 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]*/
2295 { 2521 {
2296 int value; 2522 int value;
2297 Py_ssize_t where = -1, n = Py_SIZE(self); 2523 Py_ssize_t n = Py_SIZE(self);
2298 2524 char *buf;
2299 if (!PyArg_ParseTuple(args, "|n:pop", &where))
2300 return NULL;
2301 2525
2302 if (n == 0) { 2526 if (n == 0) {
2303 PyErr_SetString(PyExc_IndexError, 2527 PyErr_SetString(PyExc_IndexError,
2304 "pop from empty bytearray"); 2528 "pop from empty bytearray");
2305 return NULL; 2529 return NULL;
2306 } 2530 }
2307 if (where < 0) 2531 if (index < 0)
2308 where += n; 2532 index += Py_SIZE(self);
2309 if (where < 0 || where >= n) { 2533 if (index < 0 || index >= Py_SIZE(self)) {
2310 PyErr_SetString(PyExc_IndexError, "pop index out of range"); 2534 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2311 return NULL; 2535 return NULL;
2312 } 2536 }
2313 if (!_canresize(self)) 2537 if (!_canresize(self))
2314 return NULL; 2538 return NULL;
2315 2539
2316 value = self->ob_bytes[where]; 2540 buf = PyByteArray_AS_STRING(self);
2317 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);
2318 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2543 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2319 return NULL; 2544 return NULL;
2320 2545
2321 return PyLong_FromLong((unsigned char)value); 2546 return PyLong_FromLong((unsigned char)value);
2322 } 2547 }
2323 2548
2324 PyDoc_STRVAR(remove__doc__, 2549 /*[clinic input]
2325 "B.remove(int) -> None\n\ 2550 bytearray.remove
2326 \n\ 2551
2327 Remove the first occurrence of a value in B."); 2552 self: self(type="PyByteArrayObject *")
2328 static PyObject * 2553 value: bytesvalue
2329 bytearray_remove(PyByteArrayObject *self, PyObject *arg) 2554 The value to remove.
2330 { 2555 /
2331 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 {
2332 Py_ssize_t where, n = Py_SIZE(self); 2564 Py_ssize_t where, n = Py_SIZE(self);
2333 2565 char *buf = PyByteArray_AS_STRING(self);
2334 if (! _getbytevalue(arg, &value))
2335 return NULL;
2336 2566
2337 for (where = 0; where < n; where++) { 2567 for (where = 0; where < n; where++) {
2338 if (self->ob_bytes[where] == value) 2568 if (buf[where] == value)
2339 break; 2569 break;
2340 } 2570 }
2341 if (where == n) { 2571 if (where == n) {
2342 PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); 2572 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
2343 return NULL; 2573 return NULL;
2344 } 2574 }
2345 if (!_canresize(self)) 2575 if (!_canresize(self))
2346 return NULL; 2576 return NULL;
2347 2577
2348 memmove(self->ob_bytes + where, self->ob_bytes + where + 1, n - where - 1); 2578 memmove(buf + where, buf + where + 1, n - where);
2349 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2579 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2350 return NULL; 2580 return NULL;
2351 2581
2352 Py_RETURN_NONE; 2582 Py_RETURN_NONE;
2353 } 2583 }
2354 2584
2355 /* XXX These two helpers could be optimized if argsize == 1 */ 2585 /* XXX These two helpers could be optimized if argsize == 1 */
2356 2586
2357 static Py_ssize_t 2587 static Py_ssize_t
2358 lstrip_helper(unsigned char *myptr, Py_ssize_t mysize, 2588 lstrip_helper(char *myptr, Py_ssize_t mysize,
2359 void *argptr, Py_ssize_t argsize) 2589 void *argptr, Py_ssize_t argsize)
2360 { 2590 {
2361 Py_ssize_t i = 0; 2591 Py_ssize_t i = 0;
2362 while (i < mysize && memchr(argptr, myptr[i], argsize)) 2592 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
2363 i++; 2593 i++;
2364 return i; 2594 return i;
2365 } 2595 }
2366 2596
2367 static Py_ssize_t 2597 static Py_ssize_t
2368 rstrip_helper(unsigned char *myptr, Py_ssize_t mysize, 2598 rstrip_helper(char *myptr, Py_ssize_t mysize,
2369 void *argptr, Py_ssize_t argsize) 2599 void *argptr, Py_ssize_t argsize)
2370 { 2600 {
2371 Py_ssize_t i = mysize - 1; 2601 Py_ssize_t i = mysize - 1;
2372 while (i >= 0 && memchr(argptr, myptr[i], argsize)) 2602 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
2373 i--; 2603 i--;
2374 return i + 1; 2604 return i + 1;
2375 } 2605 }
2376 2606
2377 PyDoc_STRVAR(strip__doc__, 2607 /*[clinic input]
2378 "B.strip([bytes]) -> bytearray\n\ 2608 bytearray.strip
2379 \n\ 2609
2380 Strip leading and trailing bytes contained in the argument\n\ 2610 bytes: object = None
2381 and return the result as a new bytearray.\n\ 2611 /
2382 If the argument is omitted, strip ASCII whitespace."); 2612
2383 static PyObject * 2613 Strip leading and trailing bytes contained in the argument.
2384 bytearray_strip(PyByteArrayObject *self, PyObject *args) 2614
2385 { 2615 If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2386 Py_ssize_t left, right, mysize, argsize; 2616 [clinic start generated code]*/
2387 void *myptr, *argptr; 2617
2388 PyObject *arg = Py_None; 2618 static PyObject *
2389 Py_buffer varg; 2619 bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
2390 if (!PyArg_ParseTuple(args, "|O:strip", &arg)) 2620 /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
2391 return NULL; 2621 {
2392 if (arg == Py_None) { 2622 Py_ssize_t left, right, mysize, byteslen;
2393 argptr = "\t\n\r\f\v "; 2623 char *myptr, *bytesptr;
2394 argsize = 6; 2624 Py_buffer vbytes;
2625
2626 if (bytes == Py_None) {
2627 bytesptr = "\t\n\r\f\v ";
2628 byteslen = 6;
2395 } 2629 }
2396 else { 2630 else {
2397 if (_getbuffer(arg, &varg) < 0) 2631 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2398 return NULL; 2632 return NULL;
2399 argptr = varg.buf; 2633 bytesptr = (char *) vbytes.buf;
2400 argsize = varg.len; 2634 byteslen = vbytes.len;
2401 } 2635 }
2402 myptr = self->ob_bytes; 2636 myptr = PyByteArray_AS_STRING(self);
2403 mysize = Py_SIZE(self); 2637 mysize = Py_SIZE(self);
2404 left = lstrip_helper(myptr, mysize, argptr, argsize); 2638 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
2405 if (left == mysize) 2639 if (left == mysize)
2406 right = left; 2640 right = left;
2407 else 2641 else
2408 right = rstrip_helper(myptr, mysize, argptr, argsize); 2642 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2409 if (arg != Py_None) 2643 if (bytes != Py_None)
2410 PyBuffer_Release(&varg); 2644 PyBuffer_Release(&vbytes);
2411 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); 2645 return PyByteArray_FromStringAndSize(myptr + left, right - left);
2412 } 2646 }
2413 2647
2414 PyDoc_STRVAR(lstrip__doc__, 2648 /*[clinic input]
2415 "B.lstrip([bytes]) -> bytearray\n\ 2649 bytearray.lstrip
2416 \n\ 2650
2417 Strip leading bytes contained in the argument\n\ 2651 bytes: object = None
2418 and return the result as a new bytearray.\n\ 2652 /
2419 If the argument is omitted, strip leading ASCII whitespace."); 2653
2420 static PyObject * 2654 Strip leading bytes contained in the argument.
2421 bytearray_lstrip(PyByteArrayObject *self, PyObject *args) 2655
2422 { 2656 If the argument is omitted or None, strip leading ASCII whitespace.
2423 Py_ssize_t left, right, mysize, argsize; 2657 [clinic start generated code]*/
2424 void *myptr, *argptr; 2658
2425 PyObject *arg = Py_None; 2659 static PyObject *
2426 Py_buffer varg; 2660 bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
2427 if (!PyArg_ParseTuple(args, "|O:lstrip", &arg)) 2661 /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
2428 return NULL; 2662 {
2429 if (arg == Py_None) { 2663 Py_ssize_t left, right, mysize, byteslen;
2430 argptr = "\t\n\r\f\v "; 2664 char *myptr, *bytesptr;
2431 argsize = 6; 2665 Py_buffer vbytes;
2666
2667 if (bytes == Py_None) {
2668 bytesptr = "\t\n\r\f\v ";
2669 byteslen = 6;
2432 } 2670 }
2433 else { 2671 else {
2434 if (_getbuffer(arg, &varg) < 0) 2672 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2435 return NULL; 2673 return NULL;
2436 argptr = varg.buf; 2674 bytesptr = (char *) vbytes.buf;
2437 argsize = varg.len; 2675 byteslen = vbytes.len;
2438 } 2676 }
2439 myptr = self->ob_bytes; 2677 myptr = PyByteArray_AS_STRING(self);
2440 mysize = Py_SIZE(self); 2678 mysize = Py_SIZE(self);
2441 left = lstrip_helper(myptr, mysize, argptr, argsize); 2679 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
2442 right = mysize; 2680 right = mysize;
2443 if (arg != Py_None) 2681 if (bytes != Py_None)
2444 PyBuffer_Release(&varg); 2682 PyBuffer_Release(&vbytes);
2445 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); 2683 return PyByteArray_FromStringAndSize(myptr + left, right - left);
2446 } 2684 }
2447 2685
2448 PyDoc_STRVAR(rstrip__doc__, 2686 /*[clinic input]
2449 "B.rstrip([bytes]) -> bytearray\n\ 2687 bytearray.rstrip
2450 \n\ 2688
2451 Strip trailing bytes contained in the argument\n\ 2689 bytes: object = None
2452 and return the result as a new bytearray.\n\ 2690 /
2453 If the argument is omitted, strip trailing ASCII whitespace."); 2691
2454 static PyObject * 2692 Strip trailing bytes contained in the argument.
2455 bytearray_rstrip(PyByteArrayObject *self, PyObject *args) 2693
2456 { 2694 If the argument is omitted or None, strip trailing ASCII whitespace.
2457 Py_ssize_t left, right, mysize, argsize; 2695 [clinic start generated code]*/
2458 void *myptr, *argptr; 2696
2459 PyObject *arg = Py_None; 2697 static PyObject *
2460 Py_buffer varg; 2698 bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
2461 if (!PyArg_ParseTuple(args, "|O:rstrip", &arg)) 2699 /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
2462 return NULL; 2700 {
2463 if (arg == Py_None) { 2701 Py_ssize_t right, mysize, byteslen;
2464 argptr = "\t\n\r\f\v "; 2702 char *myptr, *bytesptr;
2465 argsize = 6; 2703 Py_buffer vbytes;
2704
2705 if (bytes == Py_None) {
2706 bytesptr = "\t\n\r\f\v ";
2707 byteslen = 6;
2466 } 2708 }
2467 else { 2709 else {
2468 if (_getbuffer(arg, &varg) < 0) 2710 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2469 return NULL; 2711 return NULL;
2470 argptr = varg.buf; 2712 bytesptr = (char *) vbytes.buf;
2471 argsize = varg.len; 2713 byteslen = vbytes.len;
2472 } 2714 }
2473 myptr = self->ob_bytes; 2715 myptr = PyByteArray_AS_STRING(self);
2474 mysize = Py_SIZE(self); 2716 mysize = Py_SIZE(self);
2475 left = 0; 2717 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2476 right = rstrip_helper(myptr, mysize, argptr, argsize); 2718 if (bytes != Py_None)
2477 if (arg != Py_None) 2719 PyBuffer_Release(&vbytes);
2478 PyBuffer_Release(&varg); 2720 return PyByteArray_FromStringAndSize(myptr, right);
2479 return PyByteArray_FromStringAndSize(self->ob_bytes + left, right - left); 2721 }
2480 } 2722
2481 2723 /*[clinic input]
2482 PyDoc_STRVAR(decode_doc, 2724 bytearray.decode
2483 "B.decode(encoding='utf-8', errors='strict') -> str\n\ 2725
2484 \n\ 2726 encoding: str(c_default="NULL") = 'utf-8'
2485 Decode B using the codec registered for encoding. Default encoding\n\ 2727 The encoding with which to decode the bytearray.
2486 is 'utf-8'. errors may be given to set a different error\n\ 2728 errors: str(c_default="NULL") = 'strict'
2487 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ 2729 The error handling scheme to use for the handling of decoding errors.
2488 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ 2730 The default is 'strict' meaning that decoding errors raise a
2489 as well as any other name registered with codecs.register_error that is\n\ 2731 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2490 able to handle UnicodeDecodeErrors."); 2732 as well as any other name registered with codecs.register_error that
2491 2733 can handle UnicodeDecodeErrors.
2492 static PyObject * 2734
2493 bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs) 2735 Decode the bytearray using the codec registered for encoding.
2494 { 2736 [clinic start generated code]*/
2495 const char *encoding = NULL; 2737
2496 const char *errors = NULL; 2738 static PyObject *
2497 static char *kwlist[] = {"encoding", "errors", 0}; 2739 bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
2498 2740 const char *errors)
2499 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encodi ng, &errors)) 2741 /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
2500 return NULL; 2742 {
2501 if (encoding == NULL) 2743 if (encoding == NULL)
2502 encoding = PyUnicode_GetDefaultEncoding(); 2744 encoding = PyUnicode_GetDefaultEncoding();
2503 return PyUnicode_FromEncodedObject(self, encoding, errors); 2745 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
2504 } 2746 }
2505 2747
2506 PyDoc_STRVAR(alloc_doc, 2748 PyDoc_STRVAR(alloc_doc,
2507 "B.__alloc__() -> int\n\ 2749 "B.__alloc__() -> int\n\
2508 \n\ 2750 \n\
2509 Return the number of bytes actually allocated."); 2751 Return the number of bytes actually allocated.");
2510 2752
2511 static PyObject * 2753 static PyObject *
2512 bytearray_alloc(PyByteArrayObject *self) 2754 bytearray_alloc(PyByteArrayObject *self)
2513 { 2755 {
2514 return PyLong_FromSsize_t(self->ob_alloc); 2756 return PyLong_FromSsize_t(self->ob_alloc);
2515 } 2757 }
2516 2758
2517 PyDoc_STRVAR(join_doc, 2759 /*[clinic input]
2518 "B.join(iterable_of_bytes) -> bytearray\n\ 2760 bytearray.join
2519 \n\ 2761
2520 Concatenate any number of bytes/bytearray objects, with B\n\ 2762 iterable_of_bytes: object
2521 in between each pair, and return the result as a new bytearray."); 2763 /
2522 2764
2523 static PyObject * 2765 Concatenate any number of bytes/bytearray objects.
2524 bytearray_join(PyByteArrayObject *self, PyObject *it) 2766
2525 { 2767 The bytearray whose method is called is inserted in between each pair.
2526 PyObject *seq; 2768
2527 Py_ssize_t mysize = Py_SIZE(self); 2769 The result is returned as a new bytearray object.
2528 Py_ssize_t i; 2770 [clinic start generated code]*/
2529 Py_ssize_t n; 2771
2530 PyObject **items; 2772 static PyObject *
2531 Py_ssize_t totalsize = 0; 2773 bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
2532 PyObject *result; 2774 /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
2533 char *dest; 2775 {
2534 2776 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
2535 seq = PySequence_Fast(it, "can only join an iterable"); 2777 }
2536 if (seq == NULL) 2778
2537 return NULL; 2779 /*[clinic input]
2538 n = PySequence_Fast_GET_SIZE(seq); 2780 bytearray.splitlines
2539 items = PySequence_Fast_ITEMS(seq); 2781
2540 2782 keepends: int(c_default="0") = False
2541 /* Compute the total size, and check that they are all bytes */ 2783
2542 /* XXX Shouldn't we use _getbuffer() on these items instead? */ 2784 Return a list of the lines in the bytearray, breaking at line boundaries.
2543 for (i = 0; i < n; i++) { 2785
2544 PyObject *obj = items[i]; 2786 Line breaks are not included in the resulting list unless keepends is given and
2545 if (!PyByteArray_Check(obj) && !PyBytes_Check(obj)) { 2787 true.
2546 PyErr_Format(PyExc_TypeError, 2788 [clinic start generated code]*/
2547 "can only join an iterable of bytes " 2789
2548 "(item %ld has type '%.100s')", 2790 static PyObject *
2549 /* XXX %ld isn't right on Win64 */ 2791 bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
2550 (long)i, Py_TYPE(obj)->tp_name); 2792 /*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
2551 goto error; 2793 {
2552 }
2553 if (i > 0)
2554 totalsize += mysize;
2555 totalsize += Py_SIZE(obj);
2556 if (totalsize < 0) {
2557 PyErr_NoMemory();
2558 goto error;
2559 }
2560 }
2561
2562 /* Allocate the result, and copy the bytes */
2563 result = PyByteArray_FromStringAndSize(NULL, totalsize);
2564 if (result == NULL)
2565 goto error;
2566 dest = PyByteArray_AS_STRING(result);
2567 for (i = 0; i < n; i++) {
2568 PyObject *obj = items[i];
2569 Py_ssize_t size = Py_SIZE(obj);
2570 char *buf;
2571 if (PyByteArray_Check(obj))
2572 buf = PyByteArray_AS_STRING(obj);
2573 else
2574 buf = PyBytes_AS_STRING(obj);
2575 if (i) {
2576 memcpy(dest, self->ob_bytes, mysize);
2577 dest += mysize;
2578 }
2579 memcpy(dest, buf, size);
2580 dest += size;
2581 }
2582
2583 /* Done */
2584 Py_DECREF(seq);
2585 return result;
2586
2587 /* Error handling */
2588 error:
2589 Py_DECREF(seq);
2590 return NULL;
2591 }
2592
2593 PyDoc_STRVAR(splitlines__doc__,
2594 "B.splitlines([keepends]) -> list of lines\n\
2595 \n\
2596 Return a list of the lines in B, breaking at line boundaries.\n\
2597 Line breaks are not included in the resulting list unless keepends\n\
2598 is given and true.");
2599
2600 static PyObject*
2601 bytearray_splitlines(PyObject *self, PyObject *args)
2602 {
2603 int keepends = 0;
2604
2605 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends))
2606 return NULL;
2607
2608 return stringlib_splitlines( 2794 return stringlib_splitlines(
2609 (PyObject*) self, PyByteArray_AS_STRING(self), 2795 (PyObject*) self, PyByteArray_AS_STRING(self),
2610 PyByteArray_GET_SIZE(self), keepends 2796 PyByteArray_GET_SIZE(self), keepends
2611 ); 2797 );
2612 } 2798 }
2613 2799
2614 PyDoc_STRVAR(fromhex_doc,
2615 "bytearray.fromhex(string) -> bytearray (static method)\n\
2616 \n\
2617 Create a bytearray object from a string of hexadecimal numbers.\n\
2618 Spaces between two numbers are accepted.\n\
2619 Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
2620
2621 static int 2800 static int
2622 hex_digit_to_int(Py_UNICODE c) 2801 hex_digit_to_int(Py_UCS4 c)
2623 { 2802 {
2624 if (c >= 128) 2803 if (c >= 128)
2625 return -1; 2804 return -1;
2626 if (Py_ISDIGIT(c)) 2805 if (Py_ISDIGIT(c))
2627 return c - '0'; 2806 return c - '0';
2628 else { 2807 else {
2629 if (Py_ISUPPER(c)) 2808 if (Py_ISUPPER(c))
2630 c = Py_TOLOWER(c); 2809 c = Py_TOLOWER(c);
2631 if (c >= 'a' && c <= 'f') 2810 if (c >= 'a' && c <= 'f')
2632 return c - 'a' + 10; 2811 return c - 'a' + 10;
2633 } 2812 }
2634 return -1; 2813 return -1;
2635 } 2814 }
2636 2815
2637 static PyObject * 2816 /*[clinic input]
2638 bytearray_fromhex(PyObject *cls, PyObject *args) 2817 @classmethod
2639 { 2818 bytearray.fromhex
2640 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;
2641 char *buf; 2835 char *buf;
2642 Py_UNICODE *hex;
2643 Py_ssize_t hexlen, byteslen, i, j; 2836 Py_ssize_t hexlen, byteslen, i, j;
2644 int top, bot; 2837 int top, bot;
2645 2838 void *data;
2646 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) 2839 unsigned int kind;
2647 return NULL; 2840
2648 assert(PyUnicode_Check(hexobj)); 2841 assert(PyUnicode_Check(string));
2649 hexlen = PyUnicode_GET_SIZE(hexobj); 2842 if (PyUnicode_READY(string))
2650 hex = PyUnicode_AS_UNICODE(hexobj); 2843 return NULL;
2844 kind = PyUnicode_KIND(string);
2845 data = PyUnicode_DATA(string);
2846 hexlen = PyUnicode_GET_LENGTH(string);
2847
2651 byteslen = hexlen/2; /* This overestimates if there are spaces */ 2848 byteslen = hexlen/2; /* This overestimates if there are spaces */
2652 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); 2849 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
2653 if (!newbytes) 2850 if (!newbytes)
2654 return NULL; 2851 return NULL;
2655 buf = PyByteArray_AS_STRING(newbytes); 2852 buf = PyByteArray_AS_STRING(newbytes);
2656 for (i = j = 0; i < hexlen; i += 2) { 2853 for (i = j = 0; i < hexlen; i += 2) {
2657 /* skip over spaces in the input */ 2854 /* skip over spaces in the input */
2658 while (hex[i] == ' ') 2855 while (PyUnicode_READ(kind, data, i) == ' ')
2659 i++; 2856 i++;
2660 if (i >= hexlen) 2857 if (i >= hexlen)
2661 break; 2858 break;
2662 top = hex_digit_to_int(hex[i]); 2859 top = hex_digit_to_int(PyUnicode_READ(kind, data, i));
2663 bot = hex_digit_to_int(hex[i+1]); 2860 bot = hex_digit_to_int(PyUnicode_READ(kind, data, i+1));
2664 if (top == -1 || bot == -1) { 2861 if (top == -1 || bot == -1) {
2665 PyErr_Format(PyExc_ValueError, 2862 PyErr_Format(PyExc_ValueError,
2666 "non-hexadecimal number found in " 2863 "non-hexadecimal number found in "
2667 "fromhex() arg at position %zd", i); 2864 "fromhex() arg at position %zd", i);
2668 goto error; 2865 goto error;
2669 } 2866 }
2670 buf[j++] = (top << 4) + bot; 2867 buf[j++] = (top << 4) + bot;
2671 } 2868 }
2672 if (PyByteArray_Resize(newbytes, j) < 0) 2869 if (PyByteArray_Resize(newbytes, j) < 0)
2673 goto error; 2870 goto error;
2674 return newbytes; 2871 return newbytes;
2675 2872
2676 error: 2873 error:
2677 Py_DECREF(newbytes); 2874 Py_DECREF(newbytes);
2678 return NULL; 2875 return NULL;
2679 } 2876 }
2680 2877
2681 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); 2878 PyDoc_STRVAR(hex__doc__,
2682 2879 "B.hex() -> string\n\
2683 static PyObject * 2880 \n\
2684 bytearray_reduce(PyByteArrayObject *self) 2881 Create a string of hexadecimal numbers from a bytearray object.\n\
2685 { 2882 Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2686 PyObject *latin1, *dict; 2883
2687 if (self->ob_bytes) 2884 static PyObject *
2688 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, 2885 bytearray_hex(PyBytesObject *self)
2689 Py_SIZE(self), NULL); 2886 {
2690 else 2887 char* argbuf = PyByteArray_AS_STRING(self);
2691 latin1 = PyUnicode_FromString(""); 2888 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2692 2889 return _Py_strhex(argbuf, arglen);
2693 dict = PyObject_GetAttrString((PyObject *)self, "__dict__"); 2890 }
2891
2892 static PyObject *
2893 _common_reduce(PyByteArrayObject *self, int proto)
2894 {
2895 PyObject *dict;
2896 _Py_IDENTIFIER(__dict__);
2897 char *buf;
2898
2899 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
2694 if (dict == NULL) { 2900 if (dict == NULL) {
2695 PyErr_Clear(); 2901 PyErr_Clear();
2696 dict = Py_None; 2902 dict = Py_None;
2697 Py_INCREF(dict); 2903 Py_INCREF(dict);
2698 } 2904 }
2699 2905
2700 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict); 2906 buf = PyByteArray_AS_STRING(self);
2701 } 2907 if (proto < 3) {
2702 2908 /* use str based reduction for backwards compatibility with Python 2.x * /
2703 PyDoc_STRVAR(sizeof_doc, 2909 PyObject *latin1;
2704 "B.__sizeof__() -> int\n\ 2910 if (Py_SIZE(self))
2705 \n\ 2911 latin1 = PyUnicode_DecodeLatin1(buf, Py_SIZE(self), NULL);
2706 Returns the size of B in memory, in bytes"); 2912 else
2707 static PyObject * 2913 latin1 = PyUnicode_FromString("");
2708 bytearray_sizeof(PyByteArrayObject *self) 2914 return Py_BuildValue("(O(Ns)N)", Py_TYPE(self), latin1, "latin-1", dict) ;
2915 }
2916 else {
2917 /* use more efficient byte based reduction */
2918 if (Py_SIZE(self)) {
2919 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
2920 }
2921 else {
2922 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2923 }
2924 }
2925 }
2926
2927 /*[clinic input]
2928 bytearray.__reduce__ as bytearray_reduce
2929
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]*/
2938 {
2939 return _common_reduce(self, 2);
2940 }
2941
2942 /*[clinic input]
2943 bytearray.__reduce_ex__ as bytearray_reduce_ex
2944
2945 self: self(type="PyByteArrayObject *")
2946 proto: int = 0
2947 /
2948
2949 Return state information for pickling.
2950 [clinic start generated code]*/
2951
2952 static PyObject *
2953 bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
2954 /*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/
2955 {
2956 return _common_reduce(self, proto);
2957 }
2958
2959 /*[clinic input]
2960 bytearray.__sizeof__ as bytearray_sizeof
2961
2962 self: self(type="PyByteArrayObject *")
2963
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]*/
2709 { 2970 {
2710 Py_ssize_t res; 2971 Py_ssize_t res;
2711 2972
2712 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char); 2973 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
2713 return PyLong_FromSsize_t(res); 2974 return PyLong_FromSsize_t(res);
2714 } 2975 }
2715 2976
2716 static PySequenceMethods bytearray_as_sequence = { 2977 static PySequenceMethods bytearray_as_sequence = {
2717 (lenfunc)bytearray_length, /* sq_length */ 2978 (lenfunc)bytearray_length, /* sq_length */
2718 (binaryfunc)PyByteArray_Concat, /* sq_concat */ 2979 (binaryfunc)PyByteArray_Concat, /* sq_concat */
(...skipping 14 matching lines...) Expand all
2733 }; 2994 };
2734 2995
2735 static PyBufferProcs bytearray_as_buffer = { 2996 static PyBufferProcs bytearray_as_buffer = {
2736 (getbufferproc)bytearray_getbuffer, 2997 (getbufferproc)bytearray_getbuffer,
2737 (releasebufferproc)bytearray_releasebuffer, 2998 (releasebufferproc)bytearray_releasebuffer,
2738 }; 2999 };
2739 3000
2740 static PyMethodDef 3001 static PyMethodDef
2741 bytearray_methods[] = { 3002 bytearray_methods[] = {
2742 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, 3003 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
2743 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, reduce_doc}, 3004 BYTEARRAY_REDUCE_METHODDEF
2744 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, sizeof_doc}, 3005 BYTEARRAY_REDUCE_EX_METHODDEF
2745 {"append", (PyCFunction)bytearray_append, METH_O, append__doc__}, 3006 BYTEARRAY_SIZEOF_METHODDEF
3007 BYTEARRAY_APPEND_METHODDEF
2746 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, 3008 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2747 _Py_capitalize__doc__}, 3009 _Py_capitalize__doc__},
2748 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, 3010 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
3011 BYTEARRAY_CLEAR_METHODDEF
3012 BYTEARRAY_COPY_METHODDEF
2749 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, 3013 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
2750 {"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, deco de_doc}, 3014 BYTEARRAY_DECODE_METHODDEF
2751 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__} , 3015 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__} ,
2752 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, 3016 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWOR DS,
2753 expandtabs__doc__}, 3017 expandtabs__doc__},
2754 {"extend", (PyCFunction)bytearray_extend, METH_O, extend__doc__}, 3018 BYTEARRAY_EXTEND_METHODDEF
2755 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, 3019 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
2756 {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS, 3020 BYTEARRAY_FROMHEX_METHODDEF
2757 fromhex_doc}, 3021 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2758 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, 3022 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
2759 {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, insert__doc__}, 3023 BYTEARRAY_INSERT_METHODDEF
2760 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, 3024 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2761 _Py_isalnum__doc__}, 3025 _Py_isalnum__doc__},
2762 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, 3026 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2763 _Py_isalpha__doc__}, 3027 _Py_isalpha__doc__},
2764 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, 3028 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2765 _Py_isdigit__doc__}, 3029 _Py_isdigit__doc__},
2766 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, 3030 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2767 _Py_islower__doc__}, 3031 _Py_islower__doc__},
2768 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, 3032 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2769 _Py_isspace__doc__}, 3033 _Py_isspace__doc__},
2770 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, 3034 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2771 _Py_istitle__doc__}, 3035 _Py_istitle__doc__},
2772 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, 3036 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2773 _Py_isupper__doc__}, 3037 _Py_isupper__doc__},
2774 {"join", (PyCFunction)bytearray_join, METH_O, join_doc}, 3038 BYTEARRAY_JOIN_METHODDEF
2775 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, 3039 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
2776 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, 3040 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
2777 {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, lstrip__doc__}, 3041 BYTEARRAY_LSTRIP_METHODDEF
2778 {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC, 3042 BYTEARRAY_MAKETRANS_METHODDEF
2779 _Py_maketrans__doc__}, 3043 BYTEARRAY_PARTITION_METHODDEF
2780 {"partition", (PyCFunction)bytearray_partition, METH_O, partition__doc__}, 3044 BYTEARRAY_POP_METHODDEF
2781 {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, pop__doc__}, 3045 BYTEARRAY_REMOVE_METHODDEF
2782 {"remove", (PyCFunction)bytearray_remove, METH_O, remove__doc__}, 3046 BYTEARRAY_REPLACE_METHODDEF
2783 {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, replace__doc__}, 3047 BYTEARRAY_REVERSE_METHODDEF
2784 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, reverse__doc__},
2785 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, 3048 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
2786 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, 3049 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
2787 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, 3050 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
2788 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__} , 3051 BYTEARRAY_RPARTITION_METHODDEF
2789 {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS, rsplit__doc__}, 3052 BYTEARRAY_RSPLIT_METHODDEF
2790 {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__}, 3053 BYTEARRAY_RSTRIP_METHODDEF
2791 {"split", (PyCFunction)bytearray_split, METH_VARARGS, split__doc__}, 3054 BYTEARRAY_SPLIT_METHODDEF
2792 {"splitlines", (PyCFunction)bytearray_splitlines, METH_VARARGS, 3055 BYTEARRAY_SPLITLINES_METHODDEF
2793 splitlines__doc__},
2794 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , 3056 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
2795 startswith__doc__}, 3057 startswith__doc__},
2796 {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__}, 3058 BYTEARRAY_STRIP_METHODDEF
2797 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, 3059 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2798 _Py_swapcase__doc__}, 3060 _Py_swapcase__doc__},
2799 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, 3061 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
2800 {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, 3062 BYTEARRAY_TRANSLATE_METHODDEF
2801 translate__doc__},
2802 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, 3063 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2803 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, 3064 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
2804 {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*/
2805 }; 3081 };
2806 3082
2807 PyDoc_STRVAR(bytearray_doc, 3083 PyDoc_STRVAR(bytearray_doc,
2808 "bytearray(iterable_of_ints) -> bytearray\n\ 3084 "bytearray(iterable_of_ints) -> bytearray\n\
2809 bytearray(string, encoding[, errors]) -> bytearray\n\ 3085 bytearray(string, encoding[, errors]) -> bytearray\n\
2810 bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ 3086 bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2811 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\
2812 bytearray() -> empty bytes array\n\ 3088 bytearray() -> empty bytes array\n\
2813 \n\ 3089 \n\
2814 Construct an mutable bytearray object from:\n\ 3090 Construct an mutable bytearray object from:\n\
(...skipping 10 matching lines...) Expand all
2825 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3101 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2826 "bytearray", 3102 "bytearray",
2827 sizeof(PyByteArrayObject), 3103 sizeof(PyByteArrayObject),
2828 0, 3104 0,
2829 (destructor)bytearray_dealloc, /* tp_dealloc */ 3105 (destructor)bytearray_dealloc, /* tp_dealloc */
2830 0, /* tp_print */ 3106 0, /* tp_print */
2831 0, /* tp_getattr */ 3107 0, /* tp_getattr */
2832 0, /* tp_setattr */ 3108 0, /* tp_setattr */
2833 0, /* tp_reserved */ 3109 0, /* tp_reserved */
2834 (reprfunc)bytearray_repr, /* tp_repr */ 3110 (reprfunc)bytearray_repr, /* tp_repr */
2835 0, /* tp_as_number */ 3111 &bytearray_as_number, /* tp_as_number */
2836 &bytearray_as_sequence, /* tp_as_sequence */ 3112 &bytearray_as_sequence, /* tp_as_sequence */
2837 &bytearray_as_mapping, /* tp_as_mapping */ 3113 &bytearray_as_mapping, /* tp_as_mapping */
2838 0, /* tp_hash */ 3114 0, /* tp_hash */
2839 0, /* tp_call */ 3115 0, /* tp_call */
2840 bytearray_str, /* tp_str */ 3116 bytearray_str, /* tp_str */
2841 PyObject_GenericGetAttr, /* tp_getattro */ 3117 PyObject_GenericGetAttr, /* tp_getattro */
2842 0, /* tp_setattro */ 3118 0, /* tp_setattro */
2843 &bytearray_as_buffer, /* tp_as_buffer */ 3119 &bytearray_as_buffer, /* tp_as_buffer */
2844 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ 3120 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2845 bytearray_doc, /* tp_doc */ 3121 bytearray_doc, /* tp_doc */
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
2893 PyObject *item; 3169 PyObject *item;
2894 3170
2895 assert(it != NULL); 3171 assert(it != NULL);
2896 seq = it->it_seq; 3172 seq = it->it_seq;
2897 if (seq == NULL) 3173 if (seq == NULL)
2898 return NULL; 3174 return NULL;
2899 assert(PyByteArray_Check(seq)); 3175 assert(PyByteArray_Check(seq));
2900 3176
2901 if (it->it_index < PyByteArray_GET_SIZE(seq)) { 3177 if (it->it_index < PyByteArray_GET_SIZE(seq)) {
2902 item = PyLong_FromLong( 3178 item = PyLong_FromLong(
2903 (unsigned char)seq->ob_bytes[it->it_index]); 3179 (unsigned char)PyByteArray_AS_STRING(seq)[it->it_index]);
2904 if (item != NULL) 3180 if (item != NULL)
2905 ++it->it_index; 3181 ++it->it_index;
2906 return item; 3182 return item;
2907 } 3183 }
2908 3184
2909 Py_DECREF(seq); 3185 Py_DECREF(seq);
2910 it->it_seq = NULL; 3186 it->it_seq = NULL;
2911 return NULL; 3187 return NULL;
2912 } 3188 }
2913 3189
2914 static PyObject * 3190 static PyObject *
2915 bytesarrayiter_length_hint(bytesiterobject *it) 3191 bytearrayiter_length_hint(bytesiterobject *it)
2916 { 3192 {
2917 Py_ssize_t len = 0; 3193 Py_ssize_t len = 0;
2918 if (it->it_seq) 3194 if (it->it_seq)
2919 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index; 3195 len = PyByteArray_GET_SIZE(it->it_seq) - it->it_index;
2920 return PyLong_FromSsize_t(len); 3196 return PyLong_FromSsize_t(len);
2921 } 3197 }
2922 3198
2923 PyDoc_STRVAR(length_hint_doc, 3199 PyDoc_STRVAR(length_hint_doc,
2924 "Private method returning an estimate of len(list(it))."); 3200 "Private method returning an estimate of len(list(it)).");
2925 3201
3202 static PyObject *
3203 bytearrayiter_reduce(bytesiterobject *it)
3204 {
3205 if (it->it_seq != NULL) {
3206 return Py_BuildValue("N(O)n", _PyObject_GetBuiltin("iter"),
3207 it->it_seq, it->it_index);
3208 } else {
3209 PyObject *u = PyUnicode_FromUnicode(NULL, 0);
3210 if (u == NULL)
3211 return NULL;
3212 return Py_BuildValue("N(N)", _PyObject_GetBuiltin("iter"), u);
3213 }
3214 }
3215
3216 static PyObject *
3217 bytearrayiter_setstate(bytesiterobject *it, PyObject *state)
3218 {
3219 Py_ssize_t index = PyLong_AsSsize_t(state);
3220 if (index == -1 && PyErr_Occurred())
3221 return NULL;
3222 if (it->it_seq != NULL) {
3223 if (index < 0)
3224 index = 0;
3225 else if (index > PyByteArray_GET_SIZE(it->it_seq))
3226 index = PyByteArray_GET_SIZE(it->it_seq); /* iterator exhausted */
3227 it->it_index = index;
3228 }
3229 Py_RETURN_NONE;
3230 }
3231
3232 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3233
2926 static PyMethodDef bytearrayiter_methods[] = { 3234 static PyMethodDef bytearrayiter_methods[] = {
2927 {"__length_hint__", (PyCFunction)bytesarrayiter_length_hint, METH_NOARGS, 3235 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
2928 length_hint_doc}, 3236 length_hint_doc},
3237 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
3238 bytearray_reduce__doc__},
3239 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3240 setstate_doc},
2929 {NULL, NULL} /* sentinel */ 3241 {NULL, NULL} /* sentinel */
2930 }; 3242 };
2931 3243
2932 PyTypeObject PyByteArrayIter_Type = { 3244 PyTypeObject PyByteArrayIter_Type = {
2933 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3245 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2934 "bytearray_iterator", /* tp_name */ 3246 "bytearray_iterator", /* tp_name */
2935 sizeof(bytesiterobject), /* tp_basicsize */ 3247 sizeof(bytesiterobject), /* tp_basicsize */
2936 0, /* tp_itemsize */ 3248 0, /* tp_itemsize */
2937 /* methods */ 3249 /* methods */
2938 (destructor)bytearrayiter_dealloc, /* tp_dealloc */ 3250 (destructor)bytearrayiter_dealloc, /* tp_dealloc */
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
2973 } 3285 }
2974 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); 3286 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
2975 if (it == NULL) 3287 if (it == NULL)
2976 return NULL; 3288 return NULL;
2977 it->it_index = 0; 3289 it->it_index = 0;
2978 Py_INCREF(seq); 3290 Py_INCREF(seq);
2979 it->it_seq = (PyByteArrayObject *)seq; 3291 it->it_seq = (PyByteArrayObject *)seq;
2980 _PyObject_GC_TRACK(it); 3292 _PyObject_GC_TRACK(it);
2981 return (PyObject *)it; 3293 return (PyObject *)it;
2982 } 3294 }
LEFTRIGHT

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