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}; |
| 30 | static _PyArg_Parser _parser = {"|On:split", _keywords, 0}; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 31 | PyObject *sep = Py_None; |
| 32 | Py_ssize_t maxsplit = -1; |
| 33 | |
Victor Stinner | 3e1fad6 | 2017-01-17 01:29:01 +0100 | [diff] [blame] | 34 | if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 35 | &sep, &maxsplit)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 36 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 37 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 38 | return_value = bytes_split_impl(self, sep, maxsplit); |
| 39 | |
| 40 | exit: |
| 41 | return return_value; |
| 42 | } |
| 43 | |
| 44 | PyDoc_STRVAR(bytes_partition__doc__, |
| 45 | "partition($self, sep, /)\n" |
| 46 | "--\n" |
| 47 | "\n" |
| 48 | "Partition the bytes into three parts using the given separator.\n" |
| 49 | "\n" |
| 50 | "This will search for the separator sep in the bytes. If the separator is found,\n" |
| 51 | "returns a 3-tuple containing the part before the separator, the separator\n" |
| 52 | "itself, and the part after it.\n" |
| 53 | "\n" |
| 54 | "If the separator is not found, returns a 3-tuple containing the original bytes\n" |
| 55 | "object and two empty bytes objects."); |
| 56 | |
| 57 | #define BYTES_PARTITION_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 58 | {"partition", (PyCFunction)bytes_partition, METH_O, bytes_partition__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 59 | |
| 60 | static PyObject * |
| 61 | bytes_partition_impl(PyBytesObject *self, Py_buffer *sep); |
| 62 | |
| 63 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 64 | bytes_partition(PyBytesObject *self, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 65 | { |
| 66 | PyObject *return_value = NULL; |
| 67 | Py_buffer sep = {NULL, NULL}; |
| 68 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 69 | if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) { |
| 70 | goto exit; |
| 71 | } |
| 72 | if (!PyBuffer_IsContiguous(&sep, 'C')) { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 73 | _PyArg_BadArgument("partition", 0, "contiguous buffer", arg); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 74 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 75 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 76 | return_value = bytes_partition_impl(self, &sep); |
| 77 | |
| 78 | exit: |
| 79 | /* Cleanup for sep */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 80 | if (sep.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 81 | PyBuffer_Release(&sep); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 82 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 83 | |
| 84 | return return_value; |
| 85 | } |
| 86 | |
| 87 | PyDoc_STRVAR(bytes_rpartition__doc__, |
| 88 | "rpartition($self, sep, /)\n" |
| 89 | "--\n" |
| 90 | "\n" |
| 91 | "Partition the bytes into three parts using the given separator.\n" |
| 92 | "\n" |
Serhiy Storchaka | a231428 | 2017-10-29 02:11:54 +0300 | [diff] [blame] | 93 | "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] | 94 | "the separator is found, returns a 3-tuple containing the part before the\n" |
| 95 | "separator, the separator itself, and the part after it.\n" |
| 96 | "\n" |
| 97 | "If the separator is not found, returns a 3-tuple containing two empty bytes\n" |
| 98 | "objects and the original bytes object."); |
| 99 | |
| 100 | #define BYTES_RPARTITION_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 101 | {"rpartition", (PyCFunction)bytes_rpartition, METH_O, bytes_rpartition__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 102 | |
| 103 | static PyObject * |
| 104 | bytes_rpartition_impl(PyBytesObject *self, Py_buffer *sep); |
| 105 | |
| 106 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 107 | bytes_rpartition(PyBytesObject *self, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 108 | { |
| 109 | PyObject *return_value = NULL; |
| 110 | Py_buffer sep = {NULL, NULL}; |
| 111 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 112 | if (PyObject_GetBuffer(arg, &sep, PyBUF_SIMPLE) != 0) { |
| 113 | goto exit; |
| 114 | } |
| 115 | if (!PyBuffer_IsContiguous(&sep, 'C')) { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 116 | _PyArg_BadArgument("rpartition", 0, "contiguous buffer", arg); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 117 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 118 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 119 | return_value = bytes_rpartition_impl(self, &sep); |
| 120 | |
| 121 | exit: |
| 122 | /* Cleanup for sep */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 123 | if (sep.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 124 | PyBuffer_Release(&sep); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 125 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 126 | |
| 127 | return return_value; |
| 128 | } |
| 129 | |
| 130 | PyDoc_STRVAR(bytes_rsplit__doc__, |
| 131 | "rsplit($self, /, sep=None, maxsplit=-1)\n" |
| 132 | "--\n" |
| 133 | "\n" |
| 134 | "Return a list of the sections in the bytes, using sep as the delimiter.\n" |
| 135 | "\n" |
| 136 | " sep\n" |
| 137 | " The delimiter according which to split the bytes.\n" |
| 138 | " None (the default value) means split on ASCII whitespace characters\n" |
| 139 | " (space, tab, return, newline, formfeed, vertical tab).\n" |
| 140 | " maxsplit\n" |
| 141 | " Maximum number of splits to do.\n" |
| 142 | " -1 (the default value) means no limit.\n" |
| 143 | "\n" |
| 144 | "Splitting is done starting at the end of the bytes and working to the front."); |
| 145 | |
| 146 | #define BYTES_RSPLIT_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 147 | {"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] | 148 | |
| 149 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 150 | bytes_rsplit_impl(PyBytesObject *self, PyObject *sep, Py_ssize_t maxsplit); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 151 | |
| 152 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 153 | 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] | 154 | { |
| 155 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 156 | static const char * const _keywords[] = {"sep", "maxsplit", NULL}; |
| 157 | static _PyArg_Parser _parser = {"|On:rsplit", _keywords, 0}; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 158 | PyObject *sep = Py_None; |
| 159 | Py_ssize_t maxsplit = -1; |
| 160 | |
Victor Stinner | 3e1fad6 | 2017-01-17 01:29:01 +0100 | [diff] [blame] | 161 | if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 162 | &sep, &maxsplit)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 163 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 164 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 165 | return_value = bytes_rsplit_impl(self, sep, maxsplit); |
| 166 | |
| 167 | exit: |
| 168 | return return_value; |
| 169 | } |
| 170 | |
| 171 | PyDoc_STRVAR(bytes_join__doc__, |
| 172 | "join($self, iterable_of_bytes, /)\n" |
| 173 | "--\n" |
| 174 | "\n" |
| 175 | "Concatenate any number of bytes objects.\n" |
| 176 | "\n" |
| 177 | "The bytes whose method is called is inserted in between each pair.\n" |
| 178 | "\n" |
| 179 | "The result is returned as a new bytes object.\n" |
| 180 | "\n" |
| 181 | "Example: b\'.\'.join([b\'ab\', b\'pq\', b\'rs\']) -> b\'ab.pq.rs\'."); |
| 182 | |
| 183 | #define BYTES_JOIN_METHODDEF \ |
| 184 | {"join", (PyCFunction)bytes_join, METH_O, bytes_join__doc__}, |
| 185 | |
| 186 | PyDoc_STRVAR(bytes_strip__doc__, |
| 187 | "strip($self, bytes=None, /)\n" |
| 188 | "--\n" |
| 189 | "\n" |
| 190 | "Strip leading and trailing bytes contained in the argument.\n" |
| 191 | "\n" |
| 192 | "If the argument is omitted or None, strip leading and trailing ASCII whitespace."); |
| 193 | |
| 194 | #define BYTES_STRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 195 | {"strip", (PyCFunction)(void(*)(void))bytes_strip, METH_FASTCALL, bytes_strip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 196 | |
| 197 | static PyObject * |
| 198 | bytes_strip_impl(PyBytesObject *self, PyObject *bytes); |
| 199 | |
| 200 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 201 | bytes_strip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 202 | { |
| 203 | PyObject *return_value = NULL; |
| 204 | PyObject *bytes = Py_None; |
| 205 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 206 | if (!_PyArg_UnpackStack(args, nargs, "strip", |
| 207 | 0, 1, |
| 208 | &bytes)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 209 | goto exit; |
| 210 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 211 | return_value = bytes_strip_impl(self, bytes); |
| 212 | |
| 213 | exit: |
| 214 | return return_value; |
| 215 | } |
| 216 | |
| 217 | PyDoc_STRVAR(bytes_lstrip__doc__, |
| 218 | "lstrip($self, bytes=None, /)\n" |
| 219 | "--\n" |
| 220 | "\n" |
| 221 | "Strip leading bytes contained in the argument.\n" |
| 222 | "\n" |
| 223 | "If the argument is omitted or None, strip leading ASCII whitespace."); |
| 224 | |
| 225 | #define BYTES_LSTRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 226 | {"lstrip", (PyCFunction)(void(*)(void))bytes_lstrip, METH_FASTCALL, bytes_lstrip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 227 | |
| 228 | static PyObject * |
| 229 | bytes_lstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 230 | |
| 231 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 232 | bytes_lstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 233 | { |
| 234 | PyObject *return_value = NULL; |
| 235 | PyObject *bytes = Py_None; |
| 236 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 237 | if (!_PyArg_UnpackStack(args, nargs, "lstrip", |
| 238 | 0, 1, |
| 239 | &bytes)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 240 | goto exit; |
| 241 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 242 | return_value = bytes_lstrip_impl(self, bytes); |
| 243 | |
| 244 | exit: |
| 245 | return return_value; |
| 246 | } |
| 247 | |
| 248 | PyDoc_STRVAR(bytes_rstrip__doc__, |
| 249 | "rstrip($self, bytes=None, /)\n" |
| 250 | "--\n" |
| 251 | "\n" |
| 252 | "Strip trailing bytes contained in the argument.\n" |
| 253 | "\n" |
| 254 | "If the argument is omitted or None, strip trailing ASCII whitespace."); |
| 255 | |
| 256 | #define BYTES_RSTRIP_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 257 | {"rstrip", (PyCFunction)(void(*)(void))bytes_rstrip, METH_FASTCALL, bytes_rstrip__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 258 | |
| 259 | static PyObject * |
| 260 | bytes_rstrip_impl(PyBytesObject *self, PyObject *bytes); |
| 261 | |
| 262 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 263 | bytes_rstrip(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 264 | { |
| 265 | PyObject *return_value = NULL; |
| 266 | PyObject *bytes = Py_None; |
| 267 | |
Sylvain | 7445381 | 2017-06-10 06:51:48 +0200 | [diff] [blame] | 268 | if (!_PyArg_UnpackStack(args, nargs, "rstrip", |
| 269 | 0, 1, |
| 270 | &bytes)) { |
Victor Stinner | 0c4a828 | 2017-01-17 02:21:47 +0100 | [diff] [blame] | 271 | goto exit; |
| 272 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 273 | return_value = bytes_rstrip_impl(self, bytes); |
| 274 | |
| 275 | exit: |
| 276 | return return_value; |
| 277 | } |
| 278 | |
| 279 | PyDoc_STRVAR(bytes_translate__doc__, |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 280 | "translate($self, table, /, delete=b\'\')\n" |
| 281 | "--\n" |
| 282 | "\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 283 | "Return a copy with each character mapped by the given translation table.\n" |
| 284 | "\n" |
| 285 | " table\n" |
| 286 | " Translation table, which must be a bytes object of length 256.\n" |
| 287 | "\n" |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 288 | "All characters occurring in the optional argument delete are removed.\n" |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 289 | "The remaining characters are mapped through the given translation table."); |
| 290 | |
| 291 | #define BYTES_TRANSLATE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 292 | {"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] | 293 | |
| 294 | static PyObject * |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 295 | bytes_translate_impl(PyBytesObject *self, PyObject *table, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 296 | PyObject *deletechars); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 297 | |
| 298 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 299 | 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] | 300 | { |
| 301 | PyObject *return_value = NULL; |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 302 | static const char * const _keywords[] = {"", "delete", NULL}; |
| 303 | static _PyArg_Parser _parser = {"O|O:translate", _keywords, 0}; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 304 | PyObject *table; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 305 | PyObject *deletechars = NULL; |
| 306 | |
Victor Stinner | 3e1fad6 | 2017-01-17 01:29:01 +0100 | [diff] [blame] | 307 | if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 308 | &table, &deletechars)) { |
| 309 | goto exit; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 310 | } |
Martin Panter | 1b6c6da | 2016-08-27 08:35:02 +0000 | [diff] [blame] | 311 | return_value = bytes_translate_impl(self, table, deletechars); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 312 | |
| 313 | exit: |
| 314 | return return_value; |
| 315 | } |
| 316 | |
| 317 | PyDoc_STRVAR(bytes_maketrans__doc__, |
| 318 | "maketrans(frm, to, /)\n" |
| 319 | "--\n" |
| 320 | "\n" |
| 321 | "Return a translation table useable for the bytes or bytearray translate method.\n" |
| 322 | "\n" |
| 323 | "The returned table will be one where each byte in frm is mapped to the byte at\n" |
| 324 | "the same position in to.\n" |
| 325 | "\n" |
| 326 | "The bytes objects frm and to must be of the same length."); |
| 327 | |
| 328 | #define BYTES_MAKETRANS_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 329 | {"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] | 330 | |
| 331 | static PyObject * |
| 332 | bytes_maketrans_impl(Py_buffer *frm, Py_buffer *to); |
| 333 | |
| 334 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 335 | bytes_maketrans(void *null, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 336 | { |
| 337 | PyObject *return_value = NULL; |
| 338 | Py_buffer frm = {NULL, NULL}; |
| 339 | Py_buffer to = {NULL, NULL}; |
| 340 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 341 | if (!_PyArg_CheckPositional("maketrans", nargs, 2, 2)) { |
| 342 | goto exit; |
| 343 | } |
| 344 | if (PyObject_GetBuffer(args[0], &frm, PyBUF_SIMPLE) != 0) { |
| 345 | goto exit; |
| 346 | } |
| 347 | if (!PyBuffer_IsContiguous(&frm, 'C')) { |
| 348 | _PyArg_BadArgument("maketrans", 1, "contiguous buffer", args[0]); |
| 349 | goto exit; |
| 350 | } |
| 351 | if (PyObject_GetBuffer(args[1], &to, PyBUF_SIMPLE) != 0) { |
| 352 | goto exit; |
| 353 | } |
| 354 | if (!PyBuffer_IsContiguous(&to, 'C')) { |
| 355 | _PyArg_BadArgument("maketrans", 2, "contiguous buffer", args[1]); |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 356 | goto exit; |
| 357 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 358 | return_value = bytes_maketrans_impl(&frm, &to); |
| 359 | |
| 360 | exit: |
| 361 | /* Cleanup for frm */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 362 | if (frm.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 363 | PyBuffer_Release(&frm); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 364 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 365 | /* Cleanup for to */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 366 | if (to.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 367 | PyBuffer_Release(&to); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 368 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 369 | |
| 370 | return return_value; |
| 371 | } |
| 372 | |
| 373 | PyDoc_STRVAR(bytes_replace__doc__, |
| 374 | "replace($self, old, new, count=-1, /)\n" |
| 375 | "--\n" |
| 376 | "\n" |
| 377 | "Return a copy with all occurrences of substring old replaced by new.\n" |
| 378 | "\n" |
| 379 | " count\n" |
| 380 | " Maximum number of occurrences to replace.\n" |
| 381 | " -1 (the default value) means replace all occurrences.\n" |
| 382 | "\n" |
| 383 | "If the optional argument count is given, only the first count occurrences are\n" |
| 384 | "replaced."); |
| 385 | |
| 386 | #define BYTES_REPLACE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 387 | {"replace", (PyCFunction)(void(*)(void))bytes_replace, METH_FASTCALL, bytes_replace__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 388 | |
| 389 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 390 | bytes_replace_impl(PyBytesObject *self, Py_buffer *old, Py_buffer *new, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 391 | Py_ssize_t count); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 392 | |
| 393 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 394 | bytes_replace(PyBytesObject *self, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 395 | { |
| 396 | PyObject *return_value = NULL; |
| 397 | Py_buffer old = {NULL, NULL}; |
| 398 | Py_buffer new = {NULL, NULL}; |
| 399 | Py_ssize_t count = -1; |
| 400 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 401 | if (!_PyArg_CheckPositional("replace", nargs, 2, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 402 | goto exit; |
| 403 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 404 | if (PyObject_GetBuffer(args[0], &old, PyBUF_SIMPLE) != 0) { |
| 405 | goto exit; |
| 406 | } |
| 407 | if (!PyBuffer_IsContiguous(&old, 'C')) { |
| 408 | _PyArg_BadArgument("replace", 1, "contiguous buffer", args[0]); |
| 409 | goto exit; |
| 410 | } |
| 411 | if (PyObject_GetBuffer(args[1], &new, PyBUF_SIMPLE) != 0) { |
| 412 | goto exit; |
| 413 | } |
| 414 | if (!PyBuffer_IsContiguous(&new, 'C')) { |
| 415 | _PyArg_BadArgument("replace", 2, "contiguous buffer", args[1]); |
| 416 | goto exit; |
| 417 | } |
| 418 | if (nargs < 3) { |
| 419 | goto skip_optional; |
| 420 | } |
| 421 | if (PyFloat_Check(args[2])) { |
| 422 | PyErr_SetString(PyExc_TypeError, |
| 423 | "integer argument expected, got float" ); |
| 424 | goto exit; |
| 425 | } |
| 426 | { |
| 427 | Py_ssize_t ival = -1; |
| 428 | PyObject *iobj = PyNumber_Index(args[2]); |
| 429 | if (iobj != NULL) { |
| 430 | ival = PyLong_AsSsize_t(iobj); |
| 431 | Py_DECREF(iobj); |
| 432 | } |
| 433 | if (ival == -1 && PyErr_Occurred()) { |
| 434 | goto exit; |
| 435 | } |
| 436 | count = ival; |
| 437 | } |
| 438 | skip_optional: |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 439 | return_value = bytes_replace_impl(self, &old, &new, count); |
| 440 | |
| 441 | exit: |
| 442 | /* Cleanup for old */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 443 | if (old.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 444 | PyBuffer_Release(&old); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 445 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 446 | /* Cleanup for new */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 447 | if (new.obj) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 448 | PyBuffer_Release(&new); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 449 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 450 | |
| 451 | return return_value; |
| 452 | } |
| 453 | |
| 454 | PyDoc_STRVAR(bytes_decode__doc__, |
| 455 | "decode($self, /, encoding=\'utf-8\', errors=\'strict\')\n" |
| 456 | "--\n" |
| 457 | "\n" |
| 458 | "Decode the bytes using the codec registered for encoding.\n" |
| 459 | "\n" |
| 460 | " encoding\n" |
| 461 | " The encoding with which to decode the bytes.\n" |
| 462 | " errors\n" |
| 463 | " The error handling scheme to use for the handling of decoding errors.\n" |
| 464 | " The default is \'strict\' meaning that decoding errors raise a\n" |
| 465 | " UnicodeDecodeError. Other possible values are \'ignore\' and \'replace\'\n" |
| 466 | " as well as any other name registered with codecs.register_error that\n" |
| 467 | " can handle UnicodeDecodeErrors."); |
| 468 | |
| 469 | #define BYTES_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 470 | {"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] | 471 | |
| 472 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 473 | bytes_decode_impl(PyBytesObject *self, const char *encoding, |
Larry Hastings | 89964c4 | 2015-04-14 18:07:59 -0400 | [diff] [blame] | 474 | const char *errors); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 475 | |
| 476 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 477 | 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] | 478 | { |
| 479 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 480 | static const char * const _keywords[] = {"encoding", "errors", NULL}; |
| 481 | static _PyArg_Parser _parser = {"|ss:decode", _keywords, 0}; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 482 | const char *encoding = NULL; |
| 483 | const char *errors = NULL; |
| 484 | |
Victor Stinner | 3e1fad6 | 2017-01-17 01:29:01 +0100 | [diff] [blame] | 485 | if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 486 | &encoding, &errors)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 487 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 488 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 489 | return_value = bytes_decode_impl(self, encoding, errors); |
| 490 | |
| 491 | exit: |
| 492 | return return_value; |
| 493 | } |
| 494 | |
| 495 | PyDoc_STRVAR(bytes_splitlines__doc__, |
| 496 | "splitlines($self, /, keepends=False)\n" |
| 497 | "--\n" |
| 498 | "\n" |
| 499 | "Return a list of the lines in the bytes, breaking at line boundaries.\n" |
| 500 | "\n" |
| 501 | "Line breaks are not included in the resulting list unless keepends is given and\n" |
| 502 | "true."); |
| 503 | |
| 504 | #define BYTES_SPLITLINES_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 505 | {"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] | 506 | |
| 507 | static PyObject * |
Serhiy Storchaka | 7a9579c | 2016-05-02 13:45:20 +0300 | [diff] [blame] | 508 | bytes_splitlines_impl(PyBytesObject *self, int keepends); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 509 | |
| 510 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 511 | 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] | 512 | { |
| 513 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 514 | static const char * const _keywords[] = {"keepends", NULL}; |
| 515 | static _PyArg_Parser _parser = {"|i:splitlines", _keywords, 0}; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 516 | int keepends = 0; |
| 517 | |
Victor Stinner | 3e1fad6 | 2017-01-17 01:29:01 +0100 | [diff] [blame] | 518 | if (!_PyArg_ParseStackAndKeywords(args, nargs, kwnames, &_parser, |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 519 | &keepends)) { |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 520 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 521 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 522 | return_value = bytes_splitlines_impl(self, keepends); |
| 523 | |
| 524 | exit: |
| 525 | return return_value; |
| 526 | } |
| 527 | |
| 528 | PyDoc_STRVAR(bytes_fromhex__doc__, |
| 529 | "fromhex($type, string, /)\n" |
| 530 | "--\n" |
| 531 | "\n" |
| 532 | "Create a bytes object from a string of hexadecimal numbers.\n" |
| 533 | "\n" |
| 534 | "Spaces between two numbers are accepted.\n" |
| 535 | "Example: bytes.fromhex(\'B9 01EF\') -> b\'\\\\xb9\\\\x01\\\\xef\'."); |
| 536 | |
| 537 | #define BYTES_FROMHEX_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 538 | {"fromhex", (PyCFunction)bytes_fromhex, METH_O|METH_CLASS, bytes_fromhex__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 539 | |
| 540 | static PyObject * |
| 541 | bytes_fromhex_impl(PyTypeObject *type, PyObject *string); |
| 542 | |
| 543 | static PyObject * |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 544 | bytes_fromhex(PyTypeObject *type, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 545 | { |
| 546 | PyObject *return_value = NULL; |
| 547 | PyObject *string; |
| 548 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 549 | if (!PyUnicode_Check(arg)) { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 550 | _PyArg_BadArgument("fromhex", 0, "str", arg); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 551 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 552 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 553 | if (PyUnicode_READY(arg) == -1) { |
| 554 | goto exit; |
| 555 | } |
| 556 | string = arg; |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 557 | return_value = bytes_fromhex_impl(type, string); |
| 558 | |
| 559 | exit: |
| 560 | return return_value; |
| 561 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame^] | 562 | /*[clinic end generated code: output=810c8dfc72520ca4 input=a9049054013a1b77]*/ |