blob: 249065c9fd05a449bb51becf861ca1a95f46c73e [file] [log] [blame]
Serhiy Storchaka1009bf12015-04-03 23:53:51 +03001/*[clinic input]
2preserve
3[clinic start generated code]*/
4
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03005PyDoc_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
18PyDoc_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
27static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030028_codecs_lookup_impl(PyObject *module, const char *encoding);
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030029
30static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030031_codecs_lookup(PyObject *module, PyObject *arg)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030032{
33 PyObject *return_value = NULL;
34 const char *encoding;
35
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020036 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020037 _PyArg_BadArgument("lookup", "argument", "str", arg);
Serhiy Storchaka32d96a22018-12-25 13:23:47 +020038 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 Storchaka0c59ff62015-05-12 13:15:57 +030047 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030048 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030049 return_value = _codecs_lookup_impl(module, encoding);
50
51exit:
52 return return_value;
53}
54
55PyDoc_STRVAR(_codecs_encode__doc__,
Serhiy Storchakac97a9622015-08-09 12:23:08 +030056"encode($module, /, obj, encoding=\'utf-8\', errors=\'strict\')\n"
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030057"--\n"
58"\n"
59"Encodes obj using the codec registered for encoding.\n"
60"\n"
Serhiy Storchakac97a9622015-08-09 12:23:08 +030061"The default encoding is \'utf-8\'. errors may be given to set a\n"
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030062"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 Storchaka4a934d42018-11-27 11:27:36 +020068 {"encode", (PyCFunction)(void(*)(void))_codecs_encode, METH_FASTCALL|METH_KEYWORDS, _codecs_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030069
70static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030071_codecs_encode_impl(PyObject *module, PyObject *obj, const char *encoding,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030072 const char *errors);
73
74static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +020075_codecs_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030076{
77 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +030078 static const char * const _keywords[] = {"obj", "encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +020079 static _PyArg_Parser _parser = {NULL, _keywords, "encode", 0};
80 PyObject *argsbuf[3];
81 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030082 PyObject *obj;
83 const char *encoding = NULL;
84 const char *errors = NULL;
85
Serhiy Storchaka31913912019-03-14 10:32:22 +020086 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
87 if (!args) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030088 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +030089 }
Serhiy Storchaka31913912019-03-14 10:32:22 +020090 obj = args[0];
91 if (!noptargs) {
92 goto skip_optional_pos;
93 }
94 if (args[1]) {
95 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +020096 _PyArg_BadArgument("encode", "argument 'encoding'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +020097 goto exit;
98 }
99 Py_ssize_t encoding_length;
100 encoding = PyUnicode_AsUTF8AndSize(args[1], &encoding_length);
101 if (encoding == NULL) {
102 goto exit;
103 }
104 if (strlen(encoding) != (size_t)encoding_length) {
105 PyErr_SetString(PyExc_ValueError, "embedded null character");
106 goto exit;
107 }
108 if (!--noptargs) {
109 goto skip_optional_pos;
110 }
111 }
112 if (!PyUnicode_Check(args[2])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200113 _PyArg_BadArgument("encode", "argument 'errors'", "str", args[2]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200114 goto exit;
115 }
116 Py_ssize_t errors_length;
117 errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length);
118 if (errors == NULL) {
119 goto exit;
120 }
121 if (strlen(errors) != (size_t)errors_length) {
122 PyErr_SetString(PyExc_ValueError, "embedded null character");
123 goto exit;
124 }
125skip_optional_pos:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300126 return_value = _codecs_encode_impl(module, obj, encoding, errors);
127
128exit:
129 return return_value;
130}
131
132PyDoc_STRVAR(_codecs_decode__doc__,
Serhiy Storchakac97a9622015-08-09 12:23:08 +0300133"decode($module, /, obj, encoding=\'utf-8\', errors=\'strict\')\n"
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300134"--\n"
135"\n"
136"Decodes obj using the codec registered for encoding.\n"
137"\n"
Serhiy Storchakac97a9622015-08-09 12:23:08 +0300138"Default encoding is \'utf-8\'. errors may be given to set a\n"
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300139"different error handling scheme. Default is \'strict\' meaning that encoding\n"
140"errors raise a ValueError. Other possible values are \'ignore\', \'replace\'\n"
141"and \'backslashreplace\' as well as any other name registered with\n"
142"codecs.register_error that can handle ValueErrors.");
143
144#define _CODECS_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200145 {"decode", (PyCFunction)(void(*)(void))_codecs_decode, METH_FASTCALL|METH_KEYWORDS, _codecs_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300146
147static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300148_codecs_decode_impl(PyObject *module, PyObject *obj, const char *encoding,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300149 const char *errors);
150
151static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200152_codecs_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs, PyObject *kwnames)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300153{
154 PyObject *return_value = NULL;
Serhiy Storchaka9171a8b2016-08-14 10:52:18 +0300155 static const char * const _keywords[] = {"obj", "encoding", "errors", NULL};
Serhiy Storchaka31913912019-03-14 10:32:22 +0200156 static _PyArg_Parser _parser = {NULL, _keywords, "decode", 0};
157 PyObject *argsbuf[3];
158 Py_ssize_t noptargs = nargs + (kwnames ? PyTuple_GET_SIZE(kwnames) : 0) - 1;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300159 PyObject *obj;
160 const char *encoding = NULL;
161 const char *errors = NULL;
162
Serhiy Storchaka31913912019-03-14 10:32:22 +0200163 args = _PyArg_UnpackKeywords(args, nargs, NULL, kwnames, &_parser, 1, 3, 0, argsbuf);
164 if (!args) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300165 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300166 }
Serhiy Storchaka31913912019-03-14 10:32:22 +0200167 obj = args[0];
168 if (!noptargs) {
169 goto skip_optional_pos;
170 }
171 if (args[1]) {
172 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200173 _PyArg_BadArgument("decode", "argument 'encoding'", "str", args[1]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200174 goto exit;
175 }
176 Py_ssize_t encoding_length;
177 encoding = PyUnicode_AsUTF8AndSize(args[1], &encoding_length);
178 if (encoding == NULL) {
179 goto exit;
180 }
181 if (strlen(encoding) != (size_t)encoding_length) {
182 PyErr_SetString(PyExc_ValueError, "embedded null character");
183 goto exit;
184 }
185 if (!--noptargs) {
186 goto skip_optional_pos;
187 }
188 }
189 if (!PyUnicode_Check(args[2])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200190 _PyArg_BadArgument("decode", "argument 'errors'", "str", args[2]);
Serhiy Storchaka31913912019-03-14 10:32:22 +0200191 goto exit;
192 }
193 Py_ssize_t errors_length;
194 errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length);
195 if (errors == NULL) {
196 goto exit;
197 }
198 if (strlen(errors) != (size_t)errors_length) {
199 PyErr_SetString(PyExc_ValueError, "embedded null character");
200 goto exit;
201 }
202skip_optional_pos:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300203 return_value = _codecs_decode_impl(module, obj, encoding, errors);
204
205exit:
206 return return_value;
207}
208
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300209PyDoc_STRVAR(_codecs__forget_codec__doc__,
210"_forget_codec($module, encoding, /)\n"
211"--\n"
212"\n"
213"Purge the named codec from the internal codec lookup cache");
214
215#define _CODECS__FORGET_CODEC_METHODDEF \
Serhiy Storchaka92e8af62015-04-04 00:12:11 +0300216 {"_forget_codec", (PyCFunction)_codecs__forget_codec, METH_O, _codecs__forget_codec__doc__},
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300217
218static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300219_codecs__forget_codec_impl(PyObject *module, const char *encoding);
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300220
221static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300222_codecs__forget_codec(PyObject *module, PyObject *arg)
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300223{
224 PyObject *return_value = NULL;
225 const char *encoding;
226
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200227 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200228 _PyArg_BadArgument("_forget_codec", "argument", "str", arg);
Serhiy Storchaka32d96a22018-12-25 13:23:47 +0200229 goto exit;
230 }
231 Py_ssize_t encoding_length;
232 encoding = PyUnicode_AsUTF8AndSize(arg, &encoding_length);
233 if (encoding == NULL) {
234 goto exit;
235 }
236 if (strlen(encoding) != (size_t)encoding_length) {
237 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300238 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300239 }
Serhiy Storchaka1009bf12015-04-03 23:53:51 +0300240 return_value = _codecs__forget_codec_impl(module, encoding);
241
242exit:
243 return return_value;
244}
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300245
246PyDoc_STRVAR(_codecs_escape_decode__doc__,
247"escape_decode($module, data, errors=None, /)\n"
248"--\n"
249"\n");
250
251#define _CODECS_ESCAPE_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200252 {"escape_decode", (PyCFunction)(void(*)(void))_codecs_escape_decode, METH_FASTCALL, _codecs_escape_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300253
254static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300255_codecs_escape_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300256 const char *errors);
257
258static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200259_codecs_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300260{
261 PyObject *return_value = NULL;
262 Py_buffer data = {NULL, NULL};
263 const char *errors = NULL;
264
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200265 if (!_PyArg_CheckPositional("escape_decode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100266 goto exit;
267 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200268 if (PyUnicode_Check(args[0])) {
269 Py_ssize_t len;
270 const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
271 if (ptr == NULL) {
272 goto exit;
273 }
274 PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
275 }
276 else { /* any bytes-like object */
277 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
278 goto exit;
279 }
280 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200281 _PyArg_BadArgument("escape_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200282 goto exit;
283 }
284 }
285 if (nargs < 2) {
286 goto skip_optional;
287 }
288 if (args[1] == Py_None) {
289 errors = NULL;
290 }
291 else if (PyUnicode_Check(args[1])) {
292 Py_ssize_t errors_length;
293 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
294 if (errors == NULL) {
295 goto exit;
296 }
297 if (strlen(errors) != (size_t)errors_length) {
298 PyErr_SetString(PyExc_ValueError, "embedded null character");
299 goto exit;
300 }
301 }
302 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200303 _PyArg_BadArgument("escape_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200304 goto exit;
305 }
306skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300307 return_value = _codecs_escape_decode_impl(module, &data, errors);
308
309exit:
310 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300311 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300312 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300313 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300314
315 return return_value;
316}
317
318PyDoc_STRVAR(_codecs_escape_encode__doc__,
319"escape_encode($module, data, errors=None, /)\n"
320"--\n"
321"\n");
322
323#define _CODECS_ESCAPE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200324 {"escape_encode", (PyCFunction)(void(*)(void))_codecs_escape_encode, METH_FASTCALL, _codecs_escape_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300325
326static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300327_codecs_escape_encode_impl(PyObject *module, PyObject *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300328 const char *errors);
329
330static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200331_codecs_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300332{
333 PyObject *return_value = NULL;
334 PyObject *data;
335 const char *errors = NULL;
336
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200337 if (!_PyArg_CheckPositional("escape_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100338 goto exit;
339 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200340 if (!PyBytes_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200341 _PyArg_BadArgument("escape_encode", "argument 1", "bytes", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200342 goto exit;
343 }
344 data = args[0];
345 if (nargs < 2) {
346 goto skip_optional;
347 }
348 if (args[1] == Py_None) {
349 errors = NULL;
350 }
351 else if (PyUnicode_Check(args[1])) {
352 Py_ssize_t errors_length;
353 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
354 if (errors == NULL) {
355 goto exit;
356 }
357 if (strlen(errors) != (size_t)errors_length) {
358 PyErr_SetString(PyExc_ValueError, "embedded null character");
359 goto exit;
360 }
361 }
362 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200363 _PyArg_BadArgument("escape_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200364 goto exit;
365 }
366skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300367 return_value = _codecs_escape_encode_impl(module, data, errors);
368
369exit:
370 return return_value;
371}
372
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300373PyDoc_STRVAR(_codecs_utf_7_decode__doc__,
374"utf_7_decode($module, data, errors=None, final=False, /)\n"
375"--\n"
376"\n");
377
378#define _CODECS_UTF_7_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200379 {"utf_7_decode", (PyCFunction)(void(*)(void))_codecs_utf_7_decode, METH_FASTCALL, _codecs_utf_7_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300380
381static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300382_codecs_utf_7_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300383 const char *errors, int final);
384
385static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200386_codecs_utf_7_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300387{
388 PyObject *return_value = NULL;
389 Py_buffer data = {NULL, NULL};
390 const char *errors = NULL;
391 int final = 0;
392
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200393 if (!_PyArg_CheckPositional("utf_7_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100394 goto exit;
395 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200396 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
397 goto exit;
398 }
399 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200400 _PyArg_BadArgument("utf_7_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200401 goto exit;
402 }
403 if (nargs < 2) {
404 goto skip_optional;
405 }
406 if (args[1] == Py_None) {
407 errors = NULL;
408 }
409 else if (PyUnicode_Check(args[1])) {
410 Py_ssize_t errors_length;
411 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
412 if (errors == NULL) {
413 goto exit;
414 }
415 if (strlen(errors) != (size_t)errors_length) {
416 PyErr_SetString(PyExc_ValueError, "embedded null character");
417 goto exit;
418 }
419 }
420 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200421 _PyArg_BadArgument("utf_7_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200422 goto exit;
423 }
424 if (nargs < 3) {
425 goto skip_optional;
426 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200427 final = _PyLong_AsInt(args[2]);
428 if (final == -1 && PyErr_Occurred()) {
429 goto exit;
430 }
431skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300432 return_value = _codecs_utf_7_decode_impl(module, &data, errors, final);
433
434exit:
435 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300436 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300437 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300438 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300439
440 return return_value;
441}
442
443PyDoc_STRVAR(_codecs_utf_8_decode__doc__,
444"utf_8_decode($module, data, errors=None, final=False, /)\n"
445"--\n"
446"\n");
447
448#define _CODECS_UTF_8_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200449 {"utf_8_decode", (PyCFunction)(void(*)(void))_codecs_utf_8_decode, METH_FASTCALL, _codecs_utf_8_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300450
451static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300452_codecs_utf_8_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300453 const char *errors, int final);
454
455static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200456_codecs_utf_8_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300457{
458 PyObject *return_value = NULL;
459 Py_buffer data = {NULL, NULL};
460 const char *errors = NULL;
461 int final = 0;
462
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200463 if (!_PyArg_CheckPositional("utf_8_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100464 goto exit;
465 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200466 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
467 goto exit;
468 }
469 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200470 _PyArg_BadArgument("utf_8_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200471 goto exit;
472 }
473 if (nargs < 2) {
474 goto skip_optional;
475 }
476 if (args[1] == Py_None) {
477 errors = NULL;
478 }
479 else if (PyUnicode_Check(args[1])) {
480 Py_ssize_t errors_length;
481 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
482 if (errors == NULL) {
483 goto exit;
484 }
485 if (strlen(errors) != (size_t)errors_length) {
486 PyErr_SetString(PyExc_ValueError, "embedded null character");
487 goto exit;
488 }
489 }
490 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200491 _PyArg_BadArgument("utf_8_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200492 goto exit;
493 }
494 if (nargs < 3) {
495 goto skip_optional;
496 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200497 final = _PyLong_AsInt(args[2]);
498 if (final == -1 && PyErr_Occurred()) {
499 goto exit;
500 }
501skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300502 return_value = _codecs_utf_8_decode_impl(module, &data, errors, final);
503
504exit:
505 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300506 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300507 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300508 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300509
510 return return_value;
511}
512
513PyDoc_STRVAR(_codecs_utf_16_decode__doc__,
514"utf_16_decode($module, data, errors=None, final=False, /)\n"
515"--\n"
516"\n");
517
518#define _CODECS_UTF_16_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200519 {"utf_16_decode", (PyCFunction)(void(*)(void))_codecs_utf_16_decode, METH_FASTCALL, _codecs_utf_16_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300520
521static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300522_codecs_utf_16_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300523 const char *errors, int final);
524
525static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200526_codecs_utf_16_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300527{
528 PyObject *return_value = NULL;
529 Py_buffer data = {NULL, NULL};
530 const char *errors = NULL;
531 int final = 0;
532
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200533 if (!_PyArg_CheckPositional("utf_16_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100534 goto exit;
535 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200536 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
537 goto exit;
538 }
539 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200540 _PyArg_BadArgument("utf_16_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200541 goto exit;
542 }
543 if (nargs < 2) {
544 goto skip_optional;
545 }
546 if (args[1] == Py_None) {
547 errors = NULL;
548 }
549 else if (PyUnicode_Check(args[1])) {
550 Py_ssize_t errors_length;
551 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
552 if (errors == NULL) {
553 goto exit;
554 }
555 if (strlen(errors) != (size_t)errors_length) {
556 PyErr_SetString(PyExc_ValueError, "embedded null character");
557 goto exit;
558 }
559 }
560 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200561 _PyArg_BadArgument("utf_16_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200562 goto exit;
563 }
564 if (nargs < 3) {
565 goto skip_optional;
566 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200567 final = _PyLong_AsInt(args[2]);
568 if (final == -1 && PyErr_Occurred()) {
569 goto exit;
570 }
571skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300572 return_value = _codecs_utf_16_decode_impl(module, &data, errors, final);
573
574exit:
575 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300576 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300577 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300578 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300579
580 return return_value;
581}
582
583PyDoc_STRVAR(_codecs_utf_16_le_decode__doc__,
584"utf_16_le_decode($module, data, errors=None, final=False, /)\n"
585"--\n"
586"\n");
587
588#define _CODECS_UTF_16_LE_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200589 {"utf_16_le_decode", (PyCFunction)(void(*)(void))_codecs_utf_16_le_decode, METH_FASTCALL, _codecs_utf_16_le_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300590
591static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300592_codecs_utf_16_le_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300593 const char *errors, int final);
594
595static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200596_codecs_utf_16_le_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300597{
598 PyObject *return_value = NULL;
599 Py_buffer data = {NULL, NULL};
600 const char *errors = NULL;
601 int final = 0;
602
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200603 if (!_PyArg_CheckPositional("utf_16_le_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100604 goto exit;
605 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200606 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
607 goto exit;
608 }
609 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200610 _PyArg_BadArgument("utf_16_le_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200611 goto exit;
612 }
613 if (nargs < 2) {
614 goto skip_optional;
615 }
616 if (args[1] == Py_None) {
617 errors = NULL;
618 }
619 else if (PyUnicode_Check(args[1])) {
620 Py_ssize_t errors_length;
621 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
622 if (errors == NULL) {
623 goto exit;
624 }
625 if (strlen(errors) != (size_t)errors_length) {
626 PyErr_SetString(PyExc_ValueError, "embedded null character");
627 goto exit;
628 }
629 }
630 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200631 _PyArg_BadArgument("utf_16_le_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200632 goto exit;
633 }
634 if (nargs < 3) {
635 goto skip_optional;
636 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200637 final = _PyLong_AsInt(args[2]);
638 if (final == -1 && PyErr_Occurred()) {
639 goto exit;
640 }
641skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300642 return_value = _codecs_utf_16_le_decode_impl(module, &data, errors, final);
643
644exit:
645 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300646 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300647 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300648 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300649
650 return return_value;
651}
652
653PyDoc_STRVAR(_codecs_utf_16_be_decode__doc__,
654"utf_16_be_decode($module, data, errors=None, final=False, /)\n"
655"--\n"
656"\n");
657
658#define _CODECS_UTF_16_BE_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200659 {"utf_16_be_decode", (PyCFunction)(void(*)(void))_codecs_utf_16_be_decode, METH_FASTCALL, _codecs_utf_16_be_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300660
661static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300662_codecs_utf_16_be_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300663 const char *errors, int final);
664
665static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200666_codecs_utf_16_be_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300667{
668 PyObject *return_value = NULL;
669 Py_buffer data = {NULL, NULL};
670 const char *errors = NULL;
671 int final = 0;
672
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200673 if (!_PyArg_CheckPositional("utf_16_be_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100674 goto exit;
675 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200676 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
677 goto exit;
678 }
679 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200680 _PyArg_BadArgument("utf_16_be_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200681 goto exit;
682 }
683 if (nargs < 2) {
684 goto skip_optional;
685 }
686 if (args[1] == Py_None) {
687 errors = NULL;
688 }
689 else if (PyUnicode_Check(args[1])) {
690 Py_ssize_t errors_length;
691 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
692 if (errors == NULL) {
693 goto exit;
694 }
695 if (strlen(errors) != (size_t)errors_length) {
696 PyErr_SetString(PyExc_ValueError, "embedded null character");
697 goto exit;
698 }
699 }
700 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200701 _PyArg_BadArgument("utf_16_be_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200702 goto exit;
703 }
704 if (nargs < 3) {
705 goto skip_optional;
706 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200707 final = _PyLong_AsInt(args[2]);
708 if (final == -1 && PyErr_Occurred()) {
709 goto exit;
710 }
711skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300712 return_value = _codecs_utf_16_be_decode_impl(module, &data, errors, final);
713
714exit:
715 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300716 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300717 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300718 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300719
720 return return_value;
721}
722
723PyDoc_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 Storchaka4a934d42018-11-27 11:27:36 +0200730 {"utf_16_ex_decode", (PyCFunction)(void(*)(void))_codecs_utf_16_ex_decode, METH_FASTCALL, _codecs_utf_16_ex_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300731
732static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300733_codecs_utf_16_ex_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300734 const char *errors, int byteorder, int final);
735
736static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200737_codecs_utf_16_ex_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300738{
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 Storchaka4fa95912019-01-11 16:01:14 +0200745 if (!_PyArg_CheckPositional("utf_16_ex_decode", nargs, 1, 4)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100746 goto exit;
747 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200748 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
749 goto exit;
750 }
751 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200752 _PyArg_BadArgument("utf_16_ex_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200753 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 {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200773 _PyArg_BadArgument("utf_16_ex_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200774 goto exit;
775 }
776 if (nargs < 3) {
777 goto skip_optional;
778 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200779 byteorder = _PyLong_AsInt(args[2]);
780 if (byteorder == -1 && PyErr_Occurred()) {
781 goto exit;
782 }
783 if (nargs < 4) {
784 goto skip_optional;
785 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200786 final = _PyLong_AsInt(args[3]);
787 if (final == -1 && PyErr_Occurred()) {
788 goto exit;
789 }
790skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300791 return_value = _codecs_utf_16_ex_decode_impl(module, &data, errors, byteorder, final);
792
793exit:
794 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300795 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300796 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300797 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300798
799 return return_value;
800}
801
802PyDoc_STRVAR(_codecs_utf_32_decode__doc__,
803"utf_32_decode($module, data, errors=None, final=False, /)\n"
804"--\n"
805"\n");
806
807#define _CODECS_UTF_32_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200808 {"utf_32_decode", (PyCFunction)(void(*)(void))_codecs_utf_32_decode, METH_FASTCALL, _codecs_utf_32_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300809
810static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300811_codecs_utf_32_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300812 const char *errors, int final);
813
814static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200815_codecs_utf_32_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300816{
817 PyObject *return_value = NULL;
818 Py_buffer data = {NULL, NULL};
819 const char *errors = NULL;
820 int final = 0;
821
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200822 if (!_PyArg_CheckPositional("utf_32_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100823 goto exit;
824 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200825 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
826 goto exit;
827 }
828 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200829 _PyArg_BadArgument("utf_32_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200830 goto exit;
831 }
832 if (nargs < 2) {
833 goto skip_optional;
834 }
835 if (args[1] == Py_None) {
836 errors = NULL;
837 }
838 else if (PyUnicode_Check(args[1])) {
839 Py_ssize_t errors_length;
840 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
841 if (errors == NULL) {
842 goto exit;
843 }
844 if (strlen(errors) != (size_t)errors_length) {
845 PyErr_SetString(PyExc_ValueError, "embedded null character");
846 goto exit;
847 }
848 }
849 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200850 _PyArg_BadArgument("utf_32_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200851 goto exit;
852 }
853 if (nargs < 3) {
854 goto skip_optional;
855 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200856 final = _PyLong_AsInt(args[2]);
857 if (final == -1 && PyErr_Occurred()) {
858 goto exit;
859 }
860skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300861 return_value = _codecs_utf_32_decode_impl(module, &data, errors, final);
862
863exit:
864 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300865 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300866 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300867 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300868
869 return return_value;
870}
871
872PyDoc_STRVAR(_codecs_utf_32_le_decode__doc__,
873"utf_32_le_decode($module, data, errors=None, final=False, /)\n"
874"--\n"
875"\n");
876
877#define _CODECS_UTF_32_LE_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200878 {"utf_32_le_decode", (PyCFunction)(void(*)(void))_codecs_utf_32_le_decode, METH_FASTCALL, _codecs_utf_32_le_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300879
880static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300881_codecs_utf_32_le_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300882 const char *errors, int final);
883
884static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200885_codecs_utf_32_le_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300886{
887 PyObject *return_value = NULL;
888 Py_buffer data = {NULL, NULL};
889 const char *errors = NULL;
890 int final = 0;
891
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200892 if (!_PyArg_CheckPositional("utf_32_le_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100893 goto exit;
894 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200895 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
896 goto exit;
897 }
898 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200899 _PyArg_BadArgument("utf_32_le_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200900 goto exit;
901 }
902 if (nargs < 2) {
903 goto skip_optional;
904 }
905 if (args[1] == Py_None) {
906 errors = NULL;
907 }
908 else if (PyUnicode_Check(args[1])) {
909 Py_ssize_t errors_length;
910 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
911 if (errors == NULL) {
912 goto exit;
913 }
914 if (strlen(errors) != (size_t)errors_length) {
915 PyErr_SetString(PyExc_ValueError, "embedded null character");
916 goto exit;
917 }
918 }
919 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200920 _PyArg_BadArgument("utf_32_le_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200921 goto exit;
922 }
923 if (nargs < 3) {
924 goto skip_optional;
925 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200926 final = _PyLong_AsInt(args[2]);
927 if (final == -1 && PyErr_Occurred()) {
928 goto exit;
929 }
930skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300931 return_value = _codecs_utf_32_le_decode_impl(module, &data, errors, final);
932
933exit:
934 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300935 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300936 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +0300937 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300938
939 return return_value;
940}
941
942PyDoc_STRVAR(_codecs_utf_32_be_decode__doc__,
943"utf_32_be_decode($module, data, errors=None, final=False, /)\n"
944"--\n"
945"\n");
946
947#define _CODECS_UTF_32_BE_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +0200948 {"utf_32_be_decode", (PyCFunction)(void(*)(void))_codecs_utf_32_be_decode, METH_FASTCALL, _codecs_utf_32_be_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300949
950static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300951_codecs_utf_32_be_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300952 const char *errors, int final);
953
954static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +0200955_codecs_utf_32_be_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300956{
957 PyObject *return_value = NULL;
958 Py_buffer data = {NULL, NULL};
959 const char *errors = NULL;
960 int final = 0;
961
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200962 if (!_PyArg_CheckPositional("utf_32_be_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +0100963 goto exit;
964 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200965 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
966 goto exit;
967 }
968 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200969 _PyArg_BadArgument("utf_32_be_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200970 goto exit;
971 }
972 if (nargs < 2) {
973 goto skip_optional;
974 }
975 if (args[1] == Py_None) {
976 errors = NULL;
977 }
978 else if (PyUnicode_Check(args[1])) {
979 Py_ssize_t errors_length;
980 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
981 if (errors == NULL) {
982 goto exit;
983 }
984 if (strlen(errors) != (size_t)errors_length) {
985 PyErr_SetString(PyExc_ValueError, "embedded null character");
986 goto exit;
987 }
988 }
989 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +0200990 _PyArg_BadArgument("utf_32_be_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200991 goto exit;
992 }
993 if (nargs < 3) {
994 goto skip_optional;
995 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +0200996 final = _PyLong_AsInt(args[2]);
997 if (final == -1 && PyErr_Occurred()) {
998 goto exit;
999 }
1000skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001001 return_value = _codecs_utf_32_be_decode_impl(module, &data, errors, final);
1002
1003exit:
1004 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001005 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001006 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001007 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001008
1009 return return_value;
1010}
1011
1012PyDoc_STRVAR(_codecs_utf_32_ex_decode__doc__,
1013"utf_32_ex_decode($module, data, errors=None, byteorder=0, final=False,\n"
1014" /)\n"
1015"--\n"
1016"\n");
1017
1018#define _CODECS_UTF_32_EX_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001019 {"utf_32_ex_decode", (PyCFunction)(void(*)(void))_codecs_utf_32_ex_decode, METH_FASTCALL, _codecs_utf_32_ex_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001020
1021static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001022_codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001023 const char *errors, int byteorder, int final);
1024
1025static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001026_codecs_utf_32_ex_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001027{
1028 PyObject *return_value = NULL;
1029 Py_buffer data = {NULL, NULL};
1030 const char *errors = NULL;
1031 int byteorder = 0;
1032 int final = 0;
1033
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001034 if (!_PyArg_CheckPositional("utf_32_ex_decode", nargs, 1, 4)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001035 goto exit;
1036 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001037 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1038 goto exit;
1039 }
1040 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001041 _PyArg_BadArgument("utf_32_ex_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001042 goto exit;
1043 }
1044 if (nargs < 2) {
1045 goto skip_optional;
1046 }
1047 if (args[1] == Py_None) {
1048 errors = NULL;
1049 }
1050 else if (PyUnicode_Check(args[1])) {
1051 Py_ssize_t errors_length;
1052 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1053 if (errors == NULL) {
1054 goto exit;
1055 }
1056 if (strlen(errors) != (size_t)errors_length) {
1057 PyErr_SetString(PyExc_ValueError, "embedded null character");
1058 goto exit;
1059 }
1060 }
1061 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001062 _PyArg_BadArgument("utf_32_ex_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001063 goto exit;
1064 }
1065 if (nargs < 3) {
1066 goto skip_optional;
1067 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001068 byteorder = _PyLong_AsInt(args[2]);
1069 if (byteorder == -1 && PyErr_Occurred()) {
1070 goto exit;
1071 }
1072 if (nargs < 4) {
1073 goto skip_optional;
1074 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001075 final = _PyLong_AsInt(args[3]);
1076 if (final == -1 && PyErr_Occurred()) {
1077 goto exit;
1078 }
1079skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001080 return_value = _codecs_utf_32_ex_decode_impl(module, &data, errors, byteorder, final);
1081
1082exit:
1083 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001084 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001085 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001086 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001087
1088 return return_value;
1089}
1090
1091PyDoc_STRVAR(_codecs_unicode_escape_decode__doc__,
1092"unicode_escape_decode($module, data, errors=None, /)\n"
1093"--\n"
1094"\n");
1095
1096#define _CODECS_UNICODE_ESCAPE_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001097 {"unicode_escape_decode", (PyCFunction)(void(*)(void))_codecs_unicode_escape_decode, METH_FASTCALL, _codecs_unicode_escape_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001098
1099static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001100_codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001101 const char *errors);
1102
1103static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001104_codecs_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001105{
1106 PyObject *return_value = NULL;
1107 Py_buffer data = {NULL, NULL};
1108 const char *errors = NULL;
1109
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001110 if (!_PyArg_CheckPositional("unicode_escape_decode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001111 goto exit;
1112 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001113 if (PyUnicode_Check(args[0])) {
1114 Py_ssize_t len;
1115 const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
1116 if (ptr == NULL) {
1117 goto exit;
1118 }
1119 PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
1120 }
1121 else { /* any bytes-like object */
1122 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1123 goto exit;
1124 }
1125 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001126 _PyArg_BadArgument("unicode_escape_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001127 goto exit;
1128 }
1129 }
1130 if (nargs < 2) {
1131 goto skip_optional;
1132 }
1133 if (args[1] == Py_None) {
1134 errors = NULL;
1135 }
1136 else if (PyUnicode_Check(args[1])) {
1137 Py_ssize_t errors_length;
1138 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1139 if (errors == NULL) {
1140 goto exit;
1141 }
1142 if (strlen(errors) != (size_t)errors_length) {
1143 PyErr_SetString(PyExc_ValueError, "embedded null character");
1144 goto exit;
1145 }
1146 }
1147 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001148 _PyArg_BadArgument("unicode_escape_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001149 goto exit;
1150 }
1151skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001152 return_value = _codecs_unicode_escape_decode_impl(module, &data, errors);
1153
1154exit:
1155 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001156 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001157 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001158 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001159
1160 return return_value;
1161}
1162
1163PyDoc_STRVAR(_codecs_raw_unicode_escape_decode__doc__,
1164"raw_unicode_escape_decode($module, data, errors=None, /)\n"
1165"--\n"
1166"\n");
1167
1168#define _CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001169 {"raw_unicode_escape_decode", (PyCFunction)(void(*)(void))_codecs_raw_unicode_escape_decode, METH_FASTCALL, _codecs_raw_unicode_escape_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001170
1171static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001172_codecs_raw_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001173 const char *errors);
1174
1175static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001176_codecs_raw_unicode_escape_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001177{
1178 PyObject *return_value = NULL;
1179 Py_buffer data = {NULL, NULL};
1180 const char *errors = NULL;
1181
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001182 if (!_PyArg_CheckPositional("raw_unicode_escape_decode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001183 goto exit;
1184 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001185 if (PyUnicode_Check(args[0])) {
1186 Py_ssize_t len;
1187 const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
1188 if (ptr == NULL) {
1189 goto exit;
1190 }
1191 PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
1192 }
1193 else { /* any bytes-like object */
1194 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1195 goto exit;
1196 }
1197 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001198 _PyArg_BadArgument("raw_unicode_escape_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001199 goto exit;
1200 }
1201 }
1202 if (nargs < 2) {
1203 goto skip_optional;
1204 }
1205 if (args[1] == Py_None) {
1206 errors = NULL;
1207 }
1208 else if (PyUnicode_Check(args[1])) {
1209 Py_ssize_t errors_length;
1210 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1211 if (errors == NULL) {
1212 goto exit;
1213 }
1214 if (strlen(errors) != (size_t)errors_length) {
1215 PyErr_SetString(PyExc_ValueError, "embedded null character");
1216 goto exit;
1217 }
1218 }
1219 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001220 _PyArg_BadArgument("raw_unicode_escape_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001221 goto exit;
1222 }
1223skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001224 return_value = _codecs_raw_unicode_escape_decode_impl(module, &data, errors);
1225
1226exit:
1227 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001228 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001229 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001230 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001231
1232 return return_value;
1233}
1234
1235PyDoc_STRVAR(_codecs_latin_1_decode__doc__,
1236"latin_1_decode($module, data, errors=None, /)\n"
1237"--\n"
1238"\n");
1239
1240#define _CODECS_LATIN_1_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001241 {"latin_1_decode", (PyCFunction)(void(*)(void))_codecs_latin_1_decode, METH_FASTCALL, _codecs_latin_1_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001242
1243static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001244_codecs_latin_1_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001245 const char *errors);
1246
1247static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001248_codecs_latin_1_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001249{
1250 PyObject *return_value = NULL;
1251 Py_buffer data = {NULL, NULL};
1252 const char *errors = NULL;
1253
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001254 if (!_PyArg_CheckPositional("latin_1_decode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001255 goto exit;
1256 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001257 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1258 goto exit;
1259 }
1260 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001261 _PyArg_BadArgument("latin_1_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001262 goto exit;
1263 }
1264 if (nargs < 2) {
1265 goto skip_optional;
1266 }
1267 if (args[1] == Py_None) {
1268 errors = NULL;
1269 }
1270 else if (PyUnicode_Check(args[1])) {
1271 Py_ssize_t errors_length;
1272 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1273 if (errors == NULL) {
1274 goto exit;
1275 }
1276 if (strlen(errors) != (size_t)errors_length) {
1277 PyErr_SetString(PyExc_ValueError, "embedded null character");
1278 goto exit;
1279 }
1280 }
1281 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001282 _PyArg_BadArgument("latin_1_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001283 goto exit;
1284 }
1285skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001286 return_value = _codecs_latin_1_decode_impl(module, &data, errors);
1287
1288exit:
1289 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001290 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001291 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001292 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001293
1294 return return_value;
1295}
1296
1297PyDoc_STRVAR(_codecs_ascii_decode__doc__,
1298"ascii_decode($module, data, errors=None, /)\n"
1299"--\n"
1300"\n");
1301
1302#define _CODECS_ASCII_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001303 {"ascii_decode", (PyCFunction)(void(*)(void))_codecs_ascii_decode, METH_FASTCALL, _codecs_ascii_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001304
1305static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001306_codecs_ascii_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001307 const char *errors);
1308
1309static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001310_codecs_ascii_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001311{
1312 PyObject *return_value = NULL;
1313 Py_buffer data = {NULL, NULL};
1314 const char *errors = NULL;
1315
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001316 if (!_PyArg_CheckPositional("ascii_decode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001317 goto exit;
1318 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001319 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1320 goto exit;
1321 }
1322 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001323 _PyArg_BadArgument("ascii_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001324 goto exit;
1325 }
1326 if (nargs < 2) {
1327 goto skip_optional;
1328 }
1329 if (args[1] == Py_None) {
1330 errors = NULL;
1331 }
1332 else if (PyUnicode_Check(args[1])) {
1333 Py_ssize_t errors_length;
1334 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1335 if (errors == NULL) {
1336 goto exit;
1337 }
1338 if (strlen(errors) != (size_t)errors_length) {
1339 PyErr_SetString(PyExc_ValueError, "embedded null character");
1340 goto exit;
1341 }
1342 }
1343 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001344 _PyArg_BadArgument("ascii_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001345 goto exit;
1346 }
1347skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001348 return_value = _codecs_ascii_decode_impl(module, &data, errors);
1349
1350exit:
1351 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001352 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001353 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001354 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001355
1356 return return_value;
1357}
1358
1359PyDoc_STRVAR(_codecs_charmap_decode__doc__,
1360"charmap_decode($module, data, errors=None, mapping=None, /)\n"
1361"--\n"
1362"\n");
1363
1364#define _CODECS_CHARMAP_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001365 {"charmap_decode", (PyCFunction)(void(*)(void))_codecs_charmap_decode, METH_FASTCALL, _codecs_charmap_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001366
1367static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001368_codecs_charmap_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001369 const char *errors, PyObject *mapping);
1370
1371static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001372_codecs_charmap_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001373{
1374 PyObject *return_value = NULL;
1375 Py_buffer data = {NULL, NULL};
1376 const char *errors = NULL;
Serhiy Storchaka279f4462019-09-14 12:24:05 +03001377 PyObject *mapping = Py_None;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001378
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001379 if (!_PyArg_CheckPositional("charmap_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001380 goto exit;
1381 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001382 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1383 goto exit;
1384 }
1385 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001386 _PyArg_BadArgument("charmap_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001387 goto exit;
1388 }
1389 if (nargs < 2) {
1390 goto skip_optional;
1391 }
1392 if (args[1] == Py_None) {
1393 errors = NULL;
1394 }
1395 else if (PyUnicode_Check(args[1])) {
1396 Py_ssize_t errors_length;
1397 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1398 if (errors == NULL) {
1399 goto exit;
1400 }
1401 if (strlen(errors) != (size_t)errors_length) {
1402 PyErr_SetString(PyExc_ValueError, "embedded null character");
1403 goto exit;
1404 }
1405 }
1406 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001407 _PyArg_BadArgument("charmap_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001408 goto exit;
1409 }
1410 if (nargs < 3) {
1411 goto skip_optional;
1412 }
1413 mapping = args[2];
1414skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001415 return_value = _codecs_charmap_decode_impl(module, &data, errors, mapping);
1416
1417exit:
1418 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001419 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001420 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001421 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001422
1423 return return_value;
1424}
1425
Steve Dowercc16be82016-09-08 10:35:16 -07001426#if defined(MS_WINDOWS)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001427
1428PyDoc_STRVAR(_codecs_mbcs_decode__doc__,
1429"mbcs_decode($module, data, errors=None, final=False, /)\n"
1430"--\n"
1431"\n");
1432
1433#define _CODECS_MBCS_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001434 {"mbcs_decode", (PyCFunction)(void(*)(void))_codecs_mbcs_decode, METH_FASTCALL, _codecs_mbcs_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001435
1436static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001437_codecs_mbcs_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001438 const char *errors, int final);
1439
1440static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001441_codecs_mbcs_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001442{
1443 PyObject *return_value = NULL;
1444 Py_buffer data = {NULL, NULL};
1445 const char *errors = NULL;
1446 int final = 0;
1447
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001448 if (!_PyArg_CheckPositional("mbcs_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001449 goto exit;
1450 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001451 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1452 goto exit;
1453 }
1454 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001455 _PyArg_BadArgument("mbcs_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001456 goto exit;
1457 }
1458 if (nargs < 2) {
1459 goto skip_optional;
1460 }
1461 if (args[1] == Py_None) {
1462 errors = NULL;
1463 }
1464 else if (PyUnicode_Check(args[1])) {
1465 Py_ssize_t errors_length;
1466 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1467 if (errors == NULL) {
1468 goto exit;
1469 }
1470 if (strlen(errors) != (size_t)errors_length) {
1471 PyErr_SetString(PyExc_ValueError, "embedded null character");
1472 goto exit;
1473 }
1474 }
1475 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001476 _PyArg_BadArgument("mbcs_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001477 goto exit;
1478 }
1479 if (nargs < 3) {
1480 goto skip_optional;
1481 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001482 final = _PyLong_AsInt(args[2]);
1483 if (final == -1 && PyErr_Occurred()) {
1484 goto exit;
1485 }
1486skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001487 return_value = _codecs_mbcs_decode_impl(module, &data, errors, final);
1488
1489exit:
1490 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001491 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001492 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001493 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001494
1495 return return_value;
1496}
1497
Steve Dowercc16be82016-09-08 10:35:16 -07001498#endif /* defined(MS_WINDOWS) */
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001499
Steve Dowercc16be82016-09-08 10:35:16 -07001500#if defined(MS_WINDOWS)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001501
Steve Dowerf5aba582016-09-06 19:42:27 -07001502PyDoc_STRVAR(_codecs_oem_decode__doc__,
1503"oem_decode($module, data, errors=None, final=False, /)\n"
1504"--\n"
1505"\n");
1506
1507#define _CODECS_OEM_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001508 {"oem_decode", (PyCFunction)(void(*)(void))_codecs_oem_decode, METH_FASTCALL, _codecs_oem_decode__doc__},
Steve Dowerf5aba582016-09-06 19:42:27 -07001509
1510static PyObject *
1511_codecs_oem_decode_impl(PyObject *module, Py_buffer *data,
1512 const char *errors, int final);
1513
1514static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001515_codecs_oem_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Steve Dowerf5aba582016-09-06 19:42:27 -07001516{
1517 PyObject *return_value = NULL;
1518 Py_buffer data = {NULL, NULL};
1519 const char *errors = NULL;
1520 int final = 0;
1521
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001522 if (!_PyArg_CheckPositional("oem_decode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001523 goto exit;
1524 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001525 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1526 goto exit;
1527 }
1528 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001529 _PyArg_BadArgument("oem_decode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001530 goto exit;
1531 }
1532 if (nargs < 2) {
1533 goto skip_optional;
1534 }
1535 if (args[1] == Py_None) {
1536 errors = NULL;
1537 }
1538 else if (PyUnicode_Check(args[1])) {
1539 Py_ssize_t errors_length;
1540 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1541 if (errors == NULL) {
1542 goto exit;
1543 }
1544 if (strlen(errors) != (size_t)errors_length) {
1545 PyErr_SetString(PyExc_ValueError, "embedded null character");
1546 goto exit;
1547 }
1548 }
1549 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001550 _PyArg_BadArgument("oem_decode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001551 goto exit;
1552 }
1553 if (nargs < 3) {
1554 goto skip_optional;
1555 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001556 final = _PyLong_AsInt(args[2]);
1557 if (final == -1 && PyErr_Occurred()) {
1558 goto exit;
1559 }
1560skip_optional:
Steve Dowerf5aba582016-09-06 19:42:27 -07001561 return_value = _codecs_oem_decode_impl(module, &data, errors, final);
1562
1563exit:
1564 /* Cleanup for data */
1565 if (data.obj) {
1566 PyBuffer_Release(&data);
1567 }
1568
1569 return return_value;
1570}
1571
Steve Dowercc16be82016-09-08 10:35:16 -07001572#endif /* defined(MS_WINDOWS) */
Steve Dowerf5aba582016-09-06 19:42:27 -07001573
Steve Dowercc16be82016-09-08 10:35:16 -07001574#if defined(MS_WINDOWS)
Steve Dowerf5aba582016-09-06 19:42:27 -07001575
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001576PyDoc_STRVAR(_codecs_code_page_decode__doc__,
1577"code_page_decode($module, codepage, data, errors=None, final=False, /)\n"
1578"--\n"
1579"\n");
1580
1581#define _CODECS_CODE_PAGE_DECODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001582 {"code_page_decode", (PyCFunction)(void(*)(void))_codecs_code_page_decode, METH_FASTCALL, _codecs_code_page_decode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001583
1584static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001585_codecs_code_page_decode_impl(PyObject *module, int codepage,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001586 Py_buffer *data, const char *errors, int final);
1587
1588static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001589_codecs_code_page_decode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001590{
1591 PyObject *return_value = NULL;
1592 int codepage;
1593 Py_buffer data = {NULL, NULL};
1594 const char *errors = NULL;
1595 int final = 0;
1596
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001597 if (!_PyArg_CheckPositional("code_page_decode", nargs, 2, 4)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001598 goto exit;
1599 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001600 codepage = _PyLong_AsInt(args[0]);
1601 if (codepage == -1 && PyErr_Occurred()) {
1602 goto exit;
1603 }
1604 if (PyObject_GetBuffer(args[1], &data, PyBUF_SIMPLE) != 0) {
1605 goto exit;
1606 }
1607 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001608 _PyArg_BadArgument("code_page_decode", "argument 2", "contiguous buffer", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001609 goto exit;
1610 }
1611 if (nargs < 3) {
1612 goto skip_optional;
1613 }
1614 if (args[2] == Py_None) {
1615 errors = NULL;
1616 }
1617 else if (PyUnicode_Check(args[2])) {
1618 Py_ssize_t errors_length;
1619 errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length);
1620 if (errors == NULL) {
1621 goto exit;
1622 }
1623 if (strlen(errors) != (size_t)errors_length) {
1624 PyErr_SetString(PyExc_ValueError, "embedded null character");
1625 goto exit;
1626 }
1627 }
1628 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001629 _PyArg_BadArgument("code_page_decode", "argument 3", "str or None", args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001630 goto exit;
1631 }
1632 if (nargs < 4) {
1633 goto skip_optional;
1634 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001635 final = _PyLong_AsInt(args[3]);
1636 if (final == -1 && PyErr_Occurred()) {
1637 goto exit;
1638 }
1639skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001640 return_value = _codecs_code_page_decode_impl(module, codepage, &data, errors, final);
1641
1642exit:
1643 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001644 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001645 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001646 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001647
1648 return return_value;
1649}
1650
Steve Dowercc16be82016-09-08 10:35:16 -07001651#endif /* defined(MS_WINDOWS) */
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001652
1653PyDoc_STRVAR(_codecs_readbuffer_encode__doc__,
1654"readbuffer_encode($module, data, errors=None, /)\n"
1655"--\n"
1656"\n");
1657
1658#define _CODECS_READBUFFER_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001659 {"readbuffer_encode", (PyCFunction)(void(*)(void))_codecs_readbuffer_encode, METH_FASTCALL, _codecs_readbuffer_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001660
1661static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001662_codecs_readbuffer_encode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001663 const char *errors);
1664
1665static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001666_codecs_readbuffer_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001667{
1668 PyObject *return_value = NULL;
1669 Py_buffer data = {NULL, NULL};
1670 const char *errors = NULL;
1671
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001672 if (!_PyArg_CheckPositional("readbuffer_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001673 goto exit;
1674 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001675 if (PyUnicode_Check(args[0])) {
1676 Py_ssize_t len;
1677 const char *ptr = PyUnicode_AsUTF8AndSize(args[0], &len);
1678 if (ptr == NULL) {
1679 goto exit;
1680 }
1681 PyBuffer_FillInfo(&data, args[0], (void *)ptr, len, 1, 0);
1682 }
1683 else { /* any bytes-like object */
1684 if (PyObject_GetBuffer(args[0], &data, PyBUF_SIMPLE) != 0) {
1685 goto exit;
1686 }
1687 if (!PyBuffer_IsContiguous(&data, 'C')) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001688 _PyArg_BadArgument("readbuffer_encode", "argument 1", "contiguous buffer", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001689 goto exit;
1690 }
1691 }
1692 if (nargs < 2) {
1693 goto skip_optional;
1694 }
1695 if (args[1] == Py_None) {
1696 errors = NULL;
1697 }
1698 else if (PyUnicode_Check(args[1])) {
1699 Py_ssize_t errors_length;
1700 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1701 if (errors == NULL) {
1702 goto exit;
1703 }
1704 if (strlen(errors) != (size_t)errors_length) {
1705 PyErr_SetString(PyExc_ValueError, "embedded null character");
1706 goto exit;
1707 }
1708 }
1709 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001710 _PyArg_BadArgument("readbuffer_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001711 goto exit;
1712 }
1713skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001714 return_value = _codecs_readbuffer_encode_impl(module, &data, errors);
1715
1716exit:
1717 /* Cleanup for data */
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001718 if (data.obj) {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001719 PyBuffer_Release(&data);
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03001720 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001721
1722 return return_value;
1723}
1724
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001725PyDoc_STRVAR(_codecs_utf_7_encode__doc__,
1726"utf_7_encode($module, str, errors=None, /)\n"
1727"--\n"
1728"\n");
1729
1730#define _CODECS_UTF_7_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001731 {"utf_7_encode", (PyCFunction)(void(*)(void))_codecs_utf_7_encode, METH_FASTCALL, _codecs_utf_7_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001732
1733static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001734_codecs_utf_7_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001735 const char *errors);
1736
1737static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001738_codecs_utf_7_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001739{
1740 PyObject *return_value = NULL;
1741 PyObject *str;
1742 const char *errors = NULL;
1743
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001744 if (!_PyArg_CheckPositional("utf_7_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001745 goto exit;
1746 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001747 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001748 _PyArg_BadArgument("utf_7_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001749 goto exit;
1750 }
1751 if (PyUnicode_READY(args[0]) == -1) {
1752 goto exit;
1753 }
1754 str = args[0];
1755 if (nargs < 2) {
1756 goto skip_optional;
1757 }
1758 if (args[1] == Py_None) {
1759 errors = NULL;
1760 }
1761 else if (PyUnicode_Check(args[1])) {
1762 Py_ssize_t errors_length;
1763 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1764 if (errors == NULL) {
1765 goto exit;
1766 }
1767 if (strlen(errors) != (size_t)errors_length) {
1768 PyErr_SetString(PyExc_ValueError, "embedded null character");
1769 goto exit;
1770 }
1771 }
1772 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001773 _PyArg_BadArgument("utf_7_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001774 goto exit;
1775 }
1776skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001777 return_value = _codecs_utf_7_encode_impl(module, str, errors);
1778
1779exit:
1780 return return_value;
1781}
1782
1783PyDoc_STRVAR(_codecs_utf_8_encode__doc__,
1784"utf_8_encode($module, str, errors=None, /)\n"
1785"--\n"
1786"\n");
1787
1788#define _CODECS_UTF_8_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001789 {"utf_8_encode", (PyCFunction)(void(*)(void))_codecs_utf_8_encode, METH_FASTCALL, _codecs_utf_8_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001790
1791static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001792_codecs_utf_8_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001793 const char *errors);
1794
1795static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001796_codecs_utf_8_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001797{
1798 PyObject *return_value = NULL;
1799 PyObject *str;
1800 const char *errors = NULL;
1801
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001802 if (!_PyArg_CheckPositional("utf_8_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001803 goto exit;
1804 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001805 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001806 _PyArg_BadArgument("utf_8_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001807 goto exit;
1808 }
1809 if (PyUnicode_READY(args[0]) == -1) {
1810 goto exit;
1811 }
1812 str = args[0];
1813 if (nargs < 2) {
1814 goto skip_optional;
1815 }
1816 if (args[1] == Py_None) {
1817 errors = NULL;
1818 }
1819 else if (PyUnicode_Check(args[1])) {
1820 Py_ssize_t errors_length;
1821 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1822 if (errors == NULL) {
1823 goto exit;
1824 }
1825 if (strlen(errors) != (size_t)errors_length) {
1826 PyErr_SetString(PyExc_ValueError, "embedded null character");
1827 goto exit;
1828 }
1829 }
1830 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001831 _PyArg_BadArgument("utf_8_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001832 goto exit;
1833 }
1834skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001835 return_value = _codecs_utf_8_encode_impl(module, str, errors);
1836
1837exit:
1838 return return_value;
1839}
1840
1841PyDoc_STRVAR(_codecs_utf_16_encode__doc__,
1842"utf_16_encode($module, str, errors=None, byteorder=0, /)\n"
1843"--\n"
1844"\n");
1845
1846#define _CODECS_UTF_16_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001847 {"utf_16_encode", (PyCFunction)(void(*)(void))_codecs_utf_16_encode, METH_FASTCALL, _codecs_utf_16_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001848
1849static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001850_codecs_utf_16_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001851 const char *errors, int byteorder);
1852
1853static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001854_codecs_utf_16_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001855{
1856 PyObject *return_value = NULL;
1857 PyObject *str;
1858 const char *errors = NULL;
1859 int byteorder = 0;
1860
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001861 if (!_PyArg_CheckPositional("utf_16_encode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001862 goto exit;
1863 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001864 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001865 _PyArg_BadArgument("utf_16_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001866 goto exit;
1867 }
1868 if (PyUnicode_READY(args[0]) == -1) {
1869 goto exit;
1870 }
1871 str = args[0];
1872 if (nargs < 2) {
1873 goto skip_optional;
1874 }
1875 if (args[1] == Py_None) {
1876 errors = NULL;
1877 }
1878 else if (PyUnicode_Check(args[1])) {
1879 Py_ssize_t errors_length;
1880 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1881 if (errors == NULL) {
1882 goto exit;
1883 }
1884 if (strlen(errors) != (size_t)errors_length) {
1885 PyErr_SetString(PyExc_ValueError, "embedded null character");
1886 goto exit;
1887 }
1888 }
1889 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001890 _PyArg_BadArgument("utf_16_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001891 goto exit;
1892 }
1893 if (nargs < 3) {
1894 goto skip_optional;
1895 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001896 byteorder = _PyLong_AsInt(args[2]);
1897 if (byteorder == -1 && PyErr_Occurred()) {
1898 goto exit;
1899 }
1900skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001901 return_value = _codecs_utf_16_encode_impl(module, str, errors, byteorder);
1902
1903exit:
1904 return return_value;
1905}
1906
1907PyDoc_STRVAR(_codecs_utf_16_le_encode__doc__,
1908"utf_16_le_encode($module, str, errors=None, /)\n"
1909"--\n"
1910"\n");
1911
1912#define _CODECS_UTF_16_LE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001913 {"utf_16_le_encode", (PyCFunction)(void(*)(void))_codecs_utf_16_le_encode, METH_FASTCALL, _codecs_utf_16_le_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001914
1915static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001916_codecs_utf_16_le_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001917 const char *errors);
1918
1919static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001920_codecs_utf_16_le_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001921{
1922 PyObject *return_value = NULL;
1923 PyObject *str;
1924 const char *errors = NULL;
1925
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001926 if (!_PyArg_CheckPositional("utf_16_le_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001927 goto exit;
1928 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001929 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001930 _PyArg_BadArgument("utf_16_le_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001931 goto exit;
1932 }
1933 if (PyUnicode_READY(args[0]) == -1) {
1934 goto exit;
1935 }
1936 str = args[0];
1937 if (nargs < 2) {
1938 goto skip_optional;
1939 }
1940 if (args[1] == Py_None) {
1941 errors = NULL;
1942 }
1943 else if (PyUnicode_Check(args[1])) {
1944 Py_ssize_t errors_length;
1945 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
1946 if (errors == NULL) {
1947 goto exit;
1948 }
1949 if (strlen(errors) != (size_t)errors_length) {
1950 PyErr_SetString(PyExc_ValueError, "embedded null character");
1951 goto exit;
1952 }
1953 }
1954 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001955 _PyArg_BadArgument("utf_16_le_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001956 goto exit;
1957 }
1958skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001959 return_value = _codecs_utf_16_le_encode_impl(module, str, errors);
1960
1961exit:
1962 return return_value;
1963}
1964
1965PyDoc_STRVAR(_codecs_utf_16_be_encode__doc__,
1966"utf_16_be_encode($module, str, errors=None, /)\n"
1967"--\n"
1968"\n");
1969
1970#define _CODECS_UTF_16_BE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02001971 {"utf_16_be_encode", (PyCFunction)(void(*)(void))_codecs_utf_16_be_encode, METH_FASTCALL, _codecs_utf_16_be_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001972
1973static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03001974_codecs_utf_16_be_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001975 const char *errors);
1976
1977static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02001978_codecs_utf_16_be_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001979{
1980 PyObject *return_value = NULL;
1981 PyObject *str;
1982 const char *errors = NULL;
1983
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001984 if (!_PyArg_CheckPositional("utf_16_be_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01001985 goto exit;
1986 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001987 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02001988 _PyArg_BadArgument("utf_16_be_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02001989 goto exit;
1990 }
1991 if (PyUnicode_READY(args[0]) == -1) {
1992 goto exit;
1993 }
1994 str = args[0];
1995 if (nargs < 2) {
1996 goto skip_optional;
1997 }
1998 if (args[1] == Py_None) {
1999 errors = NULL;
2000 }
2001 else if (PyUnicode_Check(args[1])) {
2002 Py_ssize_t errors_length;
2003 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
2004 if (errors == NULL) {
2005 goto exit;
2006 }
2007 if (strlen(errors) != (size_t)errors_length) {
2008 PyErr_SetString(PyExc_ValueError, "embedded null character");
2009 goto exit;
2010 }
2011 }
2012 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002013 _PyArg_BadArgument("utf_16_be_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002014 goto exit;
2015 }
2016skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002017 return_value = _codecs_utf_16_be_encode_impl(module, str, errors);
2018
2019exit:
2020 return return_value;
2021}
2022
2023PyDoc_STRVAR(_codecs_utf_32_encode__doc__,
2024"utf_32_encode($module, str, errors=None, byteorder=0, /)\n"
2025"--\n"
2026"\n");
2027
2028#define _CODECS_UTF_32_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002029 {"utf_32_encode", (PyCFunction)(void(*)(void))_codecs_utf_32_encode, METH_FASTCALL, _codecs_utf_32_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002030
2031static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002032_codecs_utf_32_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002033 const char *errors, int byteorder);
2034
2035static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002036_codecs_utf_32_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002037{
2038 PyObject *return_value = NULL;
2039 PyObject *str;
2040 const char *errors = NULL;
2041 int byteorder = 0;
2042
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002043 if (!_PyArg_CheckPositional("utf_32_encode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002044 goto exit;
2045 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002046 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002047 _PyArg_BadArgument("utf_32_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002048 goto exit;
2049 }
2050 if (PyUnicode_READY(args[0]) == -1) {
2051 goto exit;
2052 }
2053 str = args[0];
2054 if (nargs < 2) {
2055 goto skip_optional;
2056 }
2057 if (args[1] == Py_None) {
2058 errors = NULL;
2059 }
2060 else if (PyUnicode_Check(args[1])) {
2061 Py_ssize_t errors_length;
2062 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
2063 if (errors == NULL) {
2064 goto exit;
2065 }
2066 if (strlen(errors) != (size_t)errors_length) {
2067 PyErr_SetString(PyExc_ValueError, "embedded null character");
2068 goto exit;
2069 }
2070 }
2071 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002072 _PyArg_BadArgument("utf_32_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002073 goto exit;
2074 }
2075 if (nargs < 3) {
2076 goto skip_optional;
2077 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002078 byteorder = _PyLong_AsInt(args[2]);
2079 if (byteorder == -1 && PyErr_Occurred()) {
2080 goto exit;
2081 }
2082skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002083 return_value = _codecs_utf_32_encode_impl(module, str, errors, byteorder);
2084
2085exit:
2086 return return_value;
2087}
2088
2089PyDoc_STRVAR(_codecs_utf_32_le_encode__doc__,
2090"utf_32_le_encode($module, str, errors=None, /)\n"
2091"--\n"
2092"\n");
2093
2094#define _CODECS_UTF_32_LE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002095 {"utf_32_le_encode", (PyCFunction)(void(*)(void))_codecs_utf_32_le_encode, METH_FASTCALL, _codecs_utf_32_le_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002096
2097static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002098_codecs_utf_32_le_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002099 const char *errors);
2100
2101static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002102_codecs_utf_32_le_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002103{
2104 PyObject *return_value = NULL;
2105 PyObject *str;
2106 const char *errors = NULL;
2107
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002108 if (!_PyArg_CheckPositional("utf_32_le_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002109 goto exit;
2110 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002111 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002112 _PyArg_BadArgument("utf_32_le_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002113 goto exit;
2114 }
2115 if (PyUnicode_READY(args[0]) == -1) {
2116 goto exit;
2117 }
2118 str = args[0];
2119 if (nargs < 2) {
2120 goto skip_optional;
2121 }
2122 if (args[1] == Py_None) {
2123 errors = NULL;
2124 }
2125 else if (PyUnicode_Check(args[1])) {
2126 Py_ssize_t errors_length;
2127 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
2128 if (errors == NULL) {
2129 goto exit;
2130 }
2131 if (strlen(errors) != (size_t)errors_length) {
2132 PyErr_SetString(PyExc_ValueError, "embedded null character");
2133 goto exit;
2134 }
2135 }
2136 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002137 _PyArg_BadArgument("utf_32_le_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002138 goto exit;
2139 }
2140skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002141 return_value = _codecs_utf_32_le_encode_impl(module, str, errors);
2142
2143exit:
2144 return return_value;
2145}
2146
2147PyDoc_STRVAR(_codecs_utf_32_be_encode__doc__,
2148"utf_32_be_encode($module, str, errors=None, /)\n"
2149"--\n"
2150"\n");
2151
2152#define _CODECS_UTF_32_BE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002153 {"utf_32_be_encode", (PyCFunction)(void(*)(void))_codecs_utf_32_be_encode, METH_FASTCALL, _codecs_utf_32_be_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002154
2155static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002156_codecs_utf_32_be_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002157 const char *errors);
2158
2159static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002160_codecs_utf_32_be_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002161{
2162 PyObject *return_value = NULL;
2163 PyObject *str;
2164 const char *errors = NULL;
2165
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002166 if (!_PyArg_CheckPositional("utf_32_be_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002167 goto exit;
2168 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002169 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002170 _PyArg_BadArgument("utf_32_be_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002171 goto exit;
2172 }
2173 if (PyUnicode_READY(args[0]) == -1) {
2174 goto exit;
2175 }
2176 str = args[0];
2177 if (nargs < 2) {
2178 goto skip_optional;
2179 }
2180 if (args[1] == Py_None) {
2181 errors = NULL;
2182 }
2183 else if (PyUnicode_Check(args[1])) {
2184 Py_ssize_t errors_length;
2185 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
2186 if (errors == NULL) {
2187 goto exit;
2188 }
2189 if (strlen(errors) != (size_t)errors_length) {
2190 PyErr_SetString(PyExc_ValueError, "embedded null character");
2191 goto exit;
2192 }
2193 }
2194 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002195 _PyArg_BadArgument("utf_32_be_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002196 goto exit;
2197 }
2198skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002199 return_value = _codecs_utf_32_be_encode_impl(module, str, errors);
2200
2201exit:
2202 return return_value;
2203}
2204
2205PyDoc_STRVAR(_codecs_unicode_escape_encode__doc__,
2206"unicode_escape_encode($module, str, errors=None, /)\n"
2207"--\n"
2208"\n");
2209
2210#define _CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002211 {"unicode_escape_encode", (PyCFunction)(void(*)(void))_codecs_unicode_escape_encode, METH_FASTCALL, _codecs_unicode_escape_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002212
2213static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002214_codecs_unicode_escape_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002215 const char *errors);
2216
2217static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002218_codecs_unicode_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002219{
2220 PyObject *return_value = NULL;
2221 PyObject *str;
2222 const char *errors = NULL;
2223
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002224 if (!_PyArg_CheckPositional("unicode_escape_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002225 goto exit;
2226 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002227 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002228 _PyArg_BadArgument("unicode_escape_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002229 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 {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002253 _PyArg_BadArgument("unicode_escape_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002254 goto exit;
2255 }
2256skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002257 return_value = _codecs_unicode_escape_encode_impl(module, str, errors);
2258
2259exit:
2260 return return_value;
2261}
2262
2263PyDoc_STRVAR(_codecs_raw_unicode_escape_encode__doc__,
2264"raw_unicode_escape_encode($module, str, errors=None, /)\n"
2265"--\n"
2266"\n");
2267
2268#define _CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002269 {"raw_unicode_escape_encode", (PyCFunction)(void(*)(void))_codecs_raw_unicode_escape_encode, METH_FASTCALL, _codecs_raw_unicode_escape_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002270
2271static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002272_codecs_raw_unicode_escape_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002273 const char *errors);
2274
2275static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002276_codecs_raw_unicode_escape_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002277{
2278 PyObject *return_value = NULL;
2279 PyObject *str;
2280 const char *errors = NULL;
2281
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002282 if (!_PyArg_CheckPositional("raw_unicode_escape_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002283 goto exit;
2284 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002285 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002286 _PyArg_BadArgument("raw_unicode_escape_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002287 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 {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002311 _PyArg_BadArgument("raw_unicode_escape_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002312 goto exit;
2313 }
2314skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002315 return_value = _codecs_raw_unicode_escape_encode_impl(module, str, errors);
2316
2317exit:
2318 return return_value;
2319}
2320
2321PyDoc_STRVAR(_codecs_latin_1_encode__doc__,
2322"latin_1_encode($module, str, errors=None, /)\n"
2323"--\n"
2324"\n");
2325
2326#define _CODECS_LATIN_1_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002327 {"latin_1_encode", (PyCFunction)(void(*)(void))_codecs_latin_1_encode, METH_FASTCALL, _codecs_latin_1_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002328
2329static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002330_codecs_latin_1_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002331 const char *errors);
2332
2333static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002334_codecs_latin_1_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002335{
2336 PyObject *return_value = NULL;
2337 PyObject *str;
2338 const char *errors = NULL;
2339
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002340 if (!_PyArg_CheckPositional("latin_1_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002341 goto exit;
2342 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002343 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002344 _PyArg_BadArgument("latin_1_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002345 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 {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002369 _PyArg_BadArgument("latin_1_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002370 goto exit;
2371 }
2372skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002373 return_value = _codecs_latin_1_encode_impl(module, str, errors);
2374
2375exit:
2376 return return_value;
2377}
2378
2379PyDoc_STRVAR(_codecs_ascii_encode__doc__,
2380"ascii_encode($module, str, errors=None, /)\n"
2381"--\n"
2382"\n");
2383
2384#define _CODECS_ASCII_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002385 {"ascii_encode", (PyCFunction)(void(*)(void))_codecs_ascii_encode, METH_FASTCALL, _codecs_ascii_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002386
2387static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002388_codecs_ascii_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002389 const char *errors);
2390
2391static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002392_codecs_ascii_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002393{
2394 PyObject *return_value = NULL;
2395 PyObject *str;
2396 const char *errors = NULL;
2397
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002398 if (!_PyArg_CheckPositional("ascii_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002399 goto exit;
2400 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002401 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002402 _PyArg_BadArgument("ascii_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002403 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 {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002427 _PyArg_BadArgument("ascii_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002428 goto exit;
2429 }
2430skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002431 return_value = _codecs_ascii_encode_impl(module, str, errors);
2432
2433exit:
2434 return return_value;
2435}
2436
2437PyDoc_STRVAR(_codecs_charmap_encode__doc__,
2438"charmap_encode($module, str, errors=None, mapping=None, /)\n"
2439"--\n"
2440"\n");
2441
2442#define _CODECS_CHARMAP_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002443 {"charmap_encode", (PyCFunction)(void(*)(void))_codecs_charmap_encode, METH_FASTCALL, _codecs_charmap_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002444
2445static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002446_codecs_charmap_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002447 const char *errors, PyObject *mapping);
2448
2449static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002450_codecs_charmap_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002451{
2452 PyObject *return_value = NULL;
2453 PyObject *str;
2454 const char *errors = NULL;
Serhiy Storchaka279f4462019-09-14 12:24:05 +03002455 PyObject *mapping = Py_None;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002456
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002457 if (!_PyArg_CheckPositional("charmap_encode", nargs, 1, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002458 goto exit;
2459 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002460 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002461 _PyArg_BadArgument("charmap_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002462 goto exit;
2463 }
2464 if (PyUnicode_READY(args[0]) == -1) {
2465 goto exit;
2466 }
2467 str = args[0];
2468 if (nargs < 2) {
2469 goto skip_optional;
2470 }
2471 if (args[1] == Py_None) {
2472 errors = NULL;
2473 }
2474 else if (PyUnicode_Check(args[1])) {
2475 Py_ssize_t errors_length;
2476 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
2477 if (errors == NULL) {
2478 goto exit;
2479 }
2480 if (strlen(errors) != (size_t)errors_length) {
2481 PyErr_SetString(PyExc_ValueError, "embedded null character");
2482 goto exit;
2483 }
2484 }
2485 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002486 _PyArg_BadArgument("charmap_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002487 goto exit;
2488 }
2489 if (nargs < 3) {
2490 goto skip_optional;
2491 }
2492 mapping = args[2];
2493skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002494 return_value = _codecs_charmap_encode_impl(module, str, errors, mapping);
2495
2496exit:
2497 return return_value;
2498}
2499
2500PyDoc_STRVAR(_codecs_charmap_build__doc__,
2501"charmap_build($module, map, /)\n"
2502"--\n"
2503"\n");
2504
2505#define _CODECS_CHARMAP_BUILD_METHODDEF \
2506 {"charmap_build", (PyCFunction)_codecs_charmap_build, METH_O, _codecs_charmap_build__doc__},
2507
2508static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002509_codecs_charmap_build_impl(PyObject *module, PyObject *map);
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002510
2511static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002512_codecs_charmap_build(PyObject *module, PyObject *arg)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002513{
2514 PyObject *return_value = NULL;
2515 PyObject *map;
2516
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02002517 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002518 _PyArg_BadArgument("charmap_build", "argument", "str", arg);
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002519 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002520 }
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02002521 if (PyUnicode_READY(arg) == -1) {
2522 goto exit;
2523 }
2524 map = arg;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002525 return_value = _codecs_charmap_build_impl(module, map);
2526
2527exit:
2528 return return_value;
2529}
2530
Steve Dowercc16be82016-09-08 10:35:16 -07002531#if defined(MS_WINDOWS)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002532
2533PyDoc_STRVAR(_codecs_mbcs_encode__doc__,
2534"mbcs_encode($module, str, errors=None, /)\n"
2535"--\n"
2536"\n");
2537
2538#define _CODECS_MBCS_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002539 {"mbcs_encode", (PyCFunction)(void(*)(void))_codecs_mbcs_encode, METH_FASTCALL, _codecs_mbcs_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002540
2541static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002542_codecs_mbcs_encode_impl(PyObject *module, PyObject *str, const char *errors);
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002543
2544static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002545_codecs_mbcs_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002546{
2547 PyObject *return_value = NULL;
2548 PyObject *str;
2549 const char *errors = NULL;
2550
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002551 if (!_PyArg_CheckPositional("mbcs_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002552 goto exit;
2553 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002554 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002555 _PyArg_BadArgument("mbcs_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002556 goto exit;
2557 }
2558 if (PyUnicode_READY(args[0]) == -1) {
2559 goto exit;
2560 }
2561 str = args[0];
2562 if (nargs < 2) {
2563 goto skip_optional;
2564 }
2565 if (args[1] == Py_None) {
2566 errors = NULL;
2567 }
2568 else if (PyUnicode_Check(args[1])) {
2569 Py_ssize_t errors_length;
2570 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
2571 if (errors == NULL) {
2572 goto exit;
2573 }
2574 if (strlen(errors) != (size_t)errors_length) {
2575 PyErr_SetString(PyExc_ValueError, "embedded null character");
2576 goto exit;
2577 }
2578 }
2579 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002580 _PyArg_BadArgument("mbcs_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002581 goto exit;
2582 }
2583skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002584 return_value = _codecs_mbcs_encode_impl(module, str, errors);
2585
2586exit:
2587 return return_value;
2588}
2589
Steve Dowercc16be82016-09-08 10:35:16 -07002590#endif /* defined(MS_WINDOWS) */
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002591
Steve Dowercc16be82016-09-08 10:35:16 -07002592#if defined(MS_WINDOWS)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002593
Steve Dowerf5aba582016-09-06 19:42:27 -07002594PyDoc_STRVAR(_codecs_oem_encode__doc__,
2595"oem_encode($module, str, errors=None, /)\n"
2596"--\n"
2597"\n");
2598
2599#define _CODECS_OEM_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002600 {"oem_encode", (PyCFunction)(void(*)(void))_codecs_oem_encode, METH_FASTCALL, _codecs_oem_encode__doc__},
Steve Dowerf5aba582016-09-06 19:42:27 -07002601
2602static PyObject *
2603_codecs_oem_encode_impl(PyObject *module, PyObject *str, const char *errors);
2604
2605static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002606_codecs_oem_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Steve Dowerf5aba582016-09-06 19:42:27 -07002607{
2608 PyObject *return_value = NULL;
2609 PyObject *str;
2610 const char *errors = NULL;
2611
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002612 if (!_PyArg_CheckPositional("oem_encode", nargs, 1, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002613 goto exit;
2614 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002615 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002616 _PyArg_BadArgument("oem_encode", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002617 goto exit;
2618 }
2619 if (PyUnicode_READY(args[0]) == -1) {
2620 goto exit;
2621 }
2622 str = args[0];
2623 if (nargs < 2) {
2624 goto skip_optional;
2625 }
2626 if (args[1] == Py_None) {
2627 errors = NULL;
2628 }
2629 else if (PyUnicode_Check(args[1])) {
2630 Py_ssize_t errors_length;
2631 errors = PyUnicode_AsUTF8AndSize(args[1], &errors_length);
2632 if (errors == NULL) {
2633 goto exit;
2634 }
2635 if (strlen(errors) != (size_t)errors_length) {
2636 PyErr_SetString(PyExc_ValueError, "embedded null character");
2637 goto exit;
2638 }
2639 }
2640 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002641 _PyArg_BadArgument("oem_encode", "argument 2", "str or None", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002642 goto exit;
2643 }
2644skip_optional:
Steve Dowerf5aba582016-09-06 19:42:27 -07002645 return_value = _codecs_oem_encode_impl(module, str, errors);
2646
2647exit:
2648 return return_value;
2649}
2650
Steve Dowercc16be82016-09-08 10:35:16 -07002651#endif /* defined(MS_WINDOWS) */
Steve Dowerf5aba582016-09-06 19:42:27 -07002652
Steve Dowercc16be82016-09-08 10:35:16 -07002653#if defined(MS_WINDOWS)
Steve Dowerf5aba582016-09-06 19:42:27 -07002654
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002655PyDoc_STRVAR(_codecs_code_page_encode__doc__,
2656"code_page_encode($module, code_page, str, errors=None, /)\n"
2657"--\n"
2658"\n");
2659
2660#define _CODECS_CODE_PAGE_ENCODE_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002661 {"code_page_encode", (PyCFunction)(void(*)(void))_codecs_code_page_encode, METH_FASTCALL, _codecs_code_page_encode__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002662
2663static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002664_codecs_code_page_encode_impl(PyObject *module, int code_page, PyObject *str,
2665 const char *errors);
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002666
2667static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002668_codecs_code_page_encode(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002669{
2670 PyObject *return_value = NULL;
2671 int code_page;
2672 PyObject *str;
2673 const char *errors = NULL;
2674
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002675 if (!_PyArg_CheckPositional("code_page_encode", nargs, 2, 3)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002676 goto exit;
2677 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002678 code_page = _PyLong_AsInt(args[0]);
2679 if (code_page == -1 && PyErr_Occurred()) {
2680 goto exit;
2681 }
2682 if (!PyUnicode_Check(args[1])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002683 _PyArg_BadArgument("code_page_encode", "argument 2", "str", args[1]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002684 goto exit;
2685 }
2686 if (PyUnicode_READY(args[1]) == -1) {
2687 goto exit;
2688 }
2689 str = args[1];
2690 if (nargs < 3) {
2691 goto skip_optional;
2692 }
2693 if (args[2] == Py_None) {
2694 errors = NULL;
2695 }
2696 else if (PyUnicode_Check(args[2])) {
2697 Py_ssize_t errors_length;
2698 errors = PyUnicode_AsUTF8AndSize(args[2], &errors_length);
2699 if (errors == NULL) {
2700 goto exit;
2701 }
2702 if (strlen(errors) != (size_t)errors_length) {
2703 PyErr_SetString(PyExc_ValueError, "embedded null character");
2704 goto exit;
2705 }
2706 }
2707 else {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002708 _PyArg_BadArgument("code_page_encode", "argument 3", "str or None", args[2]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002709 goto exit;
2710 }
2711skip_optional:
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002712 return_value = _codecs_code_page_encode_impl(module, code_page, str, errors);
2713
2714exit:
2715 return return_value;
2716}
2717
Steve Dowercc16be82016-09-08 10:35:16 -07002718#endif /* defined(MS_WINDOWS) */
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002719
2720PyDoc_STRVAR(_codecs_register_error__doc__,
2721"register_error($module, errors, handler, /)\n"
2722"--\n"
2723"\n"
2724"Register the specified error handler under the name errors.\n"
2725"\n"
2726"handler must be a callable object, that will be called with an exception\n"
2727"instance containing information about the location of the encoding/decoding\n"
2728"error and must return a (replacement, new position) tuple.");
2729
2730#define _CODECS_REGISTER_ERROR_METHODDEF \
Serhiy Storchaka4a934d42018-11-27 11:27:36 +02002731 {"register_error", (PyCFunction)(void(*)(void))_codecs_register_error, METH_FASTCALL, _codecs_register_error__doc__},
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002732
2733static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002734_codecs_register_error_impl(PyObject *module, const char *errors,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002735 PyObject *handler);
2736
2737static PyObject *
Serhiy Storchakaa5552f02017-12-15 13:11:11 +02002738_codecs_register_error(PyObject *module, PyObject *const *args, Py_ssize_t nargs)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002739{
2740 PyObject *return_value = NULL;
2741 const char *errors;
2742 PyObject *handler;
2743
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002744 if (!_PyArg_CheckPositional("register_error", nargs, 2, 2)) {
Victor Stinner259f0e42017-01-17 01:35:17 +01002745 goto exit;
2746 }
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002747 if (!PyUnicode_Check(args[0])) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002748 _PyArg_BadArgument("register_error", "argument 1", "str", args[0]);
Serhiy Storchaka4fa95912019-01-11 16:01:14 +02002749 goto exit;
2750 }
2751 Py_ssize_t errors_length;
2752 errors = PyUnicode_AsUTF8AndSize(args[0], &errors_length);
2753 if (errors == NULL) {
2754 goto exit;
2755 }
2756 if (strlen(errors) != (size_t)errors_length) {
2757 PyErr_SetString(PyExc_ValueError, "embedded null character");
2758 goto exit;
2759 }
2760 handler = args[1];
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002761 return_value = _codecs_register_error_impl(module, errors, handler);
2762
2763exit:
2764 return return_value;
2765}
2766
2767PyDoc_STRVAR(_codecs_lookup_error__doc__,
2768"lookup_error($module, name, /)\n"
2769"--\n"
2770"\n"
2771"lookup_error(errors) -> handler\n"
2772"\n"
2773"Return the error handler for the specified error handling name or raise a\n"
2774"LookupError, if no handler exists under this name.");
2775
2776#define _CODECS_LOOKUP_ERROR_METHODDEF \
2777 {"lookup_error", (PyCFunction)_codecs_lookup_error, METH_O, _codecs_lookup_error__doc__},
2778
2779static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002780_codecs_lookup_error_impl(PyObject *module, const char *name);
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002781
2782static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +03002783_codecs_lookup_error(PyObject *module, PyObject *arg)
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002784{
2785 PyObject *return_value = NULL;
2786 const char *name;
2787
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02002788 if (!PyUnicode_Check(arg)) {
Rémi Lapeyre4901fe22019-08-29 16:49:08 +02002789 _PyArg_BadArgument("lookup_error", "argument", "str", arg);
Serhiy Storchaka32d96a22018-12-25 13:23:47 +02002790 goto exit;
2791 }
2792 Py_ssize_t name_length;
2793 name = PyUnicode_AsUTF8AndSize(arg, &name_length);
2794 if (name == NULL) {
2795 goto exit;
2796 }
2797 if (strlen(name) != (size_t)name_length) {
2798 PyErr_SetString(PyExc_ValueError, "embedded null character");
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002799 goto exit;
Serhiy Storchaka5dee6552016-06-09 16:16:06 +03002800 }
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002801 return_value = _codecs_lookup_error_impl(module, name);
2802
2803exit:
2804 return return_value;
2805}
2806
2807#ifndef _CODECS_MBCS_DECODE_METHODDEF
2808 #define _CODECS_MBCS_DECODE_METHODDEF
2809#endif /* !defined(_CODECS_MBCS_DECODE_METHODDEF) */
2810
Steve Dowerf5aba582016-09-06 19:42:27 -07002811#ifndef _CODECS_OEM_DECODE_METHODDEF
2812 #define _CODECS_OEM_DECODE_METHODDEF
2813#endif /* !defined(_CODECS_OEM_DECODE_METHODDEF) */
2814
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002815#ifndef _CODECS_CODE_PAGE_DECODE_METHODDEF
2816 #define _CODECS_CODE_PAGE_DECODE_METHODDEF
2817#endif /* !defined(_CODECS_CODE_PAGE_DECODE_METHODDEF) */
2818
2819#ifndef _CODECS_MBCS_ENCODE_METHODDEF
2820 #define _CODECS_MBCS_ENCODE_METHODDEF
2821#endif /* !defined(_CODECS_MBCS_ENCODE_METHODDEF) */
2822
Steve Dowerf5aba582016-09-06 19:42:27 -07002823#ifndef _CODECS_OEM_ENCODE_METHODDEF
2824 #define _CODECS_OEM_ENCODE_METHODDEF
2825#endif /* !defined(_CODECS_OEM_ENCODE_METHODDEF) */
2826
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03002827#ifndef _CODECS_CODE_PAGE_ENCODE_METHODDEF
2828 #define _CODECS_CODE_PAGE_ENCODE_METHODDEF
2829#endif /* !defined(_CODECS_CODE_PAGE_ENCODE_METHODDEF) */
Serhiy Storchaka578c3952020-05-26 18:43:38 +03002830/*[clinic end generated code: output=eeead01414be6e42 input=a9049054013a1b77]*/