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