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