Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
| 5 | PyDoc_STRVAR(bytearray_clear__doc__, |
| 6 | "clear($self, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Remove all items from the bytearray."); |
| 10 | |
| 11 | #define BYTEARRAY_CLEAR_METHODDEF \ |
| 12 | {"clear", (PyCFunction)bytearray_clear, METH_NOARGS, bytearray_clear__doc__}, |
| 13 | |
| 14 | static PyObject * |
| 15 | bytearray_clear_impl(PyByteArrayObject *self); |
| 16 | |
| 17 | static PyObject * |
| 18 | bytearray_clear(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 19 | { |
| 20 | return bytearray_clear_impl(self); |
| 21 | } |
| 22 | |
| 23 | PyDoc_STRVAR(bytearray_copy__doc__, |
| 24 | "copy($self, /)\n" |
| 25 | "--\n" |
| 26 | "\n" |
| 27 | "Return a copy of B."); |
| 28 | |
| 29 | #define BYTEARRAY_COPY_METHODDEF \ |
| 30 | {"copy", (PyCFunction)bytearray_copy, METH_NOARGS, bytearray_copy__doc__}, |
| 31 | |
| 32 | static PyObject * |
| 33 | bytearray_copy_impl(PyByteArrayObject *self); |
| 34 | |
| 35 | static PyObject * |
| 36 | bytearray_copy(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 37 | { |
| 38 | return bytearray_copy_impl(self); |
| 39 | } |
| 40 | |
| 41 | PyDoc_STRVAR(bytearray_translate__doc__, |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 42 | "translate($self, table, /, delete=b\'\')\n" |
| 43 | "--\n" |
| 44 | "\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 45 | "Return a copy with each character mapped by the given translation table.\n" |
| 46 | "\n" |
| 47 | " table\n" |
| 48 | " Translation table, which must be a bytes object of length 256.\n" |
| 49 | "\n" |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 50 | "All characters occurring in the optional argument delete are removed.\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 51 | "The remaining characters are mapped through the given translation table."); |
| 52 | |
| 53 | #define BYTEARRAY_TRANSLATE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 54 | {"translate", (PyCFunction)(void(*)(void))bytearray_translate, METH_FASTCALL|METH_KEYWORDS, bytearray_translate__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 55 | |
| 56 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 57 | bytearray_translate_impl(PyByteArrayObject *self, PyObject *table, |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 58 | PyObject *deletechars); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 59 | |
| 60 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 61 | bytearray_translate(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 62 | { |
| 63 | PyObject *return_value = NULL; |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 64 | static const char * const _keywords[] = {"", "delete", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 65 | static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0}; |
| 66 | PyObject *argsbuf[2]; |
| 67 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 68 | PyObject *table; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 69 | PyObject *deletechars = NULL; |
| 70 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 71 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
| 72 | if (!args) { |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 73 | goto exit; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 74 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 75 | table = args[0]; |
| 76 | if (!noptargs) { |
| 77 | goto skip_optional_pos; |
| 78 | } |
| 79 | deletechars = args[1]; |
| 80 | skip_optional_pos: |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 81 | return_value = bytearray_translate_impl(self, table, deletechars); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 82 | |
| 83 | exit: |
| 84 | return return_value; |
| 85 | } |
| 86 | |
| 87 | PyDoc_STRVAR(bytearray_maketrans__doc__, |
| 88 | "maketrans(frm, to, /)\n" |
| 89 | "--\n" |
| 90 | "\n" |
| 91 | "Return a translation table useable for the bytes or bytearray translate method.\n" |
| 92 | "\n" |
| 93 | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
| 94 | "the same position in to.\n" |
| 95 | "\n" |
| 96 | "The bytes objects frm and to must be of the same length."); |
| 97 | |
| 98 | #define BYTEARRAY_MAKETRANS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 99 | {"maketrans", (PyCFunction)(void(*)(void))bytearray_maketrans, METH_FASTCALL|METH_STATIC, bytearray_maketrans__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 100 | |
| 101 | static PyObject * |
| 102 | bytearray_maketrans_impl(Py_buffer *frm, Py_buffer *to); |
| 103 | |
| 104 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 105 | bytearray_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 106 | { |
| 107 | PyObject *return_value = NULL; |
| 108 | Py_buffer frm = {NULL, NULL}; |
| 109 | Py_buffer to = {NULL, NULL}; |
| 110 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 111 | if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) { |
| 112 | goto exit; |
| 113 | } |
| 114 | if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) { |
| 115 | goto exit; |
| 116 | } |
| 117 | if (!PyBuffer_IsContiguous(&frm, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 118 | _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 119 | goto exit; |
| 120 | } |
| 121 | if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) { |
| 122 | goto exit; |
| 123 | } |
| 124 | if (!PyBuffer_IsContiguous(&to, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 125 | _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]); |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 126 | goto exit; |
| 127 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 128 | return_value = bytearray_maketrans_impl(&frm, &to); |
| 129 | |
| 130 | exit: |
| 131 | /* Cleanup for frm */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 132 | if (frm.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 133 | PyBuffer_Release(&frm); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 134 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 135 | /* Cleanup for to */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 136 | if (to.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 137 | PyBuffer_Release(&to); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 138 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 139 | |
| 140 | return return_value; |
| 141 | } |
| 142 | |
| 143 | PyDoc_STRVAR(bytearray_replace__doc__, |
| 144 | "replace($self, old, new, count=-1, /)\n" |
| 145 | "--\n" |
| 146 | "\n" |
| 147 | "Return a copy with all occurrences of substring old replaced by new.\n" |
| 148 | "\n" |
| 149 | " count\n" |
| 150 | " Maximum number of occurrences to replace.\n" |
| 151 | " -1 (the default value) means replace all occurrences.\n" |
| 152 | "\n" |
| 153 | "If the optional argument count is given, only the first count occurrences are\n" |
| 154 | "replaced."); |
| 155 | |
| 156 | #define BYTEARRAY_REPLACE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 157 | {"replace", (PyCFunction)(void(*)(void))bytearray_replace, METH_FASTCALL, bytearray_replace__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 158 | |
| 159 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 160 | bytearray_replace_impl(PyByteArrayObject *self, Py_buffer *old, |
| 161 | Py_buffer *new, Py_ssize_t count); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 162 | |
| 163 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 164 | bytearray_replace(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 165 | { |
| 166 | PyObject *return_value = NULL; |
| 167 | Py_buffer old = {NULL, NULL}; |
| 168 | Py_buffer new = {NULL, NULL}; |
| 169 | Py_ssize_t count = -1; |
| 170 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 171 | if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 172 | goto exit; |
| 173 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 174 | if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) { |
| 175 | goto exit; |
| 176 | } |
| 177 | if (!PyBuffer_IsContiguous(&old, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 178 | _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 179 | goto exit; |
| 180 | } |
| 181 | if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) { |
| 182 | goto exit; |
| 183 | } |
| 184 | if (!PyBuffer_IsContiguous(&new, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 185 | _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 186 | goto exit; |
| 187 | } |
| 188 | if (nargs < 3) { |
| 189 | goto skip_optional; |
| 190 | } |
| 191 | if (PyFloat_Check(args[2])) { |
| 192 | PyErr_SetString(PyExc_TypeError, |
| 193 | "integer argument expected, got float" ); |
| 194 | goto exit; |
| 195 | } |
| 196 | { |
| 197 | Py_ssize_t ival = -1; |
| 198 | PyObject *iobj = PyNumber_Index(args[2]); |
| 199 | if (iobj != NULL) { |
| 200 | ival = PyLong_AsSsize_t(iobj); |
| 201 | Py_DECREF(iobj); |
| 202 | } |
| 203 | if (ival == -1 && PyErr_Occurred()) { |
| 204 | goto exit; |
| 205 | } |
| 206 | count = ival; |
| 207 | } |
| 208 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 209 | return_value = bytearray_replace_impl(self, &old, &new, count); |
| 210 | |
| 211 | exit: |
| 212 | /* Cleanup for old */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 213 | if (old.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 214 | PyBuffer_Release(&old); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 215 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 216 | /* Cleanup for new */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 217 | if (new.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 218 | PyBuffer_Release(&new); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 219 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 220 | |
| 221 | return return_value; |
| 222 | } |
| 223 | |
| 224 | PyDoc_STRVAR(bytearray_split__doc__, |
| 225 | "split($self, /, sep=None, maxsplit=-1)\n" |
| 226 | "--\n" |
| 227 | "\n" |
| 228 | "Return a list of the sections in the bytearray, using sep as the delimiter.\n" |
| 229 | "\n" |
| 230 | " sep\n" |
| 231 | " The delimiter according which to split the bytearray.\n" |
| 232 | " None (the default value) means split on ASCII whitespace characters\n" |
| 233 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 234 | " maxsplit\n" |
| 235 | " Maximum number of splits to do.\n" |
| 236 | " -1 (the default value) means no limit."); |
| 237 | |
| 238 | #define BYTEARRAY_SPLIT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 239 | {"split", (PyCFunction)(void(*)(void))bytearray_split, METH_FASTCALL|METH_KEYWORDS, bytearray_split__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 240 | |
| 241 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 242 | bytearray_split_impl(PyByteArrayObject *self, PyObject *sep, |
| 243 | Py_ssize_t maxsplit); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 244 | |
| 245 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 246 | bytearray_split(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 247 | { |
| 248 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 249 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 250 | static _PyArg_Parser _parser = {NULL, _keywords, "split", 0}; |
| 251 | PyObject *argsbuf[2]; |
| 252 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 253 | PyObject *sep = Py_None; |
| 254 | Py_ssize_t maxsplit = -1; |
| 255 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 256 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 257 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 258 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 259 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 260 | if (!noptargs) { |
| 261 | goto skip_optional_pos; |
| 262 | } |
| 263 | if (args[0]) { |
| 264 | sep = args[0]; |
| 265 | if (!--noptargs) { |
| 266 | goto skip_optional_pos; |
| 267 | } |
| 268 | } |
| 269 | if (PyFloat_Check(args[1])) { |
| 270 | PyErr_SetString(PyExc_TypeError, |
| 271 | "integer argument expected, got float" ); |
| 272 | goto exit; |
| 273 | } |
| 274 | { |
| 275 | Py_ssize_t ival = -1; |
| 276 | PyObject *iobj = PyNumber_Index(args[1]); |
| 277 | if (iobj != NULL) { |
| 278 | ival = PyLong_AsSsize_t(iobj); |
| 279 | Py_DECREF(iobj); |
| 280 | } |
| 281 | if (ival == -1 && PyErr_Occurred()) { |
| 282 | goto exit; |
| 283 | } |
| 284 | maxsplit = ival; |
| 285 | } |
| 286 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 287 | return_value = bytearray_split_impl(self, sep, maxsplit); |
| 288 | |
| 289 | exit: |
| 290 | return return_value; |
| 291 | } |
| 292 | |
| 293 | PyDoc_STRVAR(bytearray_partition__doc__, |
| 294 | "partition($self, sep, /)\n" |
| 295 | "--\n" |
| 296 | "\n" |
| 297 | "Partition the bytearray into three parts using the given separator.\n" |
| 298 | "\n" |
| 299 | "This will search for the separator sep in the bytearray. If the separator is\n" |
| 300 | "found, returns a 3-tuple containing the part before the separator, the\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 301 | "separator itself, and the part after it as new bytearray objects.\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 302 | "\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 303 | "If the separator is not found, returns a 3-tuple containing the copy of the\n" |
| 304 | "original bytearray object and two empty bytearray objects."); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 305 | |
| 306 | #define BYTEARRAY_PARTITION_METHODDEF \ |
| 307 | {"partition", (PyCFunction)bytearray_partition, METH_O, bytearray_partition__doc__}, |
| 308 | |
| 309 | PyDoc_STRVAR(bytearray_rpartition__doc__, |
| 310 | "rpartition($self, sep, /)\n" |
| 311 | "--\n" |
| 312 | "\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 313 | "Partition the bytearray into three parts using the given separator.\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 314 | "\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 315 | "This will search for the separator sep in the bytearray, starting at the end.\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 316 | "If the separator is found, returns a 3-tuple containing the part before the\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 317 | "separator, the separator itself, and the part after it as new bytearray\n" |
| 318 | "objects.\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 319 | "\n" |
| 320 | "If the separator is not found, returns a 3-tuple containing two empty bytearray\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 321 | "objects and the copy of the original bytearray object."); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 322 | |
| 323 | #define BYTEARRAY_RPARTITION_METHODDEF \ |
| 324 | {"rpartition", (PyCFunction)bytearray_rpartition, METH_O, bytearray_rpartition__doc__}, |
| 325 | |
| 326 | PyDoc_STRVAR(bytearray_rsplit__doc__, |
| 327 | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
| 328 | "--\n" |
| 329 | "\n" |
| 330 | "Return a list of the sections in the bytearray, using sep as the delimiter.\n" |
| 331 | "\n" |
| 332 | " sep\n" |
| 333 | " The delimiter according which to split the bytearray.\n" |
| 334 | " None (the default value) means split on ASCII whitespace characters\n" |
| 335 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 336 | " maxsplit\n" |
| 337 | " Maximum number of splits to do.\n" |
| 338 | " -1 (the default value) means no limit.\n" |
| 339 | "\n" |
| 340 | "Splitting is done starting at the end of the bytearray and working to the front."); |
| 341 | |
| 342 | #define BYTEARRAY_RSPLIT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 343 | {"rsplit", (PyCFunction)(void(*)(void))bytearray_rsplit, METH_FASTCALL|METH_KEYWORDS, bytearray_rsplit__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 344 | |
| 345 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 346 | bytearray_rsplit_impl(PyByteArrayObject *self, PyObject *sep, |
| 347 | Py_ssize_t maxsplit); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 348 | |
| 349 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 350 | bytearray_rsplit(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 351 | { |
| 352 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 353 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 354 | static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0}; |
| 355 | PyObject *argsbuf[2]; |
| 356 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 357 | PyObject *sep = Py_None; |
| 358 | Py_ssize_t maxsplit = -1; |
| 359 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 360 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 361 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 362 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 363 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 364 | if (!noptargs) { |
| 365 | goto skip_optional_pos; |
| 366 | } |
| 367 | if (args[0]) { |
| 368 | sep = args[0]; |
| 369 | if (!--noptargs) { |
| 370 | goto skip_optional_pos; |
| 371 | } |
| 372 | } |
| 373 | if (PyFloat_Check(args[1])) { |
| 374 | PyErr_SetString(PyExc_TypeError, |
| 375 | "integer argument expected, got float" ); |
| 376 | goto exit; |
| 377 | } |
| 378 | { |
| 379 | Py_ssize_t ival = -1; |
| 380 | PyObject *iobj = PyNumber_Index(args[1]); |
| 381 | if (iobj != NULL) { |
| 382 | ival = PyLong_AsSsize_t(iobj); |
| 383 | Py_DECREF(iobj); |
| 384 | } |
| 385 | if (ival == -1 && PyErr_Occurred()) { |
| 386 | goto exit; |
| 387 | } |
| 388 | maxsplit = ival; |
| 389 | } |
| 390 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 391 | return_value = bytearray_rsplit_impl(self, sep, maxsplit); |
| 392 | |
| 393 | exit: |
| 394 | return return_value; |
| 395 | } |
| 396 | |
| 397 | PyDoc_STRVAR(bytearray_reverse__doc__, |
| 398 | "reverse($self, /)\n" |
| 399 | "--\n" |
| 400 | "\n" |
| 401 | "Reverse the order of the values in B in place."); |
| 402 | |
| 403 | #define BYTEARRAY_REVERSE_METHODDEF \ |
| 404 | {"reverse", (PyCFunction)bytearray_reverse, METH_NOARGS, bytearray_reverse__doc__}, |
| 405 | |
| 406 | static PyObject * |
| 407 | bytearray_reverse_impl(PyByteArrayObject *self); |
| 408 | |
| 409 | static PyObject * |
| 410 | bytearray_reverse(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 411 | { |
| 412 | return bytearray_reverse_impl(self); |
| 413 | } |
| 414 | |
| 415 | PyDoc_STRVAR(bytearray_insert__doc__, |
| 416 | "insert($self, index, item, /)\n" |
| 417 | "--\n" |
| 418 | "\n" |
| 419 | "Insert a single item into the bytearray before the given index.\n" |
| 420 | "\n" |
| 421 | " index\n" |
| 422 | " The index where the value is to be inserted.\n" |
| 423 | " item\n" |
| 424 | " The item to be inserted."); |
| 425 | |
| 426 | #define BYTEARRAY_INSERT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 427 | {"insert", (PyCFunction)(void(*)(void))bytearray_insert, METH_FASTCALL, bytearray_insert__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 428 | |
| 429 | static PyObject * |
| 430 | bytearray_insert_impl(PyByteArrayObject *self, Py_ssize_t index, int item); |
| 431 | |
| 432 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 433 | bytearray_insert(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 434 | { |
| 435 | PyObject *return_value = NULL; |
| 436 | Py_ssize_t index; |
| 437 | int item; |
| 438 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 439 | if (!_PyArg_CheckPositional("insert", nargs, 2, 2)) { |
| 440 | goto exit; |
| 441 | } |
| 442 | if (PyFloat_Check(args[0])) { |
| 443 | PyErr_SetString(PyExc_TypeError, |
| 444 | "integer argument expected, got float" ); |
| 445 | goto exit; |
| 446 | } |
| 447 | { |
| 448 | Py_ssize_t ival = -1; |
| 449 | PyObject *iobj = PyNumber_Index(args[0]); |
| 450 | if (iobj != NULL) { |
| 451 | ival = PyLong_AsSsize_t(iobj); |
| 452 | Py_DECREF(iobj); |
| 453 | } |
| 454 | if (ival == -1 && PyErr_Occurred()) { |
| 455 | goto exit; |
| 456 | } |
| 457 | index = ival; |
| 458 | } |
| 459 | if (!_getbytevalue(args[1], &item)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 460 | goto exit; |
| 461 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 462 | return_value = bytearray_insert_impl(self, index, item); |
| 463 | |
| 464 | exit: |
| 465 | return return_value; |
| 466 | } |
| 467 | |
| 468 | PyDoc_STRVAR(bytearray_append__doc__, |
| 469 | "append($self, item, /)\n" |
| 470 | "--\n" |
| 471 | "\n" |
| 472 | "Append a single item to the end of the bytearray.\n" |
| 473 | "\n" |
| 474 | " item\n" |
| 475 | " The item to be appended."); |
| 476 | |
| 477 | #define BYTEARRAY_APPEND_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 478 | {"append", (PyCFunction)bytearray_append, METH_O, bytearray_append__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 479 | |
| 480 | static PyObject * |
| 481 | bytearray_append_impl(PyByteArrayObject *self, int item); |
| 482 | |
| 483 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 484 | bytearray_append(PyByteArrayObject *self, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 485 | { |
| 486 | PyObject *return_value = NULL; |
| 487 | int item; |
| 488 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 489 | if (!_getbytevalue(arg, &item)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 490 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 491 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 492 | return_value = bytearray_append_impl(self, item); |
| 493 | |
| 494 | exit: |
| 495 | return return_value; |
| 496 | } |
| 497 | |
| 498 | PyDoc_STRVAR(bytearray_extend__doc__, |
| 499 | "extend($self, iterable_of_ints, /)\n" |
| 500 | "--\n" |
| 501 | "\n" |
| 502 | "Append all the items from the iterator or sequence to the end of the bytearray.\n" |
| 503 | "\n" |
| 504 | " iterable_of_ints\n" |
| 505 | " The iterable of items to append."); |
| 506 | |
| 507 | #define BYTEARRAY_EXTEND_METHODDEF \ |
| 508 | {"extend", (PyCFunction)bytearray_extend, METH_O, bytearray_extend__doc__}, |
| 509 | |
| 510 | PyDoc_STRVAR(bytearray_pop__doc__, |
| 511 | "pop($self, index=-1, /)\n" |
| 512 | "--\n" |
| 513 | "\n" |
| 514 | "Remove and return a single item from B.\n" |
| 515 | "\n" |
| 516 | " index\n" |
| 517 | " The index from where to remove the item.\n" |
| 518 | " -1 (the default value) means remove the last item.\n" |
| 519 | "\n" |
| 520 | "If no index argument is given, will pop the last item."); |
| 521 | |
| 522 | #define BYTEARRAY_POP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 523 | {"pop", (PyCFunction)(void(*)(void))bytearray_pop, METH_FASTCALL, bytearray_pop__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 524 | |
| 525 | static PyObject * |
| 526 | bytearray_pop_impl(PyByteArrayObject *self, Py_ssize_t index); |
| 527 | |
| 528 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 529 | bytearray_pop(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 530 | { |
| 531 | PyObject *return_value = NULL; |
| 532 | Py_ssize_t index = -1; |
| 533 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 534 | if (!_PyArg_CheckPositional("pop", nargs, 0, 1)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 535 | goto exit; |
| 536 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 537 | if (nargs < 1) { |
| 538 | goto skip_optional; |
| 539 | } |
| 540 | if (PyFloat_Check(args[0])) { |
| 541 | PyErr_SetString(PyExc_TypeError, |
| 542 | "integer argument expected, got float" ); |
| 543 | goto exit; |
| 544 | } |
| 545 | { |
| 546 | Py_ssize_t ival = -1; |
| 547 | PyObject *iobj = PyNumber_Index(args[0]); |
| 548 | if (iobj != NULL) { |
| 549 | ival = PyLong_AsSsize_t(iobj); |
| 550 | Py_DECREF(iobj); |
| 551 | } |
| 552 | if (ival == -1 && PyErr_Occurred()) { |
| 553 | goto exit; |
| 554 | } |
| 555 | index = ival; |
| 556 | } |
| 557 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 558 | return_value = bytearray_pop_impl(self, index); |
| 559 | |
| 560 | exit: |
| 561 | return return_value; |
| 562 | } |
| 563 | |
| 564 | PyDoc_STRVAR(bytearray_remove__doc__, |
| 565 | "remove($self, value, /)\n" |
| 566 | "--\n" |
| 567 | "\n" |
| 568 | "Remove the first occurrence of a value in the bytearray.\n" |
| 569 | "\n" |
| 570 | " value\n" |
| 571 | " The value to remove."); |
| 572 | |
| 573 | #define BYTEARRAY_REMOVE_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 574 | {"remove", (PyCFunction)bytearray_remove, METH_O, bytearray_remove__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 575 | |
| 576 | static PyObject * |
| 577 | bytearray_remove_impl(PyByteArrayObject *self, int value); |
| 578 | |
| 579 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 580 | bytearray_remove(PyByteArrayObject *self, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 581 | { |
| 582 | PyObject *return_value = NULL; |
| 583 | int value; |
| 584 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 585 | if (!_getbytevalue(arg, &value)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 586 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 587 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 588 | return_value = bytearray_remove_impl(self, value); |
| 589 | |
| 590 | exit: |
| 591 | return return_value; |
| 592 | } |
| 593 | |
| 594 | PyDoc_STRVAR(bytearray_strip__doc__, |
| 595 | "strip($self, bytes=None, /)\n" |
| 596 | "--\n" |
| 597 | "\n" |
| 598 | "Strip leading and trailing bytes contained in the argument.\n" |
| 599 | "\n" |
| 600 | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
| 601 | |
| 602 | #define BYTEARRAY_STRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 603 | {"strip", (PyCFunction)(void(*)(void))bytearray_strip, METH_FASTCALL, bytearray_strip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 604 | |
| 605 | static PyObject * |
| 606 | bytearray_strip_impl(PyByteArrayObject *self, PyObject *bytes); |
| 607 | |
| 608 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 609 | bytearray_strip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 610 | { |
| 611 | PyObject *return_value = NULL; |
| 612 | PyObject *bytes = Py_None; |
| 613 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 614 | if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 615 | goto exit; |
| 616 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 617 | if (nargs < 1) { |
| 618 | goto skip_optional; |
| 619 | } |
| 620 | bytes = args[0]; |
| 621 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 622 | return_value = bytearray_strip_impl(self, bytes); |
| 623 | |
| 624 | exit: |
| 625 | return return_value; |
| 626 | } |
| 627 | |
| 628 | PyDoc_STRVAR(bytearray_lstrip__doc__, |
| 629 | "lstrip($self, bytes=None, /)\n" |
| 630 | "--\n" |
| 631 | "\n" |
| 632 | "Strip leading bytes contained in the argument.\n" |
| 633 | "\n" |
| 634 | "If the argument is omitted or None, strip leading ASCII whitespace."); |
| 635 | |
| 636 | #define BYTEARRAY_LSTRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 637 | {"lstrip", (PyCFunction)(void(*)(void))bytearray_lstrip, METH_FASTCALL, bytearray_lstrip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 638 | |
| 639 | static PyObject * |
| 640 | bytearray_lstrip_impl(PyByteArrayObject *self, PyObject *bytes); |
| 641 | |
| 642 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 643 | bytearray_lstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 644 | { |
| 645 | PyObject *return_value = NULL; |
| 646 | PyObject *bytes = Py_None; |
| 647 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 648 | if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 649 | goto exit; |
| 650 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 651 | if (nargs < 1) { |
| 652 | goto skip_optional; |
| 653 | } |
| 654 | bytes = args[0]; |
| 655 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 656 | return_value = bytearray_lstrip_impl(self, bytes); |
| 657 | |
| 658 | exit: |
| 659 | return return_value; |
| 660 | } |
| 661 | |
| 662 | PyDoc_STRVAR(bytearray_rstrip__doc__, |
| 663 | "rstrip($self, bytes=None, /)\n" |
| 664 | "--\n" |
| 665 | "\n" |
| 666 | "Strip trailing bytes contained in the argument.\n" |
| 667 | "\n" |
| 668 | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
| 669 | |
| 670 | #define BYTEARRAY_RSTRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 671 | {"rstrip", (PyCFunction)(void(*)(void))bytearray_rstrip, METH_FASTCALL, bytearray_rstrip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 672 | |
| 673 | static PyObject * |
| 674 | bytearray_rstrip_impl(PyByteArrayObject *self, PyObject *bytes); |
| 675 | |
| 676 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 677 | bytearray_rstrip(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 678 | { |
| 679 | PyObject *return_value = NULL; |
| 680 | PyObject *bytes = Py_None; |
| 681 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 682 | if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 683 | goto exit; |
| 684 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 685 | if (nargs < 1) { |
| 686 | goto skip_optional; |
| 687 | } |
| 688 | bytes = args[0]; |
| 689 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 690 | return_value = bytearray_rstrip_impl(self, bytes); |
| 691 | |
| 692 | exit: |
| 693 | return return_value; |
| 694 | } |
| 695 | |
| 696 | PyDoc_STRVAR(bytearray_decode__doc__, |
| 697 | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
| 698 | "--\n" |
| 699 | "\n" |
| 700 | "Decode the bytearray using the codec registered for encoding.\n" |
| 701 | "\n" |
| 702 | " encoding\n" |
| 703 | " The encoding with which to decode the bytearray.\n" |
| 704 | " errors\n" |
| 705 | " The error handling scheme to use for the handling of decoding errors.\n" |
| 706 | " The default is \'strict\' meaning that decoding errors raise a\n" |
| 707 | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
| 708 | " as well as any other name registered with codecs.register_error that\n" |
| 709 | " can handle UnicodeDecodeErrors."); |
| 710 | |
| 711 | #define BYTEARRAY_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 712 | {"decode", (PyCFunction)(void(*)(void))bytearray_decode, METH_FASTCALL|METH_KEYWORDS, bytearray_decode__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 713 | |
| 714 | static PyObject * |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 715 | bytearray_decode_impl(PyByteArrayObject *self, const char *encoding, |
| 716 | const char *errors); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 717 | |
| 718 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 719 | bytearray_decode(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 720 | { |
| 721 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 722 | static const char * const _keywords[] = {"encoding", "errors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 723 | static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0}; |
| 724 | PyObject *argsbuf[2]; |
| 725 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 726 | const char *encoding = NULL; |
| 727 | const char *errors = NULL; |
| 728 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 729 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 730 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 731 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 732 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 733 | if (!noptargs) { |
| 734 | goto skip_optional_pos; |
| 735 | } |
| 736 | if (args[0]) { |
| 737 | if (!PyUnicode_Check(args[0])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 738 | _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 739 | goto exit; |
| 740 | } |
| 741 | Py_ssize_t encoding_length; |
| 742 | encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length); |
| 743 | if (encoding == NULL) { |
| 744 | goto exit; |
| 745 | } |
| 746 | if (strlen(encoding) != (size_t)encoding_length) { |
| 747 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 748 | goto exit; |
| 749 | } |
| 750 | if (!--noptargs) { |
| 751 | goto skip_optional_pos; |
| 752 | } |
| 753 | } |
| 754 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 755 | _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 756 | goto exit; |
| 757 | } |
| 758 | Py_ssize_t errors_length; |
| 759 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 760 | if (errors == NULL) { |
| 761 | goto exit; |
| 762 | } |
| 763 | if (strlen(errors) != (size_t)errors_length) { |
| 764 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 765 | goto exit; |
| 766 | } |
| 767 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 768 | return_value = bytearray_decode_impl(self, encoding, errors); |
| 769 | |
| 770 | exit: |
| 771 | return return_value; |
| 772 | } |
| 773 | |
| 774 | PyDoc_STRVAR(bytearray_join__doc__, |
| 775 | "join($self, iterable_of_bytes, /)\n" |
| 776 | "--\n" |
| 777 | "\n" |
| 778 | "Concatenate any number of bytes/bytearray objects.\n" |
| 779 | "\n" |
| 780 | "The bytearray whose method is called is inserted in between each pair.\n" |
| 781 | "\n" |
| 782 | "The result is returned as a new bytearray object."); |
| 783 | |
| 784 | #define BYTEARRAY_JOIN_METHODDEF \ |
| 785 | {"join", (PyCFunction)bytearray_join, METH_O, bytearray_join__doc__}, |
| 786 | |
| 787 | PyDoc_STRVAR(bytearray_splitlines__doc__, |
| 788 | "splitlines($self, /, keepends=False)\n" |
| 789 | "--\n" |
| 790 | "\n" |
| 791 | "Return a list of the lines in the bytearray, breaking at line boundaries.\n" |
| 792 | "\n" |
| 793 | "Line breaks are not included in the resulting list unless keepends is given and\n" |
| 794 | "true."); |
| 795 | |
| 796 | #define BYTEARRAY_SPLITLINES_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 797 | {"splitlines", (PyCFunction)(void(*)(void))bytearray_splitlines, METH_FASTCALL|METH_KEYWORDS, bytearray_splitlines__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 798 | |
| 799 | static PyObject * |
| 800 | bytearray_splitlines_impl(PyByteArrayObject *self, int keepends); |
| 801 | |
| 802 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 803 | bytearray_splitlines(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 804 | { |
| 805 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 806 | static const char * const _keywords[] = {"keepends", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 807 | static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0}; |
| 808 | PyObject *argsbuf[1]; |
| 809 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 810 | int keepends = 0; |
| 811 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 812 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); |
| 813 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 814 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 815 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 816 | if (!noptargs) { |
| 817 | goto skip_optional_pos; |
| 818 | } |
| 819 | if (PyFloat_Check(args[0])) { |
| 820 | PyErr_SetString(PyExc_TypeError, |
| 821 | "integer argument expected, got float" ); |
| 822 | goto exit; |
| 823 | } |
| 824 | keepends = _PyLong_AsInt(args[0]); |
| 825 | if (keepends == -1 && PyErr_Occurred()) { |
| 826 | goto exit; |
| 827 | } |
| 828 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 829 | return_value = bytearray_splitlines_impl(self, keepends); |
| 830 | |
| 831 | exit: |
| 832 | return return_value; |
| 833 | } |
| 834 | |
| 835 | PyDoc_STRVAR(bytearray_fromhex__doc__, |
| 836 | "fromhex($type, string, /)\n" |
| 837 | "--\n" |
| 838 | "\n" |
| 839 | "Create a bytearray object from a string of hexadecimal numbers.\n" |
| 840 | "\n" |
| 841 | "Spaces between two numbers are accepted.\n" |
| 842 | "Example: bytearray.fromhex(\'B9 01EF\') -> bytearray(b\'\\\\xb9\\\\x01\\\\xef\')"); |
| 843 | |
| 844 | #define BYTEARRAY_FROMHEX_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 845 | {"fromhex", (PyCFunction)bytearray_fromhex, METH_O|METH_CLASS, bytearray_fromhex__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 846 | |
| 847 | static PyObject * |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 848 | bytearray_fromhex_impl(PyTypeObject *type, PyObject *string); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 849 | |
| 850 | static PyObject * |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 851 | bytearray_fromhex(PyTypeObject *type, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 852 | { |
| 853 | PyObject *return_value = NULL; |
| 854 | PyObject *string; |
| 855 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 856 | if (!PyUnicode_Check(arg)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 857 | _PyArg_BadArgument("fromhex", "argument", "str", arg); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 858 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 859 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 860 | if (PyUnicode_READY(arg) == -1) { |
| 861 | goto exit; |
| 862 | } |
| 863 | string = arg; |
Serhiy Storchaka | 0855e70 | 2016-07-01 17:22:31 +0300 | [diff] [blame] | 864 | return_value = bytearray_fromhex_impl(type, string); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 865 | |
| 866 | exit: |
| 867 | return return_value; |
| 868 | } |
| 869 | |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 870 | PyDoc_STRVAR(bytearray_hex__doc__, |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame^] | 871 | "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n" |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 872 | "--\n" |
| 873 | "\n" |
| 874 | "Create a str of hexadecimal numbers from a bytearray object.\n" |
| 875 | "\n" |
| 876 | " sep\n" |
| 877 | " An optional single character or byte to separate hex bytes.\n" |
| 878 | " bytes_per_sep\n" |
| 879 | " How many bytes between separators. Positive values count from the\n" |
| 880 | " right, negative values count from the left.\n" |
| 881 | "\n" |
| 882 | "Example:\n" |
| 883 | ">>> value = bytearray([0xb9, 0x01, 0xef])\n" |
| 884 | ">>> value.hex()\n" |
| 885 | "\'b901ef\'\n" |
| 886 | ">>> value.hex(\':\')\n" |
| 887 | "\'b9:01:ef\'\n" |
| 888 | ">>> value.hex(\':\', 2)\n" |
| 889 | "\'b9:01ef\'\n" |
| 890 | ">>> value.hex(\':\', -2)\n" |
| 891 | "\'b901:ef\'"); |
| 892 | |
| 893 | #define BYTEARRAY_HEX_METHODDEF \ |
| 894 | {"hex", (PyCFunction)(void(*)(void))bytearray_hex, METH_FASTCALL|METH_KEYWORDS, bytearray_hex__doc__}, |
| 895 | |
| 896 | static PyObject * |
| 897 | bytearray_hex_impl(PyByteArrayObject *self, PyObject *sep, int bytes_per_sep); |
| 898 | |
| 899 | static PyObject * |
| 900 | bytearray_hex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 901 | { |
| 902 | PyObject *return_value = NULL; |
| 903 | static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL}; |
| 904 | static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0}; |
| 905 | PyObject *argsbuf[2]; |
| 906 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
| 907 | PyObject *sep = NULL; |
| 908 | int bytes_per_sep = 1; |
| 909 | |
| 910 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 911 | if (!args) { |
| 912 | goto exit; |
| 913 | } |
| 914 | if (!noptargs) { |
| 915 | goto skip_optional_pos; |
| 916 | } |
| 917 | if (args[0]) { |
| 918 | sep = args[0]; |
| 919 | if (!--noptargs) { |
| 920 | goto skip_optional_pos; |
| 921 | } |
| 922 | } |
| 923 | if (PyFloat_Check(args[1])) { |
| 924 | PyErr_SetString(PyExc_TypeError, |
| 925 | "integer argument expected, got float" ); |
| 926 | goto exit; |
| 927 | } |
| 928 | bytes_per_sep = _PyLong_AsInt(args[1]); |
| 929 | if (bytes_per_sep == -1 && PyErr_Occurred()) { |
| 930 | goto exit; |
| 931 | } |
| 932 | skip_optional_pos: |
| 933 | return_value = bytearray_hex_impl(self, sep, bytes_per_sep); |
| 934 | |
| 935 | exit: |
| 936 | return return_value; |
| 937 | } |
| 938 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 939 | PyDoc_STRVAR(bytearray_reduce__doc__, |
| 940 | "__reduce__($self, /)\n" |
| 941 | "--\n" |
| 942 | "\n" |
| 943 | "Return state information for pickling."); |
| 944 | |
| 945 | #define BYTEARRAY_REDUCE_METHODDEF \ |
| 946 | {"__reduce__", (PyCFunction)bytearray_reduce, METH_NOARGS, bytearray_reduce__doc__}, |
| 947 | |
| 948 | static PyObject * |
| 949 | bytearray_reduce_impl(PyByteArrayObject *self); |
| 950 | |
| 951 | static PyObject * |
| 952 | bytearray_reduce(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 953 | { |
| 954 | return bytearray_reduce_impl(self); |
| 955 | } |
| 956 | |
| 957 | PyDoc_STRVAR(bytearray_reduce_ex__doc__, |
| 958 | "__reduce_ex__($self, proto=0, /)\n" |
| 959 | "--\n" |
| 960 | "\n" |
| 961 | "Return state information for pickling."); |
| 962 | |
| 963 | #define BYTEARRAY_REDUCE_EX_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 964 | {"__reduce_ex__", (PyCFunction)(void(*)(void))bytearray_reduce_ex, METH_FASTCALL, bytearray_reduce_ex__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 965 | |
| 966 | static PyObject * |
| 967 | bytearray_reduce_ex_impl(PyByteArrayObject *self, int proto); |
| 968 | |
| 969 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 970 | bytearray_reduce_ex(PyByteArrayObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 971 | { |
| 972 | PyObject *return_value = NULL; |
| 973 | int proto = 0; |
| 974 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 975 | if (!_PyArg_CheckPositional("__reduce_ex__", nargs, 0, 1)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 976 | goto exit; |
| 977 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 978 | if (nargs < 1) { |
| 979 | goto skip_optional; |
| 980 | } |
| 981 | if (PyFloat_Check(args[0])) { |
| 982 | PyErr_SetString(PyExc_TypeError, |
| 983 | "integer argument expected, got float" ); |
| 984 | goto exit; |
| 985 | } |
| 986 | proto = _PyLong_AsInt(args[0]); |
| 987 | if (proto == -1 && PyErr_Occurred()) { |
| 988 | goto exit; |
| 989 | } |
| 990 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 991 | return_value = bytearray_reduce_ex_impl(self, proto); |
| 992 | |
| 993 | exit: |
| 994 | return return_value; |
| 995 | } |
| 996 | |
| 997 | PyDoc_STRVAR(bytearray_sizeof__doc__, |
| 998 | "__sizeof__($self, /)\n" |
| 999 | "--\n" |
| 1000 | "\n" |
| 1001 | "Returns the size of the bytearray object in memory, in bytes."); |
| 1002 | |
| 1003 | #define BYTEARRAY_SIZEOF_METHODDEF \ |
| 1004 | {"__sizeof__", (PyCFunction)bytearray_sizeof, METH_NOARGS, bytearray_sizeof__doc__}, |
| 1005 | |
| 1006 | static PyObject * |
| 1007 | bytearray_sizeof_impl(PyByteArrayObject *self); |
| 1008 | |
| 1009 | static PyObject * |
| 1010 | bytearray_sizeof(PyByteArrayObject *self, PyObject *Py_UNUSED(ignored)) |
| 1011 | { |
| 1012 | return bytearray_sizeof_impl(self); |
| 1013 | } |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame^] | 1014 | /*[clinic end generated code: output=508dce79cf2dffcc input=a9049054013a1b77]*/ |