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

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, 6 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 } 81 }
76 82
77 static int 83 static int
78 _canresize(PyByteArrayObject *self) 84 _canresize(PyByteArrayObject *self)
79 { 85 {
80 if (self->ob_exports > 0) { 86 if (self->ob_exports > 0) {
81 PyErr_SetString(PyExc_BufferError, 87 PyErr_SetString(PyExc_BufferError,
82 "Existing exports of data: object cannot be re-sized"); 88 "Existing exports of data: object cannot be re-sized");
83 return 0; 89 return 0;
84 } 90 }
85 return 1; 91 return 1;
86 } 92 }
93
94 #include "clinic/bytearrayobject.c.h"
87 95
88 /* Direct API functions */ 96 /* Direct API functions */
89 97
90 PyObject * 98 PyObject *
91 PyByteArray_FromObject(PyObject *input) 99 PyByteArray_FromObject(PyObject *input)
92 { 100 {
93 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type, 101 return PyObject_CallFunctionObjArgs((PyObject *)&PyByteArray_Type,
94 input, NULL); 102 input, NULL);
95 } 103 }
96 104
(...skipping 165 matching lines...) Expand 10 before | Expand all | Expand 10 after
262 memcpy(result->ob_bytes, va.buf, va.len); 270 memcpy(result->ob_bytes, va.buf, va.len);
263 memcpy(result->ob_bytes + va.len, vb.buf, vb.len); 271 memcpy(result->ob_bytes + va.len, vb.buf, vb.len);
264 } 272 }
265 273
266 done: 274 done:
267 if (va.len != -1) 275 if (va.len != -1)
268 PyBuffer_Release(&va); 276 PyBuffer_Release(&va);
269 if (vb.len != -1) 277 if (vb.len != -1)
270 PyBuffer_Release(&vb); 278 PyBuffer_Release(&vb);
271 return (PyObject *)result; 279 return (PyObject *)result;
280 }
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;
272 } 305 }
273 306
274 /* Functions stuffed into the type object */ 307 /* Functions stuffed into the type object */
275 308
276 static Py_ssize_t 309 static Py_ssize_t
277 bytearray_length(PyByteArrayObject *self) 310 bytearray_length(PyByteArrayObject *self)
278 { 311 {
279 return Py_SIZE(self); 312 return Py_SIZE(self);
280 } 313 }
281 314
(...skipping 136 matching lines...) Expand 10 before | Expand all | Expand 10 after
418 451
419 result_buf = PyByteArray_AS_STRING(result); 452 result_buf = PyByteArray_AS_STRING(result);
420 for (cur = start, i = 0; i < slicelength; 453 for (cur = start, i = 0; i < slicelength;
421 cur += step, i++) { 454 cur += step, i++) {
422 result_buf[i] = source_buf[cur]; 455 result_buf[i] = source_buf[cur];
423 } 456 }
424 return result; 457 return result;
425 } 458 }
426 } 459 }
427 else { 460 else {
428 PyErr_SetString(PyExc_TypeError, "bytearray indices must be integers"); 461 PyErr_Format(PyExc_TypeError,
462 "bytearray indices must be integers or slices, not %.200s",
463 Py_TYPE(index)->tp_name);
429 return NULL; 464 return NULL;
430 } 465 }
431 } 466 }
432 467
433 static int 468 static int
434 bytearray_setslice_linear(PyByteArrayObject *self, 469 bytearray_setslice_linear(PyByteArrayObject *self,
435 Py_ssize_t lo, Py_ssize_t hi, 470 Py_ssize_t lo, Py_ssize_t hi,
436 char *bytes, Py_ssize_t bytes_len) 471 char *bytes, Py_ssize_t bytes_len)
437 { 472 {
438 Py_ssize_t avail = hi - lo; 473 Py_ssize_t avail = hi - lo;
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
623 } 658 }
624 } 659 }
625 else if (PySlice_Check(index)) { 660 else if (PySlice_Check(index)) {
626 if (PySlice_GetIndicesEx(index, 661 if (PySlice_GetIndicesEx(index,
627 PyByteArray_GET_SIZE(self), 662 PyByteArray_GET_SIZE(self),
628 &start, &stop, &step, &slicelen) < 0) { 663 &start, &stop, &step, &slicelen) < 0) {
629 return -1; 664 return -1;
630 } 665 }
631 } 666 }
632 else { 667 else {
633 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);
634 return -1; 671 return -1;
635 } 672 }
636 673
637 if (values == NULL) { 674 if (values == NULL) {
638 bytes = NULL; 675 bytes = NULL;
639 needed = 0; 676 needed = 0;
640 } 677 }
641 else if (values == (PyObject *)self || !PyByteArray_Check(values)) { 678 else if (values == (PyObject *)self || !PyByteArray_Check(values)) {
642 int err; 679 int err;
643 if (PyNumber_Check(values) || PyUnicode_Check(values)) { 680 if (PyNumber_Check(values) || PyUnicode_Check(values)) {
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after
847 break; 884 break;
848 } 885 }
849 886
850 /* Interpret it as an int (__index__) */ 887 /* Interpret it as an int (__index__) */
851 rc = _getbytevalue(item, &value); 888 rc = _getbytevalue(item, &value);
852 Py_DECREF(item); 889 Py_DECREF(item);
853 if (!rc) 890 if (!rc)
854 goto error; 891 goto error;
855 892
856 /* Append the byte */ 893 /* Append the byte */
857 if (Py_SIZE(self) < self->ob_alloc) 894 if (Py_SIZE(self) + 1 < self->ob_alloc) {
858 Py_SIZE(self)++; 895 Py_SIZE(self)++;
896 PyByteArray_AS_STRING(self)[Py_SIZE(self)] = '\0';
897 }
859 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0) 898 else if (PyByteArray_Resize((PyObject *)self, Py_SIZE(self)+1) < 0)
860 goto error; 899 goto error;
861 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value; 900 PyByteArray_AS_STRING(self)[Py_SIZE(self)-1] = value;
862 } 901 }
863 902
864 /* Clean up and return success */ 903 /* Clean up and return success */
865 Py_DECREF(it); 904 Py_DECREF(it);
866 return 0; 905 return 0;
867 906
868 error: 907 error:
(...skipping 232 matching lines...) Expand 10 before | Expand all | Expand 10 after
1101 start = 0; \ 1140 start = 0; \
1102 } 1141 }
1103 1142
1104 Py_LOCAL_INLINE(Py_ssize_t) 1143 Py_LOCAL_INLINE(Py_ssize_t)
1105 bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir) 1144 bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir)
1106 { 1145 {
1107 PyObject *subobj; 1146 PyObject *subobj;
1108 char byte; 1147 char byte;
1109 Py_buffer subbuf; 1148 Py_buffer subbuf;
1110 const char *sub; 1149 const char *sub;
1111 Py_ssize_t sub_len; 1150 Py_ssize_t len, sub_len;
1112 Py_ssize_t start=0, end=PY_SSIZE_T_MAX; 1151 Py_ssize_t start=0, end=PY_SSIZE_T_MAX;
1113 Py_ssize_t res; 1152 Py_ssize_t res;
1114 1153
1115 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex", 1154 if (!stringlib_parse_args_finds_byte("find/rfind/index/rindex",
1116 args, &subobj, &byte, &start, &end)) 1155 args, &subobj, &byte, &start, &end))
1117 return -2; 1156 return -2;
1118 1157
1119 if (subobj) { 1158 if (subobj) {
1120 if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0) 1159 if (PyObject_GetBuffer(subobj, &subbuf, PyBUF_SIMPLE) != 0)
1121 return -2; 1160 return -2;
1122 1161
1123 sub = subbuf.buf; 1162 sub = subbuf.buf;
1124 sub_len = subbuf.len; 1163 sub_len = subbuf.len;
1125 } 1164 }
1126 else { 1165 else {
1127 sub = &byte; 1166 sub = &byte;
1128 sub_len = 1; 1167 sub_len = 1;
1129 } 1168 }
1130 1169 len = PyByteArray_GET_SIZE(self);
1131 if (dir > 0) 1170
1132 res = stringlib_find_slice( 1171 ADJUST_INDICES(start, end, len);
1133 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 1172 if (end - start < sub_len)
1134 sub, sub_len, start, end); 1173 res = -1;
1135 else 1174 /* Issue #23573: FIXME, windows has no memrchr() */
1136 res = stringlib_rfind_slice( 1175 else if (sub_len == 1 && dir > 0) {
1137 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 1176 unsigned char needle = *sub;
1138 sub, sub_len, start, end); 1177 res = stringlib_fastsearch_memchr_1char(
1178 PyByteArray_AS_STRING(self) + start, end - start,
1179 needle, needle, FAST_SEARCH);
1180 if (res >= 0)
1181 res += start;
1182 }
1183 else {
1184 if (dir > 0)
1185 res = stringlib_find_slice(
1186 PyByteArray_AS_STRING(self), len,
1187 sub, sub_len, start, end);
1188 else
1189 res = stringlib_rfind_slice(
1190 PyByteArray_AS_STRING(self), len,
1191 sub, sub_len, start, end);
1192 }
1139 1193
1140 if (subobj) 1194 if (subobj)
1141 PyBuffer_Release(&subbuf); 1195 PyBuffer_Release(&subbuf);
1142 1196
1143 return res; 1197 return res;
1144 } 1198 }
1145 1199
1146 PyDoc_STRVAR(find__doc__, 1200 PyDoc_STRVAR(find__doc__,
1147 "B.find(sub[, start[, end]]) -> int\n\ 1201 "B.find(sub[, start[, end]]) -> int\n\
1148 \n\ 1202 \n\
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after
1201 count_obj = PyLong_FromSsize_t( 1255 count_obj = PyLong_FromSsize_t(
1202 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX) 1256 stringlib_count(str + start, end - start, sub, sub_len, PY_SSIZE_T_MAX)
1203 ); 1257 );
1204 1258
1205 if (sub_obj) 1259 if (sub_obj)
1206 PyBuffer_Release(&vsub); 1260 PyBuffer_Release(&vsub);
1207 1261
1208 return count_obj; 1262 return count_obj;
1209 } 1263 }
1210 1264
1211 PyDoc_STRVAR(clear__doc__, 1265 /*[clinic input]
1212 "B.clear() -> None\n\ 1266 bytearray.clear
1213 \n\ 1267
1214 Remove all items from B."); 1268 self: self(type="PyByteArrayObject *")
1215 1269
1216 static PyObject * 1270 Remove all items from the bytearray.
1217 bytearray_clear(PyByteArrayObject *self) 1271 [clinic start generated code]*/
1272
1273 static PyObject *
1274 bytearray_clear_impl(PyByteArrayObject *self)
1275 /*[clinic end generated code: output=85c2fe6aede0956c input=e524fd330abcdc18]*/
1218 { 1276 {
1219 if (PyByteArray_Resize((PyObject *)self, 0) < 0) 1277 if (PyByteArray_Resize((PyObject *)self, 0) < 0)
1220 return NULL; 1278 return NULL;
1221 Py_RETURN_NONE; 1279 Py_RETURN_NONE;
1222 } 1280 }
1223 1281
1224 PyDoc_STRVAR(copy__doc__, 1282 /*[clinic input]
1225 "B.copy() -> bytearray\n\ 1283 bytearray.copy
1226 \n\ 1284
1227 Return a copy of B."); 1285 self: self(type="PyByteArrayObject *")
1228 1286
1229 static PyObject * 1287 Return a copy of B.
1230 bytearray_copy(PyByteArrayObject *self) 1288 [clinic start generated code]*/
1289
1290 static PyObject *
1291 bytearray_copy_impl(PyByteArrayObject *self)
1292 /*[clinic end generated code: output=68cfbcfed484c132 input=6d5d2975aa0f33f3]*/
1231 { 1293 {
1232 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self) , 1294 return PyByteArray_FromStringAndSize(PyByteArray_AS_STRING((PyObject *)self) ,
1233 PyByteArray_GET_SIZE(self)); 1295 PyByteArray_GET_SIZE(self));
1234 } 1296 }
1235 1297
1236 PyDoc_STRVAR(index__doc__, 1298 PyDoc_STRVAR(index__doc__,
1237 "B.index(sub[, start[, end]]) -> int\n\ 1299 "B.index(sub[, start[, end]]) -> int\n\
1238 \n\ 1300 \n\
1239 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.");
1240 1302
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after
1439 if (PyErr_ExceptionMatches(PyExc_TypeError)) 1501 if (PyErr_ExceptionMatches(PyExc_TypeError))
1440 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or " 1502 PyErr_Format(PyExc_TypeError, "endswith first arg must be bytes or "
1441 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name); 1503 "a tuple of bytes, not %s", Py_TYPE(subobj)->tp_name);
1442 return NULL; 1504 return NULL;
1443 } 1505 }
1444 else 1506 else
1445 return PyBool_FromLong(result); 1507 return PyBool_FromLong(result);
1446 } 1508 }
1447 1509
1448 1510
1449 PyDoc_STRVAR(translate__doc__, 1511 /*[clinic input]
1450 "B.translate(table[, deletechars]) -> bytearray\n\ 1512 bytearray.translate
1451 \n\ 1513
1452 Return a copy of B, where all characters occurring in the\n\ 1514 self: self(type="PyByteArrayObject *")
1453 optional argument deletechars are removed, and the remaining\n\ 1515 table: object
1454 characters have been mapped through the given translation\n\ 1516 Translation table, which must be a bytes object of length 256.
1455 table, which must be a bytes object of length 256."); 1517 [
1456 1518 deletechars: object
1457 static PyObject * 1519 ]
1458 bytearray_translate(PyByteArrayObject *self, PyObject *args) 1520 /
1521
1522 Return a copy with each character mapped by the given translation table.
1523
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]*/
1459 { 1532 {
1460 char *input, *output; 1533 char *input, *output;
1461 const char *table; 1534 const char *table_chars;
1462 Py_ssize_t i, c; 1535 Py_ssize_t i, c;
1463 PyObject *input_obj = (PyObject*)self; 1536 PyObject *input_obj = (PyObject*)self;
1464 const char *output_start; 1537 const char *output_start;
1465 Py_ssize_t inlen; 1538 Py_ssize_t inlen;
1466 PyObject *result = NULL; 1539 PyObject *result = NULL;
1467 int trans_table[256]; 1540 int trans_table[256];
1468 PyObject *tableobj = NULL, *delobj = NULL;
1469 Py_buffer vtable, vdel; 1541 Py_buffer vtable, vdel;
1470 1542
1471 if (!PyArg_UnpackTuple(args, "translate", 1, 2, 1543 if (table == Py_None) {
1472 &tableobj, &delobj)) 1544 table_chars = NULL;
1473 return NULL;
1474
1475 if (tableobj == Py_None) {
1476 table = NULL; 1545 table = NULL;
1477 tableobj = NULL; 1546 } else if (PyObject_GetBuffer(table, &vtable, PyBUF_SIMPLE) != 0) {
1478 } else if (PyObject_GetBuffer(tableobj, &vtable, PyBUF_SIMPLE) != 0) {
1479 return NULL; 1547 return NULL;
1480 } else { 1548 } else {
1481 if (vtable.len != 256) { 1549 if (vtable.len != 256) {
1482 PyErr_SetString(PyExc_ValueError, 1550 PyErr_SetString(PyExc_ValueError,
1483 "translation table must be 256 characters long"); 1551 "translation table must be 256 characters long");
1484 PyBuffer_Release(&vtable); 1552 PyBuffer_Release(&vtable);
1485 return NULL; 1553 return NULL;
1486 } 1554 }
1487 table = (const char*)vtable.buf; 1555 table_chars = (const char*)vtable.buf;
1488 } 1556 }
1489 1557
1490 if (delobj != NULL) { 1558 if (deletechars != NULL) {
1491 if (PyObject_GetBuffer(delobj, &vdel, PyBUF_SIMPLE) != 0) { 1559 if (PyObject_GetBuffer(deletechars, &vdel, PyBUF_SIMPLE) != 0) {
1492 if (tableobj != NULL) 1560 if (table != NULL)
1493 PyBuffer_Release(&vtable); 1561 PyBuffer_Release(&vtable);
1494 return NULL; 1562 return NULL;
1495 } 1563 }
1496 } 1564 }
1497 else { 1565 else {
1498 vdel.buf = NULL; 1566 vdel.buf = NULL;
1499 vdel.len = 0; 1567 vdel.len = 0;
1500 } 1568 }
1501 1569
1502 inlen = PyByteArray_GET_SIZE(input_obj); 1570 inlen = PyByteArray_GET_SIZE(input_obj);
1503 result = PyByteArray_FromStringAndSize((char *)NULL, inlen); 1571 result = PyByteArray_FromStringAndSize((char *)NULL, inlen);
1504 if (result == NULL) 1572 if (result == NULL)
1505 goto done; 1573 goto done;
1506 output_start = output = PyByteArray_AsString(result); 1574 output_start = output = PyByteArray_AsString(result);
1507 input = PyByteArray_AS_STRING(input_obj); 1575 input = PyByteArray_AS_STRING(input_obj);
1508 1576
1509 if (vdel.len == 0 && table != NULL) { 1577 if (vdel.len == 0 && table_chars != NULL) {
1510 /* If no deletions are required, use faster code */ 1578 /* If no deletions are required, use faster code */
1511 for (i = inlen; --i >= 0; ) { 1579 for (i = inlen; --i >= 0; ) {
1512 c = Py_CHARMASK(*input++); 1580 c = Py_CHARMASK(*input++);
1513 *output++ = table[c]; 1581 *output++ = table_chars[c];
1514 } 1582 }
1515 goto done; 1583 goto done;
1516 } 1584 }
1517 1585
1518 if (table == NULL) { 1586 if (table_chars == NULL) {
1519 for (i = 0; i < 256; i++) 1587 for (i = 0; i < 256; i++)
1520 trans_table[i] = Py_CHARMASK(i); 1588 trans_table[i] = Py_CHARMASK(i);
1521 } else { 1589 } else {
1522 for (i = 0; i < 256; i++) 1590 for (i = 0; i < 256; i++)
1523 trans_table[i] = Py_CHARMASK(table[i]); 1591 trans_table[i] = Py_CHARMASK(table_chars[i]);
1524 } 1592 }
1525 1593
1526 for (i = 0; i < vdel.len; i++) 1594 for (i = 0; i < vdel.len; i++)
1527 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1; 1595 trans_table[(int) Py_CHARMASK( ((unsigned char*)vdel.buf)[i] )] = -1;
1528 1596
1529 for (i = inlen; --i >= 0; ) { 1597 for (i = inlen; --i >= 0; ) {
1530 c = Py_CHARMASK(*input++); 1598 c = Py_CHARMASK(*input++);
1531 if (trans_table[c] != -1) 1599 if (trans_table[c] != -1)
1532 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c) 1600 if (Py_CHARMASK(*output++ = (char)trans_table[c]) == c)
1533 continue; 1601 continue;
1534 } 1602 }
1535 /* Fix the size of the resulting string */ 1603 /* Fix the size of the resulting string */
1536 if (inlen > 0) 1604 if (inlen > 0)
1537 if (PyByteArray_Resize(result, output - output_start) < 0) { 1605 if (PyByteArray_Resize(result, output - output_start) < 0) {
1538 Py_CLEAR(result); 1606 Py_CLEAR(result);
1539 goto done; 1607 goto done;
1540 } 1608 }
1541 1609
1542 done: 1610 done:
1543 if (tableobj != NULL) 1611 if (table != NULL)
1544 PyBuffer_Release(&vtable); 1612 PyBuffer_Release(&vtable);
1545 if (delobj != NULL) 1613 if (deletechars != NULL)
1546 PyBuffer_Release(&vdel); 1614 PyBuffer_Release(&vdel);
1547 return result; 1615 return result;
1548 } 1616 }
1549 1617
1550 1618
1551 static PyObject * 1619 /*[clinic input]
1552 bytearray_maketrans(PyObject *null, PyObject *args) 1620
1553 { 1621 @staticmethod
1554 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);
1555 } 1641 }
1556 1642
1557 1643
1558 /* find and count characters and substrings */ 1644 /* find and count characters and substrings */
1559 1645
1560 #define findchar(target, target_len, c) \ 1646 #define findchar(target, target_len, c) \
1561 ((char *)memchr((const void *)(target), c, target_len)) 1647 ((char *)memchr((const void *)(target), c, target_len))
1562 1648
1563 1649
1564 /* 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
2035 if (from_len == 1) { 2121 if (from_len == 1) {
2036 return replace_single_character(self, from_s[0], 2122 return replace_single_character(self, from_s[0],
2037 to_s, to_len, maxcount); 2123 to_s, to_len, maxcount);
2038 } else { 2124 } else {
2039 /* len('from')>=2, len('to')>=1 */ 2125 /* len('from')>=2, len('to')>=1 */
2040 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) ;
2041 } 2127 }
2042 } 2128 }
2043 2129
2044 2130
2045 PyDoc_STRVAR(replace__doc__, 2131 /*[clinic input]
2046 "B.replace(old, new[, count]) -> bytearray\n\ 2132 bytearray.replace
2047 \n\ 2133
2048 Return a copy of B with all occurrences of subsection\n\ 2134 old: Py_buffer
2049 old replaced by new. If the optional argument count is\n\ 2135 new: Py_buffer
2050 given, only the first count occurrences are replaced."); 2136 count: Py_ssize_t = -1
2051 2137 Maximum number of occurrences to replace.
2052 static PyObject * 2138 -1 (the default value) means replace all occurrences.
2053 bytearray_replace(PyByteArrayObject *self, PyObject *args) 2139 /
2054 { 2140
2055 PyObject *res; 2141 Return a copy with all occurrences of substring old replaced by new.
2056 Py_buffer old = {NULL, NULL}; 2142
2057 Py_buffer new = {NULL, NULL}; 2143 If the optional argument count is given, only the first count occurrences are
2058 Py_ssize_t count = -1; 2144 replaced.
2059 2145 [clinic start generated code]*/
2060 if (!PyArg_ParseTuple(args, "y*y*|n:replace", &old, &new, &count)) 2146
2061 return NULL; 2147 static PyObject *
2062 2148 bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old,
2063 res = (PyObject *)replace((PyByteArrayObject *) self, 2149 Py_buffer *new, Py_ssize_t count)
2064 (const char *)old.buf, old.len, 2150 /*[clinic end generated code: output=d39884c4dc59412a input=aa379d988637c7fb]*/
2065 (const char *)new.buf, new.len, count); 2151 {
2066 2152 return (PyObject *)replace((PyByteArrayObject *) self,
2067 PyBuffer_Release(&old); 2153 old->buf, old->len,
2068 PyBuffer_Release(&new); 2154 new->buf, new->len, count);
2069 return res; 2155 }
2070 } 2156
2071 2157 /*[clinic input]
2072 PyDoc_STRVAR(split__doc__, 2158 bytearray.split
2073 "B.split(sep=None, maxsplit=-1) -> list of bytearrays\n\ 2159
2074 \n\ 2160 sep: object = None
2075 Return a list of the sections in B, using sep as the delimiter.\n\ 2161 The delimiter according which to split the bytearray.
2076 If sep is not given, B is split on ASCII whitespace characters\n\ 2162 None (the default value) means split on ASCII whitespace characters
2077 (space, tab, return, newline, formfeed, vertical tab).\n\ 2163 (space, tab, return, newline, formfeed, vertical tab).
2078 If maxsplit is given, at most maxsplit splits are done."); 2164 maxsplit: Py_ssize_t = -1
2079 2165 Maximum number of splits to do.
2080 static PyObject * 2166 -1 (the default value) means no limit.
2081 bytearray_split(PyByteArrayObject *self, PyObject *args, PyObject *kwds) 2167
2082 { 2168 Return a list of the sections in the bytearray, using sep as the delimiter.
2083 static char *kwlist[] = {"sep", "maxsplit", 0}; 2169 [clinic start generated code]*/
2170
2171 static PyObject *
2172 bytearray_split_impl(PyByteArrayObject *self, PyObject *sep,
2173 Py_ssize_t maxsplit)
2174 /*[clinic end generated code: output=833e2cf385d9a04d input=24f82669f41bf523]*/
2175 {
2084 Py_ssize_t len = PyByteArray_GET_SIZE(self), n; 2176 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2085 Py_ssize_t maxsplit = -1;
2086 const char *s = PyByteArray_AS_STRING(self), *sub; 2177 const char *s = PyByteArray_AS_STRING(self), *sub;
2087 PyObject *list, *subobj = Py_None; 2178 PyObject *list;
2088 Py_buffer vsub; 2179 Py_buffer vsub;
2089 2180
2090 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:split",
2091 kwlist, &subobj, &maxsplit))
2092 return NULL;
2093 if (maxsplit < 0) 2181 if (maxsplit < 0)
2094 maxsplit = PY_SSIZE_T_MAX; 2182 maxsplit = PY_SSIZE_T_MAX;
2095 2183
2096 if (subobj == Py_None) 2184 if (sep == Py_None)
2097 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); 2185 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit);
2098 2186
2099 if (PyObject_GetBuffer(subobj, &vsub, PyBUF_SIMPLE) != 0) 2187 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
2100 return NULL; 2188 return NULL;
2101 sub = vsub.buf; 2189 sub = vsub.buf;
2102 n = vsub.len; 2190 n = vsub.len;
2103 2191
2104 list = stringlib_split( 2192 list = stringlib_split(
2105 (PyObject*) self, s, len, sub, n, maxsplit 2193 (PyObject*) self, s, len, sub, n, maxsplit
2106 ); 2194 );
2107 PyBuffer_Release(&vsub); 2195 PyBuffer_Release(&vsub);
2108 return list; 2196 return list;
2109 } 2197 }
2110 2198
2111 PyDoc_STRVAR(partition__doc__, 2199 /*[clinic input]
2112 "B.partition(sep) -> (head, sep, tail)\n\ 2200 bytearray.partition
2113 \n\ 2201
2114 Search for the separator sep in B, and return the part before it,\n\ 2202 self: self(type="PyByteArrayObject *")
2115 the separator itself, and the part after it. If the separator is not\n\ 2203 sep: object
2116 found, returns B and two empty bytearray objects."); 2204 /
2117 2205
2118 static PyObject * 2206 Partition the bytearray into three parts using the given separator.
2119 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]*/
2120 { 2219 {
2121 PyObject *bytesep, *result; 2220 PyObject *bytesep, *result;
2122 2221
2123 bytesep = PyByteArray_FromObject(sep_obj); 2222 bytesep = PyByteArray_FromObject(sep);
2124 if (! bytesep) 2223 if (! bytesep)
2125 return NULL; 2224 return NULL;
2126 2225
2127 result = stringlib_partition( 2226 result = stringlib_partition(
2128 (PyObject*) self, 2227 (PyObject*) self,
2129 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 2228 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2130 bytesep, 2229 bytesep,
2131 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) 2230 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2132 ); 2231 );
2133 2232
2134 Py_DECREF(bytesep); 2233 Py_DECREF(bytesep);
2135 return result; 2234 return result;
2136 } 2235 }
2137 2236
2138 PyDoc_STRVAR(rpartition__doc__, 2237 /*[clinic input]
2139 "B.rpartition(sep) -> (head, sep, tail)\n\ 2238 bytearray.rpartition
2140 \n\ 2239
2141 Search for the separator sep in B, starting at the end of B,\n\ 2240 self: self(type="PyByteArrayObject *")
2142 and return the part before it, the separator itself, and the\n\ 2241 sep: object
2143 part after it. If the separator is not found, returns two empty\n\ 2242 /
2144 bytearray objects and B."); 2243
2145 2244 Partition the bytes into three parts using the given separator.
2146 static PyObject * 2245
2147 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]*/
2148 { 2257 {
2149 PyObject *bytesep, *result; 2258 PyObject *bytesep, *result;
2150 2259
2151 bytesep = PyByteArray_FromObject(sep_obj); 2260 bytesep = PyByteArray_FromObject(sep);
2152 if (! bytesep) 2261 if (! bytesep)
2153 return NULL; 2262 return NULL;
2154 2263
2155 result = stringlib_rpartition( 2264 result = stringlib_rpartition(
2156 (PyObject*) self, 2265 (PyObject*) self,
2157 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self), 2266 PyByteArray_AS_STRING(self), PyByteArray_GET_SIZE(self),
2158 bytesep, 2267 bytesep,
2159 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep) 2268 PyByteArray_AS_STRING(bytesep), PyByteArray_GET_SIZE(bytesep)
2160 ); 2269 );
2161 2270
2162 Py_DECREF(bytesep); 2271 Py_DECREF(bytesep);
2163 return result; 2272 return result;
2164 } 2273 }
2165 2274
2166 PyDoc_STRVAR(rsplit__doc__, 2275 /*[clinic input]
2167 "B.rsplit(sep=None, maxsplit=-1) -> list of bytearrays\n\ 2276 bytearray.rsplit = bytearray.split
2168 \n\ 2277
2169 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.
2170 starting at the end of B and working to the front.\n\ 2279
2171 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.
2172 (space, tab, return, newline, formfeed, vertical tab).\n\ 2281 [clinic start generated code]*/
2173 If maxsplit is given, at most maxsplit splits are done."); 2282
2174 2283 static PyObject *
2175 static PyObject * 2284 bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep,
2176 bytearray_rsplit(PyByteArrayObject *self, PyObject *args, PyObject *kwds) 2285 Py_ssize_t maxsplit)
2177 { 2286 /*[clinic end generated code: output=a55e0b5a03cb6190 input=a68286e4dd692ffe]*/
2178 static char *kwlist[] = {"sep", "maxsplit", 0}; 2287 {
2179 Py_ssize_t len = PyByteArray_GET_SIZE(self), n; 2288 Py_ssize_t len = PyByteArray_GET_SIZE(self), n;
2180 Py_ssize_t maxsplit = -1;
2181 const char *s = PyByteArray_AS_STRING(self), *sub; 2289 const char *s = PyByteArray_AS_STRING(self), *sub;
2182 PyObject *list, *subobj = Py_None; 2290 PyObject *list;
2183 Py_buffer vsub; 2291 Py_buffer vsub;
2184 2292
2185 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|On:rsplit",
2186 kwlist, &subobj, &maxsplit))
2187 return NULL;
2188 if (maxsplit < 0) 2293 if (maxsplit < 0)
2189 maxsplit = PY_SSIZE_T_MAX; 2294 maxsplit = PY_SSIZE_T_MAX;
2190 2295
2191 if (subobj == Py_None) 2296 if (sep == Py_None)
2192 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); 2297 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit);
2193 2298
2194 if (PyObject_GetBuffer(subobj, &vsub, PyBUF_SIMPLE) != 0) 2299 if (PyObject_GetBuffer(sep, &vsub, PyBUF_SIMPLE) != 0)
2195 return NULL; 2300 return NULL;
2196 sub = vsub.buf; 2301 sub = vsub.buf;
2197 n = vsub.len; 2302 n = vsub.len;
2198 2303
2199 list = stringlib_rsplit( 2304 list = stringlib_rsplit(
2200 (PyObject*) self, s, len, sub, n, maxsplit 2305 (PyObject*) self, s, len, sub, n, maxsplit
2201 ); 2306 );
2202 PyBuffer_Release(&vsub); 2307 PyBuffer_Release(&vsub);
2203 return list; 2308 return list;
2204 } 2309 }
2205 2310
2206 PyDoc_STRVAR(reverse__doc__, 2311 /*[clinic input]
2207 "B.reverse() -> None\n\ 2312 bytearray.reverse
2208 \n\ 2313
2209 Reverse the order of the values in B in place."); 2314 self: self(type="PyByteArrayObject *")
2210 static PyObject * 2315
2211 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]*/
2212 { 2322 {
2213 char swap, *head, *tail; 2323 char swap, *head, *tail;
2214 Py_ssize_t i, j, n = Py_SIZE(self); 2324 Py_ssize_t i, j, n = Py_SIZE(self);
2215 2325
2216 j = n / 2; 2326 j = n / 2;
2217 head = PyByteArray_AS_STRING(self); 2327 head = PyByteArray_AS_STRING(self);
2218 tail = head + n - 1; 2328 tail = head + n - 1;
2219 for (i = 0; i < j; i++) { 2329 for (i = 0; i < j; i++) {
2220 swap = *head; 2330 swap = *head;
2221 *head++ = *tail; 2331 *head++ = *tail;
2222 *tail-- = swap; 2332 *tail-- = swap;
2223 } 2333 }
2224 2334
2225 Py_RETURN_NONE; 2335 Py_RETURN_NONE;
2226 } 2336 }
2227 2337
2228 PyDoc_STRVAR(insert__doc__, 2338
2229 "B.insert(index, int) -> None\n\ 2339 /*[python input]
2230 \n\ 2340 class bytesvalue_converter(CConverter):
2231 Insert a single item into the bytearray before the given index."); 2341 type = 'int'
2232 static PyObject * 2342 converter = '_getbytevalue'
2233 bytearray_insert(PyByteArrayObject *self, PyObject *args) 2343 [python start generated code]*/
2234 { 2344 /*[python end generated code: output=da39a3ee5e6b4b0d input=29c2e7c26c212812]*/
2235 PyObject *value; 2345
2236 int ival; 2346
2237 Py_ssize_t where, n = Py_SIZE(self); 2347 /*[clinic input]
2348 bytearray.insert
2349
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);
2238 char *buf; 2365 char *buf;
2239
2240 if (!PyArg_ParseTuple(args, "nO:insert", &where, &value))
2241 return NULL;
2242 2366
2243 if (n == PY_SSIZE_T_MAX) { 2367 if (n == PY_SSIZE_T_MAX) {
2244 PyErr_SetString(PyExc_OverflowError, 2368 PyErr_SetString(PyExc_OverflowError,
2245 "cannot add more objects to bytearray"); 2369 "cannot add more objects to bytearray");
2246 return NULL; 2370 return NULL;
2247 } 2371 }
2248 if (!_getbytevalue(value, &ival))
2249 return NULL;
2250 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) 2372 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2251 return NULL; 2373 return NULL;
2252 buf = PyByteArray_AS_STRING(self); 2374 buf = PyByteArray_AS_STRING(self);
2253 2375
2254 if (where < 0) { 2376 if (index < 0) {
2255 where += n; 2377 index += n;
2256 if (where < 0) 2378 if (index < 0)
2257 where = 0; 2379 index = 0;
2258 } 2380 }
2259 if (where > n) 2381 if (index > n)
2260 where = n; 2382 index = n;
2261 memmove(buf + where + 1, buf + where, n - where); 2383 memmove(buf + index + 1, buf + index, n - index);
2262 buf[where] = ival; 2384 buf[index] = item;
2263 2385
2264 Py_RETURN_NONE; 2386 Py_RETURN_NONE;
2265 } 2387 }
2266 2388
2267 PyDoc_STRVAR(append__doc__, 2389 /*[clinic input]
2268 "B.append(int) -> None\n\ 2390 bytearray.append
2269 \n\ 2391
2270 Append a single item to the end of B."); 2392 self: self(type="PyByteArrayObject *")
2271 static PyObject * 2393 item: bytesvalue
2272 bytearray_append(PyByteArrayObject *self, PyObject *arg) 2394 The item to be appended.
2273 { 2395 /
2274 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 {
2275 Py_ssize_t n = Py_SIZE(self); 2404 Py_ssize_t n = Py_SIZE(self);
2276 2405
2277 if (! _getbytevalue(arg, &value))
2278 return NULL;
2279 if (n == PY_SSIZE_T_MAX) { 2406 if (n == PY_SSIZE_T_MAX) {
2280 PyErr_SetString(PyExc_OverflowError, 2407 PyErr_SetString(PyExc_OverflowError,
2281 "cannot add more objects to bytearray"); 2408 "cannot add more objects to bytearray");
2282 return NULL; 2409 return NULL;
2283 } 2410 }
2284 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0) 2411 if (PyByteArray_Resize((PyObject *)self, n + 1) < 0)
2285 return NULL; 2412 return NULL;
2286 2413
2287 PyByteArray_AS_STRING(self)[n] = value; 2414 PyByteArray_AS_STRING(self)[n] = item;
2288 2415
2289 Py_RETURN_NONE; 2416 Py_RETURN_NONE;
2290 } 2417 }
2291 2418
2292 PyDoc_STRVAR(extend__doc__, 2419 /*[clinic input]
2293 "B.extend(iterable_of_ints) -> None\n\ 2420 bytearray.extend
2294 \n\ 2421
2295 Append all the elements from the iterator or sequence to the\n\ 2422 self: self(type="PyByteArrayObject *")
2296 end of B."); 2423 iterable_of_ints: object
2297 static PyObject * 2424 The iterable of items to append.
2298 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]*/
2299 { 2433 {
2300 PyObject *it, *item, *bytearray_obj; 2434 PyObject *it, *item, *bytearray_obj;
2301 Py_ssize_t buf_size = 0, len = 0; 2435 Py_ssize_t buf_size = 0, len = 0;
2302 int value; 2436 int value;
2303 char *buf; 2437 char *buf;
2304 2438
2305 /* bytearray_setslice code only accepts something supporting PEP 3118. */ 2439 /* bytearray_setslice code only accepts something supporting PEP 3118. */
2306 if (PyObject_CheckBuffer(arg)) { 2440 if (PyObject_CheckBuffer(iterable_of_ints)) {
2307 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)
2308 return NULL; 2442 return NULL;
2309 2443
2310 Py_RETURN_NONE; 2444 Py_RETURN_NONE;
2311 } 2445 }
2312 2446
2313 it = PyObject_GetIter(arg); 2447 it = PyObject_GetIter(iterable_of_ints);
2314 if (it == NULL) 2448 if (it == NULL)
2315 return NULL; 2449 return NULL;
2316 2450
2317 /* Try to determine the length of the argument. 32 is arbitrary. */ 2451 /* Try to determine the length of the argument. 32 is arbitrary. */
2318 buf_size = PyObject_LengthHint(arg, 32); 2452 buf_size = PyObject_LengthHint(iterable_of_ints, 32);
2319 if (buf_size == -1) { 2453 if (buf_size == -1) {
2320 Py_DECREF(it); 2454 Py_DECREF(it);
2321 return NULL; 2455 return NULL;
2322 } 2456 }
2323 2457
2324 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); 2458 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size);
2325 if (bytearray_obj == NULL) { 2459 if (bytearray_obj == NULL) {
2326 Py_DECREF(it); 2460 Py_DECREF(it);
2327 return NULL; 2461 return NULL;
2328 } 2462 }
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after
2360 2494
2361 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) {
2362 Py_DECREF(bytearray_obj); 2496 Py_DECREF(bytearray_obj);
2363 return NULL; 2497 return NULL;
2364 } 2498 }
2365 Py_DECREF(bytearray_obj); 2499 Py_DECREF(bytearray_obj);
2366 2500
2367 Py_RETURN_NONE; 2501 Py_RETURN_NONE;
2368 } 2502 }
2369 2503
2370 PyDoc_STRVAR(pop__doc__, 2504 /*[clinic input]
2371 "B.pop([index]) -> int\n\ 2505 bytearray.pop
2372 \n\ 2506
2373 Remove and return a single item from B. If no index\n\ 2507 self: self(type="PyByteArrayObject *")
2374 argument is given, will pop the last value."); 2508 index: Py_ssize_t = -1
2375 static PyObject * 2509 The index from where to remove the item.
2376 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]*/
2377 { 2521 {
2378 int value; 2522 int value;
2379 Py_ssize_t where = -1, n = Py_SIZE(self); 2523 Py_ssize_t n = Py_SIZE(self);
2380 char *buf; 2524 char *buf;
2381
2382 if (!PyArg_ParseTuple(args, "|n:pop", &where))
2383 return NULL;
2384 2525
2385 if (n == 0) { 2526 if (n == 0) {
2386 PyErr_SetString(PyExc_IndexError, 2527 PyErr_SetString(PyExc_IndexError,
2387 "pop from empty bytearray"); 2528 "pop from empty bytearray");
2388 return NULL; 2529 return NULL;
2389 } 2530 }
2390 if (where < 0) 2531 if (index < 0)
2391 where += n; 2532 index += Py_SIZE(self);
2392 if (where < 0 || where >= n) { 2533 if (index < 0 || index >= Py_SIZE(self)) {
2393 PyErr_SetString(PyExc_IndexError, "pop index out of range"); 2534 PyErr_SetString(PyExc_IndexError, "pop index out of range");
2394 return NULL; 2535 return NULL;
2395 } 2536 }
2396 if (!_canresize(self)) 2537 if (!_canresize(self))
2397 return NULL; 2538 return NULL;
2398 2539
2399 buf = PyByteArray_AS_STRING(self); 2540 buf = PyByteArray_AS_STRING(self);
2400 value = buf[where]; 2541 value = buf[index];
2401 memmove(buf + where, buf + where + 1, n - where - 1); 2542 memmove(buf + index, buf + index + 1, n - index);
2402 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2543 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2403 return NULL; 2544 return NULL;
2404 2545
2405 return PyLong_FromLong((unsigned char)value); 2546 return PyLong_FromLong((unsigned char)value);
2406 } 2547 }
2407 2548
2408 PyDoc_STRVAR(remove__doc__, 2549 /*[clinic input]
2409 "B.remove(int) -> None\n\ 2550 bytearray.remove
2410 \n\ 2551
2411 Remove the first occurrence of a value in B."); 2552 self: self(type="PyByteArrayObject *")
2412 static PyObject * 2553 value: bytesvalue
2413 bytearray_remove(PyByteArrayObject *self, PyObject *arg) 2554 The value to remove.
2414 { 2555 /
2415 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 {
2416 Py_ssize_t where, n = Py_SIZE(self); 2564 Py_ssize_t where, n = Py_SIZE(self);
2417 char *buf = PyByteArray_AS_STRING(self); 2565 char *buf = PyByteArray_AS_STRING(self);
2418
2419 if (! _getbytevalue(arg, &value))
2420 return NULL;
2421 2566
2422 for (where = 0; where < n; where++) { 2567 for (where = 0; where < n; where++) {
2423 if (buf[where] == value) 2568 if (buf[where] == value)
2424 break; 2569 break;
2425 } 2570 }
2426 if (where == n) { 2571 if (where == n) {
2427 PyErr_SetString(PyExc_ValueError, "value not found in bytearray"); 2572 PyErr_SetString(PyExc_ValueError, "value not found in bytearray");
2428 return NULL; 2573 return NULL;
2429 } 2574 }
2430 if (!_canresize(self)) 2575 if (!_canresize(self))
2431 return NULL; 2576 return NULL;
2432 2577
2433 memmove(buf + where, buf + where + 1, n - where - 1); 2578 memmove(buf + where, buf + where + 1, n - where);
2434 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0) 2579 if (PyByteArray_Resize((PyObject *)self, n - 1) < 0)
2435 return NULL; 2580 return NULL;
2436 2581
2437 Py_RETURN_NONE; 2582 Py_RETURN_NONE;
2438 } 2583 }
2439 2584
2440 /* XXX These two helpers could be optimized if argsize == 1 */ 2585 /* XXX These two helpers could be optimized if argsize == 1 */
2441 2586
2442 static Py_ssize_t 2587 static Py_ssize_t
2443 lstrip_helper(char *myptr, Py_ssize_t mysize, 2588 lstrip_helper(char *myptr, Py_ssize_t mysize,
2444 void *argptr, Py_ssize_t argsize) 2589 void *argptr, Py_ssize_t argsize)
2445 { 2590 {
2446 Py_ssize_t i = 0; 2591 Py_ssize_t i = 0;
2447 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize)) 2592 while (i < mysize && memchr(argptr, (unsigned char) myptr[i], argsize))
2448 i++; 2593 i++;
2449 return i; 2594 return i;
2450 } 2595 }
2451 2596
2452 static Py_ssize_t 2597 static Py_ssize_t
2453 rstrip_helper(char *myptr, Py_ssize_t mysize, 2598 rstrip_helper(char *myptr, Py_ssize_t mysize,
2454 void *argptr, Py_ssize_t argsize) 2599 void *argptr, Py_ssize_t argsize)
2455 { 2600 {
2456 Py_ssize_t i = mysize - 1; 2601 Py_ssize_t i = mysize - 1;
2457 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize)) 2602 while (i >= 0 && memchr(argptr, (unsigned char) myptr[i], argsize))
2458 i--; 2603 i--;
2459 return i + 1; 2604 return i + 1;
2460 } 2605 }
2461 2606
2462 PyDoc_STRVAR(strip__doc__, 2607 /*[clinic input]
2463 "B.strip([bytes]) -> bytearray\n\ 2608 bytearray.strip
2464 \n\ 2609
2465 Strip leading and trailing bytes contained in the argument\n\ 2610 bytes: object = None
2466 and return the result as a new bytearray.\n\ 2611 /
2467 If the argument is omitted, strip ASCII whitespace."); 2612
2468 static PyObject * 2613 Strip leading and trailing bytes contained in the argument.
2469 bytearray_strip(PyByteArrayObject *self, PyObject *args) 2614
2470 { 2615 If the argument is omitted or None, strip leading and trailing ASCII whitespace.
2471 Py_ssize_t left, right, mysize, argsize; 2616 [clinic start generated code]*/
2472 char *myptr, *argptr; 2617
2473 PyObject *arg = Py_None; 2618 static PyObject *
2474 Py_buffer varg; 2619 bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes)
2475 if (!PyArg_ParseTuple(args, "|O:strip", &arg)) 2620 /*[clinic end generated code: output=760412661a34ad5a input=ef7bb59b09c21d62]*/
2476 return NULL; 2621 {
2477 if (arg == Py_None) { 2622 Py_ssize_t left, right, mysize, byteslen;
2478 argptr = "\t\n\r\f\v "; 2623 char *myptr, *bytesptr;
2479 argsize = 6; 2624 Py_buffer vbytes;
2625
2626 if (bytes == Py_None) {
2627 bytesptr = "\t\n\r\f\v ";
2628 byteslen = 6;
2480 } 2629 }
2481 else { 2630 else {
2482 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0) 2631 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2483 return NULL; 2632 return NULL;
2484 argptr = (char *) varg.buf; 2633 bytesptr = (char *) vbytes.buf;
2485 argsize = varg.len; 2634 byteslen = vbytes.len;
2486 } 2635 }
2487 myptr = PyByteArray_AS_STRING(self); 2636 myptr = PyByteArray_AS_STRING(self);
2488 mysize = Py_SIZE(self); 2637 mysize = Py_SIZE(self);
2489 left = lstrip_helper(myptr, mysize, argptr, argsize); 2638 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
2490 if (left == mysize) 2639 if (left == mysize)
2491 right = left; 2640 right = left;
2492 else 2641 else
2493 right = rstrip_helper(myptr, mysize, argptr, argsize); 2642 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2494 if (arg != Py_None) 2643 if (bytes != Py_None)
2495 PyBuffer_Release(&varg); 2644 PyBuffer_Release(&vbytes);
2496 return PyByteArray_FromStringAndSize(myptr + left, right - left); 2645 return PyByteArray_FromStringAndSize(myptr + left, right - left);
2497 } 2646 }
2498 2647
2499 PyDoc_STRVAR(lstrip__doc__, 2648 /*[clinic input]
2500 "B.lstrip([bytes]) -> bytearray\n\ 2649 bytearray.lstrip
2501 \n\ 2650
2502 Strip leading bytes contained in the argument\n\ 2651 bytes: object = None
2503 and return the result as a new bytearray.\n\ 2652 /
2504 If the argument is omitted, strip leading ASCII whitespace."); 2653
2505 static PyObject * 2654 Strip leading bytes contained in the argument.
2506 bytearray_lstrip(PyByteArrayObject *self, PyObject *args) 2655
2507 { 2656 If the argument is omitted or None, strip leading ASCII whitespace.
2508 Py_ssize_t left, right, mysize, argsize; 2657 [clinic start generated code]*/
2509 char *myptr, *argptr; 2658
2510 PyObject *arg = Py_None; 2659 static PyObject *
2511 Py_buffer varg; 2660 bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes)
2512 if (!PyArg_ParseTuple(args, "|O:lstrip", &arg)) 2661 /*[clinic end generated code: output=d005c9d0ab909e66 input=80843f975dd7c480]*/
2513 return NULL; 2662 {
2514 if (arg == Py_None) { 2663 Py_ssize_t left, right, mysize, byteslen;
2515 argptr = "\t\n\r\f\v "; 2664 char *myptr, *bytesptr;
2516 argsize = 6; 2665 Py_buffer vbytes;
2666
2667 if (bytes == Py_None) {
2668 bytesptr = "\t\n\r\f\v ";
2669 byteslen = 6;
2517 } 2670 }
2518 else { 2671 else {
2519 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0) 2672 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2520 return NULL; 2673 return NULL;
2521 argptr = (char *) varg.buf; 2674 bytesptr = (char *) vbytes.buf;
2522 argsize = varg.len; 2675 byteslen = vbytes.len;
2523 } 2676 }
2524 myptr = PyByteArray_AS_STRING(self); 2677 myptr = PyByteArray_AS_STRING(self);
2525 mysize = Py_SIZE(self); 2678 mysize = Py_SIZE(self);
2526 left = lstrip_helper(myptr, mysize, argptr, argsize); 2679 left = lstrip_helper(myptr, mysize, bytesptr, byteslen);
2527 right = mysize; 2680 right = mysize;
2528 if (arg != Py_None) 2681 if (bytes != Py_None)
2529 PyBuffer_Release(&varg); 2682 PyBuffer_Release(&vbytes);
2530 return PyByteArray_FromStringAndSize(myptr + left, right - left); 2683 return PyByteArray_FromStringAndSize(myptr + left, right - left);
2531 } 2684 }
2532 2685
2533 PyDoc_STRVAR(rstrip__doc__, 2686 /*[clinic input]
2534 "B.rstrip([bytes]) -> bytearray\n\ 2687 bytearray.rstrip
2535 \n\ 2688
2536 Strip trailing bytes contained in the argument\n\ 2689 bytes: object = None
2537 and return the result as a new bytearray.\n\ 2690 /
2538 If the argument is omitted, strip trailing ASCII whitespace."); 2691
2539 static PyObject * 2692 Strip trailing bytes contained in the argument.
2540 bytearray_rstrip(PyByteArrayObject *self, PyObject *args) 2693
2541 { 2694 If the argument is omitted or None, strip trailing ASCII whitespace.
2542 Py_ssize_t right, mysize, argsize; 2695 [clinic start generated code]*/
2543 char *myptr, *argptr; 2696
2544 PyObject *arg = Py_None; 2697 static PyObject *
2545 Py_buffer varg; 2698 bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes)
2546 if (!PyArg_ParseTuple(args, "|O:rstrip", &arg)) 2699 /*[clinic end generated code: output=030e2fbd2f7276bd input=e728b994954cfd91]*/
2547 return NULL; 2700 {
2548 if (arg == Py_None) { 2701 Py_ssize_t right, mysize, byteslen;
2549 argptr = "\t\n\r\f\v "; 2702 char *myptr, *bytesptr;
2550 argsize = 6; 2703 Py_buffer vbytes;
2704
2705 if (bytes == Py_None) {
2706 bytesptr = "\t\n\r\f\v ";
2707 byteslen = 6;
2551 } 2708 }
2552 else { 2709 else {
2553 if (PyObject_GetBuffer(arg, &varg, PyBUF_SIMPLE) != 0) 2710 if (PyObject_GetBuffer(bytes, &vbytes, PyBUF_SIMPLE) != 0)
2554 return NULL; 2711 return NULL;
2555 argptr = (char *) varg.buf; 2712 bytesptr = (char *) vbytes.buf;
2556 argsize = varg.len; 2713 byteslen = vbytes.len;
2557 } 2714 }
2558 myptr = PyByteArray_AS_STRING(self); 2715 myptr = PyByteArray_AS_STRING(self);
2559 mysize = Py_SIZE(self); 2716 mysize = Py_SIZE(self);
2560 right = rstrip_helper(myptr, mysize, argptr, argsize); 2717 right = rstrip_helper(myptr, mysize, bytesptr, byteslen);
2561 if (arg != Py_None) 2718 if (bytes != Py_None)
2562 PyBuffer_Release(&varg); 2719 PyBuffer_Release(&vbytes);
2563 return PyByteArray_FromStringAndSize(myptr, right); 2720 return PyByteArray_FromStringAndSize(myptr, right);
2564 } 2721 }
2565 2722
2566 PyDoc_STRVAR(decode_doc, 2723 /*[clinic input]
2567 "B.decode(encoding='utf-8', errors='strict') -> str\n\ 2724 bytearray.decode
2568 \n\ 2725
2569 Decode B using the codec registered for encoding. Default encoding\n\ 2726 encoding: str(c_default="NULL") = 'utf-8'
2570 is 'utf-8'. errors may be given to set a different error\n\ 2727 The encoding with which to decode the bytearray.
2571 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ 2728 errors: str(c_default="NULL") = 'strict'
2572 a UnicodeDecodeError. Other possible values are 'ignore' and 'replace'\n\ 2729 The error handling scheme to use for the handling of decoding errors.
2573 as well as any other name registered with codecs.register_error that is\n\ 2730 The default is 'strict' meaning that decoding errors raise a
2574 able to handle UnicodeDecodeErrors."); 2731 UnicodeDecodeError. Other possible values are 'ignore' and 'replace'
2575 2732 as well as any other name registered with codecs.register_error that
2576 static PyObject * 2733 can handle UnicodeDecodeErrors.
2577 bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs) 2734
2578 { 2735 Decode the bytearray using the codec registered for encoding.
2579 const char *encoding = NULL; 2736 [clinic start generated code]*/
2580 const char *errors = NULL; 2737
2581 static char *kwlist[] = {"encoding", "errors", 0}; 2738 static PyObject *
2582 2739 bytearray_decode_impl(PyByteArrayObject *self, const char *encoding,
2583 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encodi ng, &errors)) 2740 const char *errors)
2584 return NULL; 2741 /*[clinic end generated code: output=f57d43f4a00b42c5 input=f28d8f903020257b]*/
2742 {
2585 if (encoding == NULL) 2743 if (encoding == NULL)
2586 encoding = PyUnicode_GetDefaultEncoding(); 2744 encoding = PyUnicode_GetDefaultEncoding();
2587 return PyUnicode_FromEncodedObject(self, encoding, errors); 2745 return PyUnicode_FromEncodedObject((PyObject*)self, encoding, errors);
2588 } 2746 }
2589 2747
2590 PyDoc_STRVAR(alloc_doc, 2748 PyDoc_STRVAR(alloc_doc,
2591 "B.__alloc__() -> int\n\ 2749 "B.__alloc__() -> int\n\
2592 \n\ 2750 \n\
2593 Return the number of bytes actually allocated."); 2751 Return the number of bytes actually allocated.");
2594 2752
2595 static PyObject * 2753 static PyObject *
2596 bytearray_alloc(PyByteArrayObject *self) 2754 bytearray_alloc(PyByteArrayObject *self)
2597 { 2755 {
2598 return PyLong_FromSsize_t(self->ob_alloc); 2756 return PyLong_FromSsize_t(self->ob_alloc);
2599 } 2757 }
2600 2758
2601 PyDoc_STRVAR(join_doc, 2759 /*[clinic input]
2602 "B.join(iterable_of_bytes) -> bytearray\n\ 2760 bytearray.join
2603 \n\ 2761
2604 Concatenate any number of bytes/bytearray objects, with B\n\ 2762 iterable_of_bytes: object
2605 in between each pair, and return the result as a new bytearray."); 2763 /
2606 2764
2607 static PyObject * 2765 Concatenate any number of bytes/bytearray objects.
2608 bytearray_join(PyObject *self, PyObject *iterable) 2766
2609 { 2767 The bytearray whose method is called is inserted in between each pair.
2610 return stringlib_bytes_join(self, iterable); 2768
2611 } 2769 The result is returned as a new bytearray object.
2612 2770 [clinic start generated code]*/
2613 PyDoc_STRVAR(splitlines__doc__, 2771
2614 "B.splitlines([keepends]) -> list of lines\n\ 2772 static PyObject *
2615 \n\ 2773 bytearray_join(PyByteArrayObject *self, PyObject *iterable_of_bytes)
2616 Return a list of the lines in B, breaking at line boundaries.\n\ 2774 /*[clinic end generated code: output=a8516370bf68ae08 input=aba6b1f9b30fcb8e]*/
2617 Line breaks are not included in the resulting list unless keepends\n\ 2775 {
2618 is given and true."); 2776 return stringlib_bytes_join((PyObject*)self, iterable_of_bytes);
2619 2777 }
2620 static PyObject* 2778
2621 bytearray_splitlines(PyObject *self, PyObject *args, PyObject *kwds) 2779 /*[clinic input]
2622 { 2780 bytearray.splitlines
2623 static char *kwlist[] = {"keepends", 0}; 2781
2624 int keepends = 0; 2782 keepends: int(c_default="0") = False
2625 2783
2626 if (!PyArg_ParseTupleAndKeywords(args, kwds, "|i:splitlines", 2784 Return a list of the lines in the bytearray, breaking at line boundaries.
2627 kwlist, &keepends)) 2785
2628 return NULL; 2786 Line breaks are not included in the resulting list unless keepends is given and
2629 2787 true.
2788 [clinic start generated code]*/
2789
2790 static PyObject *
2791 bytearray_splitlines_impl(PyByteArrayObject *self, int keepends)
2792 /*[clinic end generated code: output=4223c94b895f6ad9 input=8ccade941e5ea0bd]*/
2793 {
2630 return stringlib_splitlines( 2794 return stringlib_splitlines(
2631 (PyObject*) self, PyByteArray_AS_STRING(self), 2795 (PyObject*) self, PyByteArray_AS_STRING(self),
2632 PyByteArray_GET_SIZE(self), keepends 2796 PyByteArray_GET_SIZE(self), keepends
2633 ); 2797 );
2634 } 2798 }
2635
2636 PyDoc_STRVAR(fromhex_doc,
2637 "bytearray.fromhex(string) -> bytearray (static method)\n\
2638 \n\
2639 Create a bytearray object from a string of hexadecimal numbers.\n\
2640 Spaces between two numbers are accepted.\n\
2641 Example: bytearray.fromhex('B9 01EF') -> bytearray(b'\\xb9\\x01\\xef').");
2642 2799
2643 static int 2800 static int
2644 hex_digit_to_int(Py_UCS4 c) 2801 hex_digit_to_int(Py_UCS4 c)
2645 { 2802 {
2646 if (c >= 128) 2803 if (c >= 128)
2647 return -1; 2804 return -1;
2648 if (Py_ISDIGIT(c)) 2805 if (Py_ISDIGIT(c))
2649 return c - '0'; 2806 return c - '0';
2650 else { 2807 else {
2651 if (Py_ISUPPER(c)) 2808 if (Py_ISUPPER(c))
2652 c = Py_TOLOWER(c); 2809 c = Py_TOLOWER(c);
2653 if (c >= 'a' && c <= 'f') 2810 if (c >= 'a' && c <= 'f')
2654 return c - 'a' + 10; 2811 return c - 'a' + 10;
2655 } 2812 }
2656 return -1; 2813 return -1;
2657 } 2814 }
2658 2815
2659 static PyObject * 2816 /*[clinic input]
2660 bytearray_fromhex(PyObject *cls, PyObject *args) 2817 @classmethod
2661 { 2818 bytearray.fromhex
2662 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;
2663 char *buf; 2835 char *buf;
2664 Py_ssize_t hexlen, byteslen, i, j; 2836 Py_ssize_t hexlen, byteslen, i, j;
2665 int top, bot; 2837 int top, bot;
2666 void *data; 2838 void *data;
2667 unsigned int kind; 2839 unsigned int kind;
2668 2840
2669 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) 2841 assert(PyUnicode_Check(string));
2670 return NULL; 2842 if (PyUnicode_READY(string))
2671 assert(PyUnicode_Check(hexobj)); 2843 return NULL;
2672 if (PyUnicode_READY(hexobj)) 2844 kind = PyUnicode_KIND(string);
2673 return NULL; 2845 data = PyUnicode_DATA(string);
2674 kind = PyUnicode_KIND(hexobj); 2846 hexlen = PyUnicode_GET_LENGTH(string);
2675 data = PyUnicode_DATA(hexobj);
2676 hexlen = PyUnicode_GET_LENGTH(hexobj);
2677 2847
2678 byteslen = hexlen/2; /* This overestimates if there are spaces */ 2848 byteslen = hexlen/2; /* This overestimates if there are spaces */
2679 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); 2849 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen);
2680 if (!newbytes) 2850 if (!newbytes)
2681 return NULL; 2851 return NULL;
2682 buf = PyByteArray_AS_STRING(newbytes); 2852 buf = PyByteArray_AS_STRING(newbytes);
2683 for (i = j = 0; i < hexlen; i += 2) { 2853 for (i = j = 0; i < hexlen; i += 2) {
2684 /* skip over spaces in the input */ 2854 /* skip over spaces in the input */
2685 while (PyUnicode_READ(kind, data, i) == ' ') 2855 while (PyUnicode_READ(kind, data, i) == ' ')
2686 i++; 2856 i++;
(...skipping 11 matching lines...) Expand all
2698 } 2868 }
2699 if (PyByteArray_Resize(newbytes, j) < 0) 2869 if (PyByteArray_Resize(newbytes, j) < 0)
2700 goto error; 2870 goto error;
2701 return newbytes; 2871 return newbytes;
2702 2872
2703 error: 2873 error:
2704 Py_DECREF(newbytes); 2874 Py_DECREF(newbytes);
2705 return NULL; 2875 return NULL;
2706 } 2876 }
2707 2877
2878 PyDoc_STRVAR(hex__doc__,
2879 "B.hex() -> string\n\
2880 \n\
2881 Create a string of hexadecimal numbers from a bytearray object.\n\
2882 Example: bytearray([0xb9, 0x01, 0xef]).hex() -> 'b901ef'.");
2883
2884 static PyObject *
2885 bytearray_hex(PyBytesObject *self)
2886 {
2887 char* argbuf = PyByteArray_AS_STRING(self);
2888 Py_ssize_t arglen = PyByteArray_GET_SIZE(self);
2889 return _Py_strhex(argbuf, arglen);
2890 }
2708 2891
2709 static PyObject * 2892 static PyObject *
2710 _common_reduce(PyByteArrayObject *self, int proto) 2893 _common_reduce(PyByteArrayObject *self, int proto)
2711 { 2894 {
2712 PyObject *dict; 2895 PyObject *dict;
2713 _Py_IDENTIFIER(__dict__); 2896 _Py_IDENTIFIER(__dict__);
2714 char *buf; 2897 char *buf;
2715 2898
2716 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__); 2899 dict = _PyObject_GetAttrId((PyObject *)self, &PyId___dict__);
2717 if (dict == NULL) { 2900 if (dict == NULL) {
(...skipping 16 matching lines...) Expand all
2734 /* use more efficient byte based reduction */ 2917 /* use more efficient byte based reduction */
2735 if (Py_SIZE(self)) { 2918 if (Py_SIZE(self)) {
2736 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict); 2919 return Py_BuildValue("(O(y#)N)", Py_TYPE(self), buf, Py_SIZE(self), dict);
2737 } 2920 }
2738 else { 2921 else {
2739 return Py_BuildValue("(O()N)", Py_TYPE(self), dict); 2922 return Py_BuildValue("(O()N)", Py_TYPE(self), dict);
2740 } 2923 }
2741 } 2924 }
2742 } 2925 }
2743 2926
2744 PyDoc_STRVAR(reduce_doc, "Return state information for pickling."); 2927 /*[clinic input]
2745 2928 bytearray.__reduce__ as bytearray_reduce
2746 static PyObject * 2929
2747 bytearray_reduce(PyByteArrayObject *self) 2930 self: self(type="PyByteArrayObject *")
2931
2932 Return state information for pickling.
2933 [clinic start generated code]*/
2934
2935 static PyObject *
2936 bytearray_reduce_impl(PyByteArrayObject *self)
2937 /*[clinic end generated code: output=52bf304086464cab input=fbb07de4d102a03a]*/
2748 { 2938 {
2749 return _common_reduce(self, 2); 2939 return _common_reduce(self, 2);
2750 } 2940 }
2751 2941
2752 PyDoc_STRVAR(reduce_ex_doc, "Return state information for pickling."); 2942 /*[clinic input]
2753 2943 bytearray.__reduce_ex__ as bytearray_reduce_ex
2754 static PyObject * 2944
2755 bytearray_reduce_ex(PyByteArrayObject *self, PyObject *args) 2945 self: self(type="PyByteArrayObject *")
2756 { 2946 proto: int = 0
2757 int proto = 0; 2947 /
2758 2948
2759 if (!PyArg_ParseTuple(args, "|i:__reduce_ex__", &proto)) 2949 Return state information for pickling.
2760 return NULL; 2950 [clinic start generated code]*/
2761 2951
2952 static PyObject *
2953 bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto)
2954 /*[clinic end generated code: output=52eac33377197520 input=0e091a42ca6dbd91]*/
2955 {
2762 return _common_reduce(self, proto); 2956 return _common_reduce(self, proto);
2763 } 2957 }
2764 2958
2765 PyDoc_STRVAR(sizeof_doc, 2959 /*[clinic input]
2766 "B.__sizeof__() -> int\n\ 2960 bytearray.__sizeof__ as bytearray_sizeof
2767 \n\ 2961
2768 Returns the size of B in memory, in bytes"); 2962 self: self(type="PyByteArrayObject *")
2769 static PyObject * 2963
2770 bytearray_sizeof(PyByteArrayObject *self) 2964 Returns the size of the bytearray object in memory, in bytes.
2965 [clinic start generated code]*/
2966
2967 static PyObject *
2968 bytearray_sizeof_impl(PyByteArrayObject *self)
2969 /*[clinic end generated code: output=738abdd17951c427 input=6b23d305362b462b]*/
2771 { 2970 {
2772 Py_ssize_t res; 2971 Py_ssize_t res;
2773 2972
2774 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char); 2973 res = sizeof(PyByteArrayObject) + self->ob_alloc * sizeof(char);
2775 return PyLong_FromSsize_t(res); 2974 return PyLong_FromSsize_t(res);
2776 } 2975 }
2777 2976
2778 static PySequenceMethods bytearray_as_sequence = { 2977 static PySequenceMethods bytearray_as_sequence = {
2779 (lenfunc)bytearray_length, /* sq_length */ 2978 (lenfunc)bytearray_length, /* sq_length */
2780 (binaryfunc)PyByteArray_Concat, /* sq_concat */ 2979 (binaryfunc)PyByteArray_Concat, /* sq_concat */
(...skipping 14 matching lines...) Expand all
2795 }; 2994 };
2796 2995
2797 static PyBufferProcs bytearray_as_buffer = { 2996 static PyBufferProcs bytearray_as_buffer = {
2798 (getbufferproc)bytearray_getbuffer, 2997 (getbufferproc)bytearray_getbuffer,
2799 (releasebufferproc)bytearray_releasebuffer, 2998 (releasebufferproc)bytearray_releasebuffer,
2800 }; 2999 };
2801 3000
2802 static PyMethodDef 3001 static PyMethodDef
2803 bytearray_methods[] = { 3002 bytearray_methods[] = {
2804 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, 3003 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc},
2805 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, reduce_doc}, 3004 BYTEARRAY_REDUCE_METHODDEF
2806 {"__reduce_ex__", (PyCFunction)bytearray_reduce_ex, METH_VARARGS, reduce_ex_ doc}, 3005 BYTEARRAY_REDUCE_EX_METHODDEF
2807 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, sizeof_doc}, 3006 BYTEARRAY_SIZEOF_METHODDEF
2808 {"append", (PyCFunction)bytearray_append, METH_O, append__doc__}, 3007 BYTEARRAY_APPEND_METHODDEF
2809 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, 3008 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS,
2810 _Py_capitalize__doc__}, 3009 _Py_capitalize__doc__},
2811 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, 3010 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__},
2812 {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, clear__doc__}, 3011 BYTEARRAY_CLEAR_METHODDEF
2813 {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, copy__doc__}, 3012 BYTEARRAY_COPY_METHODDEF
2814 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, 3013 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__},
2815 {"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, deco de_doc}, 3014 BYTEARRAY_DECODE_METHODDEF
2816 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__} , 3015 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__} ,
2817 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWOR DS, 3016 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS | METH_KEYWOR DS,
2818 expandtabs__doc__}, 3017 expandtabs__doc__},
2819 {"extend", (PyCFunction)bytearray_extend, METH_O, extend__doc__}, 3018 BYTEARRAY_EXTEND_METHODDEF
2820 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, 3019 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__},
2821 {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS, 3020 BYTEARRAY_FROMHEX_METHODDEF
2822 fromhex_doc}, 3021 {"hex", (PyCFunction)bytearray_hex, METH_NOARGS, hex__doc__},
2823 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, 3022 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__},
2824 {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, insert__doc__}, 3023 BYTEARRAY_INSERT_METHODDEF
2825 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, 3024 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS,
2826 _Py_isalnum__doc__}, 3025 _Py_isalnum__doc__},
2827 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS, 3026 {"isalpha", (PyCFunction)stringlib_isalpha, METH_NOARGS,
2828 _Py_isalpha__doc__}, 3027 _Py_isalpha__doc__},
2829 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS, 3028 {"isdigit", (PyCFunction)stringlib_isdigit, METH_NOARGS,
2830 _Py_isdigit__doc__}, 3029 _Py_isdigit__doc__},
2831 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS, 3030 {"islower", (PyCFunction)stringlib_islower, METH_NOARGS,
2832 _Py_islower__doc__}, 3031 _Py_islower__doc__},
2833 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS, 3032 {"isspace", (PyCFunction)stringlib_isspace, METH_NOARGS,
2834 _Py_isspace__doc__}, 3033 _Py_isspace__doc__},
2835 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS, 3034 {"istitle", (PyCFunction)stringlib_istitle, METH_NOARGS,
2836 _Py_istitle__doc__}, 3035 _Py_istitle__doc__},
2837 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, 3036 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS,
2838 _Py_isupper__doc__}, 3037 _Py_isupper__doc__},
2839 {"join", (PyCFunction)bytearray_join, METH_O, join_doc}, 3038 BYTEARRAY_JOIN_METHODDEF
2840 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, 3039 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__},
2841 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, 3040 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__},
2842 {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, lstrip__doc__}, 3041 BYTEARRAY_LSTRIP_METHODDEF
2843 {"maketrans", (PyCFunction)bytearray_maketrans, METH_VARARGS|METH_STATIC, 3042 BYTEARRAY_MAKETRANS_METHODDEF
2844 _Py_maketrans__doc__}, 3043 BYTEARRAY_PARTITION_METHODDEF
2845 {"partition", (PyCFunction)bytearray_partition, METH_O, partition__doc__}, 3044 BYTEARRAY_POP_METHODDEF
2846 {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, pop__doc__}, 3045 BYTEARRAY_REMOVE_METHODDEF
2847 {"remove", (PyCFunction)bytearray_remove, METH_O, remove__doc__}, 3046 BYTEARRAY_REPLACE_METHODDEF
2848 {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, replace__doc__}, 3047 BYTEARRAY_REVERSE_METHODDEF
2849 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, reverse__doc__},
2850 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, 3048 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__},
2851 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, 3049 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__},
2852 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, 3050 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__},
2853 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__} , 3051 BYTEARRAY_RPARTITION_METHODDEF
2854 {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS | METH_KEYWORDS, rspl it__doc__}, 3052 BYTEARRAY_RSPLIT_METHODDEF
2855 {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__}, 3053 BYTEARRAY_RSTRIP_METHODDEF
2856 {"split", (PyCFunction)bytearray_split, METH_VARARGS | METH_KEYWORDS, split_ _doc__}, 3054 BYTEARRAY_SPLIT_METHODDEF
2857 {"splitlines", (PyCFunction)bytearray_splitlines, 3055 BYTEARRAY_SPLITLINES_METHODDEF
2858 METH_VARARGS | METH_KEYWORDS, splitlines__doc__},
2859 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , 3056 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS ,
2860 startswith__doc__}, 3057 startswith__doc__},
2861 {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__}, 3058 BYTEARRAY_STRIP_METHODDEF
2862 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, 3059 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS,
2863 _Py_swapcase__doc__}, 3060 _Py_swapcase__doc__},
2864 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, 3061 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__},
2865 {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, 3062 BYTEARRAY_TRANSLATE_METHODDEF
2866 translate__doc__},
2867 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, 3063 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__},
2868 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__}, 3064 {"zfill", (PyCFunction)stringlib_zfill, METH_VARARGS, zfill__doc__},
2869 {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*/
2870 }; 3081 };
2871 3082
2872 PyDoc_STRVAR(bytearray_doc, 3083 PyDoc_STRVAR(bytearray_doc,
2873 "bytearray(iterable_of_ints) -> bytearray\n\ 3084 "bytearray(iterable_of_ints) -> bytearray\n\
2874 bytearray(string, encoding[, errors]) -> bytearray\n\ 3085 bytearray(string, encoding[, errors]) -> bytearray\n\
2875 bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\ 3086 bytearray(bytes_or_buffer) -> mutable copy of bytes_or_buffer\n\
2876 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\
2877 bytearray() -> empty bytes array\n\ 3088 bytearray() -> empty bytes array\n\
2878 \n\ 3089 \n\
2879 Construct an mutable bytearray object from:\n\ 3090 Construct an mutable bytearray object from:\n\
(...skipping 10 matching lines...) Expand all
2890 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3101 PyVarObject_HEAD_INIT(&PyType_Type, 0)
2891 "bytearray", 3102 "bytearray",
2892 sizeof(PyByteArrayObject), 3103 sizeof(PyByteArrayObject),
2893 0, 3104 0,
2894 (destructor)bytearray_dealloc, /* tp_dealloc */ 3105 (destructor)bytearray_dealloc, /* tp_dealloc */
2895 0, /* tp_print */ 3106 0, /* tp_print */
2896 0, /* tp_getattr */ 3107 0, /* tp_getattr */
2897 0, /* tp_setattr */ 3108 0, /* tp_setattr */
2898 0, /* tp_reserved */ 3109 0, /* tp_reserved */
2899 (reprfunc)bytearray_repr, /* tp_repr */ 3110 (reprfunc)bytearray_repr, /* tp_repr */
2900 0, /* tp_as_number */ 3111 &bytearray_as_number, /* tp_as_number */
2901 &bytearray_as_sequence, /* tp_as_sequence */ 3112 &bytearray_as_sequence, /* tp_as_sequence */
2902 &bytearray_as_mapping, /* tp_as_mapping */ 3113 &bytearray_as_mapping, /* tp_as_mapping */
2903 0, /* tp_hash */ 3114 0, /* tp_hash */
2904 0, /* tp_call */ 3115 0, /* tp_call */
2905 bytearray_str, /* tp_str */ 3116 bytearray_str, /* tp_str */
2906 PyObject_GenericGetAttr, /* tp_getattro */ 3117 PyObject_GenericGetAttr, /* tp_getattro */
2907 0, /* tp_setattro */ 3118 0, /* tp_setattro */
2908 &bytearray_as_buffer, /* tp_as_buffer */ 3119 &bytearray_as_buffer, /* tp_as_buffer */
2909 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */ 3120 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE, /* tp_flags */
2910 bytearray_doc, /* tp_doc */ 3121 bytearray_doc, /* tp_doc */
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after
3017 } 3228 }
3018 Py_RETURN_NONE; 3229 Py_RETURN_NONE;
3019 } 3230 }
3020 3231
3021 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling."); 3232 PyDoc_STRVAR(setstate_doc, "Set state information for unpickling.");
3022 3233
3023 static PyMethodDef bytearrayiter_methods[] = { 3234 static PyMethodDef bytearrayiter_methods[] = {
3024 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS, 3235 {"__length_hint__", (PyCFunction)bytearrayiter_length_hint, METH_NOARGS,
3025 length_hint_doc}, 3236 length_hint_doc},
3026 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS, 3237 {"__reduce__", (PyCFunction)bytearrayiter_reduce, METH_NOARGS,
3027 reduce_doc}, 3238 bytearray_reduce__doc__},
3028 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O, 3239 {"__setstate__", (PyCFunction)bytearrayiter_setstate, METH_O,
3029 setstate_doc}, 3240 setstate_doc},
3030 {NULL, NULL} /* sentinel */ 3241 {NULL, NULL} /* sentinel */
3031 }; 3242 };
3032 3243
3033 PyTypeObject PyByteArrayIter_Type = { 3244 PyTypeObject PyByteArrayIter_Type = {
3034 PyVarObject_HEAD_INIT(&PyType_Type, 0) 3245 PyVarObject_HEAD_INIT(&PyType_Type, 0)
3035 "bytearray_iterator", /* tp_name */ 3246 "bytearray_iterator", /* tp_name */
3036 sizeof(bytesiterobject), /* tp_basicsize */ 3247 sizeof(bytesiterobject), /* tp_basicsize */
3037 0, /* tp_itemsize */ 3248 0, /* tp_itemsize */
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after
3074 } 3285 }
3075 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type); 3286 it = PyObject_GC_New(bytesiterobject, &PyByteArrayIter_Type);
3076 if (it == NULL) 3287 if (it == NULL)
3077 return NULL; 3288 return NULL;
3078 it->it_index = 0; 3289 it->it_index = 0;
3079 Py_INCREF(seq); 3290 Py_INCREF(seq);
3080 it->it_seq = (PyByteArrayObject *)seq; 3291 it->it_seq = (PyByteArrayObject *)seq;
3081 _PyObject_GC_TRACK(it); 3292 _PyObject_GC_TRACK(it);
3082 return (PyObject *)it; 3293 return (PyObject *)it;
3083 } 3294 }
LEFTRIGHT

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