Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 1 | /*[clinic input] |
| 2 | preserve |
| 3 | [clinic start generated code]*/ |
| 4 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 5 | PyDoc_STRVAR(_codecs_register__doc__, |
| 6 | "register($module, search_function, /)\n" |
| 7 | "--\n" |
| 8 | "\n" |
| 9 | "Register a codec search function.\n" |
| 10 | "\n" |
| 11 | "Search functions are expected to take one argument, the encoding name in\n" |
| 12 | "all lower case letters, and either return None, or a tuple of functions\n" |
| 13 | "(encoder, decoder, stream_reader, stream_writer) (or a CodecInfo object)."); |
| 14 | |
| 15 | #define _CODECS_REGISTER_METHODDEF \ |
| 16 | {"register", (PyCFunction)_codecs_register, METH_O, _codecs_register__doc__}, |
| 17 | |
| 18 | PyDoc_STRVAR(_codecs_lookup__doc__, |
| 19 | "lookup($module, encoding, /)\n" |
| 20 | "--\n" |
| 21 | "\n" |
| 22 | "Looks up a codec tuple in the Python codec registry and returns a CodecInfo object."); |
| 23 | |
| 24 | #define _CODECS_LOOKUP_METHODDEF \ |
| 25 | {"lookup", (PyCFunction)_codecs_lookup, METH_O, _codecs_lookup__doc__}, |
| 26 | |
| 27 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 28 | _codecs_lookup_impl(PyObject *module, const char *encoding); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 29 | |
| 30 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 31 | _codecs_lookup(PyObject *module, PyObject *arg) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 32 | { |
| 33 | PyObject *return_value = NULL; |
| 34 | const char *encoding; |
| 35 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 36 | if (!PyUnicode_Check(arg)) { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 37 | _PyArg_BadArgument("lookup", 0, "str", arg); |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 38 | goto exit; |
| 39 | } |
| 40 | Py_ssize_t encoding_length; |
| 41 | encoding = PyUnicode_AsUTF8AndSize(arg, &encoding_length); |
| 42 | if (encoding == NULL) { |
| 43 | goto exit; |
| 44 | } |
| 45 | if (strlen(encoding) != (size_t)encoding_length) { |
| 46 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 47 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 48 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 49 | return_value = _codecs_lookup_impl(module, encoding); |
| 50 | |
| 51 | exit: |
| 52 | return return_value; |
| 53 | } |
| 54 | |
| 55 | PyDoc_STRVAR(_codecs_encode__doc__, |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 56 | "encode($module, /, obj, encoding=\'utf-8\', errors=\'strict\')\n" |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 57 | "--\n" |
| 58 | "\n" |
| 59 | "Encodes obj using the codec registered for encoding.\n" |
| 60 | "\n" |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 61 | "The default encoding is \'utf-8\'. errors may be given to set a\n" |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 62 | "different error handling scheme. Default is \'strict\' meaning that encoding\n" |
| 63 | "errors raise a ValueError. Other possible values are \'ignore\', \'replace\'\n" |
| 64 | "and \'backslashreplace\' as well as any other name registered with\n" |
| 65 | "codecs.register_error that can handle ValueErrors."); |
| 66 | |
| 67 | #define _CODECS_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 68 | {"encode", (PyCFunction)(void(*)(void))_codecs_encode, METH_FASTCALL|METH_KEYWORDS, _codecs_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 69 | |
| 70 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 71 | _codecs_encode_impl(PyObject *module, PyObject *obj, const char *encoding, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 72 | const char *errors); |
| 73 | |
| 74 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 75 | _codecs_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 76 | { |
| 77 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 78 | static const char * const _keywords[] = {"obj", "encoding", "errors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 79 | static _PyArg_Parser _parser = {NULL, _keywords, "encode", 0}; |
| 80 | PyObject *argsbuf[3]; |
| 81 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 82 | PyObject *obj; |
| 83 | const char *encoding = NULL; |
| 84 | const char *errors = NULL; |
| 85 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 86 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf); |
| 87 | if (!args) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 88 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 89 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 90 | obj = args[0]; |
| 91 | if (!noptargs) { |
| 92 | goto skip_optional_pos; |
| 93 | } |
| 94 | if (args[1]) { |
| 95 | if (!PyUnicode_Check(args[1])) { |
| 96 | _PyArg_BadArgument("encode", 2, "str", args[1]); |
| 97 | goto exit; |
| 98 | } |
| 99 | Py_ssize_t encoding_length; |
| 100 | encoding = PyUnicode_AsUTF8AndSize(args[1], &encoding_length); |
| 101 | if (encoding == NULL) { |
| 102 | goto exit; |
| 103 | } |
| 104 | if (strlen(encoding) != (size_t)encoding_length) { |
| 105 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 106 | goto exit; |
| 107 | } |
| 108 | if (!--noptargs) { |
| 109 | goto skip_optional_pos; |
| 110 | } |
| 111 | } |
| 112 | if (!PyUnicode_Check(args[2])) { |
| 113 | _PyArg_BadArgument("encode", 3, "str", args[2]); |
| 114 | goto exit; |
| 115 | } |
| 116 | Py_ssize_t errors_length; |
| 117 | errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length); |
| 118 | if (errors == NULL) { |
| 119 | goto exit; |
| 120 | } |
| 121 | if (strlen(errors) != (size_t)errors_length) { |
| 122 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 123 | goto exit; |
| 124 | } |
| 125 | skip_optional_pos: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 126 | return_value = _codecs_encode_impl(module, obj, encoding, errors); |
| 127 | |
| 128 | exit: |
| 129 | return return_value; |
| 130 | } |
| 131 | |
| 132 | PyDoc_STRVAR(_codecs_decode__doc__, |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 133 | "decode($module, /, obj, encoding=\'utf-8\', errors=\'strict\')\n" |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 134 | "--\n" |
| 135 | "\n" |
| 136 | "Decodes obj using the codec registered for encoding.\n" |
| 137 | "\n" |
Serhiy Storchaka | c97a962 | 2015-08-09 12:23:08 +0300 | [diff] [blame] | 138 | "Default encoding is \'utf-8\'. errors may be given to set a\n" |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 139 | "different error handling scheme. Default is \'strict\' meaning that encoding\n" |
| 140 | "errors raise a ValueError. Other possible values are \'ignore\', \'replace\'\n" |
| 141 | "and \'backslashreplace\' as well as any other name registered with\n" |
| 142 | "codecs.register_error that can handle ValueErrors."); |
| 143 | |
| 144 | #define _CODECS_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 145 | {"decode", (PyCFunction)(void(*)(void))_codecs_decode, METH_FASTCALL|METH_KEYWORDS, _codecs_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 146 | |
| 147 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 148 | _codecs_decode_impl(PyObject *module, PyObject *obj, const char *encoding, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 149 | const char *errors); |
| 150 | |
| 151 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 152 | _codecs_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 153 | { |
| 154 | PyObject *return_value = NULL; |
Serhiy Storchaka | 9171a8b | 2016-08-14 10:52:18 +0300 | [diff] [blame] | 155 | static const char * const _keywords[] = {"obj", "encoding", "errors", NULL}; |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 156 | static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0}; |
| 157 | PyObject *argsbuf[3]; |
| 158 | Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 159 | PyObject *obj; |
| 160 | const char *encoding = NULL; |
| 161 | const char *errors = NULL; |
| 162 | |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 163 | args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf); |
| 164 | if (!args) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 165 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 166 | } |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 167 | obj = args[0]; |
| 168 | if (!noptargs) { |
| 169 | goto skip_optional_pos; |
| 170 | } |
| 171 | if (args[1]) { |
| 172 | if (!PyUnicode_Check(args[1])) { |
| 173 | _PyArg_BadArgument("decode", 2, "str", args[1]); |
| 174 | goto exit; |
| 175 | } |
| 176 | Py_ssize_t encoding_length; |
| 177 | encoding = PyUnicode_AsUTF8AndSize(args[1], &encoding_length); |
| 178 | if (encoding == NULL) { |
| 179 | goto exit; |
| 180 | } |
| 181 | if (strlen(encoding) != (size_t)encoding_length) { |
| 182 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 183 | goto exit; |
| 184 | } |
| 185 | if (!--noptargs) { |
| 186 | goto skip_optional_pos; |
| 187 | } |
| 188 | } |
| 189 | if (!PyUnicode_Check(args[2])) { |
| 190 | _PyArg_BadArgument("decode", 3, "str", args[2]); |
| 191 | goto exit; |
| 192 | } |
| 193 | Py_ssize_t errors_length; |
| 194 | errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length); |
| 195 | if (errors == NULL) { |
| 196 | goto exit; |
| 197 | } |
| 198 | if (strlen(errors) != (size_t)errors_length) { |
| 199 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 200 | goto exit; |
| 201 | } |
| 202 | skip_optional_pos: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 203 | return_value = _codecs_decode_impl(module, obj, encoding, errors); |
| 204 | |
| 205 | exit: |
| 206 | return return_value; |
| 207 | } |
| 208 | |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 209 | PyDoc_STRVAR(_codecs__forget_codec__doc__, |
| 210 | "_forget_codec($module, encoding, /)\n" |
| 211 | "--\n" |
| 212 | "\n" |
| 213 | "Purge the named codec from the internal codec lookup cache"); |
| 214 | |
| 215 | #define _CODECS__FORGET_CODEC_METHODDEF \ |
Serhiy Storchaka | 92e8af6 | 2015-04-04 00:12:11 +0300 | [diff] [blame] | 216 | {"_forget_codec", (PyCFunction)_codecs__forget_codec, METH_O, _codecs__forget_codec__doc__}, |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 217 | |
| 218 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 219 | _codecs__forget_codec_impl(PyObject *module, const char *encoding); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 220 | |
| 221 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 222 | _codecs__forget_codec(PyObject *module, PyObject *arg) |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 223 | { |
| 224 | PyObject *return_value = NULL; |
| 225 | const char *encoding; |
| 226 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 227 | if (!PyUnicode_Check(arg)) { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 228 | _PyArg_BadArgument("_forget_codec", 0, "str", arg); |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 229 | goto exit; |
| 230 | } |
| 231 | Py_ssize_t encoding_length; |
| 232 | encoding = PyUnicode_AsUTF8AndSize(arg, &encoding_length); |
| 233 | if (encoding == NULL) { |
| 234 | goto exit; |
| 235 | } |
| 236 | if (strlen(encoding) != (size_t)encoding_length) { |
| 237 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 238 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 239 | } |
Serhiy Storchaka | 1009bf1 | 2015-04-03 23:53:51 +0300 | [diff] [blame] | 240 | return_value = _codecs__forget_codec_impl(module, encoding); |
| 241 | |
| 242 | exit: |
| 243 | return return_value; |
| 244 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 245 | |
| 246 | PyDoc_STRVAR(_codecs_escape_decode__doc__, |
| 247 | "escape_decode($module, data, errors=None, /)\n" |
| 248 | "--\n" |
| 249 | "\n"); |
| 250 | |
| 251 | #define _CODECS_ESCAPE_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 252 | {"escape_decode", (PyCFunction)(void(*)(void))_codecs_escape_decode, METH_FASTCALL, _codecs_escape_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 253 | |
| 254 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 255 | _codecs_escape_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 256 | const char *errors); |
| 257 | |
| 258 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 259 | _codecs_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 260 | { |
| 261 | PyObject *return_value = NULL; |
| 262 | Py_buffer data = {NULL, NULL}; |
| 263 | const char *errors = NULL; |
| 264 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 265 | if (!_PyArg_CheckPositional("escape_decode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 266 | goto exit; |
| 267 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 268 | if (PyUnicode_Check(args[0])) { |
| 269 | Py_ssize_t len; |
| 270 | const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len); |
| 271 | if (ptr == NULL) { |
| 272 | goto exit; |
| 273 | } |
| 274 | PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0); |
| 275 | } |
| 276 | else { /* any bytes-like object */ |
| 277 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 278 | goto exit; |
| 279 | } |
| 280 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 281 | _PyArg_BadArgument("escape_decode", 1, "contiguous buffer", args[0]); |
| 282 | goto exit; |
| 283 | } |
| 284 | } |
| 285 | if (nargs < 2) { |
| 286 | goto skip_optional; |
| 287 | } |
| 288 | if (args[1] == Py_None) { |
| 289 | errors = NULL; |
| 290 | } |
| 291 | else if (PyUnicode_Check(args[1])) { |
| 292 | Py_ssize_t errors_length; |
| 293 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 294 | if (errors == NULL) { |
| 295 | goto exit; |
| 296 | } |
| 297 | if (strlen(errors) != (size_t)errors_length) { |
| 298 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 299 | goto exit; |
| 300 | } |
| 301 | } |
| 302 | else { |
| 303 | _PyArg_BadArgument("escape_decode", 2, "str or None", args[1]); |
| 304 | goto exit; |
| 305 | } |
| 306 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 307 | return_value = _codecs_escape_decode_impl(module, &data, errors); |
| 308 | |
| 309 | exit: |
| 310 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 311 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 312 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 313 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 314 | |
| 315 | return return_value; |
| 316 | } |
| 317 | |
| 318 | PyDoc_STRVAR(_codecs_escape_encode__doc__, |
| 319 | "escape_encode($module, data, errors=None, /)\n" |
| 320 | "--\n" |
| 321 | "\n"); |
| 322 | |
| 323 | #define _CODECS_ESCAPE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 324 | {"escape_encode", (PyCFunction)(void(*)(void))_codecs_escape_encode, METH_FASTCALL, _codecs_escape_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 325 | |
| 326 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 327 | _codecs_escape_encode_impl(PyObject *module, PyObject *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 328 | const char *errors); |
| 329 | |
| 330 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 331 | _codecs_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 332 | { |
| 333 | PyObject *return_value = NULL; |
| 334 | PyObject *data; |
| 335 | const char *errors = NULL; |
| 336 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 337 | if (!_PyArg_CheckPositional("escape_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 338 | goto exit; |
| 339 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 340 | if (!PyBytes_Check(args[0])) { |
| 341 | _PyArg_BadArgument("escape_encode", 1, "bytes", args[0]); |
| 342 | goto exit; |
| 343 | } |
| 344 | data = args[0]; |
| 345 | if (nargs < 2) { |
| 346 | goto skip_optional; |
| 347 | } |
| 348 | if (args[1] == Py_None) { |
| 349 | errors = NULL; |
| 350 | } |
| 351 | else if (PyUnicode_Check(args[1])) { |
| 352 | Py_ssize_t errors_length; |
| 353 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 354 | if (errors == NULL) { |
| 355 | goto exit; |
| 356 | } |
| 357 | if (strlen(errors) != (size_t)errors_length) { |
| 358 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 359 | goto exit; |
| 360 | } |
| 361 | } |
| 362 | else { |
| 363 | _PyArg_BadArgument("escape_encode", 2, "str or None", args[1]); |
| 364 | goto exit; |
| 365 | } |
| 366 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 367 | return_value = _codecs_escape_encode_impl(module, data, errors); |
| 368 | |
| 369 | exit: |
| 370 | return return_value; |
| 371 | } |
| 372 | |
| 373 | PyDoc_STRVAR(_codecs_unicode_internal_decode__doc__, |
| 374 | "unicode_internal_decode($module, obj, errors=None, /)\n" |
| 375 | "--\n" |
| 376 | "\n"); |
| 377 | |
| 378 | #define _CODECS_UNICODE_INTERNAL_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 379 | {"unicode_internal_decode", (PyCFunction)(void(*)(void))_codecs_unicode_internal_decode, METH_FASTCALL, _codecs_unicode_internal_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 380 | |
| 381 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 382 | _codecs_unicode_internal_decode_impl(PyObject *module, PyObject *obj, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 383 | const char *errors); |
| 384 | |
| 385 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 386 | _codecs_unicode_internal_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 387 | { |
| 388 | PyObject *return_value = NULL; |
| 389 | PyObject *obj; |
| 390 | const char *errors = NULL; |
| 391 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 392 | if (!_PyArg_CheckPositional("unicode_internal_decode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 393 | goto exit; |
| 394 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 395 | obj = args[0]; |
| 396 | if (nargs < 2) { |
| 397 | goto skip_optional; |
| 398 | } |
| 399 | if (args[1] == Py_None) { |
| 400 | errors = NULL; |
| 401 | } |
| 402 | else if (PyUnicode_Check(args[1])) { |
| 403 | Py_ssize_t errors_length; |
| 404 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 405 | if (errors == NULL) { |
| 406 | goto exit; |
| 407 | } |
| 408 | if (strlen(errors) != (size_t)errors_length) { |
| 409 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 410 | goto exit; |
| 411 | } |
| 412 | } |
| 413 | else { |
| 414 | _PyArg_BadArgument("unicode_internal_decode", 2, "str or None", args[1]); |
| 415 | goto exit; |
| 416 | } |
| 417 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 418 | return_value = _codecs_unicode_internal_decode_impl(module, obj, errors); |
| 419 | |
| 420 | exit: |
| 421 | return return_value; |
| 422 | } |
| 423 | |
| 424 | PyDoc_STRVAR(_codecs_utf_7_decode__doc__, |
| 425 | "utf_7_decode($module, data, errors=None, final=False, /)\n" |
| 426 | "--\n" |
| 427 | "\n"); |
| 428 | |
| 429 | #define _CODECS_UTF_7_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 430 | {"utf_7_decode", (PyCFunction)(void(*)(void))_codecs_utf_7_decode, METH_FASTCALL, _codecs_utf_7_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 431 | |
| 432 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 433 | _codecs_utf_7_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 434 | const char *errors, int final); |
| 435 | |
| 436 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 437 | _codecs_utf_7_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 438 | { |
| 439 | PyObject *return_value = NULL; |
| 440 | Py_buffer data = {NULL, NULL}; |
| 441 | const char *errors = NULL; |
| 442 | int final = 0; |
| 443 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 444 | if (!_PyArg_CheckPositional("utf_7_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 445 | goto exit; |
| 446 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 447 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 448 | goto exit; |
| 449 | } |
| 450 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 451 | _PyArg_BadArgument("utf_7_decode", 1, "contiguous buffer", args[0]); |
| 452 | goto exit; |
| 453 | } |
| 454 | if (nargs < 2) { |
| 455 | goto skip_optional; |
| 456 | } |
| 457 | if (args[1] == Py_None) { |
| 458 | errors = NULL; |
| 459 | } |
| 460 | else if (PyUnicode_Check(args[1])) { |
| 461 | Py_ssize_t errors_length; |
| 462 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 463 | if (errors == NULL) { |
| 464 | goto exit; |
| 465 | } |
| 466 | if (strlen(errors) != (size_t)errors_length) { |
| 467 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 468 | goto exit; |
| 469 | } |
| 470 | } |
| 471 | else { |
| 472 | _PyArg_BadArgument("utf_7_decode", 2, "str or None", args[1]); |
| 473 | goto exit; |
| 474 | } |
| 475 | if (nargs < 3) { |
| 476 | goto skip_optional; |
| 477 | } |
| 478 | if (PyFloat_Check(args[2])) { |
| 479 | PyErr_SetString(PyExc_TypeError, |
| 480 | "integer argument expected, got float" ); |
| 481 | goto exit; |
| 482 | } |
| 483 | final = _PyLong_AsInt(args[2]); |
| 484 | if (final == -1 && PyErr_Occurred()) { |
| 485 | goto exit; |
| 486 | } |
| 487 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 488 | return_value = _codecs_utf_7_decode_impl(module, &data, errors, final); |
| 489 | |
| 490 | exit: |
| 491 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 492 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 493 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 494 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 495 | |
| 496 | return return_value; |
| 497 | } |
| 498 | |
| 499 | PyDoc_STRVAR(_codecs_utf_8_decode__doc__, |
| 500 | "utf_8_decode($module, data, errors=None, final=False, /)\n" |
| 501 | "--\n" |
| 502 | "\n"); |
| 503 | |
| 504 | #define _CODECS_UTF_8_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 505 | {"utf_8_decode", (PyCFunction)(void(*)(void))_codecs_utf_8_decode, METH_FASTCALL, _codecs_utf_8_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 506 | |
| 507 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 508 | _codecs_utf_8_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 509 | const char *errors, int final); |
| 510 | |
| 511 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 512 | _codecs_utf_8_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 513 | { |
| 514 | PyObject *return_value = NULL; |
| 515 | Py_buffer data = {NULL, NULL}; |
| 516 | const char *errors = NULL; |
| 517 | int final = 0; |
| 518 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 519 | if (!_PyArg_CheckPositional("utf_8_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 520 | goto exit; |
| 521 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 522 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 523 | goto exit; |
| 524 | } |
| 525 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 526 | _PyArg_BadArgument("utf_8_decode", 1, "contiguous buffer", args[0]); |
| 527 | goto exit; |
| 528 | } |
| 529 | if (nargs < 2) { |
| 530 | goto skip_optional; |
| 531 | } |
| 532 | if (args[1] == Py_None) { |
| 533 | errors = NULL; |
| 534 | } |
| 535 | else if (PyUnicode_Check(args[1])) { |
| 536 | Py_ssize_t errors_length; |
| 537 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 538 | if (errors == NULL) { |
| 539 | goto exit; |
| 540 | } |
| 541 | if (strlen(errors) != (size_t)errors_length) { |
| 542 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 543 | goto exit; |
| 544 | } |
| 545 | } |
| 546 | else { |
| 547 | _PyArg_BadArgument("utf_8_decode", 2, "str or None", args[1]); |
| 548 | goto exit; |
| 549 | } |
| 550 | if (nargs < 3) { |
| 551 | goto skip_optional; |
| 552 | } |
| 553 | if (PyFloat_Check(args[2])) { |
| 554 | PyErr_SetString(PyExc_TypeError, |
| 555 | "integer argument expected, got float" ); |
| 556 | goto exit; |
| 557 | } |
| 558 | final = _PyLong_AsInt(args[2]); |
| 559 | if (final == -1 && PyErr_Occurred()) { |
| 560 | goto exit; |
| 561 | } |
| 562 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 563 | return_value = _codecs_utf_8_decode_impl(module, &data, errors, final); |
| 564 | |
| 565 | exit: |
| 566 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 567 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 568 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 569 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 570 | |
| 571 | return return_value; |
| 572 | } |
| 573 | |
| 574 | PyDoc_STRVAR(_codecs_utf_16_decode__doc__, |
| 575 | "utf_16_decode($module, data, errors=None, final=False, /)\n" |
| 576 | "--\n" |
| 577 | "\n"); |
| 578 | |
| 579 | #define _CODECS_UTF_16_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 580 | {"utf_16_decode", (PyCFunction)(void(*)(void))_codecs_utf_16_decode, METH_FASTCALL, _codecs_utf_16_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 581 | |
| 582 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 583 | _codecs_utf_16_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 584 | const char *errors, int final); |
| 585 | |
| 586 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 587 | _codecs_utf_16_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 588 | { |
| 589 | PyObject *return_value = NULL; |
| 590 | Py_buffer data = {NULL, NULL}; |
| 591 | const char *errors = NULL; |
| 592 | int final = 0; |
| 593 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 594 | if (!_PyArg_CheckPositional("utf_16_decode", nargs, 1, 3)) { |
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 (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 598 | goto exit; |
| 599 | } |
| 600 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 601 | _PyArg_BadArgument("utf_16_decode", 1, "contiguous buffer", args[0]); |
| 602 | goto exit; |
| 603 | } |
| 604 | if (nargs < 2) { |
| 605 | goto skip_optional; |
| 606 | } |
| 607 | if (args[1] == Py_None) { |
| 608 | errors = NULL; |
| 609 | } |
| 610 | else if (PyUnicode_Check(args[1])) { |
| 611 | Py_ssize_t errors_length; |
| 612 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 613 | if (errors == NULL) { |
| 614 | goto exit; |
| 615 | } |
| 616 | if (strlen(errors) != (size_t)errors_length) { |
| 617 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 618 | goto exit; |
| 619 | } |
| 620 | } |
| 621 | else { |
| 622 | _PyArg_BadArgument("utf_16_decode", 2, "str or None", args[1]); |
| 623 | goto exit; |
| 624 | } |
| 625 | if (nargs < 3) { |
| 626 | goto skip_optional; |
| 627 | } |
| 628 | if (PyFloat_Check(args[2])) { |
| 629 | PyErr_SetString(PyExc_TypeError, |
| 630 | "integer argument expected, got float" ); |
| 631 | goto exit; |
| 632 | } |
| 633 | final = _PyLong_AsInt(args[2]); |
| 634 | if (final == -1 && PyErr_Occurred()) { |
| 635 | goto exit; |
| 636 | } |
| 637 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 638 | return_value = _codecs_utf_16_decode_impl(module, &data, errors, final); |
| 639 | |
| 640 | exit: |
| 641 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 642 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 643 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 644 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 645 | |
| 646 | return return_value; |
| 647 | } |
| 648 | |
| 649 | PyDoc_STRVAR(_codecs_utf_16_le_decode__doc__, |
| 650 | "utf_16_le_decode($module, data, errors=None, final=False, /)\n" |
| 651 | "--\n" |
| 652 | "\n"); |
| 653 | |
| 654 | #define _CODECS_UTF_16_LE_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 655 | {"utf_16_le_decode", (PyCFunction)(void(*)(void))_codecs_utf_16_le_decode, METH_FASTCALL, _codecs_utf_16_le_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 656 | |
| 657 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 658 | _codecs_utf_16_le_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 659 | const char *errors, int final); |
| 660 | |
| 661 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 662 | _codecs_utf_16_le_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 663 | { |
| 664 | PyObject *return_value = NULL; |
| 665 | Py_buffer data = {NULL, NULL}; |
| 666 | const char *errors = NULL; |
| 667 | int final = 0; |
| 668 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 669 | if (!_PyArg_CheckPositional("utf_16_le_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 670 | goto exit; |
| 671 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 672 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 673 | goto exit; |
| 674 | } |
| 675 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 676 | _PyArg_BadArgument("utf_16_le_decode", 1, "contiguous buffer", args[0]); |
| 677 | goto exit; |
| 678 | } |
| 679 | if (nargs < 2) { |
| 680 | goto skip_optional; |
| 681 | } |
| 682 | if (args[1] == Py_None) { |
| 683 | errors = NULL; |
| 684 | } |
| 685 | else if (PyUnicode_Check(args[1])) { |
| 686 | Py_ssize_t errors_length; |
| 687 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 688 | if (errors == NULL) { |
| 689 | goto exit; |
| 690 | } |
| 691 | if (strlen(errors) != (size_t)errors_length) { |
| 692 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 693 | goto exit; |
| 694 | } |
| 695 | } |
| 696 | else { |
| 697 | _PyArg_BadArgument("utf_16_le_decode", 2, "str or None", args[1]); |
| 698 | goto exit; |
| 699 | } |
| 700 | if (nargs < 3) { |
| 701 | goto skip_optional; |
| 702 | } |
| 703 | if (PyFloat_Check(args[2])) { |
| 704 | PyErr_SetString(PyExc_TypeError, |
| 705 | "integer argument expected, got float" ); |
| 706 | goto exit; |
| 707 | } |
| 708 | final = _PyLong_AsInt(args[2]); |
| 709 | if (final == -1 && PyErr_Occurred()) { |
| 710 | goto exit; |
| 711 | } |
| 712 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 713 | return_value = _codecs_utf_16_le_decode_impl(module, &data, errors, final); |
| 714 | |
| 715 | exit: |
| 716 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 717 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 718 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 719 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 720 | |
| 721 | return return_value; |
| 722 | } |
| 723 | |
| 724 | PyDoc_STRVAR(_codecs_utf_16_be_decode__doc__, |
| 725 | "utf_16_be_decode($module, data, errors=None, final=False, /)\n" |
| 726 | "--\n" |
| 727 | "\n"); |
| 728 | |
| 729 | #define _CODECS_UTF_16_BE_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 730 | {"utf_16_be_decode", (PyCFunction)(void(*)(void))_codecs_utf_16_be_decode, METH_FASTCALL, _codecs_utf_16_be_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 731 | |
| 732 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 733 | _codecs_utf_16_be_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 734 | const char *errors, int final); |
| 735 | |
| 736 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 737 | _codecs_utf_16_be_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 738 | { |
| 739 | PyObject *return_value = NULL; |
| 740 | Py_buffer data = {NULL, NULL}; |
| 741 | const char *errors = NULL; |
| 742 | int final = 0; |
| 743 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 744 | if (!_PyArg_CheckPositional("utf_16_be_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 745 | goto exit; |
| 746 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 747 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 748 | goto exit; |
| 749 | } |
| 750 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 751 | _PyArg_BadArgument("utf_16_be_decode", 1, "contiguous buffer", args[0]); |
| 752 | goto exit; |
| 753 | } |
| 754 | if (nargs < 2) { |
| 755 | goto skip_optional; |
| 756 | } |
| 757 | if (args[1] == Py_None) { |
| 758 | errors = NULL; |
| 759 | } |
| 760 | else if (PyUnicode_Check(args[1])) { |
| 761 | Py_ssize_t errors_length; |
| 762 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 763 | if (errors == NULL) { |
| 764 | goto exit; |
| 765 | } |
| 766 | if (strlen(errors) != (size_t)errors_length) { |
| 767 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 768 | goto exit; |
| 769 | } |
| 770 | } |
| 771 | else { |
| 772 | _PyArg_BadArgument("utf_16_be_decode", 2, "str or None", args[1]); |
| 773 | goto exit; |
| 774 | } |
| 775 | if (nargs < 3) { |
| 776 | goto skip_optional; |
| 777 | } |
| 778 | if (PyFloat_Check(args[2])) { |
| 779 | PyErr_SetString(PyExc_TypeError, |
| 780 | "integer argument expected, got float" ); |
| 781 | goto exit; |
| 782 | } |
| 783 | final = _PyLong_AsInt(args[2]); |
| 784 | if (final == -1 && PyErr_Occurred()) { |
| 785 | goto exit; |
| 786 | } |
| 787 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 788 | return_value = _codecs_utf_16_be_decode_impl(module, &data, errors, final); |
| 789 | |
| 790 | exit: |
| 791 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 792 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 793 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 794 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 795 | |
| 796 | return return_value; |
| 797 | } |
| 798 | |
| 799 | PyDoc_STRVAR(_codecs_utf_16_ex_decode__doc__, |
| 800 | "utf_16_ex_decode($module, data, errors=None, byteorder=0, final=False,\n" |
| 801 | " /)\n" |
| 802 | "--\n" |
| 803 | "\n"); |
| 804 | |
| 805 | #define _CODECS_UTF_16_EX_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 806 | {"utf_16_ex_decode", (PyCFunction)(void(*)(void))_codecs_utf_16_ex_decode, METH_FASTCALL, _codecs_utf_16_ex_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 807 | |
| 808 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 809 | _codecs_utf_16_ex_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 810 | const char *errors, int byteorder, int final); |
| 811 | |
| 812 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 813 | _codecs_utf_16_ex_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 814 | { |
| 815 | PyObject *return_value = NULL; |
| 816 | Py_buffer data = {NULL, NULL}; |
| 817 | const char *errors = NULL; |
| 818 | int byteorder = 0; |
| 819 | int final = 0; |
| 820 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 821 | if (!_PyArg_CheckPositional("utf_16_ex_decode", nargs, 1, 4)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 822 | goto exit; |
| 823 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 824 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 825 | goto exit; |
| 826 | } |
| 827 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 828 | _PyArg_BadArgument("utf_16_ex_decode", 1, "contiguous buffer", args[0]); |
| 829 | goto exit; |
| 830 | } |
| 831 | if (nargs < 2) { |
| 832 | goto skip_optional; |
| 833 | } |
| 834 | if (args[1] == Py_None) { |
| 835 | errors = NULL; |
| 836 | } |
| 837 | else if (PyUnicode_Check(args[1])) { |
| 838 | Py_ssize_t errors_length; |
| 839 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 840 | if (errors == NULL) { |
| 841 | goto exit; |
| 842 | } |
| 843 | if (strlen(errors) != (size_t)errors_length) { |
| 844 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 845 | goto exit; |
| 846 | } |
| 847 | } |
| 848 | else { |
| 849 | _PyArg_BadArgument("utf_16_ex_decode", 2, "str or None", args[1]); |
| 850 | goto exit; |
| 851 | } |
| 852 | if (nargs < 3) { |
| 853 | goto skip_optional; |
| 854 | } |
| 855 | if (PyFloat_Check(args[2])) { |
| 856 | PyErr_SetString(PyExc_TypeError, |
| 857 | "integer argument expected, got float" ); |
| 858 | goto exit; |
| 859 | } |
| 860 | byteorder = _PyLong_AsInt(args[2]); |
| 861 | if (byteorder == -1 && PyErr_Occurred()) { |
| 862 | goto exit; |
| 863 | } |
| 864 | if (nargs < 4) { |
| 865 | goto skip_optional; |
| 866 | } |
| 867 | if (PyFloat_Check(args[3])) { |
| 868 | PyErr_SetString(PyExc_TypeError, |
| 869 | "integer argument expected, got float" ); |
| 870 | goto exit; |
| 871 | } |
| 872 | final = _PyLong_AsInt(args[3]); |
| 873 | if (final == -1 && PyErr_Occurred()) { |
| 874 | goto exit; |
| 875 | } |
| 876 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 877 | return_value = _codecs_utf_16_ex_decode_impl(module, &data, errors, byteorder, final); |
| 878 | |
| 879 | exit: |
| 880 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 881 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 882 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 883 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 884 | |
| 885 | return return_value; |
| 886 | } |
| 887 | |
| 888 | PyDoc_STRVAR(_codecs_utf_32_decode__doc__, |
| 889 | "utf_32_decode($module, data, errors=None, final=False, /)\n" |
| 890 | "--\n" |
| 891 | "\n"); |
| 892 | |
| 893 | #define _CODECS_UTF_32_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 894 | {"utf_32_decode", (PyCFunction)(void(*)(void))_codecs_utf_32_decode, METH_FASTCALL, _codecs_utf_32_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 895 | |
| 896 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 897 | _codecs_utf_32_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 898 | const char *errors, int final); |
| 899 | |
| 900 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 901 | _codecs_utf_32_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 902 | { |
| 903 | PyObject *return_value = NULL; |
| 904 | Py_buffer data = {NULL, NULL}; |
| 905 | const char *errors = NULL; |
| 906 | int final = 0; |
| 907 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 908 | if (!_PyArg_CheckPositional("utf_32_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 909 | goto exit; |
| 910 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 911 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 912 | goto exit; |
| 913 | } |
| 914 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 915 | _PyArg_BadArgument("utf_32_decode", 1, "contiguous buffer", args[0]); |
| 916 | goto exit; |
| 917 | } |
| 918 | if (nargs < 2) { |
| 919 | goto skip_optional; |
| 920 | } |
| 921 | if (args[1] == Py_None) { |
| 922 | errors = NULL; |
| 923 | } |
| 924 | else if (PyUnicode_Check(args[1])) { |
| 925 | Py_ssize_t errors_length; |
| 926 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 927 | if (errors == NULL) { |
| 928 | goto exit; |
| 929 | } |
| 930 | if (strlen(errors) != (size_t)errors_length) { |
| 931 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 932 | goto exit; |
| 933 | } |
| 934 | } |
| 935 | else { |
| 936 | _PyArg_BadArgument("utf_32_decode", 2, "str or None", args[1]); |
| 937 | goto exit; |
| 938 | } |
| 939 | if (nargs < 3) { |
| 940 | goto skip_optional; |
| 941 | } |
| 942 | if (PyFloat_Check(args[2])) { |
| 943 | PyErr_SetString(PyExc_TypeError, |
| 944 | "integer argument expected, got float" ); |
| 945 | goto exit; |
| 946 | } |
| 947 | final = _PyLong_AsInt(args[2]); |
| 948 | if (final == -1 && PyErr_Occurred()) { |
| 949 | goto exit; |
| 950 | } |
| 951 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 952 | return_value = _codecs_utf_32_decode_impl(module, &data, errors, final); |
| 953 | |
| 954 | exit: |
| 955 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 956 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 957 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 958 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 959 | |
| 960 | return return_value; |
| 961 | } |
| 962 | |
| 963 | PyDoc_STRVAR(_codecs_utf_32_le_decode__doc__, |
| 964 | "utf_32_le_decode($module, data, errors=None, final=False, /)\n" |
| 965 | "--\n" |
| 966 | "\n"); |
| 967 | |
| 968 | #define _CODECS_UTF_32_LE_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 969 | {"utf_32_le_decode", (PyCFunction)(void(*)(void))_codecs_utf_32_le_decode, METH_FASTCALL, _codecs_utf_32_le_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 970 | |
| 971 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 972 | _codecs_utf_32_le_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 973 | const char *errors, int final); |
| 974 | |
| 975 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 976 | _codecs_utf_32_le_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 977 | { |
| 978 | PyObject *return_value = NULL; |
| 979 | Py_buffer data = {NULL, NULL}; |
| 980 | const char *errors = NULL; |
| 981 | int final = 0; |
| 982 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 983 | if (!_PyArg_CheckPositional("utf_32_le_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 984 | goto exit; |
| 985 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 986 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 987 | goto exit; |
| 988 | } |
| 989 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 990 | _PyArg_BadArgument("utf_32_le_decode", 1, "contiguous buffer", args[0]); |
| 991 | goto exit; |
| 992 | } |
| 993 | if (nargs < 2) { |
| 994 | goto skip_optional; |
| 995 | } |
| 996 | if (args[1] == Py_None) { |
| 997 | errors = NULL; |
| 998 | } |
| 999 | else if (PyUnicode_Check(args[1])) { |
| 1000 | Py_ssize_t errors_length; |
| 1001 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1002 | if (errors == NULL) { |
| 1003 | goto exit; |
| 1004 | } |
| 1005 | if (strlen(errors) != (size_t)errors_length) { |
| 1006 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1007 | goto exit; |
| 1008 | } |
| 1009 | } |
| 1010 | else { |
| 1011 | _PyArg_BadArgument("utf_32_le_decode", 2, "str or None", args[1]); |
| 1012 | goto exit; |
| 1013 | } |
| 1014 | if (nargs < 3) { |
| 1015 | goto skip_optional; |
| 1016 | } |
| 1017 | if (PyFloat_Check(args[2])) { |
| 1018 | PyErr_SetString(PyExc_TypeError, |
| 1019 | "integer argument expected, got float" ); |
| 1020 | goto exit; |
| 1021 | } |
| 1022 | final = _PyLong_AsInt(args[2]); |
| 1023 | if (final == -1 && PyErr_Occurred()) { |
| 1024 | goto exit; |
| 1025 | } |
| 1026 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1027 | return_value = _codecs_utf_32_le_decode_impl(module, &data, errors, final); |
| 1028 | |
| 1029 | exit: |
| 1030 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1031 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1032 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1033 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1034 | |
| 1035 | return return_value; |
| 1036 | } |
| 1037 | |
| 1038 | PyDoc_STRVAR(_codecs_utf_32_be_decode__doc__, |
| 1039 | "utf_32_be_decode($module, data, errors=None, final=False, /)\n" |
| 1040 | "--\n" |
| 1041 | "\n"); |
| 1042 | |
| 1043 | #define _CODECS_UTF_32_BE_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1044 | {"utf_32_be_decode", (PyCFunction)(void(*)(void))_codecs_utf_32_be_decode, METH_FASTCALL, _codecs_utf_32_be_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1045 | |
| 1046 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1047 | _codecs_utf_32_be_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1048 | const char *errors, int final); |
| 1049 | |
| 1050 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1051 | _codecs_utf_32_be_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1052 | { |
| 1053 | PyObject *return_value = NULL; |
| 1054 | Py_buffer data = {NULL, NULL}; |
| 1055 | const char *errors = NULL; |
| 1056 | int final = 0; |
| 1057 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1058 | if (!_PyArg_CheckPositional("utf_32_be_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1059 | goto exit; |
| 1060 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1061 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1062 | goto exit; |
| 1063 | } |
| 1064 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1065 | _PyArg_BadArgument("utf_32_be_decode", 1, "contiguous buffer", args[0]); |
| 1066 | goto exit; |
| 1067 | } |
| 1068 | if (nargs < 2) { |
| 1069 | goto skip_optional; |
| 1070 | } |
| 1071 | if (args[1] == Py_None) { |
| 1072 | errors = NULL; |
| 1073 | } |
| 1074 | else if (PyUnicode_Check(args[1])) { |
| 1075 | Py_ssize_t errors_length; |
| 1076 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1077 | if (errors == NULL) { |
| 1078 | goto exit; |
| 1079 | } |
| 1080 | if (strlen(errors) != (size_t)errors_length) { |
| 1081 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1082 | goto exit; |
| 1083 | } |
| 1084 | } |
| 1085 | else { |
| 1086 | _PyArg_BadArgument("utf_32_be_decode", 2, "str or None", args[1]); |
| 1087 | goto exit; |
| 1088 | } |
| 1089 | if (nargs < 3) { |
| 1090 | goto skip_optional; |
| 1091 | } |
| 1092 | if (PyFloat_Check(args[2])) { |
| 1093 | PyErr_SetString(PyExc_TypeError, |
| 1094 | "integer argument expected, got float" ); |
| 1095 | goto exit; |
| 1096 | } |
| 1097 | final = _PyLong_AsInt(args[2]); |
| 1098 | if (final == -1 && PyErr_Occurred()) { |
| 1099 | goto exit; |
| 1100 | } |
| 1101 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1102 | return_value = _codecs_utf_32_be_decode_impl(module, &data, errors, final); |
| 1103 | |
| 1104 | exit: |
| 1105 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1106 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1107 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1108 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1109 | |
| 1110 | return return_value; |
| 1111 | } |
| 1112 | |
| 1113 | PyDoc_STRVAR(_codecs_utf_32_ex_decode__doc__, |
| 1114 | "utf_32_ex_decode($module, data, errors=None, byteorder=0, final=False,\n" |
| 1115 | " /)\n" |
| 1116 | "--\n" |
| 1117 | "\n"); |
| 1118 | |
| 1119 | #define _CODECS_UTF_32_EX_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1120 | {"utf_32_ex_decode", (PyCFunction)(void(*)(void))_codecs_utf_32_ex_decode, METH_FASTCALL, _codecs_utf_32_ex_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1121 | |
| 1122 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1123 | _codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1124 | const char *errors, int byteorder, int final); |
| 1125 | |
| 1126 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1127 | _codecs_utf_32_ex_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1128 | { |
| 1129 | PyObject *return_value = NULL; |
| 1130 | Py_buffer data = {NULL, NULL}; |
| 1131 | const char *errors = NULL; |
| 1132 | int byteorder = 0; |
| 1133 | int final = 0; |
| 1134 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1135 | if (!_PyArg_CheckPositional("utf_32_ex_decode", nargs, 1, 4)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1136 | goto exit; |
| 1137 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1138 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1139 | goto exit; |
| 1140 | } |
| 1141 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1142 | _PyArg_BadArgument("utf_32_ex_decode", 1, "contiguous buffer", args[0]); |
| 1143 | goto exit; |
| 1144 | } |
| 1145 | if (nargs < 2) { |
| 1146 | goto skip_optional; |
| 1147 | } |
| 1148 | if (args[1] == Py_None) { |
| 1149 | errors = NULL; |
| 1150 | } |
| 1151 | else if (PyUnicode_Check(args[1])) { |
| 1152 | Py_ssize_t errors_length; |
| 1153 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1154 | if (errors == NULL) { |
| 1155 | goto exit; |
| 1156 | } |
| 1157 | if (strlen(errors) != (size_t)errors_length) { |
| 1158 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1159 | goto exit; |
| 1160 | } |
| 1161 | } |
| 1162 | else { |
| 1163 | _PyArg_BadArgument("utf_32_ex_decode", 2, "str or None", args[1]); |
| 1164 | goto exit; |
| 1165 | } |
| 1166 | if (nargs < 3) { |
| 1167 | goto skip_optional; |
| 1168 | } |
| 1169 | if (PyFloat_Check(args[2])) { |
| 1170 | PyErr_SetString(PyExc_TypeError, |
| 1171 | "integer argument expected, got float" ); |
| 1172 | goto exit; |
| 1173 | } |
| 1174 | byteorder = _PyLong_AsInt(args[2]); |
| 1175 | if (byteorder == -1 && PyErr_Occurred()) { |
| 1176 | goto exit; |
| 1177 | } |
| 1178 | if (nargs < 4) { |
| 1179 | goto skip_optional; |
| 1180 | } |
| 1181 | if (PyFloat_Check(args[3])) { |
| 1182 | PyErr_SetString(PyExc_TypeError, |
| 1183 | "integer argument expected, got float" ); |
| 1184 | goto exit; |
| 1185 | } |
| 1186 | final = _PyLong_AsInt(args[3]); |
| 1187 | if (final == -1 && PyErr_Occurred()) { |
| 1188 | goto exit; |
| 1189 | } |
| 1190 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1191 | return_value = _codecs_utf_32_ex_decode_impl(module, &data, errors, byteorder, final); |
| 1192 | |
| 1193 | exit: |
| 1194 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1195 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1196 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1197 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1198 | |
| 1199 | return return_value; |
| 1200 | } |
| 1201 | |
| 1202 | PyDoc_STRVAR(_codecs_unicode_escape_decode__doc__, |
| 1203 | "unicode_escape_decode($module, data, errors=None, /)\n" |
| 1204 | "--\n" |
| 1205 | "\n"); |
| 1206 | |
| 1207 | #define _CODECS_UNICODE_ESCAPE_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1208 | {"unicode_escape_decode", (PyCFunction)(void(*)(void))_codecs_unicode_escape_decode, METH_FASTCALL, _codecs_unicode_escape_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1209 | |
| 1210 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1211 | _codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1212 | const char *errors); |
| 1213 | |
| 1214 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1215 | _codecs_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1216 | { |
| 1217 | PyObject *return_value = NULL; |
| 1218 | Py_buffer data = {NULL, NULL}; |
| 1219 | const char *errors = NULL; |
| 1220 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1221 | if (!_PyArg_CheckPositional("unicode_escape_decode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1222 | goto exit; |
| 1223 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1224 | if (PyUnicode_Check(args[0])) { |
| 1225 | Py_ssize_t len; |
| 1226 | const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len); |
| 1227 | if (ptr == NULL) { |
| 1228 | goto exit; |
| 1229 | } |
| 1230 | PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0); |
| 1231 | } |
| 1232 | else { /* any bytes-like object */ |
| 1233 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1234 | goto exit; |
| 1235 | } |
| 1236 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1237 | _PyArg_BadArgument("unicode_escape_decode", 1, "contiguous buffer", args[0]); |
| 1238 | goto exit; |
| 1239 | } |
| 1240 | } |
| 1241 | if (nargs < 2) { |
| 1242 | goto skip_optional; |
| 1243 | } |
| 1244 | if (args[1] == Py_None) { |
| 1245 | errors = NULL; |
| 1246 | } |
| 1247 | else if (PyUnicode_Check(args[1])) { |
| 1248 | Py_ssize_t errors_length; |
| 1249 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1250 | if (errors == NULL) { |
| 1251 | goto exit; |
| 1252 | } |
| 1253 | if (strlen(errors) != (size_t)errors_length) { |
| 1254 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1255 | goto exit; |
| 1256 | } |
| 1257 | } |
| 1258 | else { |
| 1259 | _PyArg_BadArgument("unicode_escape_decode", 2, "str or None", args[1]); |
| 1260 | goto exit; |
| 1261 | } |
| 1262 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1263 | return_value = _codecs_unicode_escape_decode_impl(module, &data, errors); |
| 1264 | |
| 1265 | exit: |
| 1266 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1267 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1268 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1269 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1270 | |
| 1271 | return return_value; |
| 1272 | } |
| 1273 | |
| 1274 | PyDoc_STRVAR(_codecs_raw_unicode_escape_decode__doc__, |
| 1275 | "raw_unicode_escape_decode($module, data, errors=None, /)\n" |
| 1276 | "--\n" |
| 1277 | "\n"); |
| 1278 | |
| 1279 | #define _CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1280 | {"raw_unicode_escape_decode", (PyCFunction)(void(*)(void))_codecs_raw_unicode_escape_decode, METH_FASTCALL, _codecs_raw_unicode_escape_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1281 | |
| 1282 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1283 | _codecs_raw_unicode_escape_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1284 | const char *errors); |
| 1285 | |
| 1286 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1287 | _codecs_raw_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1288 | { |
| 1289 | PyObject *return_value = NULL; |
| 1290 | Py_buffer data = {NULL, NULL}; |
| 1291 | const char *errors = NULL; |
| 1292 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1293 | if (!_PyArg_CheckPositional("raw_unicode_escape_decode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1294 | goto exit; |
| 1295 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1296 | if (PyUnicode_Check(args[0])) { |
| 1297 | Py_ssize_t len; |
| 1298 | const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len); |
| 1299 | if (ptr == NULL) { |
| 1300 | goto exit; |
| 1301 | } |
| 1302 | PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0); |
| 1303 | } |
| 1304 | else { /* any bytes-like object */ |
| 1305 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1306 | goto exit; |
| 1307 | } |
| 1308 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1309 | _PyArg_BadArgument("raw_unicode_escape_decode", 1, "contiguous buffer", args[0]); |
| 1310 | goto exit; |
| 1311 | } |
| 1312 | } |
| 1313 | if (nargs < 2) { |
| 1314 | goto skip_optional; |
| 1315 | } |
| 1316 | if (args[1] == Py_None) { |
| 1317 | errors = NULL; |
| 1318 | } |
| 1319 | else if (PyUnicode_Check(args[1])) { |
| 1320 | Py_ssize_t errors_length; |
| 1321 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1322 | if (errors == NULL) { |
| 1323 | goto exit; |
| 1324 | } |
| 1325 | if (strlen(errors) != (size_t)errors_length) { |
| 1326 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1327 | goto exit; |
| 1328 | } |
| 1329 | } |
| 1330 | else { |
| 1331 | _PyArg_BadArgument("raw_unicode_escape_decode", 2, "str or None", args[1]); |
| 1332 | goto exit; |
| 1333 | } |
| 1334 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1335 | return_value = _codecs_raw_unicode_escape_decode_impl(module, &data, errors); |
| 1336 | |
| 1337 | exit: |
| 1338 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1339 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1340 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1341 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1342 | |
| 1343 | return return_value; |
| 1344 | } |
| 1345 | |
| 1346 | PyDoc_STRVAR(_codecs_latin_1_decode__doc__, |
| 1347 | "latin_1_decode($module, data, errors=None, /)\n" |
| 1348 | "--\n" |
| 1349 | "\n"); |
| 1350 | |
| 1351 | #define _CODECS_LATIN_1_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1352 | {"latin_1_decode", (PyCFunction)(void(*)(void))_codecs_latin_1_decode, METH_FASTCALL, _codecs_latin_1_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1353 | |
| 1354 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1355 | _codecs_latin_1_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1356 | const char *errors); |
| 1357 | |
| 1358 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1359 | _codecs_latin_1_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1360 | { |
| 1361 | PyObject *return_value = NULL; |
| 1362 | Py_buffer data = {NULL, NULL}; |
| 1363 | const char *errors = NULL; |
| 1364 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1365 | if (!_PyArg_CheckPositional("latin_1_decode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1366 | goto exit; |
| 1367 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1368 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1369 | goto exit; |
| 1370 | } |
| 1371 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1372 | _PyArg_BadArgument("latin_1_decode", 1, "contiguous buffer", args[0]); |
| 1373 | goto exit; |
| 1374 | } |
| 1375 | if (nargs < 2) { |
| 1376 | goto skip_optional; |
| 1377 | } |
| 1378 | if (args[1] == Py_None) { |
| 1379 | errors = NULL; |
| 1380 | } |
| 1381 | else if (PyUnicode_Check(args[1])) { |
| 1382 | Py_ssize_t errors_length; |
| 1383 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1384 | if (errors == NULL) { |
| 1385 | goto exit; |
| 1386 | } |
| 1387 | if (strlen(errors) != (size_t)errors_length) { |
| 1388 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1389 | goto exit; |
| 1390 | } |
| 1391 | } |
| 1392 | else { |
| 1393 | _PyArg_BadArgument("latin_1_decode", 2, "str or None", args[1]); |
| 1394 | goto exit; |
| 1395 | } |
| 1396 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1397 | return_value = _codecs_latin_1_decode_impl(module, &data, errors); |
| 1398 | |
| 1399 | exit: |
| 1400 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1401 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1402 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1403 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1404 | |
| 1405 | return return_value; |
| 1406 | } |
| 1407 | |
| 1408 | PyDoc_STRVAR(_codecs_ascii_decode__doc__, |
| 1409 | "ascii_decode($module, data, errors=None, /)\n" |
| 1410 | "--\n" |
| 1411 | "\n"); |
| 1412 | |
| 1413 | #define _CODECS_ASCII_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1414 | {"ascii_decode", (PyCFunction)(void(*)(void))_codecs_ascii_decode, METH_FASTCALL, _codecs_ascii_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1415 | |
| 1416 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1417 | _codecs_ascii_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1418 | const char *errors); |
| 1419 | |
| 1420 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1421 | _codecs_ascii_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1422 | { |
| 1423 | PyObject *return_value = NULL; |
| 1424 | Py_buffer data = {NULL, NULL}; |
| 1425 | const char *errors = NULL; |
| 1426 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1427 | if (!_PyArg_CheckPositional("ascii_decode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1428 | goto exit; |
| 1429 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1430 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1431 | goto exit; |
| 1432 | } |
| 1433 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1434 | _PyArg_BadArgument("ascii_decode", 1, "contiguous buffer", args[0]); |
| 1435 | goto exit; |
| 1436 | } |
| 1437 | if (nargs < 2) { |
| 1438 | goto skip_optional; |
| 1439 | } |
| 1440 | if (args[1] == Py_None) { |
| 1441 | errors = NULL; |
| 1442 | } |
| 1443 | else if (PyUnicode_Check(args[1])) { |
| 1444 | Py_ssize_t errors_length; |
| 1445 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1446 | if (errors == NULL) { |
| 1447 | goto exit; |
| 1448 | } |
| 1449 | if (strlen(errors) != (size_t)errors_length) { |
| 1450 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1451 | goto exit; |
| 1452 | } |
| 1453 | } |
| 1454 | else { |
| 1455 | _PyArg_BadArgument("ascii_decode", 2, "str or None", args[1]); |
| 1456 | goto exit; |
| 1457 | } |
| 1458 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1459 | return_value = _codecs_ascii_decode_impl(module, &data, errors); |
| 1460 | |
| 1461 | exit: |
| 1462 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1463 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1464 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1465 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1466 | |
| 1467 | return return_value; |
| 1468 | } |
| 1469 | |
| 1470 | PyDoc_STRVAR(_codecs_charmap_decode__doc__, |
| 1471 | "charmap_decode($module, data, errors=None, mapping=None, /)\n" |
| 1472 | "--\n" |
| 1473 | "\n"); |
| 1474 | |
| 1475 | #define _CODECS_CHARMAP_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1476 | {"charmap_decode", (PyCFunction)(void(*)(void))_codecs_charmap_decode, METH_FASTCALL, _codecs_charmap_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1477 | |
| 1478 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1479 | _codecs_charmap_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1480 | const char *errors, PyObject *mapping); |
| 1481 | |
| 1482 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1483 | _codecs_charmap_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1484 | { |
| 1485 | PyObject *return_value = NULL; |
| 1486 | Py_buffer data = {NULL, NULL}; |
| 1487 | const char *errors = NULL; |
| 1488 | PyObject *mapping = NULL; |
| 1489 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1490 | if (!_PyArg_CheckPositional("charmap_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1491 | goto exit; |
| 1492 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1493 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1494 | goto exit; |
| 1495 | } |
| 1496 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1497 | _PyArg_BadArgument("charmap_decode", 1, "contiguous buffer", args[0]); |
| 1498 | goto exit; |
| 1499 | } |
| 1500 | if (nargs < 2) { |
| 1501 | goto skip_optional; |
| 1502 | } |
| 1503 | if (args[1] == Py_None) { |
| 1504 | errors = NULL; |
| 1505 | } |
| 1506 | else if (PyUnicode_Check(args[1])) { |
| 1507 | Py_ssize_t errors_length; |
| 1508 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1509 | if (errors == NULL) { |
| 1510 | goto exit; |
| 1511 | } |
| 1512 | if (strlen(errors) != (size_t)errors_length) { |
| 1513 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1514 | goto exit; |
| 1515 | } |
| 1516 | } |
| 1517 | else { |
| 1518 | _PyArg_BadArgument("charmap_decode", 2, "str or None", args[1]); |
| 1519 | goto exit; |
| 1520 | } |
| 1521 | if (nargs < 3) { |
| 1522 | goto skip_optional; |
| 1523 | } |
| 1524 | mapping = args[2]; |
| 1525 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1526 | return_value = _codecs_charmap_decode_impl(module, &data, errors, mapping); |
| 1527 | |
| 1528 | exit: |
| 1529 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1530 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1531 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1532 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1533 | |
| 1534 | return return_value; |
| 1535 | } |
| 1536 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1537 | #if defined(MS_WINDOWS) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1538 | |
| 1539 | PyDoc_STRVAR(_codecs_mbcs_decode__doc__, |
| 1540 | "mbcs_decode($module, data, errors=None, final=False, /)\n" |
| 1541 | "--\n" |
| 1542 | "\n"); |
| 1543 | |
| 1544 | #define _CODECS_MBCS_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1545 | {"mbcs_decode", (PyCFunction)(void(*)(void))_codecs_mbcs_decode, METH_FASTCALL, _codecs_mbcs_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1546 | |
| 1547 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1548 | _codecs_mbcs_decode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1549 | const char *errors, int final); |
| 1550 | |
| 1551 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1552 | _codecs_mbcs_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1553 | { |
| 1554 | PyObject *return_value = NULL; |
| 1555 | Py_buffer data = {NULL, NULL}; |
| 1556 | const char *errors = NULL; |
| 1557 | int final = 0; |
| 1558 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1559 | if (!_PyArg_CheckPositional("mbcs_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1560 | goto exit; |
| 1561 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1562 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1563 | goto exit; |
| 1564 | } |
| 1565 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1566 | _PyArg_BadArgument("mbcs_decode", 1, "contiguous buffer", args[0]); |
| 1567 | goto exit; |
| 1568 | } |
| 1569 | if (nargs < 2) { |
| 1570 | goto skip_optional; |
| 1571 | } |
| 1572 | if (args[1] == Py_None) { |
| 1573 | errors = NULL; |
| 1574 | } |
| 1575 | else if (PyUnicode_Check(args[1])) { |
| 1576 | Py_ssize_t errors_length; |
| 1577 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1578 | if (errors == NULL) { |
| 1579 | goto exit; |
| 1580 | } |
| 1581 | if (strlen(errors) != (size_t)errors_length) { |
| 1582 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1583 | goto exit; |
| 1584 | } |
| 1585 | } |
| 1586 | else { |
| 1587 | _PyArg_BadArgument("mbcs_decode", 2, "str or None", args[1]); |
| 1588 | goto exit; |
| 1589 | } |
| 1590 | if (nargs < 3) { |
| 1591 | goto skip_optional; |
| 1592 | } |
| 1593 | if (PyFloat_Check(args[2])) { |
| 1594 | PyErr_SetString(PyExc_TypeError, |
| 1595 | "integer argument expected, got float" ); |
| 1596 | goto exit; |
| 1597 | } |
| 1598 | final = _PyLong_AsInt(args[2]); |
| 1599 | if (final == -1 && PyErr_Occurred()) { |
| 1600 | goto exit; |
| 1601 | } |
| 1602 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1603 | return_value = _codecs_mbcs_decode_impl(module, &data, errors, final); |
| 1604 | |
| 1605 | exit: |
| 1606 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1607 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1608 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1609 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1610 | |
| 1611 | return return_value; |
| 1612 | } |
| 1613 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1614 | #endif /* defined(MS_WINDOWS) */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1615 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1616 | #if defined(MS_WINDOWS) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1617 | |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 1618 | PyDoc_STRVAR(_codecs_oem_decode__doc__, |
| 1619 | "oem_decode($module, data, errors=None, final=False, /)\n" |
| 1620 | "--\n" |
| 1621 | "\n"); |
| 1622 | |
| 1623 | #define _CODECS_OEM_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1624 | {"oem_decode", (PyCFunction)(void(*)(void))_codecs_oem_decode, METH_FASTCALL, _codecs_oem_decode__doc__}, |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 1625 | |
| 1626 | static PyObject * |
| 1627 | _codecs_oem_decode_impl(PyObject *module, Py_buffer *data, |
| 1628 | const char *errors, int final); |
| 1629 | |
| 1630 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1631 | _codecs_oem_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 1632 | { |
| 1633 | PyObject *return_value = NULL; |
| 1634 | Py_buffer data = {NULL, NULL}; |
| 1635 | const char *errors = NULL; |
| 1636 | int final = 0; |
| 1637 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1638 | if (!_PyArg_CheckPositional("oem_decode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1639 | goto exit; |
| 1640 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1641 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1642 | goto exit; |
| 1643 | } |
| 1644 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1645 | _PyArg_BadArgument("oem_decode", 1, "contiguous buffer", args[0]); |
| 1646 | goto exit; |
| 1647 | } |
| 1648 | if (nargs < 2) { |
| 1649 | goto skip_optional; |
| 1650 | } |
| 1651 | if (args[1] == Py_None) { |
| 1652 | errors = NULL; |
| 1653 | } |
| 1654 | else if (PyUnicode_Check(args[1])) { |
| 1655 | Py_ssize_t errors_length; |
| 1656 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1657 | if (errors == NULL) { |
| 1658 | goto exit; |
| 1659 | } |
| 1660 | if (strlen(errors) != (size_t)errors_length) { |
| 1661 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1662 | goto exit; |
| 1663 | } |
| 1664 | } |
| 1665 | else { |
| 1666 | _PyArg_BadArgument("oem_decode", 2, "str or None", args[1]); |
| 1667 | goto exit; |
| 1668 | } |
| 1669 | if (nargs < 3) { |
| 1670 | goto skip_optional; |
| 1671 | } |
| 1672 | if (PyFloat_Check(args[2])) { |
| 1673 | PyErr_SetString(PyExc_TypeError, |
| 1674 | "integer argument expected, got float" ); |
| 1675 | goto exit; |
| 1676 | } |
| 1677 | final = _PyLong_AsInt(args[2]); |
| 1678 | if (final == -1 && PyErr_Occurred()) { |
| 1679 | goto exit; |
| 1680 | } |
| 1681 | skip_optional: |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 1682 | return_value = _codecs_oem_decode_impl(module, &data, errors, final); |
| 1683 | |
| 1684 | exit: |
| 1685 | /* Cleanup for data */ |
| 1686 | if (data.obj) { |
| 1687 | PyBuffer_Release(&data); |
| 1688 | } |
| 1689 | |
| 1690 | return return_value; |
| 1691 | } |
| 1692 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1693 | #endif /* defined(MS_WINDOWS) */ |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 1694 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1695 | #if defined(MS_WINDOWS) |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 1696 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1697 | PyDoc_STRVAR(_codecs_code_page_decode__doc__, |
| 1698 | "code_page_decode($module, codepage, data, errors=None, final=False, /)\n" |
| 1699 | "--\n" |
| 1700 | "\n"); |
| 1701 | |
| 1702 | #define _CODECS_CODE_PAGE_DECODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1703 | {"code_page_decode", (PyCFunction)(void(*)(void))_codecs_code_page_decode, METH_FASTCALL, _codecs_code_page_decode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1704 | |
| 1705 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1706 | _codecs_code_page_decode_impl(PyObject *module, int codepage, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1707 | Py_buffer *data, const char *errors, int final); |
| 1708 | |
| 1709 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1710 | _codecs_code_page_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1711 | { |
| 1712 | PyObject *return_value = NULL; |
| 1713 | int codepage; |
| 1714 | Py_buffer data = {NULL, NULL}; |
| 1715 | const char *errors = NULL; |
| 1716 | int final = 0; |
| 1717 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1718 | if (!_PyArg_CheckPositional("code_page_decode", nargs, 2, 4)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1719 | goto exit; |
| 1720 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1721 | if (PyFloat_Check(args[0])) { |
| 1722 | PyErr_SetString(PyExc_TypeError, |
| 1723 | "integer argument expected, got float" ); |
| 1724 | goto exit; |
| 1725 | } |
| 1726 | codepage = _PyLong_AsInt(args[0]); |
| 1727 | if (codepage == -1 && PyErr_Occurred()) { |
| 1728 | goto exit; |
| 1729 | } |
| 1730 | if (PyObject_GetBuffer(args[1], &data, PyBUF_SIMPLE) != 0) { |
| 1731 | goto exit; |
| 1732 | } |
| 1733 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1734 | _PyArg_BadArgument("code_page_decode", 2, "contiguous buffer", args[1]); |
| 1735 | goto exit; |
| 1736 | } |
| 1737 | if (nargs < 3) { |
| 1738 | goto skip_optional; |
| 1739 | } |
| 1740 | if (args[2] == Py_None) { |
| 1741 | errors = NULL; |
| 1742 | } |
| 1743 | else if (PyUnicode_Check(args[2])) { |
| 1744 | Py_ssize_t errors_length; |
| 1745 | errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length); |
| 1746 | if (errors == NULL) { |
| 1747 | goto exit; |
| 1748 | } |
| 1749 | if (strlen(errors) != (size_t)errors_length) { |
| 1750 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1751 | goto exit; |
| 1752 | } |
| 1753 | } |
| 1754 | else { |
| 1755 | _PyArg_BadArgument("code_page_decode", 3, "str or None", args[2]); |
| 1756 | goto exit; |
| 1757 | } |
| 1758 | if (nargs < 4) { |
| 1759 | goto skip_optional; |
| 1760 | } |
| 1761 | if (PyFloat_Check(args[3])) { |
| 1762 | PyErr_SetString(PyExc_TypeError, |
| 1763 | "integer argument expected, got float" ); |
| 1764 | goto exit; |
| 1765 | } |
| 1766 | final = _PyLong_AsInt(args[3]); |
| 1767 | if (final == -1 && PyErr_Occurred()) { |
| 1768 | goto exit; |
| 1769 | } |
| 1770 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1771 | return_value = _codecs_code_page_decode_impl(module, codepage, &data, errors, final); |
| 1772 | |
| 1773 | exit: |
| 1774 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1775 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1776 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1777 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1778 | |
| 1779 | return return_value; |
| 1780 | } |
| 1781 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 1782 | #endif /* defined(MS_WINDOWS) */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1783 | |
| 1784 | PyDoc_STRVAR(_codecs_readbuffer_encode__doc__, |
| 1785 | "readbuffer_encode($module, data, errors=None, /)\n" |
| 1786 | "--\n" |
| 1787 | "\n"); |
| 1788 | |
| 1789 | #define _CODECS_READBUFFER_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1790 | {"readbuffer_encode", (PyCFunction)(void(*)(void))_codecs_readbuffer_encode, METH_FASTCALL, _codecs_readbuffer_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1791 | |
| 1792 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1793 | _codecs_readbuffer_encode_impl(PyObject *module, Py_buffer *data, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1794 | const char *errors); |
| 1795 | |
| 1796 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1797 | _codecs_readbuffer_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1798 | { |
| 1799 | PyObject *return_value = NULL; |
| 1800 | Py_buffer data = {NULL, NULL}; |
| 1801 | const char *errors = NULL; |
| 1802 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1803 | if (!_PyArg_CheckPositional("readbuffer_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1804 | goto exit; |
| 1805 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1806 | if (PyUnicode_Check(args[0])) { |
| 1807 | Py_ssize_t len; |
| 1808 | const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len); |
| 1809 | if (ptr == NULL) { |
| 1810 | goto exit; |
| 1811 | } |
| 1812 | PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0); |
| 1813 | } |
| 1814 | else { /* any bytes-like object */ |
| 1815 | if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) { |
| 1816 | goto exit; |
| 1817 | } |
| 1818 | if (!PyBuffer_IsContiguous(&data, 'C')) { |
| 1819 | _PyArg_BadArgument("readbuffer_encode", 1, "contiguous buffer", args[0]); |
| 1820 | goto exit; |
| 1821 | } |
| 1822 | } |
| 1823 | if (nargs < 2) { |
| 1824 | goto skip_optional; |
| 1825 | } |
| 1826 | if (args[1] == Py_None) { |
| 1827 | errors = NULL; |
| 1828 | } |
| 1829 | else if (PyUnicode_Check(args[1])) { |
| 1830 | Py_ssize_t errors_length; |
| 1831 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1832 | if (errors == NULL) { |
| 1833 | goto exit; |
| 1834 | } |
| 1835 | if (strlen(errors) != (size_t)errors_length) { |
| 1836 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1837 | goto exit; |
| 1838 | } |
| 1839 | } |
| 1840 | else { |
| 1841 | _PyArg_BadArgument("readbuffer_encode", 2, "str or None", args[1]); |
| 1842 | goto exit; |
| 1843 | } |
| 1844 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1845 | return_value = _codecs_readbuffer_encode_impl(module, &data, errors); |
| 1846 | |
| 1847 | exit: |
| 1848 | /* Cleanup for data */ |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1849 | if (data.obj) { |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1850 | PyBuffer_Release(&data); |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 1851 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1852 | |
| 1853 | return return_value; |
| 1854 | } |
| 1855 | |
| 1856 | PyDoc_STRVAR(_codecs_unicode_internal_encode__doc__, |
| 1857 | "unicode_internal_encode($module, obj, errors=None, /)\n" |
| 1858 | "--\n" |
| 1859 | "\n"); |
| 1860 | |
| 1861 | #define _CODECS_UNICODE_INTERNAL_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1862 | {"unicode_internal_encode", (PyCFunction)(void(*)(void))_codecs_unicode_internal_encode, METH_FASTCALL, _codecs_unicode_internal_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1863 | |
| 1864 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1865 | _codecs_unicode_internal_encode_impl(PyObject *module, PyObject *obj, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1866 | const char *errors); |
| 1867 | |
| 1868 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1869 | _codecs_unicode_internal_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1870 | { |
| 1871 | PyObject *return_value = NULL; |
| 1872 | PyObject *obj; |
| 1873 | const char *errors = NULL; |
| 1874 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1875 | if (!_PyArg_CheckPositional("unicode_internal_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1876 | goto exit; |
| 1877 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1878 | obj = args[0]; |
| 1879 | if (nargs < 2) { |
| 1880 | goto skip_optional; |
| 1881 | } |
| 1882 | if (args[1] == Py_None) { |
| 1883 | errors = NULL; |
| 1884 | } |
| 1885 | else if (PyUnicode_Check(args[1])) { |
| 1886 | Py_ssize_t errors_length; |
| 1887 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1888 | if (errors == NULL) { |
| 1889 | goto exit; |
| 1890 | } |
| 1891 | if (strlen(errors) != (size_t)errors_length) { |
| 1892 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1893 | goto exit; |
| 1894 | } |
| 1895 | } |
| 1896 | else { |
| 1897 | _PyArg_BadArgument("unicode_internal_encode", 2, "str or None", args[1]); |
| 1898 | goto exit; |
| 1899 | } |
| 1900 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1901 | return_value = _codecs_unicode_internal_encode_impl(module, obj, errors); |
| 1902 | |
| 1903 | exit: |
| 1904 | return return_value; |
| 1905 | } |
| 1906 | |
| 1907 | PyDoc_STRVAR(_codecs_utf_7_encode__doc__, |
| 1908 | "utf_7_encode($module, str, errors=None, /)\n" |
| 1909 | "--\n" |
| 1910 | "\n"); |
| 1911 | |
| 1912 | #define _CODECS_UTF_7_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1913 | {"utf_7_encode", (PyCFunction)(void(*)(void))_codecs_utf_7_encode, METH_FASTCALL, _codecs_utf_7_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1914 | |
| 1915 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1916 | _codecs_utf_7_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1917 | const char *errors); |
| 1918 | |
| 1919 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1920 | _codecs_utf_7_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1921 | { |
| 1922 | PyObject *return_value = NULL; |
| 1923 | PyObject *str; |
| 1924 | const char *errors = NULL; |
| 1925 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1926 | if (!_PyArg_CheckPositional("utf_7_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1927 | goto exit; |
| 1928 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1929 | if (!PyUnicode_Check(args[0])) { |
| 1930 | _PyArg_BadArgument("utf_7_encode", 1, "str", args[0]); |
| 1931 | goto exit; |
| 1932 | } |
| 1933 | if (PyUnicode_READY(args[0]) == -1) { |
| 1934 | goto exit; |
| 1935 | } |
| 1936 | str = args[0]; |
| 1937 | if (nargs < 2) { |
| 1938 | goto skip_optional; |
| 1939 | } |
| 1940 | if (args[1] == Py_None) { |
| 1941 | errors = NULL; |
| 1942 | } |
| 1943 | else if (PyUnicode_Check(args[1])) { |
| 1944 | Py_ssize_t errors_length; |
| 1945 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 1946 | if (errors == NULL) { |
| 1947 | goto exit; |
| 1948 | } |
| 1949 | if (strlen(errors) != (size_t)errors_length) { |
| 1950 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 1951 | goto exit; |
| 1952 | } |
| 1953 | } |
| 1954 | else { |
| 1955 | _PyArg_BadArgument("utf_7_encode", 2, "str or None", args[1]); |
| 1956 | goto exit; |
| 1957 | } |
| 1958 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1959 | return_value = _codecs_utf_7_encode_impl(module, str, errors); |
| 1960 | |
| 1961 | exit: |
| 1962 | return return_value; |
| 1963 | } |
| 1964 | |
| 1965 | PyDoc_STRVAR(_codecs_utf_8_encode__doc__, |
| 1966 | "utf_8_encode($module, str, errors=None, /)\n" |
| 1967 | "--\n" |
| 1968 | "\n"); |
| 1969 | |
| 1970 | #define _CODECS_UTF_8_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 1971 | {"utf_8_encode", (PyCFunction)(void(*)(void))_codecs_utf_8_encode, METH_FASTCALL, _codecs_utf_8_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1972 | |
| 1973 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 1974 | _codecs_utf_8_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1975 | const char *errors); |
| 1976 | |
| 1977 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 1978 | _codecs_utf_8_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 1979 | { |
| 1980 | PyObject *return_value = NULL; |
| 1981 | PyObject *str; |
| 1982 | const char *errors = NULL; |
| 1983 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1984 | if (!_PyArg_CheckPositional("utf_8_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 1985 | goto exit; |
| 1986 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 1987 | if (!PyUnicode_Check(args[0])) { |
| 1988 | _PyArg_BadArgument("utf_8_encode", 1, "str", args[0]); |
| 1989 | goto exit; |
| 1990 | } |
| 1991 | if (PyUnicode_READY(args[0]) == -1) { |
| 1992 | goto exit; |
| 1993 | } |
| 1994 | str = args[0]; |
| 1995 | if (nargs < 2) { |
| 1996 | goto skip_optional; |
| 1997 | } |
| 1998 | if (args[1] == Py_None) { |
| 1999 | errors = NULL; |
| 2000 | } |
| 2001 | else if (PyUnicode_Check(args[1])) { |
| 2002 | Py_ssize_t errors_length; |
| 2003 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2004 | if (errors == NULL) { |
| 2005 | goto exit; |
| 2006 | } |
| 2007 | if (strlen(errors) != (size_t)errors_length) { |
| 2008 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2009 | goto exit; |
| 2010 | } |
| 2011 | } |
| 2012 | else { |
| 2013 | _PyArg_BadArgument("utf_8_encode", 2, "str or None", args[1]); |
| 2014 | goto exit; |
| 2015 | } |
| 2016 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2017 | return_value = _codecs_utf_8_encode_impl(module, str, errors); |
| 2018 | |
| 2019 | exit: |
| 2020 | return return_value; |
| 2021 | } |
| 2022 | |
| 2023 | PyDoc_STRVAR(_codecs_utf_16_encode__doc__, |
| 2024 | "utf_16_encode($module, str, errors=None, byteorder=0, /)\n" |
| 2025 | "--\n" |
| 2026 | "\n"); |
| 2027 | |
| 2028 | #define _CODECS_UTF_16_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2029 | {"utf_16_encode", (PyCFunction)(void(*)(void))_codecs_utf_16_encode, METH_FASTCALL, _codecs_utf_16_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2030 | |
| 2031 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2032 | _codecs_utf_16_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2033 | const char *errors, int byteorder); |
| 2034 | |
| 2035 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2036 | _codecs_utf_16_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2037 | { |
| 2038 | PyObject *return_value = NULL; |
| 2039 | PyObject *str; |
| 2040 | const char *errors = NULL; |
| 2041 | int byteorder = 0; |
| 2042 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2043 | if (!_PyArg_CheckPositional("utf_16_encode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2044 | goto exit; |
| 2045 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2046 | if (!PyUnicode_Check(args[0])) { |
| 2047 | _PyArg_BadArgument("utf_16_encode", 1, "str", args[0]); |
| 2048 | goto exit; |
| 2049 | } |
| 2050 | if (PyUnicode_READY(args[0]) == -1) { |
| 2051 | goto exit; |
| 2052 | } |
| 2053 | str = args[0]; |
| 2054 | if (nargs < 2) { |
| 2055 | goto skip_optional; |
| 2056 | } |
| 2057 | if (args[1] == Py_None) { |
| 2058 | errors = NULL; |
| 2059 | } |
| 2060 | else if (PyUnicode_Check(args[1])) { |
| 2061 | Py_ssize_t errors_length; |
| 2062 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2063 | if (errors == NULL) { |
| 2064 | goto exit; |
| 2065 | } |
| 2066 | if (strlen(errors) != (size_t)errors_length) { |
| 2067 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2068 | goto exit; |
| 2069 | } |
| 2070 | } |
| 2071 | else { |
| 2072 | _PyArg_BadArgument("utf_16_encode", 2, "str or None", args[1]); |
| 2073 | goto exit; |
| 2074 | } |
| 2075 | if (nargs < 3) { |
| 2076 | goto skip_optional; |
| 2077 | } |
| 2078 | if (PyFloat_Check(args[2])) { |
| 2079 | PyErr_SetString(PyExc_TypeError, |
| 2080 | "integer argument expected, got float" ); |
| 2081 | goto exit; |
| 2082 | } |
| 2083 | byteorder = _PyLong_AsInt(args[2]); |
| 2084 | if (byteorder == -1 && PyErr_Occurred()) { |
| 2085 | goto exit; |
| 2086 | } |
| 2087 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2088 | return_value = _codecs_utf_16_encode_impl(module, str, errors, byteorder); |
| 2089 | |
| 2090 | exit: |
| 2091 | return return_value; |
| 2092 | } |
| 2093 | |
| 2094 | PyDoc_STRVAR(_codecs_utf_16_le_encode__doc__, |
| 2095 | "utf_16_le_encode($module, str, errors=None, /)\n" |
| 2096 | "--\n" |
| 2097 | "\n"); |
| 2098 | |
| 2099 | #define _CODECS_UTF_16_LE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2100 | {"utf_16_le_encode", (PyCFunction)(void(*)(void))_codecs_utf_16_le_encode, METH_FASTCALL, _codecs_utf_16_le_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2101 | |
| 2102 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2103 | _codecs_utf_16_le_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2104 | const char *errors); |
| 2105 | |
| 2106 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2107 | _codecs_utf_16_le_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2108 | { |
| 2109 | PyObject *return_value = NULL; |
| 2110 | PyObject *str; |
| 2111 | const char *errors = NULL; |
| 2112 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2113 | if (!_PyArg_CheckPositional("utf_16_le_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2114 | goto exit; |
| 2115 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2116 | if (!PyUnicode_Check(args[0])) { |
| 2117 | _PyArg_BadArgument("utf_16_le_encode", 1, "str", args[0]); |
| 2118 | goto exit; |
| 2119 | } |
| 2120 | if (PyUnicode_READY(args[0]) == -1) { |
| 2121 | goto exit; |
| 2122 | } |
| 2123 | str = args[0]; |
| 2124 | if (nargs < 2) { |
| 2125 | goto skip_optional; |
| 2126 | } |
| 2127 | if (args[1] == Py_None) { |
| 2128 | errors = NULL; |
| 2129 | } |
| 2130 | else if (PyUnicode_Check(args[1])) { |
| 2131 | Py_ssize_t errors_length; |
| 2132 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2133 | if (errors == NULL) { |
| 2134 | goto exit; |
| 2135 | } |
| 2136 | if (strlen(errors) != (size_t)errors_length) { |
| 2137 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2138 | goto exit; |
| 2139 | } |
| 2140 | } |
| 2141 | else { |
| 2142 | _PyArg_BadArgument("utf_16_le_encode", 2, "str or None", args[1]); |
| 2143 | goto exit; |
| 2144 | } |
| 2145 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2146 | return_value = _codecs_utf_16_le_encode_impl(module, str, errors); |
| 2147 | |
| 2148 | exit: |
| 2149 | return return_value; |
| 2150 | } |
| 2151 | |
| 2152 | PyDoc_STRVAR(_codecs_utf_16_be_encode__doc__, |
| 2153 | "utf_16_be_encode($module, str, errors=None, /)\n" |
| 2154 | "--\n" |
| 2155 | "\n"); |
| 2156 | |
| 2157 | #define _CODECS_UTF_16_BE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2158 | {"utf_16_be_encode", (PyCFunction)(void(*)(void))_codecs_utf_16_be_encode, METH_FASTCALL, _codecs_utf_16_be_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2159 | |
| 2160 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2161 | _codecs_utf_16_be_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2162 | const char *errors); |
| 2163 | |
| 2164 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2165 | _codecs_utf_16_be_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2166 | { |
| 2167 | PyObject *return_value = NULL; |
| 2168 | PyObject *str; |
| 2169 | const char *errors = NULL; |
| 2170 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2171 | if (!_PyArg_CheckPositional("utf_16_be_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2172 | goto exit; |
| 2173 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2174 | if (!PyUnicode_Check(args[0])) { |
| 2175 | _PyArg_BadArgument("utf_16_be_encode", 1, "str", args[0]); |
| 2176 | goto exit; |
| 2177 | } |
| 2178 | if (PyUnicode_READY(args[0]) == -1) { |
| 2179 | goto exit; |
| 2180 | } |
| 2181 | str = args[0]; |
| 2182 | if (nargs < 2) { |
| 2183 | goto skip_optional; |
| 2184 | } |
| 2185 | if (args[1] == Py_None) { |
| 2186 | errors = NULL; |
| 2187 | } |
| 2188 | else if (PyUnicode_Check(args[1])) { |
| 2189 | Py_ssize_t errors_length; |
| 2190 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2191 | if (errors == NULL) { |
| 2192 | goto exit; |
| 2193 | } |
| 2194 | if (strlen(errors) != (size_t)errors_length) { |
| 2195 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2196 | goto exit; |
| 2197 | } |
| 2198 | } |
| 2199 | else { |
| 2200 | _PyArg_BadArgument("utf_16_be_encode", 2, "str or None", args[1]); |
| 2201 | goto exit; |
| 2202 | } |
| 2203 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2204 | return_value = _codecs_utf_16_be_encode_impl(module, str, errors); |
| 2205 | |
| 2206 | exit: |
| 2207 | return return_value; |
| 2208 | } |
| 2209 | |
| 2210 | PyDoc_STRVAR(_codecs_utf_32_encode__doc__, |
| 2211 | "utf_32_encode($module, str, errors=None, byteorder=0, /)\n" |
| 2212 | "--\n" |
| 2213 | "\n"); |
| 2214 | |
| 2215 | #define _CODECS_UTF_32_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2216 | {"utf_32_encode", (PyCFunction)(void(*)(void))_codecs_utf_32_encode, METH_FASTCALL, _codecs_utf_32_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2217 | |
| 2218 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2219 | _codecs_utf_32_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2220 | const char *errors, int byteorder); |
| 2221 | |
| 2222 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2223 | _codecs_utf_32_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2224 | { |
| 2225 | PyObject *return_value = NULL; |
| 2226 | PyObject *str; |
| 2227 | const char *errors = NULL; |
| 2228 | int byteorder = 0; |
| 2229 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2230 | if (!_PyArg_CheckPositional("utf_32_encode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2231 | goto exit; |
| 2232 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2233 | if (!PyUnicode_Check(args[0])) { |
| 2234 | _PyArg_BadArgument("utf_32_encode", 1, "str", args[0]); |
| 2235 | goto exit; |
| 2236 | } |
| 2237 | if (PyUnicode_READY(args[0]) == -1) { |
| 2238 | goto exit; |
| 2239 | } |
| 2240 | str = args[0]; |
| 2241 | if (nargs < 2) { |
| 2242 | goto skip_optional; |
| 2243 | } |
| 2244 | if (args[1] == Py_None) { |
| 2245 | errors = NULL; |
| 2246 | } |
| 2247 | else if (PyUnicode_Check(args[1])) { |
| 2248 | Py_ssize_t errors_length; |
| 2249 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2250 | if (errors == NULL) { |
| 2251 | goto exit; |
| 2252 | } |
| 2253 | if (strlen(errors) != (size_t)errors_length) { |
| 2254 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2255 | goto exit; |
| 2256 | } |
| 2257 | } |
| 2258 | else { |
| 2259 | _PyArg_BadArgument("utf_32_encode", 2, "str or None", args[1]); |
| 2260 | goto exit; |
| 2261 | } |
| 2262 | if (nargs < 3) { |
| 2263 | goto skip_optional; |
| 2264 | } |
| 2265 | if (PyFloat_Check(args[2])) { |
| 2266 | PyErr_SetString(PyExc_TypeError, |
| 2267 | "integer argument expected, got float" ); |
| 2268 | goto exit; |
| 2269 | } |
| 2270 | byteorder = _PyLong_AsInt(args[2]); |
| 2271 | if (byteorder == -1 && PyErr_Occurred()) { |
| 2272 | goto exit; |
| 2273 | } |
| 2274 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2275 | return_value = _codecs_utf_32_encode_impl(module, str, errors, byteorder); |
| 2276 | |
| 2277 | exit: |
| 2278 | return return_value; |
| 2279 | } |
| 2280 | |
| 2281 | PyDoc_STRVAR(_codecs_utf_32_le_encode__doc__, |
| 2282 | "utf_32_le_encode($module, str, errors=None, /)\n" |
| 2283 | "--\n" |
| 2284 | "\n"); |
| 2285 | |
| 2286 | #define _CODECS_UTF_32_LE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2287 | {"utf_32_le_encode", (PyCFunction)(void(*)(void))_codecs_utf_32_le_encode, METH_FASTCALL, _codecs_utf_32_le_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2288 | |
| 2289 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2290 | _codecs_utf_32_le_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2291 | const char *errors); |
| 2292 | |
| 2293 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2294 | _codecs_utf_32_le_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2295 | { |
| 2296 | PyObject *return_value = NULL; |
| 2297 | PyObject *str; |
| 2298 | const char *errors = NULL; |
| 2299 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2300 | if (!_PyArg_CheckPositional("utf_32_le_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2301 | goto exit; |
| 2302 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2303 | if (!PyUnicode_Check(args[0])) { |
| 2304 | _PyArg_BadArgument("utf_32_le_encode", 1, "str", args[0]); |
| 2305 | goto exit; |
| 2306 | } |
| 2307 | if (PyUnicode_READY(args[0]) == -1) { |
| 2308 | goto exit; |
| 2309 | } |
| 2310 | str = args[0]; |
| 2311 | if (nargs < 2) { |
| 2312 | goto skip_optional; |
| 2313 | } |
| 2314 | if (args[1] == Py_None) { |
| 2315 | errors = NULL; |
| 2316 | } |
| 2317 | else if (PyUnicode_Check(args[1])) { |
| 2318 | Py_ssize_t errors_length; |
| 2319 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2320 | if (errors == NULL) { |
| 2321 | goto exit; |
| 2322 | } |
| 2323 | if (strlen(errors) != (size_t)errors_length) { |
| 2324 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2325 | goto exit; |
| 2326 | } |
| 2327 | } |
| 2328 | else { |
| 2329 | _PyArg_BadArgument("utf_32_le_encode", 2, "str or None", args[1]); |
| 2330 | goto exit; |
| 2331 | } |
| 2332 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2333 | return_value = _codecs_utf_32_le_encode_impl(module, str, errors); |
| 2334 | |
| 2335 | exit: |
| 2336 | return return_value; |
| 2337 | } |
| 2338 | |
| 2339 | PyDoc_STRVAR(_codecs_utf_32_be_encode__doc__, |
| 2340 | "utf_32_be_encode($module, str, errors=None, /)\n" |
| 2341 | "--\n" |
| 2342 | "\n"); |
| 2343 | |
| 2344 | #define _CODECS_UTF_32_BE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2345 | {"utf_32_be_encode", (PyCFunction)(void(*)(void))_codecs_utf_32_be_encode, METH_FASTCALL, _codecs_utf_32_be_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2346 | |
| 2347 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2348 | _codecs_utf_32_be_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2349 | const char *errors); |
| 2350 | |
| 2351 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2352 | _codecs_utf_32_be_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2353 | { |
| 2354 | PyObject *return_value = NULL; |
| 2355 | PyObject *str; |
| 2356 | const char *errors = NULL; |
| 2357 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2358 | if (!_PyArg_CheckPositional("utf_32_be_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2359 | goto exit; |
| 2360 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2361 | if (!PyUnicode_Check(args[0])) { |
| 2362 | _PyArg_BadArgument("utf_32_be_encode", 1, "str", args[0]); |
| 2363 | goto exit; |
| 2364 | } |
| 2365 | if (PyUnicode_READY(args[0]) == -1) { |
| 2366 | goto exit; |
| 2367 | } |
| 2368 | str = args[0]; |
| 2369 | if (nargs < 2) { |
| 2370 | goto skip_optional; |
| 2371 | } |
| 2372 | if (args[1] == Py_None) { |
| 2373 | errors = NULL; |
| 2374 | } |
| 2375 | else if (PyUnicode_Check(args[1])) { |
| 2376 | Py_ssize_t errors_length; |
| 2377 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2378 | if (errors == NULL) { |
| 2379 | goto exit; |
| 2380 | } |
| 2381 | if (strlen(errors) != (size_t)errors_length) { |
| 2382 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2383 | goto exit; |
| 2384 | } |
| 2385 | } |
| 2386 | else { |
| 2387 | _PyArg_BadArgument("utf_32_be_encode", 2, "str or None", args[1]); |
| 2388 | goto exit; |
| 2389 | } |
| 2390 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2391 | return_value = _codecs_utf_32_be_encode_impl(module, str, errors); |
| 2392 | |
| 2393 | exit: |
| 2394 | return return_value; |
| 2395 | } |
| 2396 | |
| 2397 | PyDoc_STRVAR(_codecs_unicode_escape_encode__doc__, |
| 2398 | "unicode_escape_encode($module, str, errors=None, /)\n" |
| 2399 | "--\n" |
| 2400 | "\n"); |
| 2401 | |
| 2402 | #define _CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2403 | {"unicode_escape_encode", (PyCFunction)(void(*)(void))_codecs_unicode_escape_encode, METH_FASTCALL, _codecs_unicode_escape_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2404 | |
| 2405 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2406 | _codecs_unicode_escape_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2407 | const char *errors); |
| 2408 | |
| 2409 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2410 | _codecs_unicode_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2411 | { |
| 2412 | PyObject *return_value = NULL; |
| 2413 | PyObject *str; |
| 2414 | const char *errors = NULL; |
| 2415 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2416 | if (!_PyArg_CheckPositional("unicode_escape_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2417 | goto exit; |
| 2418 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2419 | if (!PyUnicode_Check(args[0])) { |
| 2420 | _PyArg_BadArgument("unicode_escape_encode", 1, "str", args[0]); |
| 2421 | goto exit; |
| 2422 | } |
| 2423 | if (PyUnicode_READY(args[0]) == -1) { |
| 2424 | goto exit; |
| 2425 | } |
| 2426 | str = args[0]; |
| 2427 | if (nargs < 2) { |
| 2428 | goto skip_optional; |
| 2429 | } |
| 2430 | if (args[1] == Py_None) { |
| 2431 | errors = NULL; |
| 2432 | } |
| 2433 | else if (PyUnicode_Check(args[1])) { |
| 2434 | Py_ssize_t errors_length; |
| 2435 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2436 | if (errors == NULL) { |
| 2437 | goto exit; |
| 2438 | } |
| 2439 | if (strlen(errors) != (size_t)errors_length) { |
| 2440 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2441 | goto exit; |
| 2442 | } |
| 2443 | } |
| 2444 | else { |
| 2445 | _PyArg_BadArgument("unicode_escape_encode", 2, "str or None", args[1]); |
| 2446 | goto exit; |
| 2447 | } |
| 2448 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2449 | return_value = _codecs_unicode_escape_encode_impl(module, str, errors); |
| 2450 | |
| 2451 | exit: |
| 2452 | return return_value; |
| 2453 | } |
| 2454 | |
| 2455 | PyDoc_STRVAR(_codecs_raw_unicode_escape_encode__doc__, |
| 2456 | "raw_unicode_escape_encode($module, str, errors=None, /)\n" |
| 2457 | "--\n" |
| 2458 | "\n"); |
| 2459 | |
| 2460 | #define _CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2461 | {"raw_unicode_escape_encode", (PyCFunction)(void(*)(void))_codecs_raw_unicode_escape_encode, METH_FASTCALL, _codecs_raw_unicode_escape_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2462 | |
| 2463 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2464 | _codecs_raw_unicode_escape_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2465 | const char *errors); |
| 2466 | |
| 2467 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2468 | _codecs_raw_unicode_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2469 | { |
| 2470 | PyObject *return_value = NULL; |
| 2471 | PyObject *str; |
| 2472 | const char *errors = NULL; |
| 2473 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2474 | if (!_PyArg_CheckPositional("raw_unicode_escape_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2475 | goto exit; |
| 2476 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2477 | if (!PyUnicode_Check(args[0])) { |
| 2478 | _PyArg_BadArgument("raw_unicode_escape_encode", 1, "str", args[0]); |
| 2479 | goto exit; |
| 2480 | } |
| 2481 | if (PyUnicode_READY(args[0]) == -1) { |
| 2482 | goto exit; |
| 2483 | } |
| 2484 | str = args[0]; |
| 2485 | if (nargs < 2) { |
| 2486 | goto skip_optional; |
| 2487 | } |
| 2488 | if (args[1] == Py_None) { |
| 2489 | errors = NULL; |
| 2490 | } |
| 2491 | else if (PyUnicode_Check(args[1])) { |
| 2492 | Py_ssize_t errors_length; |
| 2493 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2494 | if (errors == NULL) { |
| 2495 | goto exit; |
| 2496 | } |
| 2497 | if (strlen(errors) != (size_t)errors_length) { |
| 2498 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2499 | goto exit; |
| 2500 | } |
| 2501 | } |
| 2502 | else { |
| 2503 | _PyArg_BadArgument("raw_unicode_escape_encode", 2, "str or None", args[1]); |
| 2504 | goto exit; |
| 2505 | } |
| 2506 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2507 | return_value = _codecs_raw_unicode_escape_encode_impl(module, str, errors); |
| 2508 | |
| 2509 | exit: |
| 2510 | return return_value; |
| 2511 | } |
| 2512 | |
| 2513 | PyDoc_STRVAR(_codecs_latin_1_encode__doc__, |
| 2514 | "latin_1_encode($module, str, errors=None, /)\n" |
| 2515 | "--\n" |
| 2516 | "\n"); |
| 2517 | |
| 2518 | #define _CODECS_LATIN_1_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2519 | {"latin_1_encode", (PyCFunction)(void(*)(void))_codecs_latin_1_encode, METH_FASTCALL, _codecs_latin_1_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2520 | |
| 2521 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2522 | _codecs_latin_1_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2523 | const char *errors); |
| 2524 | |
| 2525 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2526 | _codecs_latin_1_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2527 | { |
| 2528 | PyObject *return_value = NULL; |
| 2529 | PyObject *str; |
| 2530 | const char *errors = NULL; |
| 2531 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2532 | if (!_PyArg_CheckPositional("latin_1_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2533 | goto exit; |
| 2534 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2535 | if (!PyUnicode_Check(args[0])) { |
| 2536 | _PyArg_BadArgument("latin_1_encode", 1, "str", args[0]); |
| 2537 | goto exit; |
| 2538 | } |
| 2539 | if (PyUnicode_READY(args[0]) == -1) { |
| 2540 | goto exit; |
| 2541 | } |
| 2542 | str = args[0]; |
| 2543 | if (nargs < 2) { |
| 2544 | goto skip_optional; |
| 2545 | } |
| 2546 | if (args[1] == Py_None) { |
| 2547 | errors = NULL; |
| 2548 | } |
| 2549 | else if (PyUnicode_Check(args[1])) { |
| 2550 | Py_ssize_t errors_length; |
| 2551 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2552 | if (errors == NULL) { |
| 2553 | goto exit; |
| 2554 | } |
| 2555 | if (strlen(errors) != (size_t)errors_length) { |
| 2556 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2557 | goto exit; |
| 2558 | } |
| 2559 | } |
| 2560 | else { |
| 2561 | _PyArg_BadArgument("latin_1_encode", 2, "str or None", args[1]); |
| 2562 | goto exit; |
| 2563 | } |
| 2564 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2565 | return_value = _codecs_latin_1_encode_impl(module, str, errors); |
| 2566 | |
| 2567 | exit: |
| 2568 | return return_value; |
| 2569 | } |
| 2570 | |
| 2571 | PyDoc_STRVAR(_codecs_ascii_encode__doc__, |
| 2572 | "ascii_encode($module, str, errors=None, /)\n" |
| 2573 | "--\n" |
| 2574 | "\n"); |
| 2575 | |
| 2576 | #define _CODECS_ASCII_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2577 | {"ascii_encode", (PyCFunction)(void(*)(void))_codecs_ascii_encode, METH_FASTCALL, _codecs_ascii_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2578 | |
| 2579 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2580 | _codecs_ascii_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2581 | const char *errors); |
| 2582 | |
| 2583 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2584 | _codecs_ascii_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2585 | { |
| 2586 | PyObject *return_value = NULL; |
| 2587 | PyObject *str; |
| 2588 | const char *errors = NULL; |
| 2589 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2590 | if (!_PyArg_CheckPositional("ascii_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2591 | goto exit; |
| 2592 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2593 | if (!PyUnicode_Check(args[0])) { |
| 2594 | _PyArg_BadArgument("ascii_encode", 1, "str", args[0]); |
| 2595 | goto exit; |
| 2596 | } |
| 2597 | if (PyUnicode_READY(args[0]) == -1) { |
| 2598 | goto exit; |
| 2599 | } |
| 2600 | str = args[0]; |
| 2601 | if (nargs < 2) { |
| 2602 | goto skip_optional; |
| 2603 | } |
| 2604 | if (args[1] == Py_None) { |
| 2605 | errors = NULL; |
| 2606 | } |
| 2607 | else if (PyUnicode_Check(args[1])) { |
| 2608 | Py_ssize_t errors_length; |
| 2609 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2610 | if (errors == NULL) { |
| 2611 | goto exit; |
| 2612 | } |
| 2613 | if (strlen(errors) != (size_t)errors_length) { |
| 2614 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2615 | goto exit; |
| 2616 | } |
| 2617 | } |
| 2618 | else { |
| 2619 | _PyArg_BadArgument("ascii_encode", 2, "str or None", args[1]); |
| 2620 | goto exit; |
| 2621 | } |
| 2622 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2623 | return_value = _codecs_ascii_encode_impl(module, str, errors); |
| 2624 | |
| 2625 | exit: |
| 2626 | return return_value; |
| 2627 | } |
| 2628 | |
| 2629 | PyDoc_STRVAR(_codecs_charmap_encode__doc__, |
| 2630 | "charmap_encode($module, str, errors=None, mapping=None, /)\n" |
| 2631 | "--\n" |
| 2632 | "\n"); |
| 2633 | |
| 2634 | #define _CODECS_CHARMAP_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2635 | {"charmap_encode", (PyCFunction)(void(*)(void))_codecs_charmap_encode, METH_FASTCALL, _codecs_charmap_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2636 | |
| 2637 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2638 | _codecs_charmap_encode_impl(PyObject *module, PyObject *str, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2639 | const char *errors, PyObject *mapping); |
| 2640 | |
| 2641 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2642 | _codecs_charmap_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2643 | { |
| 2644 | PyObject *return_value = NULL; |
| 2645 | PyObject *str; |
| 2646 | const char *errors = NULL; |
| 2647 | PyObject *mapping = NULL; |
| 2648 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2649 | if (!_PyArg_CheckPositional("charmap_encode", nargs, 1, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2650 | goto exit; |
| 2651 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2652 | if (!PyUnicode_Check(args[0])) { |
| 2653 | _PyArg_BadArgument("charmap_encode", 1, "str", args[0]); |
| 2654 | goto exit; |
| 2655 | } |
| 2656 | if (PyUnicode_READY(args[0]) == -1) { |
| 2657 | goto exit; |
| 2658 | } |
| 2659 | str = args[0]; |
| 2660 | if (nargs < 2) { |
| 2661 | goto skip_optional; |
| 2662 | } |
| 2663 | if (args[1] == Py_None) { |
| 2664 | errors = NULL; |
| 2665 | } |
| 2666 | else if (PyUnicode_Check(args[1])) { |
| 2667 | Py_ssize_t errors_length; |
| 2668 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2669 | if (errors == NULL) { |
| 2670 | goto exit; |
| 2671 | } |
| 2672 | if (strlen(errors) != (size_t)errors_length) { |
| 2673 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2674 | goto exit; |
| 2675 | } |
| 2676 | } |
| 2677 | else { |
| 2678 | _PyArg_BadArgument("charmap_encode", 2, "str or None", args[1]); |
| 2679 | goto exit; |
| 2680 | } |
| 2681 | if (nargs < 3) { |
| 2682 | goto skip_optional; |
| 2683 | } |
| 2684 | mapping = args[2]; |
| 2685 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2686 | return_value = _codecs_charmap_encode_impl(module, str, errors, mapping); |
| 2687 | |
| 2688 | exit: |
| 2689 | return return_value; |
| 2690 | } |
| 2691 | |
| 2692 | PyDoc_STRVAR(_codecs_charmap_build__doc__, |
| 2693 | "charmap_build($module, map, /)\n" |
| 2694 | "--\n" |
| 2695 | "\n"); |
| 2696 | |
| 2697 | #define _CODECS_CHARMAP_BUILD_METHODDEF \ |
| 2698 | {"charmap_build", (PyCFunction)_codecs_charmap_build, METH_O, _codecs_charmap_build__doc__}, |
| 2699 | |
| 2700 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2701 | _codecs_charmap_build_impl(PyObject *module, PyObject *map); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2702 | |
| 2703 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2704 | _codecs_charmap_build(PyObject *module, PyObject *arg) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2705 | { |
| 2706 | PyObject *return_value = NULL; |
| 2707 | PyObject *map; |
| 2708 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 2709 | if (!PyUnicode_Check(arg)) { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2710 | _PyArg_BadArgument("charmap_build", 0, "str", arg); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2711 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 2712 | } |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 2713 | if (PyUnicode_READY(arg) == -1) { |
| 2714 | goto exit; |
| 2715 | } |
| 2716 | map = arg; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2717 | return_value = _codecs_charmap_build_impl(module, map); |
| 2718 | |
| 2719 | exit: |
| 2720 | return return_value; |
| 2721 | } |
| 2722 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2723 | #if defined(MS_WINDOWS) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2724 | |
| 2725 | PyDoc_STRVAR(_codecs_mbcs_encode__doc__, |
| 2726 | "mbcs_encode($module, str, errors=None, /)\n" |
| 2727 | "--\n" |
| 2728 | "\n"); |
| 2729 | |
| 2730 | #define _CODECS_MBCS_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2731 | {"mbcs_encode", (PyCFunction)(void(*)(void))_codecs_mbcs_encode, METH_FASTCALL, _codecs_mbcs_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2732 | |
| 2733 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2734 | _codecs_mbcs_encode_impl(PyObject *module, PyObject *str, const char *errors); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2735 | |
| 2736 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2737 | _codecs_mbcs_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2738 | { |
| 2739 | PyObject *return_value = NULL; |
| 2740 | PyObject *str; |
| 2741 | const char *errors = NULL; |
| 2742 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2743 | if (!_PyArg_CheckPositional("mbcs_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2744 | goto exit; |
| 2745 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2746 | if (!PyUnicode_Check(args[0])) { |
| 2747 | _PyArg_BadArgument("mbcs_encode", 1, "str", args[0]); |
| 2748 | goto exit; |
| 2749 | } |
| 2750 | if (PyUnicode_READY(args[0]) == -1) { |
| 2751 | goto exit; |
| 2752 | } |
| 2753 | str = args[0]; |
| 2754 | if (nargs < 2) { |
| 2755 | goto skip_optional; |
| 2756 | } |
| 2757 | if (args[1] == Py_None) { |
| 2758 | errors = NULL; |
| 2759 | } |
| 2760 | else if (PyUnicode_Check(args[1])) { |
| 2761 | Py_ssize_t errors_length; |
| 2762 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2763 | if (errors == NULL) { |
| 2764 | goto exit; |
| 2765 | } |
| 2766 | if (strlen(errors) != (size_t)errors_length) { |
| 2767 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2768 | goto exit; |
| 2769 | } |
| 2770 | } |
| 2771 | else { |
| 2772 | _PyArg_BadArgument("mbcs_encode", 2, "str or None", args[1]); |
| 2773 | goto exit; |
| 2774 | } |
| 2775 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2776 | return_value = _codecs_mbcs_encode_impl(module, str, errors); |
| 2777 | |
| 2778 | exit: |
| 2779 | return return_value; |
| 2780 | } |
| 2781 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2782 | #endif /* defined(MS_WINDOWS) */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2783 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2784 | #if defined(MS_WINDOWS) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2785 | |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 2786 | PyDoc_STRVAR(_codecs_oem_encode__doc__, |
| 2787 | "oem_encode($module, str, errors=None, /)\n" |
| 2788 | "--\n" |
| 2789 | "\n"); |
| 2790 | |
| 2791 | #define _CODECS_OEM_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2792 | {"oem_encode", (PyCFunction)(void(*)(void))_codecs_oem_encode, METH_FASTCALL, _codecs_oem_encode__doc__}, |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 2793 | |
| 2794 | static PyObject * |
| 2795 | _codecs_oem_encode_impl(PyObject *module, PyObject *str, const char *errors); |
| 2796 | |
| 2797 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2798 | _codecs_oem_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 2799 | { |
| 2800 | PyObject *return_value = NULL; |
| 2801 | PyObject *str; |
| 2802 | const char *errors = NULL; |
| 2803 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2804 | if (!_PyArg_CheckPositional("oem_encode", nargs, 1, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2805 | goto exit; |
| 2806 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2807 | if (!PyUnicode_Check(args[0])) { |
| 2808 | _PyArg_BadArgument("oem_encode", 1, "str", args[0]); |
| 2809 | goto exit; |
| 2810 | } |
| 2811 | if (PyUnicode_READY(args[0]) == -1) { |
| 2812 | goto exit; |
| 2813 | } |
| 2814 | str = args[0]; |
| 2815 | if (nargs < 2) { |
| 2816 | goto skip_optional; |
| 2817 | } |
| 2818 | if (args[1] == Py_None) { |
| 2819 | errors = NULL; |
| 2820 | } |
| 2821 | else if (PyUnicode_Check(args[1])) { |
| 2822 | Py_ssize_t errors_length; |
| 2823 | errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length); |
| 2824 | if (errors == NULL) { |
| 2825 | goto exit; |
| 2826 | } |
| 2827 | if (strlen(errors) != (size_t)errors_length) { |
| 2828 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2829 | goto exit; |
| 2830 | } |
| 2831 | } |
| 2832 | else { |
| 2833 | _PyArg_BadArgument("oem_encode", 2, "str or None", args[1]); |
| 2834 | goto exit; |
| 2835 | } |
| 2836 | skip_optional: |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 2837 | return_value = _codecs_oem_encode_impl(module, str, errors); |
| 2838 | |
| 2839 | exit: |
| 2840 | return return_value; |
| 2841 | } |
| 2842 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2843 | #endif /* defined(MS_WINDOWS) */ |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 2844 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2845 | #if defined(MS_WINDOWS) |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 2846 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2847 | PyDoc_STRVAR(_codecs_code_page_encode__doc__, |
| 2848 | "code_page_encode($module, code_page, str, errors=None, /)\n" |
| 2849 | "--\n" |
| 2850 | "\n"); |
| 2851 | |
| 2852 | #define _CODECS_CODE_PAGE_ENCODE_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2853 | {"code_page_encode", (PyCFunction)(void(*)(void))_codecs_code_page_encode, METH_FASTCALL, _codecs_code_page_encode__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2854 | |
| 2855 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2856 | _codecs_code_page_encode_impl(PyObject *module, int code_page, PyObject *str, |
| 2857 | const char *errors); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2858 | |
| 2859 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2860 | _codecs_code_page_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2861 | { |
| 2862 | PyObject *return_value = NULL; |
| 2863 | int code_page; |
| 2864 | PyObject *str; |
| 2865 | const char *errors = NULL; |
| 2866 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2867 | if (!_PyArg_CheckPositional("code_page_encode", nargs, 2, 3)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2868 | goto exit; |
| 2869 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2870 | if (PyFloat_Check(args[0])) { |
| 2871 | PyErr_SetString(PyExc_TypeError, |
| 2872 | "integer argument expected, got float" ); |
| 2873 | goto exit; |
| 2874 | } |
| 2875 | code_page = _PyLong_AsInt(args[0]); |
| 2876 | if (code_page == -1 && PyErr_Occurred()) { |
| 2877 | goto exit; |
| 2878 | } |
| 2879 | if (!PyUnicode_Check(args[1])) { |
| 2880 | _PyArg_BadArgument("code_page_encode", 2, "str", args[1]); |
| 2881 | goto exit; |
| 2882 | } |
| 2883 | if (PyUnicode_READY(args[1]) == -1) { |
| 2884 | goto exit; |
| 2885 | } |
| 2886 | str = args[1]; |
| 2887 | if (nargs < 3) { |
| 2888 | goto skip_optional; |
| 2889 | } |
| 2890 | if (args[2] == Py_None) { |
| 2891 | errors = NULL; |
| 2892 | } |
| 2893 | else if (PyUnicode_Check(args[2])) { |
| 2894 | Py_ssize_t errors_length; |
| 2895 | errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length); |
| 2896 | if (errors == NULL) { |
| 2897 | goto exit; |
| 2898 | } |
| 2899 | if (strlen(errors) != (size_t)errors_length) { |
| 2900 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2901 | goto exit; |
| 2902 | } |
| 2903 | } |
| 2904 | else { |
| 2905 | _PyArg_BadArgument("code_page_encode", 3, "str or None", args[2]); |
| 2906 | goto exit; |
| 2907 | } |
| 2908 | skip_optional: |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2909 | return_value = _codecs_code_page_encode_impl(module, code_page, str, errors); |
| 2910 | |
| 2911 | exit: |
| 2912 | return return_value; |
| 2913 | } |
| 2914 | |
Steve Dower | cc16be8 | 2016-09-08 10:35:16 -0700 | [diff] [blame] | 2915 | #endif /* defined(MS_WINDOWS) */ |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2916 | |
| 2917 | PyDoc_STRVAR(_codecs_register_error__doc__, |
| 2918 | "register_error($module, errors, handler, /)\n" |
| 2919 | "--\n" |
| 2920 | "\n" |
| 2921 | "Register the specified error handler under the name errors.\n" |
| 2922 | "\n" |
| 2923 | "handler must be a callable object, that will be called with an exception\n" |
| 2924 | "instance containing information about the location of the encoding/decoding\n" |
| 2925 | "error and must return a (replacement, new position) tuple."); |
| 2926 | |
| 2927 | #define _CODECS_REGISTER_ERROR_METHODDEF \ |
Serhiy Storchaka | 4a934d4 | 2018-11-27 11:27:36 +0200 | [diff] [blame] | 2928 | {"register_error", (PyCFunction)(void(*)(void))_codecs_register_error, METH_FASTCALL, _codecs_register_error__doc__}, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2929 | |
| 2930 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2931 | _codecs_register_error_impl(PyObject *module, const char *errors, |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2932 | PyObject *handler); |
| 2933 | |
| 2934 | static PyObject * |
Serhiy Storchaka | a5552f0 | 2017-12-15 13:11:11 +0200 | [diff] [blame] | 2935 | _codecs_register_error(PyObject *module, PyObject *const *args, Py_ssize_t nargs) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2936 | { |
| 2937 | PyObject *return_value = NULL; |
| 2938 | const char *errors; |
| 2939 | PyObject *handler; |
| 2940 | |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2941 | if (!_PyArg_CheckPositional("register_error", nargs, 2, 2)) { |
Victor Stinner | 259f0e4 | 2017-01-17 01:35:17 +0100 | [diff] [blame] | 2942 | goto exit; |
| 2943 | } |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2944 | if (!PyUnicode_Check(args[0])) { |
| 2945 | _PyArg_BadArgument("register_error", 1, "str", args[0]); |
| 2946 | goto exit; |
| 2947 | } |
| 2948 | Py_ssize_t errors_length; |
| 2949 | errors = PyUnicode_AsUTF8AndSize(args[0], &errors_length); |
| 2950 | if (errors == NULL) { |
| 2951 | goto exit; |
| 2952 | } |
| 2953 | if (strlen(errors) != (size_t)errors_length) { |
| 2954 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
| 2955 | goto exit; |
| 2956 | } |
| 2957 | handler = args[1]; |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2958 | return_value = _codecs_register_error_impl(module, errors, handler); |
| 2959 | |
| 2960 | exit: |
| 2961 | return return_value; |
| 2962 | } |
| 2963 | |
| 2964 | PyDoc_STRVAR(_codecs_lookup_error__doc__, |
| 2965 | "lookup_error($module, name, /)\n" |
| 2966 | "--\n" |
| 2967 | "\n" |
| 2968 | "lookup_error(errors) -> handler\n" |
| 2969 | "\n" |
| 2970 | "Return the error handler for the specified error handling name or raise a\n" |
| 2971 | "LookupError, if no handler exists under this name."); |
| 2972 | |
| 2973 | #define _CODECS_LOOKUP_ERROR_METHODDEF \ |
| 2974 | {"lookup_error", (PyCFunction)_codecs_lookup_error, METH_O, _codecs_lookup_error__doc__}, |
| 2975 | |
| 2976 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2977 | _codecs_lookup_error_impl(PyObject *module, const char *name); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2978 | |
| 2979 | static PyObject * |
Serhiy Storchaka | 1a2b24f | 2016-07-07 17:35:15 +0300 | [diff] [blame] | 2980 | _codecs_lookup_error(PyObject *module, PyObject *arg) |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2981 | { |
| 2982 | PyObject *return_value = NULL; |
| 2983 | const char *name; |
| 2984 | |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 2985 | if (!PyUnicode_Check(arg)) { |
Serhiy Storchaka | 4fa9591 | 2019-01-11 16:01:14 +0200 | [diff] [blame] | 2986 | _PyArg_BadArgument("lookup_error", 0, "str", arg); |
Serhiy Storchaka | 32d96a2 | 2018-12-25 13:23:47 +0200 | [diff] [blame] | 2987 | goto exit; |
| 2988 | } |
| 2989 | Py_ssize_t name_length; |
| 2990 | name = PyUnicode_AsUTF8AndSize(arg, &name_length); |
| 2991 | if (name == NULL) { |
| 2992 | goto exit; |
| 2993 | } |
| 2994 | if (strlen(name) != (size_t)name_length) { |
| 2995 | PyErr_SetString(PyExc_ValueError, "embedded null character"); |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2996 | goto exit; |
Serhiy Storchaka | 5dee655 | 2016-06-09 16:16:06 +0300 | [diff] [blame] | 2997 | } |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 2998 | return_value = _codecs_lookup_error_impl(module, name); |
| 2999 | |
| 3000 | exit: |
| 3001 | return return_value; |
| 3002 | } |
| 3003 | |
| 3004 | #ifndef _CODECS_MBCS_DECODE_METHODDEF |
| 3005 | #define _CODECS_MBCS_DECODE_METHODDEF |
| 3006 | #endif /* !defined(_CODECS_MBCS_DECODE_METHODDEF) */ |
| 3007 | |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 3008 | #ifndef _CODECS_OEM_DECODE_METHODDEF |
| 3009 | #define _CODECS_OEM_DECODE_METHODDEF |
| 3010 | #endif /* !defined(_CODECS_OEM_DECODE_METHODDEF) */ |
| 3011 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 3012 | #ifndef _CODECS_CODE_PAGE_DECODE_METHODDEF |
| 3013 | #define _CODECS_CODE_PAGE_DECODE_METHODDEF |
| 3014 | #endif /* !defined(_CODECS_CODE_PAGE_DECODE_METHODDEF) */ |
| 3015 | |
| 3016 | #ifndef _CODECS_MBCS_ENCODE_METHODDEF |
| 3017 | #define _CODECS_MBCS_ENCODE_METHODDEF |
| 3018 | #endif /* !defined(_CODECS_MBCS_ENCODE_METHODDEF) */ |
| 3019 | |
Steve Dower | f5aba58 | 2016-09-06 19:42:27 -0700 | [diff] [blame] | 3020 | #ifndef _CODECS_OEM_ENCODE_METHODDEF |
| 3021 | #define _CODECS_OEM_ENCODE_METHODDEF |
| 3022 | #endif /* !defined(_CODECS_OEM_ENCODE_METHODDEF) */ |
| 3023 | |
Serhiy Storchaka | 0c59ff6 | 2015-05-12 13:15:57 +0300 | [diff] [blame] | 3024 | #ifndef _CODECS_CODE_PAGE_ENCODE_METHODDEF |
| 3025 | #define _CODECS_CODE_PAGE_ENCODE_METHODDEF |
| 3026 | #endif /* !defined(_CODECS_CODE_PAGE_ENCODE_METHODDEF) */ |
Serhiy Storchaka | 3191391 | 2019-03-14 10:32:22 +0200 | [diff] [blame^] | 3027 | /*[clinic end generated code: output=02bd0f0cf9a28150 input=a9049054013a1b77]*/ |