Changeset 391 for python/trunk/Objects/bytearrayobject.c
- Timestamp:
- Mar 19, 2014, 11:31:01 PM (11 years ago)
- Location:
- python/trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
python/trunk
-
Property svn:mergeinfo
set to
/python/vendor/Python-2.7.6 merged eligible /python/vendor/current merged eligible
-
Property svn:mergeinfo
set to
-
python/trunk/Objects/bytearrayobject.c
r2 r391 6 6 #include "bytes_methods.h" 7 7 8 static PyByteArrayObject *nullbytes = NULL;9 8 char _PyByteArray_empty_string[] = ""; 10 9 … … 12 11 PyByteArray_Fini(void) 13 12 { 14 Py_CLEAR(nullbytes);15 13 } 16 14 … … 18 16 PyByteArray_Init(void) 19 17 { 20 nullbytes = PyObject_New(PyByteArrayObject, &PyByteArray_Type);21 if (nullbytes == NULL)22 return 0;23 nullbytes->ob_bytes = NULL;24 Py_SIZE(nullbytes) = nullbytes->ob_alloc = 0;25 nullbytes->ob_exports = 0;26 18 return 1; 27 19 } … … 69 61 70 62 static Py_ssize_t 71 byte s_buffer_getreadbuf(PyByteArrayObject *self, Py_ssize_t index, const void **ptr)63 bytearray_buffer_getreadbuf(PyByteArrayObject *self, Py_ssize_t index, const void **ptr) 72 64 { 73 65 if ( index != 0 ) { … … 81 73 82 74 static Py_ssize_t 83 byte s_buffer_getwritebuf(PyByteArrayObject *self, Py_ssize_t index, const void **ptr)75 bytearray_buffer_getwritebuf(PyByteArrayObject *self, Py_ssize_t index, const void **ptr) 84 76 { 85 77 if ( index != 0 ) { … … 93 85 94 86 static Py_ssize_t 95 byte s_buffer_getsegcount(PyByteArrayObject *self, Py_ssize_t *lenp)87 bytearray_buffer_getsegcount(PyByteArrayObject *self, Py_ssize_t *lenp) 96 88 { 97 89 if ( lenp ) … … 101 93 102 94 static Py_ssize_t 103 byte s_buffer_getcharbuf(PyByteArrayObject *self, Py_ssize_t index, const char **ptr)95 bytearray_buffer_getcharbuf(PyByteArrayObject *self, Py_ssize_t index, const char **ptr) 104 96 { 105 97 if ( index != 0 ) { … … 113 105 114 106 static int 115 byte s_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags)116 { 117 118 119 120 121 122 123 124 125 126 127 128 107 bytearray_getbuffer(PyByteArrayObject *obj, Py_buffer *view, int flags) 108 { 109 int ret; 110 void *ptr; 111 if (view == NULL) { 112 obj->ob_exports++; 113 return 0; 114 } 115 ptr = (void *) PyByteArray_AS_STRING(obj); 116 ret = PyBuffer_FillInfo(view, (PyObject*)obj, ptr, Py_SIZE(obj), 0, flags); 117 if (ret >= 0) { 118 obj->ob_exports++; 119 } 120 return ret; 129 121 } 130 122 131 123 static void 132 byte s_releasebuffer(PyByteArrayObject *obj, Py_buffer *view)133 { 134 124 bytearray_releasebuffer(PyByteArrayObject *obj, Py_buffer *view) 125 { 126 obj->ob_exports--; 135 127 } 136 128 … … 297 289 size = va.len + vb.len; 298 290 if (size < 0) { 299 returnPyErr_NoMemory();291 PyErr_NoMemory(); 300 292 goto done; 301 293 } … … 318 310 319 311 static Py_ssize_t 320 byte s_length(PyByteArrayObject *self)312 bytearray_length(PyByteArrayObject *self) 321 313 { 322 314 return Py_SIZE(self); … … 324 316 325 317 static PyObject * 326 byte s_iconcat(PyByteArrayObject *self, PyObject *other)318 bytearray_iconcat(PyByteArrayObject *self, PyObject *other) 327 319 { 328 320 Py_ssize_t mysize; … … 357 349 358 350 static PyObject * 359 byte s_repeat(PyByteArrayObject *self, Py_ssize_t count)351 bytearray_repeat(PyByteArrayObject *self, Py_ssize_t count) 360 352 { 361 353 PyByteArrayObject *result; … … 383 375 384 376 static PyObject * 385 byte s_irepeat(PyByteArrayObject *self, Py_ssize_t count)377 bytearray_irepeat(PyByteArrayObject *self, Py_ssize_t count) 386 378 { 387 379 Py_ssize_t mysize; … … 414 406 415 407 static PyObject * 416 byte s_getitem(PyByteArrayObject *self, Py_ssize_t i)408 bytearray_getitem(PyByteArrayObject *self, Py_ssize_t i) 417 409 { 418 410 if (i < 0) … … 426 418 427 419 static PyObject * 428 byte s_subscript(PyByteArrayObject *self, PyObject *index)420 bytearray_subscript(PyByteArrayObject *self, PyObject *index) 429 421 { 430 422 if (PyIndex_Check(index)) { … … 481 473 482 474 static int 483 byte s_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi,475 bytearray_setslice(PyByteArrayObject *self, Py_ssize_t lo, Py_ssize_t hi, 484 476 PyObject *values) 485 477 { … … 496 488 if (values == NULL) 497 489 return -1; 498 err = byte s_setslice(self, lo, hi, values);490 err = bytearray_setslice(self, lo, hi, values); 499 491 Py_DECREF(values); 500 492 return err; … … 571 563 572 564 static int 573 byte s_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value)565 bytearray_setitem(PyByteArrayObject *self, Py_ssize_t i, PyObject *value) 574 566 { 575 567 int ival; … … 584 576 585 577 if (value == NULL) 586 return byte s_setslice(self, i, i+1, NULL);578 return bytearray_setslice(self, i, i+1, NULL); 587 579 588 580 if (!_getbytevalue(value, &ival)) … … 594 586 595 587 static int 596 byte s_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values)588 bytearray_ass_subscript(PyByteArrayObject *self, PyObject *index, PyObject *values) 597 589 { 598 590 Py_ssize_t start, stop, step, slicelen, needed; … … 645 637 } 646 638 else if (values == (PyObject *)self || !PyByteArray_Check(values)) { 647 /* Make a copy an call this function recursively */648 639 int err; 640 if (PyNumber_Check(values) || PyUnicode_Check(values)) { 641 PyErr_SetString(PyExc_TypeError, 642 "can assign only bytes, buffers, or iterables " 643 "of ints in range(0, 256)"); 644 return -1; 645 } 646 /* Make a copy and call this function recursively */ 649 647 values = PyByteArray_FromObject(values); 650 648 if (values == NULL) 651 649 return -1; 652 err = byte s_ass_subscript(self, index, values);650 err = bytearray_ass_subscript(self, index, values); 653 651 Py_DECREF(values); 654 652 return err; … … 752 750 753 751 static int 754 byte s_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds)752 bytearray_init(PyByteArrayObject *self, PyObject *args, PyObject *kwds) 755 753 { 756 754 static char *kwlist[] = {"source", "encoding", "errors", 0}; … … 795 793 Py_INCREF(arg); 796 794 } 797 new = byte s_iconcat(self, arg);795 new = bytearray_iconcat(self, arg); 798 796 Py_DECREF(encoded); 799 797 if (new == NULL) … … 803 801 } 804 802 803 #ifdef Py_USING_UNICODE 805 804 if (PyUnicode_Check(arg)) { 806 805 /* Encode via the codec registry */ … … 815 814 return -1; 816 815 assert(PyBytes_Check(encoded)); 817 new = byte s_iconcat(self, encoded);816 new = bytearray_iconcat(self, encoded); 818 817 Py_DECREF(encoded); 819 818 if (new == NULL) … … 822 821 return 0; 823 822 } 823 #endif 824 824 825 825 /* If it's not unicode, there can't be encoding or errors */ … … 831 831 832 832 /* Is it an int? */ 833 count = PyNumber_AsSsize_t(arg, PyExc_ValueError); 834 if (count == -1 && PyErr_Occurred()) 833 count = PyNumber_AsSsize_t(arg, PyExc_OverflowError); 834 if (count == -1 && PyErr_Occurred()) { 835 if (PyErr_ExceptionMatches(PyExc_OverflowError)) 836 return -1; 835 837 PyErr_Clear(); 838 } 839 else if (count < 0) { 840 PyErr_SetString(PyExc_ValueError, "negative count"); 841 return -1; 842 } 836 843 else { 837 if (count < 0) {838 PyErr_SetString(PyExc_ValueError, "negative count");839 return -1;840 }841 844 if (count > 0) { 842 845 if (PyByteArray_Resize((PyObject *)self, count)) … … 915 918 "smart quote" functionality. */ 916 919 static PyObject * 917 byte s_repr(PyByteArrayObject *self)920 bytearray_repr(PyByteArrayObject *self) 918 921 { 919 922 static const char *hexdigits = "0123456789abcdef"; … … 930 933 } 931 934 newsize = 14 + 4 * length; 932 v = Py Unicode_FromUnicode(NULL, newsize);935 v = PyString_FromStringAndSize(NULL, newsize); 933 936 if (v == NULL) { 934 937 return NULL; … … 936 939 else { 937 940 register Py_ssize_t i; 938 register Py_UNICODEc;939 register Py_UNICODE*p;941 register char c; 942 register char *p; 940 943 int quote; 941 944 … … 957 960 } 958 961 959 p = Py Unicode_AS_UNICODE(v);962 p = PyString_AS_STRING(v); 960 963 while (*quote_prefix) 961 964 *p++ = *quote_prefix++; … … 965 968 /* There's at least enough room for a hex escape 966 969 and a closing quote. */ 967 assert(newsize - (p - Py Unicode_AS_UNICODE(v)) >= 5);970 assert(newsize - (p - PyString_AS_STRING(v)) >= 5); 968 971 c = self->ob_bytes[i]; 969 972 if (c == '\'' || c == '\\') … … 986 989 *p++ = c; 987 990 } 988 assert(newsize - (p - Py Unicode_AS_UNICODE(v)) >= 1);991 assert(newsize - (p - PyString_AS_STRING(v)) >= 1); 989 992 *p++ = quote; 990 993 while (*quote_postfix) { … … 992 995 } 993 996 *p = '\0'; 994 if ( PyUnicode_Resize(&v, (p - PyUnicode_AS_UNICODE(v)))) {997 if (_PyString_Resize(&v, (p - PyString_AS_STRING(v)))) { 995 998 Py_DECREF(v); 996 999 return NULL; … … 1001 1004 1002 1005 static PyObject * 1003 byte s_str(PyObject *op)1006 bytearray_str(PyObject *op) 1004 1007 { 1005 1008 #if 0 … … 1009 1012 return NULL; 1010 1013 } 1011 return byte s_repr((PyByteArrayObject*)op);1014 return bytearray_repr((PyByteArrayObject*)op); 1012 1015 #endif 1013 1016 return PyBytes_FromStringAndSize(((PyByteArrayObject*)op)->ob_bytes, Py_SIZE(op)); … … 1015 1018 1016 1019 static PyObject * 1017 byte s_richcompare(PyObject *self, PyObject *other, int op)1020 bytearray_richcompare(PyObject *self, PyObject *other, int op) 1018 1021 { 1019 1022 Py_ssize_t self_size, other_size; … … 1026 1029 buffer API. Except that a comparison with Unicode is always an 1027 1030 error, even if the comparison is for equality. */ 1031 #ifdef Py_USING_UNICODE 1028 1032 if (PyObject_IsInstance(self, (PyObject*)&PyUnicode_Type) || 1029 1033 PyObject_IsInstance(other, (PyObject*)&PyUnicode_Type)) { … … 1037 1041 return Py_NotImplemented; 1038 1042 } 1043 #endif 1039 1044 1040 1045 self_size = _getbuffer(self, &self_bytes); … … 1090 1095 1091 1096 static void 1092 byte s_dealloc(PyByteArrayObject *self)1093 { 1094 1095 1097 bytearray_dealloc(PyByteArrayObject *self) 1098 { 1099 if (self->ob_exports > 0) { 1100 PyErr_SetString(PyExc_SystemError, 1096 1101 "deallocated bytearray object has exported buffers"); 1097 1098 1102 PyErr_Print(); 1103 } 1099 1104 if (self->ob_bytes != 0) { 1100 1105 PyMem_Free(self->ob_bytes); … … 1108 1113 1109 1114 #define STRINGLIB_CHAR char 1110 #define STRINGLIB_CMP memcmp1111 1115 #define STRINGLIB_LEN PyByteArray_GET_SIZE 1112 1116 #define STRINGLIB_STR PyByteArray_AS_STRING 1113 1117 #define STRINGLIB_NEW PyByteArray_FromStringAndSize 1114 #define STRINGLIB_EMPTY nullbytes 1118 #define STRINGLIB_ISSPACE Py_ISSPACE 1119 #define STRINGLIB_ISLINEBREAK(x) ((x == '\n') || (x == '\r')) 1115 1120 #define STRINGLIB_CHECK_EXACT PyByteArray_CheckExact 1116 1121 #define STRINGLIB_MUTABLE 1 1117 #define FROM_BYTEARRAY 11118 1122 1119 1123 #include "stringlib/fastsearch.h" … … 1121 1125 #include "stringlib/find.h" 1122 1126 #include "stringlib/partition.h" 1127 #include "stringlib/split.h" 1123 1128 #include "stringlib/ctype.h" 1124 1129 #include "stringlib/transmogrify.h" … … 1128 1133 were copied from the old char* style string object. */ 1129 1134 1130 Py_LOCAL_INLINE(void) 1131 _adjust_indices(Py_ssize_t *start, Py_ssize_t *end, Py_ssize_t len) 1132 { 1133 if (*end > len) 1134 *end = len; 1135 else if (*end < 0) 1136 *end += len; 1137 if (*end < 0) 1138 *end = 0; 1139 if (*start < 0) 1140 *start += len; 1141 if (*start < 0) 1142 *start = 0; 1143 } 1144 1135 /* helper macro to fixup start/end slice values */ 1136 #define ADJUST_INDICES(start, end, len) \ 1137 if (end > len) \ 1138 end = len; \ 1139 else if (end < 0) { \ 1140 end += len; \ 1141 if (end < 0) \ 1142 end = 0; \ 1143 } \ 1144 if (start < 0) { \ 1145 start += len; \ 1146 if (start < 0) \ 1147 start = 0; \ 1148 } 1145 1149 1146 1150 Py_LOCAL_INLINE(Py_ssize_t) 1147 byte s_find_internal(PyByteArrayObject *self, PyObject *args, int dir)1151 bytearray_find_internal(PyByteArrayObject *self, PyObject *args, int dir) 1148 1152 { 1149 1153 PyObject *subobj; … … 1152 1156 Py_ssize_t res; 1153 1157 1154 if (! PyArg_ParseTuple(args, "O|O&O&:find/rfind/index/rindex", &subobj,1155 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end))1158 if (!stringlib_parse_args_finds("find/rfind/index/rindex", 1159 args, &subobj, &start, &end)) 1156 1160 return -2; 1157 1161 if (_getbuffer(subobj, &subbuf) < 0) … … 1173 1177 \n\ 1174 1178 Return the lowest index in B where subsection sub is found,\n\ 1175 such that sub is contained within s[start,end]. Optional\n\1179 such that sub is contained within B[start,end]. Optional\n\ 1176 1180 arguments start and end are interpreted as in slice notation.\n\ 1177 1181 \n\ … … 1179 1183 1180 1184 static PyObject * 1181 byte s_find(PyByteArrayObject *self, PyObject *args)1182 { 1183 Py_ssize_t result = byte s_find_internal(self, args, +1);1185 bytearray_find(PyByteArrayObject *self, PyObject *args) 1186 { 1187 Py_ssize_t result = bytearray_find_internal(self, args, +1); 1184 1188 if (result == -2) 1185 1189 return NULL; … … 1195 1199 1196 1200 static PyObject * 1197 byte s_count(PyByteArrayObject *self, PyObject *args)1201 bytearray_count(PyByteArrayObject *self, PyObject *args) 1198 1202 { 1199 1203 PyObject *sub_obj; … … 1203 1207 PyObject *count_obj; 1204 1208 1205 if (!PyArg_ParseTuple(args, "O|O&O&:count", &sub_obj, 1206 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) 1209 if (!stringlib_parse_args_finds("count", args, &sub_obj, &start, &end)) 1207 1210 return NULL; 1208 1211 … … 1210 1213 return NULL; 1211 1214 1212 _adjust_indices(&start, &end, PyByteArray_GET_SIZE(self));1215 ADJUST_INDICES(start, end, PyByteArray_GET_SIZE(self)); 1213 1216 1214 1217 count_obj = PyInt_FromSsize_t( 1215 stringlib_count(str + start, end - start, vsub.buf, vsub.len )1218 stringlib_count(str + start, end - start, vsub.buf, vsub.len, PY_SSIZE_T_MAX) 1216 1219 ); 1217 1220 PyBuffer_Release(&vsub); … … 1226 1229 1227 1230 static PyObject * 1228 byte s_index(PyByteArrayObject *self, PyObject *args)1229 { 1230 Py_ssize_t result = byte s_find_internal(self, args, +1);1231 bytearray_index(PyByteArrayObject *self, PyObject *args) 1232 { 1233 Py_ssize_t result = bytearray_find_internal(self, args, +1); 1231 1234 if (result == -2) 1232 1235 return NULL; … … 1244 1247 \n\ 1245 1248 Return the highest index in B where subsection sub is found,\n\ 1246 such that sub is contained within s[start,end]. Optional\n\1249 such that sub is contained within B[start,end]. Optional\n\ 1247 1250 arguments start and end are interpreted as in slice notation.\n\ 1248 1251 \n\ … … 1250 1253 1251 1254 static PyObject * 1252 byte s_rfind(PyByteArrayObject *self, PyObject *args)1253 { 1254 Py_ssize_t result = byte s_find_internal(self, args, -1);1255 bytearray_rfind(PyByteArrayObject *self, PyObject *args) 1256 { 1257 Py_ssize_t result = bytearray_find_internal(self, args, -1); 1255 1258 if (result == -2) 1256 1259 return NULL; … … 1265 1268 1266 1269 static PyObject * 1267 byte s_rindex(PyByteArrayObject *self, PyObject *args)1268 { 1269 Py_ssize_t result = byte s_find_internal(self, args, -1);1270 bytearray_rindex(PyByteArrayObject *self, PyObject *args) 1271 { 1272 Py_ssize_t result = bytearray_find_internal(self, args, -1); 1270 1273 if (result == -2) 1271 1274 return NULL; … … 1280 1283 1281 1284 static int 1282 byte s_contains(PyObject *self, PyObject *arg)1285 bytearray_contains(PyObject *self, PyObject *arg) 1283 1286 { 1284 1287 Py_ssize_t ival = PyNumber_AsSsize_t(arg, PyExc_ValueError); … … 1308 1311 */ 1309 1312 Py_LOCAL(int) 1310 _byte s_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start,1313 _bytearray_tailmatch(PyByteArrayObject *self, PyObject *substr, Py_ssize_t start, 1311 1314 Py_ssize_t end, int direction) 1312 1315 { … … 1321 1324 return -1; 1322 1325 1323 _adjust_indices(&start, &end, len);1326 ADJUST_INDICES(start, end, len); 1324 1327 1325 1328 if (direction < 0) { … … 1355 1358 1356 1359 static PyObject * 1357 byte s_startswith(PyByteArrayObject *self, PyObject *args)1360 bytearray_startswith(PyByteArrayObject *self, PyObject *args) 1358 1361 { 1359 1362 Py_ssize_t start = 0; … … 1362 1365 int result; 1363 1366 1364 if (!PyArg_ParseTuple(args, "O|O&O&:startswith", &subobj, 1365 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) 1367 if (!stringlib_parse_args_finds("startswith", args, &subobj, &start, &end)) 1366 1368 return NULL; 1367 1369 if (PyTuple_Check(subobj)) { 1368 1370 Py_ssize_t i; 1369 1371 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { 1370 result = _byte s_tailmatch(self,1372 result = _bytearray_tailmatch(self, 1371 1373 PyTuple_GET_ITEM(subobj, i), 1372 1374 start, end, -1); … … 1379 1381 Py_RETURN_FALSE; 1380 1382 } 1381 result = _byte s_tailmatch(self, subobj, start, end, -1);1383 result = _bytearray_tailmatch(self, subobj, start, end, -1); 1382 1384 if (result == -1) 1383 1385 return NULL; … … 1395 1397 1396 1398 static PyObject * 1397 byte s_endswith(PyByteArrayObject *self, PyObject *args)1399 bytearray_endswith(PyByteArrayObject *self, PyObject *args) 1398 1400 { 1399 1401 Py_ssize_t start = 0; … … 1402 1404 int result; 1403 1405 1404 if (!PyArg_ParseTuple(args, "O|O&O&:endswith", &subobj, 1405 _PyEval_SliceIndex, &start, _PyEval_SliceIndex, &end)) 1406 if (!stringlib_parse_args_finds("endswith", args, &subobj, &start, &end)) 1406 1407 return NULL; 1407 1408 if (PyTuple_Check(subobj)) { 1408 1409 Py_ssize_t i; 1409 1410 for (i = 0; i < PyTuple_GET_SIZE(subobj); i++) { 1410 result = _byte s_tailmatch(self,1411 result = _bytearray_tailmatch(self, 1411 1412 PyTuple_GET_ITEM(subobj, i), 1412 1413 start, end, +1); … … 1419 1420 Py_RETURN_FALSE; 1420 1421 } 1421 result = _byte s_tailmatch(self, subobj, start, end, +1);1422 result = _bytearray_tailmatch(self, subobj, start, end, +1); 1422 1423 if (result == -1) 1423 1424 return NULL; … … 1436 1437 1437 1438 static PyObject * 1438 byte s_translate(PyByteArrayObject *self, PyObject *args)1439 bytearray_translate(PyByteArrayObject *self, PyObject *args) 1439 1440 { 1440 1441 register char *input, *output; … … 1444 1445 const char *output_start; 1445 1446 Py_ssize_t inlen; 1446 PyObject *result ;1447 PyObject *result = NULL; 1447 1448 int trans_table[256]; 1448 PyObject *tableobj , *delobj = NULL;1449 PyObject *tableobj = NULL, *delobj = NULL; 1449 1450 Py_buffer vtable, vdel; 1450 1451 … … 1453 1454 return NULL; 1454 1455 1455 if (_getbuffer(tableobj, &vtable) < 0) 1456 return NULL; 1457 1458 if (vtable.len != 256) { 1459 PyErr_SetString(PyExc_ValueError, 1460 "translation table must be 256 characters long"); 1461 PyBuffer_Release(&vtable); 1462 return NULL; 1456 if (tableobj == Py_None) { 1457 table = NULL; 1458 tableobj = NULL; 1459 } else if (_getbuffer(tableobj, &vtable) < 0) { 1460 return NULL; 1461 } else { 1462 if (vtable.len != 256) { 1463 PyErr_SetString(PyExc_ValueError, 1464 "translation table must be 256 characters long"); 1465 PyBuffer_Release(&vtable); 1466 return NULL; 1467 } 1468 table = (const char*)vtable.buf; 1463 1469 } 1464 1470 1465 1471 if (delobj != NULL) { 1466 1472 if (_getbuffer(delobj, &vdel) < 0) { 1467 PyBuffer_Release(&vtable); 1468 return NULL; 1473 if (tableobj != NULL) 1474 PyBuffer_Release(&vtable); 1475 return NULL; 1469 1476 } 1470 1477 } … … 1474 1481 } 1475 1482 1476 table = (const char *)vtable.buf;1477 1483 inlen = PyByteArray_GET_SIZE(input_obj); 1478 1484 result = PyByteArray_FromStringAndSize((char *)NULL, inlen); … … 1482 1488 input = PyByteArray_AS_STRING(input_obj); 1483 1489 1484 if (vdel.len == 0 ) {1490 if (vdel.len == 0 && table != NULL) { 1485 1491 /* If no deletions are required, use faster code */ 1486 1492 for (i = inlen; --i >= 0; ) { … … 1490 1496 goto done; 1491 1497 } 1492 1493 for (i = 0; i < 256; i++) 1494 trans_table[i] = Py_CHARMASK(table[i]); 1498 1499 if (table == NULL) { 1500 for (i = 0; i < 256; i++) 1501 trans_table[i] = Py_CHARMASK(i); 1502 } else { 1503 for (i = 0; i < 256; i++) 1504 trans_table[i] = Py_CHARMASK(table[i]); 1505 } 1495 1506 1496 1507 for (i = 0; i < vdel.len; i++) … … 1508 1519 1509 1520 done: 1510 PyBuffer_Release(&vtable); 1521 if (tableobj != NULL) 1522 PyBuffer_Release(&vtable); 1511 1523 if (delobj != NULL) 1512 1524 PyBuffer_Release(&vdel); … … 1515 1527 1516 1528 1517 #define FORWARD 11518 #define REVERSE -11519 1520 1529 /* find and count characters and substrings */ 1521 1530 1522 1531 #define findchar(target, target_len, c) \ 1523 1532 ((char *)memchr((const void *)(target), c, target_len)) 1524 1525 /* Don't call if length < 2 */1526 #define Py_STRING_MATCH(target, offset, pattern, length) \1527 (target[offset] == pattern[0] && \1528 target[offset+length-1] == pattern[length-1] && \1529 !memcmp(target+offset+1, pattern+1, length-2) )1530 1533 1531 1534 … … 1551 1554 break; 1552 1555 start += 1; 1553 }1554 return count;1555 }1556 1557 Py_LOCAL(Py_ssize_t)1558 findstring(const char *target, Py_ssize_t target_len,1559 const char *pattern, Py_ssize_t pattern_len,1560 Py_ssize_t start,1561 Py_ssize_t end,1562 int direction)1563 {1564 if (start < 0) {1565 start += target_len;1566 if (start < 0)1567 start = 0;1568 }1569 if (end > target_len) {1570 end = target_len;1571 } else if (end < 0) {1572 end += target_len;1573 if (end < 0)1574 end = 0;1575 }1576 1577 /* zero-length substrings always match at the first attempt */1578 if (pattern_len == 0)1579 return (direction > 0) ? start : end;1580 1581 end -= pattern_len;1582 1583 if (direction < 0) {1584 for (; end >= start; end--)1585 if (Py_STRING_MATCH(target, end, pattern, pattern_len))1586 return end;1587 } else {1588 for (; start <= end; start++)1589 if (Py_STRING_MATCH(target, start, pattern, pattern_len))1590 return start;1591 }1592 return -1;1593 }1594 1595 Py_LOCAL_INLINE(Py_ssize_t)1596 countstring(const char *target, Py_ssize_t target_len,1597 const char *pattern, Py_ssize_t pattern_len,1598 Py_ssize_t start,1599 Py_ssize_t end,1600 int direction, Py_ssize_t maxcount)1601 {1602 Py_ssize_t count=0;1603 1604 if (start < 0) {1605 start += target_len;1606 if (start < 0)1607 start = 0;1608 }1609 if (end > target_len) {1610 end = target_len;1611 } else if (end < 0) {1612 end += target_len;1613 if (end < 0)1614 end = 0;1615 }1616 1617 /* zero-length substrings match everywhere */1618 if (pattern_len == 0 || maxcount == 0) {1619 if (target_len+1 < maxcount)1620 return target_len+1;1621 return maxcount;1622 }1623 1624 end -= pattern_len;1625 if (direction < 0) {1626 for (; (end >= start); end--)1627 if (Py_STRING_MATCH(target, end, pattern, pattern_len)) {1628 count++;1629 if (--maxcount <= 0) break;1630 end -= pattern_len-1;1631 }1632 } else {1633 for (; (start <= end); start++)1634 if (Py_STRING_MATCH(target, start, pattern, pattern_len)) {1635 count++;1636 if (--maxcount <= 0)1637 break;1638 start += pattern_len-1;1639 }1640 1556 } 1641 1557 return count; … … 1763 1679 self_s = PyByteArray_AS_STRING(self); 1764 1680 1765 count = countstring(self_s, self_len, 1766 from_s, from_len, 1767 0, self_len, 1, 1768 maxcount); 1681 count = stringlib_count(self_s, self_len, 1682 from_s, from_len, 1683 maxcount); 1769 1684 1770 1685 if (count == 0) { … … 1785 1700 end = self_s + self_len; 1786 1701 while (count-- > 0) { 1787 offset = findstring(start, end-start,1788 from_s, from_len,1789 0, end-start, FORWARD);1702 offset = stringlib_find(start, end-start, 1703 from_s, from_len, 1704 0); 1790 1705 if (offset == -1) 1791 1706 break; … … 1807 1722 Py_ssize_t maxcount) 1808 1723 { 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 1836 1837 1838 1839 1840 1841 1842 1843 1844 1845 1724 char *self_s, *result_s, *start, *end, *next; 1725 Py_ssize_t self_len; 1726 PyByteArrayObject *result; 1727 1728 /* The result string will be the same size */ 1729 self_s = PyByteArray_AS_STRING(self); 1730 self_len = PyByteArray_GET_SIZE(self); 1731 1732 next = findchar(self_s, self_len, from_c); 1733 1734 if (next == NULL) { 1735 /* No matches; return the original bytes */ 1736 return return_self(self); 1737 } 1738 1739 /* Need to make a new bytes */ 1740 result = (PyByteArrayObject *) PyByteArray_FromStringAndSize(NULL, self_len); 1741 if (result == NULL) 1742 return NULL; 1743 result_s = PyByteArray_AS_STRING(result); 1744 Py_MEMCPY(result_s, self_s, self_len); 1745 1746 /* change everything in-place, starting with this one */ 1747 start = result_s + (next-self_s); 1748 *start = to_c; 1749 start++; 1750 end = result_s + self_len; 1751 1752 while (--maxcount > 0) { 1753 next = findchar(start, end-start, from_c); 1754 if (next == NULL) 1755 break; 1756 *next = to_c; 1757 start = next+1; 1758 } 1759 1760 return result; 1846 1761 } 1847 1762 … … 1863 1778 self_len = PyByteArray_GET_SIZE(self); 1864 1779 1865 offset = findstring(self_s, self_len,1866 from_s, from_len,1867 0, self_len, FORWARD);1780 offset = stringlib_find(self_s, self_len, 1781 from_s, from_len, 1782 0); 1868 1783 if (offset == -1) { 1869 1784 /* No matches; return the original bytes */ … … 1885 1800 1886 1801 while ( --maxcount > 0) { 1887 offset = findstring(start, end-start,1888 from_s, from_len,1889 0, end-start, FORWARD);1802 offset = stringlib_find(start, end-start, 1803 from_s, from_len, 1804 0); 1890 1805 if (offset==-1) 1891 1806 break; … … 1980 1895 self_len = PyByteArray_GET_SIZE(self); 1981 1896 1982 count = countstring(self_s, self_len, 1983 from_s, from_len, 1984 0, self_len, FORWARD, maxcount); 1897 count = stringlib_count(self_s, self_len, 1898 from_s, from_len, 1899 maxcount); 1900 1985 1901 if (count == 0) { 1986 1902 /* no matches, return unchanged */ … … 2009 1925 end = self_s + self_len; 2010 1926 while (count-- > 0) { 2011 offset = findstring(start, end-start,2012 from_s, from_len,2013 0, end-start, FORWARD);1927 offset = stringlib_find(start, end-start, 1928 from_s, from_len, 1929 0); 2014 1930 if (offset == -1) 2015 1931 break; … … 2115 2031 2116 2032 static PyObject * 2117 byte s_replace(PyByteArrayObject *self, PyObject *args)2033 bytearray_replace(PyByteArrayObject *self, PyObject *args) 2118 2034 { 2119 2035 Py_ssize_t count = -1; … … 2138 2054 PyBuffer_Release(&vto); 2139 2055 return res; 2140 }2141 2142 2143 /* Overallocate the initial list to reduce the number of reallocs for small2144 split sizes. Eg, "A A A A A A A A A A".split() (10 elements) has three2145 resizes, to sizes 4, 8, then 16. Most observed string splits are for human2146 text (roughly 11 words per line) and field delimited data (usually 1-102147 fields). For large strings the split algorithms are bandwidth limited2148 so increasing the preallocation likely will not improve things.*/2149 2150 #define MAX_PREALLOC 122151 2152 /* 5 splits gives 6 elements */2153 #define PREALLOC_SIZE(maxsplit) \2154 (maxsplit >= MAX_PREALLOC ? MAX_PREALLOC : maxsplit+1)2155 2156 #define SPLIT_APPEND(data, left, right) \2157 str = PyByteArray_FromStringAndSize((data) + (left), \2158 (right) - (left)); \2159 if (str == NULL) \2160 goto onError; \2161 if (PyList_Append(list, str)) { \2162 Py_DECREF(str); \2163 goto onError; \2164 } \2165 else \2166 Py_DECREF(str);2167 2168 #define SPLIT_ADD(data, left, right) { \2169 str = PyByteArray_FromStringAndSize((data) + (left), \2170 (right) - (left)); \2171 if (str == NULL) \2172 goto onError; \2173 if (count < MAX_PREALLOC) { \2174 PyList_SET_ITEM(list, count, str); \2175 } else { \2176 if (PyList_Append(list, str)) { \2177 Py_DECREF(str); \2178 goto onError; \2179 } \2180 else \2181 Py_DECREF(str); \2182 } \2183 count++; }2184 2185 /* Always force the list to the expected size. */2186 #define FIX_PREALLOC_SIZE(list) Py_SIZE(list) = count2187 2188 2189 Py_LOCAL_INLINE(PyObject *)2190 split_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount)2191 {2192 register Py_ssize_t i, j, count = 0;2193 PyObject *str;2194 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));2195 2196 if (list == NULL)2197 return NULL;2198 2199 i = j = 0;2200 while ((j < len) && (maxcount-- > 0)) {2201 for(; j < len; j++) {2202 /* I found that using memchr makes no difference */2203 if (s[j] == ch) {2204 SPLIT_ADD(s, i, j);2205 i = j = j + 1;2206 break;2207 }2208 }2209 }2210 if (i <= len) {2211 SPLIT_ADD(s, i, len);2212 }2213 FIX_PREALLOC_SIZE(list);2214 return list;2215 2216 onError:2217 Py_DECREF(list);2218 return NULL;2219 }2220 2221 2222 Py_LOCAL_INLINE(PyObject *)2223 split_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount)2224 {2225 register Py_ssize_t i, j, count = 0;2226 PyObject *str;2227 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount));2228 2229 if (list == NULL)2230 return NULL;2231 2232 for (i = j = 0; i < len; ) {2233 /* find a token */2234 while (i < len && ISSPACE(s[i]))2235 i++;2236 j = i;2237 while (i < len && !ISSPACE(s[i]))2238 i++;2239 if (j < i) {2240 if (maxcount-- <= 0)2241 break;2242 SPLIT_ADD(s, j, i);2243 while (i < len && ISSPACE(s[i]))2244 i++;2245 j = i;2246 }2247 }2248 if (j < len) {2249 SPLIT_ADD(s, j, len);2250 }2251 FIX_PREALLOC_SIZE(list);2252 return list;2253 2254 onError:2255 Py_DECREF(list);2256 return NULL;2257 2056 } 2258 2057 … … 2266 2065 2267 2066 static PyObject * 2268 byte s_split(PyByteArrayObject *self, PyObject *args)2269 { 2270 Py_ssize_t len = PyByteArray_GET_SIZE(self), n , i, j;2271 Py_ssize_t maxsplit = -1 , count = 0;2067 bytearray_split(PyByteArrayObject *self, PyObject *args) 2068 { 2069 Py_ssize_t len = PyByteArray_GET_SIZE(self), n; 2070 Py_ssize_t maxsplit = -1; 2272 2071 const char *s = PyByteArray_AS_STRING(self), *sub; 2273 PyObject *list, *s tr, *subobj = Py_None;2072 PyObject *list, *subobj = Py_None; 2274 2073 Py_buffer vsub; 2275 #ifdef USE_FAST2276 Py_ssize_t pos;2277 #endif2278 2074 2279 2075 if (!PyArg_ParseTuple(args, "|On:split", &subobj, &maxsplit)) … … 2283 2079 2284 2080 if (subobj == Py_None) 2285 return s plit_whitespace(s, len, maxsplit);2081 return stringlib_split_whitespace((PyObject*) self, s, len, maxsplit); 2286 2082 2287 2083 if (_getbuffer(subobj, &vsub) < 0) … … 2290 2086 n = vsub.len; 2291 2087 2292 if (n == 0) { 2293 PyErr_SetString(PyExc_ValueError, "empty separator"); 2294 PyBuffer_Release(&vsub); 2295 return NULL; 2296 } 2297 if (n == 1) { 2298 list = split_char(s, len, sub[0], maxsplit); 2299 PyBuffer_Release(&vsub); 2300 return list; 2301 } 2302 2303 list = PyList_New(PREALLOC_SIZE(maxsplit)); 2304 if (list == NULL) { 2305 PyBuffer_Release(&vsub); 2306 return NULL; 2307 } 2308 2309 #ifdef USE_FAST 2310 i = j = 0; 2311 while (maxsplit-- > 0) { 2312 pos = fastsearch(s+i, len-i, sub, n, FAST_SEARCH); 2313 if (pos < 0) 2314 break; 2315 j = i+pos; 2316 SPLIT_ADD(s, i, j); 2317 i = j + n; 2318 } 2319 #else 2320 i = j = 0; 2321 while ((j+n <= len) && (maxsplit-- > 0)) { 2322 for (; j+n <= len; j++) { 2323 if (Py_STRING_MATCH(s, j, sub, n)) { 2324 SPLIT_ADD(s, i, j); 2325 i = j = j + n; 2326 break; 2327 } 2328 } 2329 } 2330 #endif 2331 SPLIT_ADD(s, i, len); 2332 FIX_PREALLOC_SIZE(list); 2088 list = stringlib_split( 2089 (PyObject*) self, s, len, sub, n, maxsplit 2090 ); 2333 2091 PyBuffer_Release(&vsub); 2334 2092 return list; 2335 2336 onError:2337 Py_DECREF(list);2338 PyBuffer_Release(&vsub);2339 return NULL;2340 }2341 2342 /* stringlib's partition shares nullbytes in some cases.2343 undo this, we don't want the nullbytes to be shared. */2344 static PyObject *2345 make_nullbytes_unique(PyObject *result)2346 {2347 if (result != NULL) {2348 int i;2349 assert(PyTuple_Check(result));2350 assert(PyTuple_GET_SIZE(result) == 3);2351 for (i = 0; i < 3; i++) {2352 if (PyTuple_GET_ITEM(result, i) == (PyObject *)nullbytes) {2353 PyObject *new = PyByteArray_FromStringAndSize(NULL, 0);2354 if (new == NULL) {2355 Py_DECREF(result);2356 result = NULL;2357 break;2358 }2359 Py_DECREF(nullbytes);2360 PyTuple_SET_ITEM(result, i, new);2361 }2362 }2363 }2364 return result;2365 2093 } 2366 2094 … … 2373 2101 2374 2102 static PyObject * 2375 byte s_partition(PyByteArrayObject *self, PyObject *sep_obj)2103 bytearray_partition(PyByteArrayObject *self, PyObject *sep_obj) 2376 2104 { 2377 2105 PyObject *bytesep, *result; … … 2389 2117 2390 2118 Py_DECREF(bytesep); 2391 return make_nullbytes_unique(result);2119 return result; 2392 2120 } 2393 2121 … … 2401 2129 2402 2130 static PyObject * 2403 byte s_rpartition(PyByteArrayObject *self, PyObject *sep_obj)2131 bytearray_rpartition(PyByteArrayObject *self, PyObject *sep_obj) 2404 2132 { 2405 2133 PyObject *bytesep, *result; … … 2417 2145 2418 2146 Py_DECREF(bytesep); 2419 return make_nullbytes_unique(result); 2420 } 2421 2422 Py_LOCAL_INLINE(PyObject *) 2423 rsplit_char(const char *s, Py_ssize_t len, char ch, Py_ssize_t maxcount) 2424 { 2425 register Py_ssize_t i, j, count=0; 2426 PyObject *str; 2427 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); 2428 2429 if (list == NULL) 2430 return NULL; 2431 2432 i = j = len - 1; 2433 while ((i >= 0) && (maxcount-- > 0)) { 2434 for (; i >= 0; i--) { 2435 if (s[i] == ch) { 2436 SPLIT_ADD(s, i + 1, j + 1); 2437 j = i = i - 1; 2438 break; 2439 } 2440 } 2441 } 2442 if (j >= -1) { 2443 SPLIT_ADD(s, 0, j + 1); 2444 } 2445 FIX_PREALLOC_SIZE(list); 2446 if (PyList_Reverse(list) < 0) 2447 goto onError; 2448 2449 return list; 2450 2451 onError: 2452 Py_DECREF(list); 2453 return NULL; 2454 } 2455 2456 Py_LOCAL_INLINE(PyObject *) 2457 rsplit_whitespace(const char *s, Py_ssize_t len, Py_ssize_t maxcount) 2458 { 2459 register Py_ssize_t i, j, count = 0; 2460 PyObject *str; 2461 PyObject *list = PyList_New(PREALLOC_SIZE(maxcount)); 2462 2463 if (list == NULL) 2464 return NULL; 2465 2466 for (i = j = len - 1; i >= 0; ) { 2467 /* find a token */ 2468 while (i >= 0 && ISSPACE(s[i])) 2469 i--; 2470 j = i; 2471 while (i >= 0 && !ISSPACE(s[i])) 2472 i--; 2473 if (j > i) { 2474 if (maxcount-- <= 0) 2475 break; 2476 SPLIT_ADD(s, i + 1, j + 1); 2477 while (i >= 0 && ISSPACE(s[i])) 2478 i--; 2479 j = i; 2480 } 2481 } 2482 if (j >= 0) { 2483 SPLIT_ADD(s, 0, j + 1); 2484 } 2485 FIX_PREALLOC_SIZE(list); 2486 if (PyList_Reverse(list) < 0) 2487 goto onError; 2488 2489 return list; 2490 2491 onError: 2492 Py_DECREF(list); 2493 return NULL; 2147 return result; 2494 2148 } 2495 2149 … … 2504 2158 2505 2159 static PyObject * 2506 byte s_rsplit(PyByteArrayObject *self, PyObject *args)2507 { 2508 Py_ssize_t len = PyByteArray_GET_SIZE(self), n , i, j;2509 Py_ssize_t maxsplit = -1 , count = 0;2160 bytearray_rsplit(PyByteArrayObject *self, PyObject *args) 2161 { 2162 Py_ssize_t len = PyByteArray_GET_SIZE(self), n; 2163 Py_ssize_t maxsplit = -1; 2510 2164 const char *s = PyByteArray_AS_STRING(self), *sub; 2511 PyObject *list, *s tr, *subobj = Py_None;2165 PyObject *list, *subobj = Py_None; 2512 2166 Py_buffer vsub; 2513 2167 … … 2518 2172 2519 2173 if (subobj == Py_None) 2520 return rsplit_whitespace(s, len, maxsplit);2174 return stringlib_rsplit_whitespace((PyObject*) self, s, len, maxsplit); 2521 2175 2522 2176 if (_getbuffer(subobj, &vsub) < 0) … … 2525 2179 n = vsub.len; 2526 2180 2527 if (n == 0) { 2528 PyErr_SetString(PyExc_ValueError, "empty separator"); 2529 PyBuffer_Release(&vsub); 2530 return NULL; 2531 } 2532 else if (n == 1) { 2533 list = rsplit_char(s, len, sub[0], maxsplit); 2534 PyBuffer_Release(&vsub); 2535 return list; 2536 } 2537 2538 list = PyList_New(PREALLOC_SIZE(maxsplit)); 2539 if (list == NULL) { 2540 PyBuffer_Release(&vsub); 2541 return NULL; 2542 } 2543 2544 j = len; 2545 i = j - n; 2546 2547 while ( (i >= 0) && (maxsplit-- > 0) ) { 2548 for (; i>=0; i--) { 2549 if (Py_STRING_MATCH(s, i, sub, n)) { 2550 SPLIT_ADD(s, i + n, j); 2551 j = i; 2552 i -= n; 2553 break; 2554 } 2555 } 2556 } 2557 SPLIT_ADD(s, 0, j); 2558 FIX_PREALLOC_SIZE(list); 2559 if (PyList_Reverse(list) < 0) 2560 goto onError; 2181 list = stringlib_rsplit( 2182 (PyObject*) self, s, len, sub, n, maxsplit 2183 ); 2561 2184 PyBuffer_Release(&vsub); 2562 2185 return list; 2563 2564 onError:2565 Py_DECREF(list);2566 PyBuffer_Release(&vsub);2567 return NULL;2568 2186 } 2569 2187 … … 2573 2191 Reverse the order of the values in B in place."); 2574 2192 static PyObject * 2575 byte s_reverse(PyByteArrayObject *self, PyObject *unused)2193 bytearray_reverse(PyByteArrayObject *self, PyObject *unused) 2576 2194 { 2577 2195 char swap, *head, *tail; … … 2595 2213 Insert a single item into the bytearray before the given index."); 2596 2214 static PyObject * 2597 byte s_insert(PyByteArrayObject *self, PyObject *args)2215 bytearray_insert(PyByteArrayObject *self, PyObject *args) 2598 2216 { 2599 2217 PyObject *value; … … 2632 2250 Append a single item to the end of B."); 2633 2251 static PyObject * 2634 byte s_append(PyByteArrayObject *self, PyObject *arg)2252 bytearray_append(PyByteArrayObject *self, PyObject *arg) 2635 2253 { 2636 2254 int value; … … 2658 2276 end of B."); 2659 2277 static PyObject * 2660 byte s_extend(PyByteArrayObject *self, PyObject *arg)2661 { 2662 PyObject *it, *item, *byte s_obj;2278 bytearray_extend(PyByteArrayObject *self, PyObject *arg) 2279 { 2280 PyObject *it, *item, *bytearray_obj; 2663 2281 Py_ssize_t buf_size = 0, len = 0; 2664 2282 int value; 2665 2283 char *buf; 2666 2284 2667 /* byte s_setslice code only accepts something supporting PEP 3118. */2285 /* bytearray_setslice code only accepts something supporting PEP 3118. */ 2668 2286 if (PyObject_CheckBuffer(arg)) { 2669 if (byte s_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1)2287 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), arg) == -1) 2670 2288 return NULL; 2671 2289 … … 2677 2295 return NULL; 2678 2296 2679 /* Try to determine the length of the argument. 32 is a bitrary. */2297 /* Try to determine the length of the argument. 32 is arbitrary. */ 2680 2298 buf_size = _PyObject_LengthHint(arg, 32); 2681 2299 if (buf_size == -1) { … … 2684 2302 } 2685 2303 2686 bytes_obj = PyByteArray_FromStringAndSize(NULL, buf_size); 2687 if (bytes_obj == NULL) 2688 return NULL; 2689 buf = PyByteArray_AS_STRING(bytes_obj); 2304 bytearray_obj = PyByteArray_FromStringAndSize(NULL, buf_size); 2305 if (bytearray_obj == NULL) { 2306 Py_DECREF(it); 2307 return NULL; 2308 } 2309 buf = PyByteArray_AS_STRING(bytearray_obj); 2690 2310 2691 2311 while ((item = PyIter_Next(it)) != NULL) { … … 2693 2313 Py_DECREF(item); 2694 2314 Py_DECREF(it); 2695 Py_DECREF(byte s_obj);2315 Py_DECREF(bytearray_obj); 2696 2316 return NULL; 2697 2317 } … … 2701 2321 if (len >= buf_size) { 2702 2322 buf_size = len + (len >> 1) + 1; 2703 if (PyByteArray_Resize((PyObject *)byte s_obj, buf_size) < 0) {2323 if (PyByteArray_Resize((PyObject *)bytearray_obj, buf_size) < 0) { 2704 2324 Py_DECREF(it); 2705 Py_DECREF(byte s_obj);2325 Py_DECREF(bytearray_obj); 2706 2326 return NULL; 2707 2327 } 2708 2328 /* Recompute the `buf' pointer, since the resizing operation may 2709 2329 have invalidated it. */ 2710 buf = PyByteArray_AS_STRING(byte s_obj);2330 buf = PyByteArray_AS_STRING(bytearray_obj); 2711 2331 } 2712 2332 } … … 2714 2334 2715 2335 /* Resize down to exact size. */ 2716 if (PyByteArray_Resize((PyObject *)bytes_obj, len) < 0) { 2717 Py_DECREF(bytes_obj); 2718 return NULL; 2719 } 2720 2721 if (bytes_setslice(self, Py_SIZE(self), Py_SIZE(self), bytes_obj) == -1) 2722 return NULL; 2723 Py_DECREF(bytes_obj); 2336 if (PyByteArray_Resize((PyObject *)bytearray_obj, len) < 0) { 2337 Py_DECREF(bytearray_obj); 2338 return NULL; 2339 } 2340 2341 if (bytearray_setslice(self, Py_SIZE(self), Py_SIZE(self), bytearray_obj) == -1) { 2342 Py_DECREF(bytearray_obj); 2343 return NULL; 2344 } 2345 Py_DECREF(bytearray_obj); 2724 2346 2725 2347 Py_RETURN_NONE; … … 2732 2354 argument is given, will pop the last value."); 2733 2355 static PyObject * 2734 byte s_pop(PyByteArrayObject *self, PyObject *args)2356 bytearray_pop(PyByteArrayObject *self, PyObject *args) 2735 2357 { 2736 2358 int value; … … 2741 2363 2742 2364 if (n == 0) { 2743 PyErr_SetString(PyExc_ OverflowError,2744 " cannot pop anempty bytearray");2365 PyErr_SetString(PyExc_IndexError, 2366 "pop from empty bytearray"); 2745 2367 return NULL; 2746 2368 } … … 2767 2389 Remove the first occurance of a value in B."); 2768 2390 static PyObject * 2769 byte s_remove(PyByteArrayObject *self, PyObject *arg)2391 bytearray_remove(PyByteArrayObject *self, PyObject *arg) 2770 2392 { 2771 2393 int value; … … 2821 2443 If the argument is omitted, strip ASCII whitespace."); 2822 2444 static PyObject * 2823 byte s_strip(PyByteArrayObject *self, PyObject *args)2445 bytearray_strip(PyByteArrayObject *self, PyObject *args) 2824 2446 { 2825 2447 Py_ssize_t left, right, mysize, argsize; … … 2857 2479 If the argument is omitted, strip leading ASCII whitespace."); 2858 2480 static PyObject * 2859 byte s_lstrip(PyByteArrayObject *self, PyObject *args)2481 bytearray_lstrip(PyByteArrayObject *self, PyObject *args) 2860 2482 { 2861 2483 Py_ssize_t left, right, mysize, argsize; … … 2890 2512 If the argument is omitted, strip trailing ASCII whitespace."); 2891 2513 static PyObject * 2892 byte s_rstrip(PyByteArrayObject *self, PyObject *args)2514 bytearray_rstrip(PyByteArrayObject *self, PyObject *args) 2893 2515 { 2894 2516 Py_ssize_t left, right, mysize, argsize; … … 2928 2550 2929 2551 static PyObject * 2930 byte s_decode(PyObject *self, PyObject *args)2552 bytearray_decode(PyObject *self, PyObject *args, PyObject *kwargs) 2931 2553 { 2932 2554 const char *encoding = NULL; 2933 2555 const char *errors = NULL; 2934 2935 if (!PyArg_ParseTuple(args, "|ss:decode", &encoding, &errors)) 2936 return NULL; 2937 if (encoding == NULL) 2556 static char *kwlist[] = {"encoding", "errors", 0}; 2557 2558 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|ss:decode", kwlist, &encoding, &errors)) 2559 return NULL; 2560 if (encoding == NULL) { 2561 #ifdef Py_USING_UNICODE 2938 2562 encoding = PyUnicode_GetDefaultEncoding(); 2563 #else 2564 PyErr_SetString(PyExc_ValueError, "no encoding specified"); 2565 return NULL; 2566 #endif 2567 } 2939 2568 return PyCodec_Decode(self, encoding, errors); 2940 2569 } … … 2946 2575 2947 2576 static PyObject * 2948 byte s_alloc(PyByteArrayObject *self)2577 bytearray_alloc(PyByteArrayObject *self) 2949 2578 { 2950 2579 return PyInt_FromSsize_t(self->ob_alloc); … … 2957 2586 2958 2587 static PyObject * 2959 byte s_join(PyByteArrayObject *self, PyObject *it)2588 bytearray_join(PyByteArrayObject *self, PyObject *it) 2960 2589 { 2961 2590 PyObject *seq; … … 3026 2655 } 3027 2656 2657 PyDoc_STRVAR(splitlines__doc__, 2658 "B.splitlines(keepends=False) -> list of lines\n\ 2659 \n\ 2660 Return a list of the lines in B, breaking at line boundaries.\n\ 2661 Line breaks are not included in the resulting list unless keepends\n\ 2662 is given and true."); 2663 2664 static PyObject* 2665 bytearray_splitlines(PyObject *self, PyObject *args) 2666 { 2667 int keepends = 0; 2668 2669 if (!PyArg_ParseTuple(args, "|i:splitlines", &keepends)) 2670 return NULL; 2671 2672 return stringlib_splitlines( 2673 (PyObject*) self, PyByteArray_AS_STRING(self), 2674 PyByteArray_GET_SIZE(self), keepends 2675 ); 2676 } 2677 3028 2678 PyDoc_STRVAR(fromhex_doc, 3029 2679 "bytearray.fromhex(string) -> bytearray\n\ … … 3034 2684 3035 2685 static int 3036 hex_digit_to_int(Py_UNICODE c) 3037 { 3038 if (c >= 128) 3039 return -1; 3040 if (ISDIGIT(c)) 2686 hex_digit_to_int(char c) 2687 { 2688 if (Py_ISDIGIT(c)) 3041 2689 return c - '0'; 3042 2690 else { 3043 if ( ISUPPER(c))3044 c = TOLOWER(c);2691 if (Py_ISUPPER(c)) 2692 c = Py_TOLOWER(c); 3045 2693 if (c >= 'a' && c <= 'f') 3046 2694 return c - 'a' + 10; … … 3050 2698 3051 2699 static PyObject * 3052 byte s_fromhex(PyObject *cls, PyObject *args)3053 { 3054 PyObject *newbytes , *hexobj;2700 bytearray_fromhex(PyObject *cls, PyObject *args) 2701 { 2702 PyObject *newbytes; 3055 2703 char *buf; 3056 Py_UNICODE*hex;2704 char *hex; 3057 2705 Py_ssize_t hexlen, byteslen, i, j; 3058 2706 int top, bot; 3059 2707 3060 if (!PyArg_ParseTuple(args, "U:fromhex", &hexobj)) 3061 return NULL; 3062 assert(PyUnicode_Check(hexobj)); 3063 hexlen = PyUnicode_GET_SIZE(hexobj); 3064 hex = PyUnicode_AS_UNICODE(hexobj); 2708 if (!PyArg_ParseTuple(args, "s#:fromhex", &hex, &hexlen)) 2709 return NULL; 3065 2710 byteslen = hexlen/2; /* This overestimates if there are spaces */ 3066 2711 newbytes = PyByteArray_FromStringAndSize(NULL, byteslen); … … 3096 2741 3097 2742 static PyObject * 3098 byte s_reduce(PyByteArrayObject *self)2743 bytearray_reduce(PyByteArrayObject *self) 3099 2744 { 3100 2745 PyObject *latin1, *dict; 3101 2746 if (self->ob_bytes) 2747 #ifdef Py_USING_UNICODE 3102 2748 latin1 = PyUnicode_DecodeLatin1(self->ob_bytes, 3103 2749 Py_SIZE(self), NULL); 2750 #else 2751 latin1 = PyString_FromStringAndSize(self->ob_bytes, Py_SIZE(self)); 2752 #endif 3104 2753 else 2754 #ifdef Py_USING_UNICODE 3105 2755 latin1 = PyUnicode_FromString(""); 2756 #else 2757 latin1 = PyString_FromString(""); 2758 #endif 3106 2759 3107 2760 dict = PyObject_GetAttrString((PyObject *)self, "__dict__"); … … 3120 2773 Returns the size of B in memory, in bytes"); 3121 2774 static PyObject * 3122 byte s_sizeof(PyByteArrayObject *self)2775 bytearray_sizeof(PyByteArrayObject *self) 3123 2776 { 3124 2777 Py_ssize_t res; … … 3128 2781 } 3129 2782 3130 static PySequenceMethods byte s_as_sequence = {3131 (lenfunc)byte s_length, /* sq_length */2783 static PySequenceMethods bytearray_as_sequence = { 2784 (lenfunc)bytearray_length, /* sq_length */ 3132 2785 (binaryfunc)PyByteArray_Concat, /* sq_concat */ 3133 (ssizeargfunc)byte s_repeat, /* sq_repeat */3134 (ssizeargfunc)byte s_getitem, /* sq_item */3135 0, /* sq_slice */3136 (ssizeobjargproc)byte s_setitem, /* sq_ass_item */3137 0, /* sq_ass_slice */3138 (objobjproc)byte s_contains, /* sq_contains */3139 (binaryfunc)byte s_iconcat, /* sq_inplace_concat */3140 (ssizeargfunc)byte s_irepeat, /* sq_inplace_repeat */2786 (ssizeargfunc)bytearray_repeat, /* sq_repeat */ 2787 (ssizeargfunc)bytearray_getitem, /* sq_item */ 2788 0, /* sq_slice */ 2789 (ssizeobjargproc)bytearray_setitem, /* sq_ass_item */ 2790 0, /* sq_ass_slice */ 2791 (objobjproc)bytearray_contains, /* sq_contains */ 2792 (binaryfunc)bytearray_iconcat, /* sq_inplace_concat */ 2793 (ssizeargfunc)bytearray_irepeat, /* sq_inplace_repeat */ 3141 2794 }; 3142 2795 3143 static PyMappingMethods byte s_as_mapping = {3144 (lenfunc)byte s_length,3145 (binaryfunc)byte s_subscript,3146 (objobjargproc)byte s_ass_subscript,2796 static PyMappingMethods bytearray_as_mapping = { 2797 (lenfunc)bytearray_length, 2798 (binaryfunc)bytearray_subscript, 2799 (objobjargproc)bytearray_ass_subscript, 3147 2800 }; 3148 2801 3149 static PyBufferProcs byte s_as_buffer = {3150 (readbufferproc)byte s_buffer_getreadbuf,3151 (writebufferproc)byte s_buffer_getwritebuf,3152 (segcountproc)byte s_buffer_getsegcount,3153 (charbufferproc)byte s_buffer_getcharbuf,3154 (getbufferproc)byte s_getbuffer,3155 (releasebufferproc)byte s_releasebuffer,2802 static PyBufferProcs bytearray_as_buffer = { 2803 (readbufferproc)bytearray_buffer_getreadbuf, 2804 (writebufferproc)bytearray_buffer_getwritebuf, 2805 (segcountproc)bytearray_buffer_getsegcount, 2806 (charbufferproc)bytearray_buffer_getcharbuf, 2807 (getbufferproc)bytearray_getbuffer, 2808 (releasebufferproc)bytearray_releasebuffer, 3156 2809 }; 3157 2810 3158 2811 static PyMethodDef 3159 byte s_methods[] = {3160 {"__alloc__", (PyCFunction)byte s_alloc, METH_NOARGS, alloc_doc},3161 {"__reduce__", (PyCFunction)byte s_reduce, METH_NOARGS, reduce_doc},3162 {"__sizeof__", (PyCFunction)byte s_sizeof, METH_NOARGS, sizeof_doc},3163 {"append", (PyCFunction)byte s_append, METH_O, append__doc__},2812 bytearray_methods[] = { 2813 {"__alloc__", (PyCFunction)bytearray_alloc, METH_NOARGS, alloc_doc}, 2814 {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, reduce_doc}, 2815 {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, sizeof_doc}, 2816 {"append", (PyCFunction)bytearray_append, METH_O, append__doc__}, 3164 2817 {"capitalize", (PyCFunction)stringlib_capitalize, METH_NOARGS, 3165 2818 _Py_capitalize__doc__}, 3166 2819 {"center", (PyCFunction)stringlib_center, METH_VARARGS, center__doc__}, 3167 {"count", (PyCFunction)byte s_count, METH_VARARGS, count__doc__},3168 {"decode", (PyCFunction)byte s_decode, METH_VARARGS, decode_doc},3169 {"endswith", (PyCFunction)byte s_endswith, METH_VARARGS, endswith__doc__},2820 {"count", (PyCFunction)bytearray_count, METH_VARARGS, count__doc__}, 2821 {"decode", (PyCFunction)bytearray_decode, METH_VARARGS | METH_KEYWORDS, decode_doc}, 2822 {"endswith", (PyCFunction)bytearray_endswith, METH_VARARGS, endswith__doc__}, 3170 2823 {"expandtabs", (PyCFunction)stringlib_expandtabs, METH_VARARGS, 3171 2824 expandtabs__doc__}, 3172 {"extend", (PyCFunction)byte s_extend, METH_O, extend__doc__},3173 {"find", (PyCFunction)byte s_find, METH_VARARGS, find__doc__},3174 {"fromhex", (PyCFunction)byte s_fromhex, METH_VARARGS|METH_CLASS,2825 {"extend", (PyCFunction)bytearray_extend, METH_O, extend__doc__}, 2826 {"find", (PyCFunction)bytearray_find, METH_VARARGS, find__doc__}, 2827 {"fromhex", (PyCFunction)bytearray_fromhex, METH_VARARGS|METH_CLASS, 3175 2828 fromhex_doc}, 3176 {"index", (PyCFunction)byte s_index, METH_VARARGS, index__doc__},3177 {"insert", (PyCFunction)byte s_insert, METH_VARARGS, insert__doc__},2829 {"index", (PyCFunction)bytearray_index, METH_VARARGS, index__doc__}, 2830 {"insert", (PyCFunction)bytearray_insert, METH_VARARGS, insert__doc__}, 3178 2831 {"isalnum", (PyCFunction)stringlib_isalnum, METH_NOARGS, 3179 2832 _Py_isalnum__doc__}, … … 3190 2843 {"isupper", (PyCFunction)stringlib_isupper, METH_NOARGS, 3191 2844 _Py_isupper__doc__}, 3192 {"join", (PyCFunction)byte s_join, METH_O, join_doc},2845 {"join", (PyCFunction)bytearray_join, METH_O, join_doc}, 3193 2846 {"ljust", (PyCFunction)stringlib_ljust, METH_VARARGS, ljust__doc__}, 3194 2847 {"lower", (PyCFunction)stringlib_lower, METH_NOARGS, _Py_lower__doc__}, 3195 {"lstrip", (PyCFunction)byte s_lstrip, METH_VARARGS, lstrip__doc__},3196 {"partition", (PyCFunction)byte s_partition, METH_O, partition__doc__},3197 {"pop", (PyCFunction)byte s_pop, METH_VARARGS, pop__doc__},3198 {"remove", (PyCFunction)byte s_remove, METH_O, remove__doc__},3199 {"replace", (PyCFunction)byte s_replace, METH_VARARGS, replace__doc__},3200 {"reverse", (PyCFunction)byte s_reverse, METH_NOARGS, reverse__doc__},3201 {"rfind", (PyCFunction)byte s_rfind, METH_VARARGS, rfind__doc__},3202 {"rindex", (PyCFunction)byte s_rindex, METH_VARARGS, rindex__doc__},2848 {"lstrip", (PyCFunction)bytearray_lstrip, METH_VARARGS, lstrip__doc__}, 2849 {"partition", (PyCFunction)bytearray_partition, METH_O, partition__doc__}, 2850 {"pop", (PyCFunction)bytearray_pop, METH_VARARGS, pop__doc__}, 2851 {"remove", (PyCFunction)bytearray_remove, METH_O, remove__doc__}, 2852 {"replace", (PyCFunction)bytearray_replace, METH_VARARGS, replace__doc__}, 2853 {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, reverse__doc__}, 2854 {"rfind", (PyCFunction)bytearray_rfind, METH_VARARGS, rfind__doc__}, 2855 {"rindex", (PyCFunction)bytearray_rindex, METH_VARARGS, rindex__doc__}, 3203 2856 {"rjust", (PyCFunction)stringlib_rjust, METH_VARARGS, rjust__doc__}, 3204 {"rpartition", (PyCFunction)byte s_rpartition, METH_O, rpartition__doc__},3205 {"rsplit", (PyCFunction)byte s_rsplit, METH_VARARGS, rsplit__doc__},3206 {"rstrip", (PyCFunction)byte s_rstrip, METH_VARARGS, rstrip__doc__},3207 {"split", (PyCFunction)byte s_split, METH_VARARGS, split__doc__},3208 {"splitlines", (PyCFunction) stringlib_splitlines, METH_VARARGS,2857 {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, rpartition__doc__}, 2858 {"rsplit", (PyCFunction)bytearray_rsplit, METH_VARARGS, rsplit__doc__}, 2859 {"rstrip", (PyCFunction)bytearray_rstrip, METH_VARARGS, rstrip__doc__}, 2860 {"split", (PyCFunction)bytearray_split, METH_VARARGS, split__doc__}, 2861 {"splitlines", (PyCFunction)bytearray_splitlines, METH_VARARGS, 3209 2862 splitlines__doc__}, 3210 {"startswith", (PyCFunction)byte s_startswith, METH_VARARGS ,2863 {"startswith", (PyCFunction)bytearray_startswith, METH_VARARGS , 3211 2864 startswith__doc__}, 3212 {"strip", (PyCFunction)byte s_strip, METH_VARARGS, strip__doc__},2865 {"strip", (PyCFunction)bytearray_strip, METH_VARARGS, strip__doc__}, 3213 2866 {"swapcase", (PyCFunction)stringlib_swapcase, METH_NOARGS, 3214 2867 _Py_swapcase__doc__}, 3215 2868 {"title", (PyCFunction)stringlib_title, METH_NOARGS, _Py_title__doc__}, 3216 {"translate", (PyCFunction)byte s_translate, METH_VARARGS,2869 {"translate", (PyCFunction)bytearray_translate, METH_VARARGS, 3217 2870 translate__doc__}, 3218 2871 {"upper", (PyCFunction)stringlib_upper, METH_NOARGS, _Py_upper__doc__}, … … 3221 2874 }; 3222 2875 3223 PyDoc_STRVAR(byte s_doc,2876 PyDoc_STRVAR(bytearray_doc, 3224 2877 "bytearray(iterable_of_ints) -> bytearray.\n\ 3225 2878 bytearray(string, encoding[, errors]) -> bytearray.\n\ … … 3238 2891 3239 2892 3240 static PyObject *byte s_iter(PyObject *seq);2893 static PyObject *bytearray_iter(PyObject *seq); 3241 2894 3242 2895 PyTypeObject PyByteArray_Type = { … … 3245 2898 sizeof(PyByteArrayObject), 3246 2899 0, 3247 (destructor)byte s_dealloc,/* tp_dealloc */2900 (destructor)bytearray_dealloc, /* tp_dealloc */ 3248 2901 0, /* tp_print */ 3249 2902 0, /* tp_getattr */ 3250 2903 0, /* tp_setattr */ 3251 2904 0, /* tp_compare */ 3252 (reprfunc)byte s_repr,/* tp_repr */2905 (reprfunc)bytearray_repr, /* tp_repr */ 3253 2906 0, /* tp_as_number */ 3254 &byte s_as_sequence,/* tp_as_sequence */3255 &byte s_as_mapping,/* tp_as_mapping */2907 &bytearray_as_sequence, /* tp_as_sequence */ 2908 &bytearray_as_mapping, /* tp_as_mapping */ 3256 2909 0, /* tp_hash */ 3257 2910 0, /* tp_call */ 3258 byte s_str,/* tp_str */2911 bytearray_str, /* tp_str */ 3259 2912 PyObject_GenericGetAttr, /* tp_getattro */ 3260 2913 0, /* tp_setattro */ 3261 &byte s_as_buffer,/* tp_as_buffer */2914 &bytearray_as_buffer, /* tp_as_buffer */ 3262 2915 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE | 3263 2916 Py_TPFLAGS_HAVE_NEWBUFFER, /* tp_flags */ 3264 byte s_doc,/* tp_doc */2917 bytearray_doc, /* tp_doc */ 3265 2918 0, /* tp_traverse */ 3266 2919 0, /* tp_clear */ 3267 (richcmpfunc)byte s_richcompare,/* tp_richcompare */2920 (richcmpfunc)bytearray_richcompare, /* tp_richcompare */ 3268 2921 0, /* tp_weaklistoffset */ 3269 byte s_iter,/* tp_iter */2922 bytearray_iter, /* tp_iter */ 3270 2923 0, /* tp_iternext */ 3271 byte s_methods,/* tp_methods */2924 bytearray_methods, /* tp_methods */ 3272 2925 0, /* tp_members */ 3273 2926 0, /* tp_getset */ … … 3277 2930 0, /* tp_descr_set */ 3278 2931 0, /* tp_dictoffset */ 3279 (initproc)byte s_init,/* tp_init */2932 (initproc)bytearray_init, /* tp_init */ 3280 2933 PyType_GenericAlloc, /* tp_alloc */ 3281 2934 PyType_GenericNew, /* tp_new */ … … 3292 2945 3293 2946 static void 3294 byte siter_dealloc(bytesiterobject *it)2947 bytearrayiter_dealloc(bytesiterobject *it) 3295 2948 { 3296 2949 _PyObject_GC_UNTRACK(it); … … 3300 2953 3301 2954 static int 3302 byte siter_traverse(bytesiterobject *it, visitproc visit, void *arg)2955 bytearrayiter_traverse(bytesiterobject *it, visitproc visit, void *arg) 3303 2956 { 3304 2957 Py_VISIT(it->it_seq); … … 3307 2960 3308 2961 static PyObject * 3309 byte siter_next(bytesiterobject *it)2962 bytearrayiter_next(bytesiterobject *it) 3310 2963 { 3311 2964 PyByteArrayObject *seq; … … 3332 2985 3333 2986 static PyObject * 3334 bytes iter_length_hint(bytesiterobject *it)2987 bytesarrayiter_length_hint(bytesiterobject *it) 3335 2988 { 3336 2989 Py_ssize_t len = 0; … … 3343 2996 "Private method returning an estimate of len(list(it))."); 3344 2997 3345 static PyMethodDef byte siter_methods[] = {3346 {"__length_hint__", (PyCFunction)bytes iter_length_hint, METH_NOARGS,2998 static PyMethodDef bytearrayiter_methods[] = { 2999 {"__length_hint__", (PyCFunction)bytesarrayiter_length_hint, METH_NOARGS, 3347 3000 length_hint_doc}, 3348 3001 {NULL, NULL} /* sentinel */ … … 3355 3008 0, /* tp_itemsize */ 3356 3009 /* methods */ 3357 (destructor)byte siter_dealloc,/* tp_dealloc */3010 (destructor)bytearrayiter_dealloc, /* tp_dealloc */ 3358 3011 0, /* tp_print */ 3359 3012 0, /* tp_getattr */ … … 3372 3025 Py_TPFLAGS_DEFAULT | Py_TPFLAGS_HAVE_GC, /* tp_flags */ 3373 3026 0, /* tp_doc */ 3374 (traverseproc)byte siter_traverse, /* tp_traverse */3027 (traverseproc)bytearrayiter_traverse, /* tp_traverse */ 3375 3028 0, /* tp_clear */ 3376 3029 0, /* tp_richcompare */ 3377 3030 0, /* tp_weaklistoffset */ 3378 3031 PyObject_SelfIter, /* tp_iter */ 3379 (iternextfunc)byte siter_next,/* tp_iternext */3380 byte siter_methods,/* tp_methods */3032 (iternextfunc)bytearrayiter_next, /* tp_iternext */ 3033 bytearrayiter_methods, /* tp_methods */ 3381 3034 0, 3382 3035 }; 3383 3036 3384 3037 static PyObject * 3385 byte s_iter(PyObject *seq)3038 bytearray_iter(PyObject *seq) 3386 3039 { 3387 3040 bytesiterobject *it;
Note:
See TracChangeset
for help on using the changeset viewer.