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