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(bytes_split__doc__, |
| 6 | "split($self, /, sep=None, maxsplit=-1)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 10 | "\n" |
| 11 | " sep\n" |
| 12 | " The delimiter according which to split the bytes.\n" |
| 13 | " None (the default value) means split on ASCII whitespace characters\n" |
| 14 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 15 | " maxsplit\n" |
| 16 | " Maximum number of splits to do.\n" |
| 17 | " -1 (the default value) means no limit."); |
| 18 | |
| 19 | #define BYTES_SPLIT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 20 | {"split", (PyCFunction)(void(*)(void))bytes_split, METH_FASTCALL|METH_KEYWORDS, bytes_split__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 21 | |
| 22 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 23 | bytes_split_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 24 | |
| 25 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 26 | bytes_split(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 27 | { |
| 28 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 29 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 30 | static _PyArg_Parser _parser = {NULL, _keywords, "split", 0}; |
| 31 | PyObject *argsbuf[2]; |
| 32 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 33 | PyObject *sep = Py_None; |
| 34 | Py_ssize_t maxsplit = -1; |
| 35 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 36 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 37 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 38 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 39 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 40 | if (!noptargs) { |
| 41 | goto skip_optional_pos; |
| 42 | } |
| 43 | if (args[0]) { |
| 44 | sep = args[0]; |
| 45 | if (!--noptargs) { |
| 46 | goto skip_optional_pos; |
| 47 | } |
| 48 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 49 | { |
| 50 | Py_ssize_t ival = -1; |
| 51 | PyObject *iobj = PyNumber_Index(args[1]); |
| 52 | if (iobj != NULL) { |
| 53 | ival = PyLong_AsSsize_t(iobj); |
| 54 | Py_DECREF(iobj); |
| 55 | } |
| 56 | if (ival == -1 && PyErr_Occurred()) { |
| 57 | goto exit; |
| 58 | } |
| 59 | maxsplit = ival; |
| 60 | } |
| 61 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 62 | return_value = bytes_split_impl(self, sep, maxsplit); |
| 63 | |
| 64 | exit: |
| 65 | return return_value; |
| 66 | } |
| 67 | |
| 68 | PyDoc_STRVAR(bytes_partition__doc__, |
| 69 | "partition($self, sep, /)\n" |
| 70 | "--\n" |
| 71 | "\n" |
| 72 | "Partition the bytes into three parts using the given separator.\n" |
| 73 | "\n" |
| 74 | "This will search for the separator sep in the bytes. If the separator is found,\n" |
| 75 | "returns a 3-tuple containing the part before the separator, the separator\n" |
| 76 | "itself, and the part after it.\n" |
| 77 | "\n" |
| 78 | "If the separator is not found, returns a 3-tuple containing the original bytes\n" |
| 79 | "object and two empty bytes objects."); |
| 80 | |
| 81 | #define BYTES_PARTITION_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 82 | {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 83 | |
| 84 | static PyObject * |
| 85 | bytes_partition_impl(PyBytesObject *self, Py_buffer *sep); |
| 86 | |
| 87 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 88 | bytes_partition(PyBytesObject *self, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 89 | { |
| 90 | PyObject *return_value = NULL; |
| 91 | Py_buffer sep = {NULL, NULL}; |
| 92 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 93 | if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) { |
| 94 | goto exit; |
| 95 | } |
| 96 | if (!PyBuffer_IsContiguous(&sep, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 97 | _PyArg_BadArgument("partition", "argument", "contiguous buffer", arg); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 98 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 99 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 100 | return_value = bytes_partition_impl(self, &sep); |
| 101 | |
| 102 | exit: |
| 103 | /* Cleanup for sep */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 104 | if (sep.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 105 | PyBuffer_Release(&sep); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 106 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 107 | |
| 108 | return return_value; |
| 109 | } |
| 110 | |
| 111 | PyDoc_STRVAR(bytes_rpartition__doc__, |
| 112 | "rpartition($self, sep, /)\n" |
| 113 | "--\n" |
| 114 | "\n" |
| 115 | "Partition the bytes into three parts using the given separator.\n" |
| 116 | "\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 117 | "This will search for the separator sep in the bytes, starting at the end. If\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 118 | "the separator is found, returns a 3-tuple containing the part before the\n" |
| 119 | "separator, the separator itself, and the part after it.\n" |
| 120 | "\n" |
| 121 | "If the separator is not found, returns a 3-tuple containing two empty bytes\n" |
| 122 | "objects and the original bytes object."); |
| 123 | |
| 124 | #define BYTES_RPARTITION_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 125 | {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 126 | |
| 127 | static PyObject * |
| 128 | bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep); |
| 129 | |
| 130 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 131 | bytes_rpartition(PyBytesObject *self, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 132 | { |
| 133 | PyObject *return_value = NULL; |
| 134 | Py_buffer sep = {NULL, NULL}; |
| 135 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 136 | if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) { |
| 137 | goto exit; |
| 138 | } |
| 139 | if (!PyBuffer_IsContiguous(&sep, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 140 | _PyArg_BadArgument("rpartition", "argument", "contiguous buffer", arg); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 141 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 142 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 143 | return_value = bytes_rpartition_impl(self, &sep); |
| 144 | |
| 145 | exit: |
| 146 | /* Cleanup for sep */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 147 | if (sep.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 148 | PyBuffer_Release(&sep); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 149 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 150 | |
| 151 | return return_value; |
| 152 | } |
| 153 | |
| 154 | PyDoc_STRVAR(bytes_rsplit__doc__, |
| 155 | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
| 156 | "--\n" |
| 157 | "\n" |
| 158 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 159 | "\n" |
| 160 | " sep\n" |
| 161 | " The delimiter according which to split the bytes.\n" |
| 162 | " None (the default value) means split on ASCII whitespace characters\n" |
| 163 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 164 | " maxsplit\n" |
| 165 | " Maximum number of splits to do.\n" |
| 166 | " -1 (the default value) means no limit.\n" |
| 167 | "\n" |
| 168 | "Splitting is done starting at the end of the bytes and working to the front."); |
| 169 | |
| 170 | #define BYTES_RSPLIT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 171 | {"rsplit", (PyCFunction)(void(*)(void))bytes_rsplit, METH_FASTCALL|METH_KEYWORDS, bytes_rsplit__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 172 | |
| 173 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 174 | bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 175 | |
| 176 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 177 | bytes_rsplit(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 178 | { |
| 179 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 180 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 181 | static _PyArg_Parser _parser = {NULL, _keywords, "rsplit", 0}; |
| 182 | PyObject *argsbuf[2]; |
| 183 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 184 | PyObject *sep = Py_None; |
| 185 | Py_ssize_t maxsplit = -1; |
| 186 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 187 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 188 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 189 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 190 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 191 | if (!noptargs) { |
| 192 | goto skip_optional_pos; |
| 193 | } |
| 194 | if (args[0]) { |
| 195 | sep = args[0]; |
| 196 | if (!--noptargs) { |
| 197 | goto skip_optional_pos; |
| 198 | } |
| 199 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 200 | { |
| 201 | Py_ssize_t ival = -1; |
| 202 | PyObject *iobj = PyNumber_Index(args[1]); |
| 203 | if (iobj != NULL) { |
| 204 | ival = PyLong_AsSsize_t(iobj); |
| 205 | Py_DECREF(iobj); |
| 206 | } |
| 207 | if (ival == -1 && PyErr_Occurred()) { |
| 208 | goto exit; |
| 209 | } |
| 210 | maxsplit = ival; |
| 211 | } |
| 212 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 213 | return_value = bytes_rsplit_impl(self, sep, maxsplit); |
| 214 | |
| 215 | exit: |
| 216 | return return_value; |
| 217 | } |
| 218 | |
| 219 | PyDoc_STRVAR(bytes_join__doc__, |
| 220 | "join($self, iterable_of_bytes, /)\n" |
| 221 | "--\n" |
| 222 | "\n" |
| 223 | "Concatenate any number of bytes objects.\n" |
| 224 | "\n" |
| 225 | "The bytes whose method is called is inserted in between each pair.\n" |
| 226 | "\n" |
| 227 | "The result is returned as a new bytes object.\n" |
| 228 | "\n" |
| 229 | "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'."); |
| 230 | |
| 231 | #define BYTES_JOIN_METHODDEF \ |
| 232 | {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__}, |
| 233 | |
| 234 | PyDoc_STRVAR(bytes_strip__doc__, |
| 235 | "strip($self, bytes=None, /)\n" |
| 236 | "--\n" |
| 237 | "\n" |
| 238 | "Strip leading and trailing bytes contained in the argument.\n" |
| 239 | "\n" |
| 240 | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
| 241 | |
| 242 | #define BYTES_STRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 243 | {"strip", (PyCFunction)(void(*)(void))bytes_strip, METH_FASTCALL, bytes_strip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 244 | |
| 245 | static PyObject * |
| 246 | bytes_strip_impl(PyBytesObject *self, PyObject *bytes); |
| 247 | |
| 248 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 249 | bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 250 | { |
| 251 | PyObject *return_value = NULL; |
| 252 | PyObject *bytes = Py_None; |
| 253 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 254 | if (!_PyArg_CheckPositional("strip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 255 | goto exit; |
| 256 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 257 | if (nargs < 1) { |
| 258 | goto skip_optional; |
| 259 | } |
| 260 | bytes = args[0]; |
| 261 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 262 | return_value = bytes_strip_impl(self, bytes); |
| 263 | |
| 264 | exit: |
| 265 | return return_value; |
| 266 | } |
| 267 | |
| 268 | PyDoc_STRVAR(bytes_lstrip__doc__, |
| 269 | "lstrip($self, bytes=None, /)\n" |
| 270 | "--\n" |
| 271 | "\n" |
| 272 | "Strip leading bytes contained in the argument.\n" |
| 273 | "\n" |
| 274 | "If the argument is omitted or None, strip leading ASCII whitespace."); |
| 275 | |
| 276 | #define BYTES_LSTRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 277 | {"lstrip", (PyCFunction)(void(*)(void))bytes_lstrip, METH_FASTCALL, bytes_lstrip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 278 | |
| 279 | static PyObject * |
| 280 | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 281 | |
| 282 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 283 | bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 284 | { |
| 285 | PyObject *return_value = NULL; |
| 286 | PyObject *bytes = Py_None; |
| 287 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 288 | if (!_PyArg_CheckPositional("lstrip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 289 | goto exit; |
| 290 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 291 | if (nargs < 1) { |
| 292 | goto skip_optional; |
| 293 | } |
| 294 | bytes = args[0]; |
| 295 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 296 | return_value = bytes_lstrip_impl(self, bytes); |
| 297 | |
| 298 | exit: |
| 299 | return return_value; |
| 300 | } |
| 301 | |
| 302 | PyDoc_STRVAR(bytes_rstrip__doc__, |
| 303 | "rstrip($self, bytes=None, /)\n" |
| 304 | "--\n" |
| 305 | "\n" |
| 306 | "Strip trailing bytes contained in the argument.\n" |
| 307 | "\n" |
| 308 | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
| 309 | |
| 310 | #define BYTES_RSTRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 311 | {"rstrip", (PyCFunction)(void(*)(void))bytes_rstrip, METH_FASTCALL, bytes_rstrip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 312 | |
| 313 | static PyObject * |
| 314 | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 315 | |
| 316 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 317 | bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 318 | { |
| 319 | PyObject *return_value = NULL; |
| 320 | PyObject *bytes = Py_None; |
| 321 | |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 322 | if (!_PyArg_CheckPositional("rstrip", nargs, 0, 1)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 323 | goto exit; |
| 324 | } |
Serhiy Storchaka | 2a39d25 | 2019-01-11 18:01:42 +0200 | [diff] [blame] | 325 | if (nargs < 1) { |
| 326 | goto skip_optional; |
| 327 | } |
| 328 | bytes = args[0]; |
| 329 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 330 | return_value = bytes_rstrip_impl(self, bytes); |
| 331 | |
| 332 | exit: |
| 333 | return return_value; |
| 334 | } |
| 335 | |
| 336 | PyDoc_STRVAR(bytes_translate__doc__, |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 337 | "translate($self, table, /, delete=b\'\')\n" |
| 338 | "--\n" |
| 339 | "\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 340 | "Return a copy with each character mapped by the given translation table.\n" |
| 341 | "\n" |
| 342 | " table\n" |
| 343 | " Translation table, which must be a bytes object of length 256.\n" |
| 344 | "\n" |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 345 | "All characters occurring in the optional argument delete are removed.\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 346 | "The remaining characters are mapped through the given translation table."); |
| 347 | |
| 348 | #define BYTES_TRANSLATE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 349 | {"translate", (PyCFunction)(void(*)(void))bytes_translate, METH_FASTCALL|METH_KEYWORDS, bytes_translate__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 350 | |
| 351 | static PyObject * |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 352 | bytes_translate_impl(PyBytesObject *self, PyObject *table, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 353 | PyObject *deletechars); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 354 | |
| 355 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 356 | bytes_translate(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 357 | { |
| 358 | PyObject *return_value = NULL; |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 359 | static const char * const _keywords[] = {"", "delete", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 360 | static _PyArg_Parser _parser = {NULL, _keywords, "translate", 0}; |
| 361 | PyObject *argsbuf[2]; |
| 362 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 363 | PyObject *table; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 364 | PyObject *deletechars = NULL; |
| 365 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 366 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 2, 0, argsbuf); |
| 367 | if (!args) { |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 368 | goto exit; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 369 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 370 | table = args[0]; |
| 371 | if (!noptargs) { |
| 372 | goto skip_optional_pos; |
| 373 | } |
| 374 | deletechars = args[1]; |
| 375 | skip_optional_pos: |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 376 | return_value = bytes_translate_impl(self, table, deletechars); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 377 | |
| 378 | exit: |
| 379 | return return_value; |
| 380 | } |
| 381 | |
| 382 | PyDoc_STRVAR(bytes_maketrans__doc__, |
| 383 | "maketrans(frm, to, /)\n" |
| 384 | "--\n" |
| 385 | "\n" |
| 386 | "Return a translation table useable for the bytes or bytearray translate method.\n" |
| 387 | "\n" |
| 388 | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
| 389 | "the same position in to.\n" |
| 390 | "\n" |
| 391 | "The bytes objects frm and to must be of the same length."); |
| 392 | |
| 393 | #define BYTES_MAKETRANS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 394 | {"maketrans", (PyCFunction)(void(*)(void))bytes_maketrans, METH_FASTCALL|METH_STATIC, bytes_maketrans__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 395 | |
| 396 | static PyObject * |
| 397 | bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to); |
| 398 | |
| 399 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 400 | bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 401 | { |
| 402 | PyObject *return_value = NULL; |
| 403 | Py_buffer frm = {NULL, NULL}; |
| 404 | Py_buffer to = {NULL, NULL}; |
| 405 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 406 | if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) { |
| 407 | goto exit; |
| 408 | } |
| 409 | if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) { |
| 410 | goto exit; |
| 411 | } |
| 412 | if (!PyBuffer_IsContiguous(&frm, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 413 | _PyArg_BadArgument("maketrans", "argument 1", "contiguous buffer", args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 414 | goto exit; |
| 415 | } |
| 416 | if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) { |
| 417 | goto exit; |
| 418 | } |
| 419 | if (!PyBuffer_IsContiguous(&to, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 420 | _PyArg_BadArgument("maketrans", "argument 2", "contiguous buffer", args[1]); |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 421 | goto exit; |
| 422 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 423 | return_value = bytes_maketrans_impl(&frm, &to); |
| 424 | |
| 425 | exit: |
| 426 | /* Cleanup for frm */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 427 | if (frm.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 428 | PyBuffer_Release(&frm); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 429 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 430 | /* Cleanup for to */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 431 | if (to.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 432 | PyBuffer_Release(&to); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 433 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 434 | |
| 435 | return return_value; |
| 436 | } |
| 437 | |
| 438 | PyDoc_STRVAR(bytes_replace__doc__, |
| 439 | "replace($self, old, new, count=-1, /)\n" |
| 440 | "--\n" |
| 441 | "\n" |
| 442 | "Return a copy with all occurrences of substring old replaced by new.\n" |
| 443 | "\n" |
| 444 | " count\n" |
| 445 | " Maximum number of occurrences to replace.\n" |
| 446 | " -1 (the default value) means replace all occurrences.\n" |
| 447 | "\n" |
| 448 | "If the optional argument count is given, only the first count occurrences are\n" |
| 449 | "replaced."); |
| 450 | |
| 451 | #define BYTES_REPLACE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 452 | {"replace", (PyCFunction)(void(*)(void))bytes_replace, METH_FASTCALL, bytes_replace__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 453 | |
| 454 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 455 | bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 456 | Py_ssize_t count); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 457 | |
| 458 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 459 | bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 460 | { |
| 461 | PyObject *return_value = NULL; |
| 462 | Py_buffer old = {NULL, NULL}; |
| 463 | Py_buffer new = {NULL, NULL}; |
| 464 | Py_ssize_t count = -1; |
| 465 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 466 | if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 467 | goto exit; |
| 468 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 469 | if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) { |
| 470 | goto exit; |
| 471 | } |
| 472 | if (!PyBuffer_IsContiguous(&old, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 473 | _PyArg_BadArgument("replace", "argument 1", "contiguous buffer", args[0]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 474 | goto exit; |
| 475 | } |
| 476 | if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) { |
| 477 | goto exit; |
| 478 | } |
| 479 | if (!PyBuffer_IsContiguous(&new, 'C')) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 480 | _PyArg_BadArgument("replace", "argument 2", "contiguous buffer", args[1]); |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 481 | goto exit; |
| 482 | } |
| 483 | if (nargs < 3) { |
| 484 | goto skip_optional; |
| 485 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 486 | { |
| 487 | Py_ssize_t ival = -1; |
| 488 | PyObject *iobj = PyNumber_Index(args[2]); |
| 489 | if (iobj != NULL) { |
| 490 | ival = PyLong_AsSsize_t(iobj); |
| 491 | Py_DECREF(iobj); |
| 492 | } |
| 493 | if (ival == -1 && PyErr_Occurred()) { |
| 494 | goto exit; |
| 495 | } |
| 496 | count = ival; |
| 497 | } |
| 498 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 499 | return_value = bytes_replace_impl(self, &old, &new, count); |
| 500 | |
| 501 | exit: |
| 502 | /* Cleanup for old */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 503 | if (old.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 504 | PyBuffer_Release(&old); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 505 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 506 | /* Cleanup for new */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 507 | if (new.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 508 | PyBuffer_Release(&new); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 509 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 510 | |
| 511 | return return_value; |
| 512 | } |
| 513 | |
sweeneyde | a81849b | 2020-04-22 17:05:48 -0400 | [diff] [blame] | 514 | PyDoc_STRVAR(bytes_removeprefix__doc__, |
| 515 | "removeprefix($self, prefix, /)\n" |
| 516 | "--\n" |
| 517 | "\n" |
| 518 | "Return a bytes object with the given prefix string removed if present.\n" |
| 519 | "\n" |
| 520 | "If the bytes starts with the prefix string, return bytes[len(prefix):].\n" |
| 521 | "Otherwise, return a copy of the original bytes."); |
| 522 | |
| 523 | #define BYTES_REMOVEPREFIX_METHODDEF \ |
| 524 | {"removeprefix", (PyCFunction)bytes_removeprefix, METH_O, bytes_removeprefix__doc__}, |
| 525 | |
| 526 | static PyObject * |
| 527 | bytes_removeprefix_impl(PyBytesObject *self, Py_buffer *prefix); |
| 528 | |
| 529 | static PyObject * |
| 530 | bytes_removeprefix(PyBytesObject *self, PyObject *arg) |
| 531 | { |
| 532 | PyObject *return_value = NULL; |
| 533 | Py_buffer prefix = {NULL, NULL}; |
| 534 | |
| 535 | if (PyObject_GetBuffer(arg, &prefix, PyBUF_SIMPLE) != 0) { |
| 536 | goto exit; |
| 537 | } |
| 538 | if (!PyBuffer_IsContiguous(&prefix, 'C')) { |
| 539 | _PyArg_BadArgument("removeprefix", "argument", "contiguous buffer", arg); |
| 540 | goto exit; |
| 541 | } |
| 542 | return_value = bytes_removeprefix_impl(self, &prefix); |
| 543 | |
| 544 | exit: |
| 545 | /* Cleanup for prefix */ |
| 546 | if (prefix.obj) { |
| 547 | PyBuffer_Release(&prefix); |
| 548 | } |
| 549 | |
| 550 | return return_value; |
| 551 | } |
| 552 | |
| 553 | PyDoc_STRVAR(bytes_removesuffix__doc__, |
| 554 | "removesuffix($self, suffix, /)\n" |
| 555 | "--\n" |
| 556 | "\n" |
| 557 | "Return a bytes object with the given suffix string removed if present.\n" |
| 558 | "\n" |
| 559 | "If the bytes ends with the suffix string and that suffix is not empty,\n" |
| 560 | "return bytes[:-len(prefix)]. Otherwise, return a copy of the original\n" |
| 561 | "bytes."); |
| 562 | |
| 563 | #define BYTES_REMOVESUFFIX_METHODDEF \ |
| 564 | {"removesuffix", (PyCFunction)bytes_removesuffix, METH_O, bytes_removesuffix__doc__}, |
| 565 | |
| 566 | static PyObject * |
| 567 | bytes_removesuffix_impl(PyBytesObject *self, Py_buffer *suffix); |
| 568 | |
| 569 | static PyObject * |
| 570 | bytes_removesuffix(PyBytesObject *self, PyObject *arg) |
| 571 | { |
| 572 | PyObject *return_value = NULL; |
| 573 | Py_buffer suffix = {NULL, NULL}; |
| 574 | |
| 575 | if (PyObject_GetBuffer(arg, &suffix, PyBUF_SIMPLE) != 0) { |
| 576 | goto exit; |
| 577 | } |
| 578 | if (!PyBuffer_IsContiguous(&suffix, 'C')) { |
| 579 | _PyArg_BadArgument("removesuffix", "argument", "contiguous buffer", arg); |
| 580 | goto exit; |
| 581 | } |
| 582 | return_value = bytes_removesuffix_impl(self, &suffix); |
| 583 | |
| 584 | exit: |
| 585 | /* Cleanup for suffix */ |
| 586 | if (suffix.obj) { |
| 587 | PyBuffer_Release(&suffix); |
| 588 | } |
| 589 | |
| 590 | return return_value; |
| 591 | } |
| 592 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 593 | PyDoc_STRVAR(bytes_decode__doc__, |
| 594 | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
| 595 | "--\n" |
| 596 | "\n" |
| 597 | "Decode the bytes using the codec registered for encoding.\n" |
| 598 | "\n" |
| 599 | " encoding\n" |
| 600 | " The encoding with which to decode the bytes.\n" |
| 601 | " errors\n" |
| 602 | " The error handling scheme to use for the handling of decoding errors.\n" |
| 603 | " The default is \'strict\' meaning that decoding errors raise a\n" |
| 604 | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
| 605 | " as well as any other name registered with codecs.register_error that\n" |
| 606 | " can handle UnicodeDecodeErrors."); |
| 607 | |
| 608 | #define BYTES_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 609 | {"decode", (PyCFunction)(void(*)(void))bytes_decode, METH_FASTCALL|METH_KEYWORDS, bytes_decode__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 610 | |
| 611 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 612 | bytes_decode_impl(PyBytesObject *self, const char *encoding, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 613 | const char *errors); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 614 | |
| 615 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 616 | bytes_decode(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 617 | { |
| 618 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 619 | static const char * const _keywords[] = {"encoding", "errors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 620 | static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0}; |
| 621 | PyObject *argsbuf[2]; |
| 622 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 623 | const char *encoding = NULL; |
| 624 | const char *errors = NULL; |
| 625 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 626 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 627 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 628 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 629 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 630 | if (!noptargs) { |
| 631 | goto skip_optional_pos; |
| 632 | } |
| 633 | if (args[0]) { |
| 634 | if (!PyUnicode_Check(args[0])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 635 | _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[0]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 636 | goto exit; |
| 637 | } |
| 638 | Py_ssize_t encoding_length; |
| 639 | encoding = PyUnicode_AsUTF8AndSize(args[0], &encoding_length); |
| 640 | if (encoding == NULL) { |
| 641 | goto exit; |
| 642 | } |
| 643 | if (strlen(encoding) != (size_t)encoding_length) { |
| 644 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 645 | goto exit; |
| 646 | } |
| 647 | if (!--noptargs) { |
| 648 | goto skip_optional_pos; |
| 649 | } |
| 650 | } |
| 651 | if (!PyUnicode_Check(args[1])) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 652 | _PyArg_BadArgument("decode", "argument 'errors'", "str", args[1]); |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 653 | goto exit; |
| 654 | } |
| 655 | Py_ssize_t errors_length; |
| 656 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 657 | if (errors == NULL) { |
| 658 | goto exit; |
| 659 | } |
| 660 | if (strlen(errors) != (size_t)errors_length) { |
| 661 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 662 | goto exit; |
| 663 | } |
| 664 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 665 | return_value = bytes_decode_impl(self, encoding, errors); |
| 666 | |
| 667 | exit: |
| 668 | return return_value; |
| 669 | } |
| 670 | |
| 671 | PyDoc_STRVAR(bytes_splitlines__doc__, |
| 672 | "splitlines($self, /, keepends=False)\n" |
| 673 | "--\n" |
| 674 | "\n" |
| 675 | "Return a list of the lines in the bytes, breaking at line boundaries.\n" |
| 676 | "\n" |
| 677 | "Line breaks are not included in the resulting list unless keepends is given and\n" |
| 678 | "true."); |
| 679 | |
| 680 | #define BYTES_SPLITLINES_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 681 | {"splitlines", (PyCFunction)(void(*)(void))bytes_splitlines, METH_FASTCALL|METH_KEYWORDS, bytes_splitlines__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 682 | |
| 683 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 684 | bytes_splitlines_impl(PyBytesObject *self, int keepends); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 685 | |
| 686 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 687 | bytes_splitlines(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 688 | { |
| 689 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 690 | static const char * const _keywords[] = {"keepends", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 691 | static _PyArg_Parser _parser = {NULL, _keywords, "splitlines", 0}; |
| 692 | PyObject *argsbuf[1]; |
| 693 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 694 | int keepends = 0; |
| 695 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 696 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 1, 0, argsbuf); |
| 697 | if (!args) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 698 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 699 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 700 | if (!noptargs) { |
| 701 | goto skip_optional_pos; |
| 702 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame] | 703 | keepends = _PyLong_AsInt(args[0]); |
| 704 | if (keepends == -1 && PyErr_Occurred()) { |
| 705 | goto exit; |
| 706 | } |
| 707 | skip_optional_pos: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 708 | return_value = bytes_splitlines_impl(self, keepends); |
| 709 | |
| 710 | exit: |
| 711 | return return_value; |
| 712 | } |
| 713 | |
| 714 | PyDoc_STRVAR(bytes_fromhex__doc__, |
| 715 | "fromhex($type, string, /)\n" |
| 716 | "--\n" |
| 717 | "\n" |
| 718 | "Create a bytes object from a string of hexadecimal numbers.\n" |
| 719 | "\n" |
| 720 | "Spaces between two numbers are accepted.\n" |
| 721 | "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'."); |
| 722 | |
| 723 | #define BYTES_FROMHEX_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 724 | {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 725 | |
| 726 | static PyObject * |
| 727 | bytes_fromhex_impl(PyTypeObject *type, PyObject *string); |
| 728 | |
| 729 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 730 | bytes_fromhex(PyTypeObject *type, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 731 | { |
| 732 | PyObject *return_value = NULL; |
| 733 | PyObject *string; |
| 734 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 735 | if (!PyUnicode_Check(arg)) { |
Rémi Lapeyre | 4901fe2 | 2019-08-29 16:49:08 +0200 | [diff] [blame] | 736 | _PyArg_BadArgument("fromhex", "argument", "str", arg); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 737 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 738 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 739 | if (PyUnicode_READY(arg) == -1) { |
| 740 | goto exit; |
| 741 | } |
| 742 | string = arg; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 743 | return_value = bytes_fromhex_impl(type, string); |
| 744 | |
| 745 | exit: |
| 746 | return return_value; |
| 747 | } |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 748 | |
| 749 | PyDoc_STRVAR(bytes_hex__doc__, |
Serhiy Storchaka | 279f446 | 2019-09-14 12:24:05 +0300 | [diff] [blame] | 750 | "hex($self, /, sep=<unrepresentable>, bytes_per_sep=1)\n" |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 751 | "--\n" |
| 752 | "\n" |
| 753 | "Create a str of hexadecimal numbers from a bytes object.\n" |
| 754 | "\n" |
| 755 | " sep\n" |
| 756 | " An optional single character or byte to separate hex bytes.\n" |
| 757 | " bytes_per_sep\n" |
| 758 | " How many bytes between separators. Positive values count from the\n" |
| 759 | " right, negative values count from the left.\n" |
| 760 | "\n" |
| 761 | "Example:\n" |
| 762 | ">>> value = b\'\\xb9\\x01\\xef\'\n" |
| 763 | ">>> value.hex()\n" |
| 764 | "\'b901ef\'\n" |
| 765 | ">>> value.hex(\':\')\n" |
| 766 | "\'b9:01:ef\'\n" |
| 767 | ">>> value.hex(\':\', 2)\n" |
| 768 | "\'b9:01ef\'\n" |
| 769 | ">>> value.hex(\':\', -2)\n" |
| 770 | "\'b901:ef\'"); |
| 771 | |
| 772 | #define BYTES_HEX_METHODDEF \ |
| 773 | {"hex", (PyCFunction)(void(*)(void))bytes_hex, METH_FASTCALL|METH_KEYWORDS, bytes_hex__doc__}, |
| 774 | |
| 775 | static PyObject * |
| 776 | bytes_hex_impl(PyBytesObject *self, PyObject *sep, int bytes_per_sep); |
| 777 | |
| 778 | static PyObject * |
| 779 | bytes_hex(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
| 780 | { |
| 781 | PyObject *return_value = NULL; |
| 782 | static const char * const _keywords[] = {"sep", "bytes_per_sep", NULL}; |
| 783 | static _PyArg_Parser _parser = {NULL, _keywords, "hex", 0}; |
| 784 | PyObject *argsbuf[2]; |
| 785 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 0; |
| 786 | PyObject *sep = NULL; |
| 787 | int bytes_per_sep = 1; |
| 788 | |
| 789 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 0, 2, 0, argsbuf); |
| 790 | if (!args) { |
| 791 | goto exit; |
| 792 | } |
| 793 | if (!noptargs) { |
| 794 | goto skip_optional_pos; |
| 795 | } |
| 796 | if (args[0]) { |
| 797 | sep = args[0]; |
| 798 | if (!--noptargs) { |
| 799 | goto skip_optional_pos; |
| 800 | } |
| 801 | } |
Gregory P. Smith | 0c2f930 | 2019-05-29 11:46:58 -0700 | [diff] [blame] | 802 | bytes_per_sep = _PyLong_AsInt(args[1]); |
| 803 | if (bytes_per_sep == -1 && PyErr_Occurred()) { |
| 804 | goto exit; |
| 805 | } |
| 806 | skip_optional_pos: |
| 807 | return_value = bytes_hex_impl(self, sep, bytes_per_sep); |
| 808 | |
| 809 | exit: |
| 810 | return return_value; |
| 811 | } |
Serhiy Storchaka | 578c395 | 2020-05-26 18:43:38 +0300 | [diff] [blame] | 812 | /*[clinic end generated code: output=a0c31faea2671a8c input=a9049054013a1b77]*/ |