blob: 2e8cb97fe77c920f54f21003ae6119306f9791d5 [file] [log] [blame]
Guido van Rossume2d67f92000-03-10 23:09:23 +00001/* ------------------------------------------------------------------------
2
3 _codecs -- Provides access to the codec registry and the builtin
4 codecs.
5
6 This module should never be imported directly. The standard library
7 module "codecs" wraps this builtin module for use within Python.
8
9 The codec registry is accessible via:
10
11 register(search_function) -> None
12
Guido van Rossum36e0a922007-07-20 04:05:57 +000013 lookup(encoding) -> CodecInfo object
Guido van Rossume2d67f92000-03-10 23:09:23 +000014
15 The builtin Unicode codecs use the following interface:
16
Walter Dörwald9fd115c2005-11-02 08:30:08 +000017 <encoding>_encode(Unicode_object[,errors='strict']) ->
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000018 (string object, bytes consumed)
Guido van Rossume2d67f92000-03-10 23:09:23 +000019
Walter Dörwald9fd115c2005-11-02 08:30:08 +000020 <encoding>_decode(char_buffer_obj[,errors='strict']) ->
Guido van Rossume2d67f92000-03-10 23:09:23 +000021 (Unicode object, bytes consumed)
22
23 These <encoding>s are available: utf_8, unicode_escape,
Inada Naoki6a16b182019-03-18 15:44:11 +090024 raw_unicode_escape, latin_1, ascii (7-bit), mbcs (on win32).
Marc-André Lemburg5f0e29e2000-07-05 11:24:13 +000025
Guido van Rossume2d67f92000-03-10 23:09:23 +000026
27Written by Marc-Andre Lemburg (mal@lemburg.com).
28
Guido van Rossum16b1ad92000-08-03 16:24:25 +000029Copyright (c) Corporation for National Research Initiatives.
Guido van Rossume2d67f92000-03-10 23:09:23 +000030
31 ------------------------------------------------------------------------ */
32
Martin v. Löwis18e16552006-02-15 17:27:45 +000033#define PY_SSIZE_T_CLEAN
Guido van Rossume2d67f92000-03-10 23:09:23 +000034#include "Python.h"
35
Victor Stinner87a7c822011-11-10 20:05:55 +010036#ifdef MS_WINDOWS
37#include <windows.h>
38#endif
39
Nick Coghlan8fad1672014-09-15 23:50:44 +120040/*[clinic input]
41module _codecs
42[clinic start generated code]*/
43/*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/
44
Serhiy Storchaka1009bf12015-04-03 23:53:51 +030045#include "clinic/_codecsmodule.c.h"
Nick Coghlan8fad1672014-09-15 23:50:44 +120046
Guido van Rossume2d67f92000-03-10 23:09:23 +000047/* --- Registry ----------------------------------------------------------- */
48
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030049/*[clinic input]
50_codecs.register
51 search_function: object
52 /
Walter Dörwald0ae29812002-10-31 13:36:29 +000053
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030054Register a codec search function.
55
56Search functions are expected to take one argument, the encoding name in
57all lower case letters, and either return None, or a tuple of functions
58(encoder, decoder, stream_reader, stream_writer) (or a CodecInfo object).
59[clinic start generated code]*/
60
61static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +030062_codecs_register(PyObject *module, PyObject *search_function)
63/*[clinic end generated code: output=d1bf21e99db7d6d3 input=369578467955cae4]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +000064{
Guido van Rossume2d67f92000-03-10 23:09:23 +000065 if (PyCodec_Register(search_function))
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000066 return NULL;
Walter Dörwald9fd115c2005-11-02 08:30:08 +000067
Thomas Wouters4d70c3d2006-06-08 14:42:34 +000068 Py_RETURN_NONE;
Guido van Rossume2d67f92000-03-10 23:09:23 +000069}
70
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030071/*[clinic input]
Hai Shid332e7b2020-09-29 05:41:11 +080072_codecs.unregister
73 search_function: object
74 /
75
76Unregister a codec search function and clear the registry's cache.
77
78If the search function is not registered, do nothing.
79[clinic start generated code]*/
80
81static PyObject *
82_codecs_unregister(PyObject *module, PyObject *search_function)
83/*[clinic end generated code: output=1f0edee9cf246399 input=dd7c004c652d345e]*/
84{
85 if (PyCodec_Unregister(search_function) < 0) {
86 return NULL;
87 }
88
89 Py_RETURN_NONE;
90}
91
92/*[clinic input]
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030093_codecs.lookup
94 encoding: str
95 /
Walter Dörwald0ae29812002-10-31 13:36:29 +000096
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +030097Looks up a codec tuple in the Python codec registry and returns a CodecInfo object.
98[clinic start generated code]*/
99
100static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300101_codecs_lookup_impl(PyObject *module, const char *encoding)
102/*[clinic end generated code: output=9f0afa572080c36d input=3c572c0db3febe9c]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000103{
Guido van Rossume2d67f92000-03-10 23:09:23 +0000104 return _PyCodec_Lookup(encoding);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000105}
106
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300107/*[clinic input]
108_codecs.encode
109 obj: object
Serhiy Storchakac97a9622015-08-09 12:23:08 +0300110 encoding: str(c_default="NULL") = "utf-8"
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300111 errors: str(c_default="NULL") = "strict"
112
113Encodes obj using the codec registered for encoding.
114
Serhiy Storchakac97a9622015-08-09 12:23:08 +0300115The default encoding is 'utf-8'. errors may be given to set a
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300116different error handling scheme. Default is 'strict' meaning that encoding
117errors raise a ValueError. Other possible values are 'ignore', 'replace'
118and 'backslashreplace' as well as any other name registered with
119codecs.register_error that can handle ValueErrors.
120[clinic start generated code]*/
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000121
122static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300123_codecs_encode_impl(PyObject *module, PyObject *obj, const char *encoding,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300124 const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300125/*[clinic end generated code: output=385148eb9a067c86 input=cd5b685040ff61f0]*/
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000126{
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000127 if (encoding == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000128 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000129
130 /* Encode via the codec registry */
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300131 return PyCodec_Encode(obj, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000132}
133
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300134/*[clinic input]
135_codecs.decode
136 obj: object
Serhiy Storchakac97a9622015-08-09 12:23:08 +0300137 encoding: str(c_default="NULL") = "utf-8"
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300138 errors: str(c_default="NULL") = "strict"
139
140Decodes obj using the codec registered for encoding.
141
Serhiy Storchakac97a9622015-08-09 12:23:08 +0300142Default encoding is 'utf-8'. errors may be given to set a
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300143different error handling scheme. Default is 'strict' meaning that encoding
144errors raise a ValueError. Other possible values are 'ignore', 'replace'
145and 'backslashreplace' as well as any other name registered with
146codecs.register_error that can handle ValueErrors.
147[clinic start generated code]*/
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000148
149static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300150_codecs_decode_impl(PyObject *module, PyObject *obj, const char *encoding,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300151 const char *errors)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300152/*[clinic end generated code: output=679882417dc3a0bd input=7702c0cc2fa1add6]*/
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000153{
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000154 if (encoding == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000155 encoding = PyUnicode_GetDefaultEncoding();
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000156
157 /* Decode via the codec registry */
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300158 return PyCodec_Decode(obj, encoding, errors);
Marc-André Lemburg3f419742004-07-10 12:06:10 +0000159}
160
Guido van Rossume2d67f92000-03-10 23:09:23 +0000161/* --- Helpers ------------------------------------------------------------ */
162
163static
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300164PyObject *codec_tuple(PyObject *decoded,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000165 Py_ssize_t len)
Guido van Rossume2d67f92000-03-10 23:09:23 +0000166{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300167 if (decoded == NULL)
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000168 return NULL;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300169 return Py_BuildValue("Nn", decoded, len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000170}
171
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000172/* --- String codecs ------------------------------------------------------ */
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300173/*[clinic input]
174_codecs.escape_decode
175 data: Py_buffer(accept={str, buffer})
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300176 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300177 /
178[clinic start generated code]*/
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000179
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300180static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300181_codecs_escape_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300182 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300183/*[clinic end generated code: output=505200ba8056979a input=77298a561c90bd82]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300184{
185 PyObject *decoded = PyBytes_DecodeEscape(data->buf, data->len,
186 errors, 0, NULL);
187 return codec_tuple(decoded, data->len);
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000188}
189
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300190/*[clinic input]
191_codecs.escape_encode
192 data: object(subclass_of='&PyBytes_Type')
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300193 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300194 /
195[clinic start generated code]*/
196
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000197static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300198_codecs_escape_encode_impl(PyObject *module, PyObject *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300199 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300200/*[clinic end generated code: output=4af1d477834bab34 input=8f4b144799a94245]*/
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000201{
Antoine Pitroud1188562010-06-09 16:38:55 +0000202 Py_ssize_t size;
203 Py_ssize_t newsize;
Antoine Pitroud1188562010-06-09 16:38:55 +0000204 PyObject *v;
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000205
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300206 size = PyBytes_GET_SIZE(data);
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100207 if (size > PY_SSIZE_T_MAX / 4) {
Antoine Pitroud1188562010-06-09 16:38:55 +0000208 PyErr_SetString(PyExc_OverflowError,
209 "string is too large to encode");
210 return NULL;
211 }
Mark Dickinsonc04ddff2012-10-06 18:04:49 +0100212 newsize = 4*size;
Antoine Pitroud1188562010-06-09 16:38:55 +0000213 v = PyBytes_FromStringAndSize(NULL, newsize);
214
215 if (v == NULL) {
216 return NULL;
217 }
218 else {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +0200219 Py_ssize_t i;
220 char c;
221 char *p = PyBytes_AS_STRING(v);
Antoine Pitroud1188562010-06-09 16:38:55 +0000222
223 for (i = 0; i < size; i++) {
224 /* There's at least enough room for a hex escape */
225 assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300226 c = PyBytes_AS_STRING(data)[i];
Antoine Pitroud1188562010-06-09 16:38:55 +0000227 if (c == '\'' || c == '\\')
228 *p++ = '\\', *p++ = c;
229 else if (c == '\t')
230 *p++ = '\\', *p++ = 't';
231 else if (c == '\n')
232 *p++ = '\\', *p++ = 'n';
233 else if (c == '\r')
234 *p++ = '\\', *p++ = 'r';
235 else if (c < ' ' || c >= 0x7f) {
236 *p++ = '\\';
237 *p++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200238 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
239 *p++ = Py_hexdigits[c & 0xf];
Antoine Pitroud1188562010-06-09 16:38:55 +0000240 }
241 else
242 *p++ = c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 }
Antoine Pitroud1188562010-06-09 16:38:55 +0000244 *p = '\0';
245 if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
246 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000247 }
Antoine Pitroud1188562010-06-09 16:38:55 +0000248 }
Walter Dörwald1ab83302007-05-18 17:15:44 +0000249
Antoine Pitroud1188562010-06-09 16:38:55 +0000250 return codec_tuple(v, size);
Martin v. Löwis8a8da792002-08-14 07:46:28 +0000251}
252
Guido van Rossume2d67f92000-03-10 23:09:23 +0000253/* --- Decoder ------------------------------------------------------------ */
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300254/*[clinic input]
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300255_codecs.utf_7_decode
256 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300257 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200258 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300259 /
260[clinic start generated code]*/
261
Guido van Rossume2d67f92000-03-10 23:09:23 +0000262static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300263_codecs_utf_7_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300264 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300265/*[clinic end generated code: output=0cd3a944a32a4089 input=22c395d357815d26]*/
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000266{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300267 Py_ssize_t consumed = data->len;
268 PyObject *decoded = PyUnicode_DecodeUTF7Stateful(data->buf, data->len,
269 errors,
270 final ? NULL : &consumed);
Christian Heimes5d14c2b2007-11-20 23:38:09 +0000271 return codec_tuple(decoded, consumed);
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000272}
273
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300274/*[clinic input]
275_codecs.utf_8_decode
276 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300277 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200278 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300279 /
280[clinic start generated code]*/
281
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000282static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300283_codecs_utf_8_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300284 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300285/*[clinic end generated code: output=10f74dec8d9bb8bf input=f611b3867352ba59]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000286{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300287 Py_ssize_t consumed = data->len;
288 PyObject *decoded = PyUnicode_DecodeUTF8Stateful(data->buf, data->len,
289 errors,
290 final ? NULL : &consumed);
Walter Dörwald69652032004-09-07 20:24:22 +0000291 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000292}
293
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300294/*[clinic input]
295_codecs.utf_16_decode
296 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300297 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200298 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300299 /
300[clinic start generated code]*/
301
Guido van Rossume2d67f92000-03-10 23:09:23 +0000302static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300303_codecs_utf_16_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300304 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300305/*[clinic end generated code: output=783b442abcbcc2d0 input=191d360bd7309180]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000306{
Guido van Rossume2d67f92000-03-10 23:09:23 +0000307 int byteorder = 0;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300308 /* This is overwritten unless final is true. */
309 Py_ssize_t consumed = data->len;
310 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
311 errors, &byteorder,
312 final ? NULL : &consumed);
Walter Dörwald69652032004-09-07 20:24:22 +0000313 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000314}
315
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300316/*[clinic input]
317_codecs.utf_16_le_decode
318 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300319 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200320 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300321 /
322[clinic start generated code]*/
323
Guido van Rossume2d67f92000-03-10 23:09:23 +0000324static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300325_codecs_utf_16_le_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300326 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300327/*[clinic end generated code: output=899b9e6364379dcd input=c6904fdc27fb4724]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000328{
Guido van Rossume2d67f92000-03-10 23:09:23 +0000329 int byteorder = -1;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300330 /* This is overwritten unless final is true. */
331 Py_ssize_t consumed = data->len;
332 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
333 errors, &byteorder,
334 final ? NULL : &consumed);
Walter Dörwald69652032004-09-07 20:24:22 +0000335 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000336}
337
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300338/*[clinic input]
339_codecs.utf_16_be_decode
340 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300341 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200342 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300343 /
344[clinic start generated code]*/
345
Guido van Rossume2d67f92000-03-10 23:09:23 +0000346static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300347_codecs_utf_16_be_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300348 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300349/*[clinic end generated code: output=49f6465ea07669c8 input=e49012400974649b]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000350{
Guido van Rossume2d67f92000-03-10 23:09:23 +0000351 int byteorder = 1;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300352 /* This is overwritten unless final is true. */
353 Py_ssize_t consumed = data->len;
354 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
355 errors, &byteorder,
356 final ? NULL : &consumed);
Walter Dörwald69652032004-09-07 20:24:22 +0000357 return codec_tuple(decoded, consumed);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000358}
359
360/* This non-standard version also provides access to the byteorder
361 parameter of the builtin UTF-16 codec.
362
363 It returns a tuple (unicode, bytesread, byteorder) with byteorder
364 being the value in effect at the end of data.
365
366*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300367/*[clinic input]
368_codecs.utf_16_ex_decode
369 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300370 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300371 byteorder: int = 0
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200372 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300373 /
374[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000375
376static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300377_codecs_utf_16_ex_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300378 const char *errors, int byteorder, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300379/*[clinic end generated code: output=0f385f251ecc1988 input=5a9c19f2e6b6cf0e]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000380{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300381 /* This is overwritten unless final is true. */
382 Py_ssize_t consumed = data->len;
Walter Dörwald69652032004-09-07 20:24:22 +0000383
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300384 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
385 errors, &byteorder,
386 final ? NULL : &consumed);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000387 if (decoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 return NULL;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300389 return Py_BuildValue("Nni", decoded, consumed, byteorder);
390}
391
392/*[clinic input]
393_codecs.utf_32_decode
394 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300395 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200396 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300397 /
398[clinic start generated code]*/
399
400static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300401_codecs_utf_32_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300402 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300403/*[clinic end generated code: output=2fc961807f7b145f input=fd7193965627eb58]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300404{
405 int byteorder = 0;
406 /* This is overwritten unless final is true. */
407 Py_ssize_t consumed = data->len;
408 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
409 errors, &byteorder,
410 final ? NULL : &consumed);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000411 return codec_tuple(decoded, consumed);
412}
413
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300414/*[clinic input]
415_codecs.utf_32_le_decode
416 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300417 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200418 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300419 /
420[clinic start generated code]*/
421
Walter Dörwald41980ca2007-08-16 21:55:45 +0000422static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300423_codecs_utf_32_le_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300424 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300425/*[clinic end generated code: output=ec8f46b67a94f3e6 input=9078ec70acfe7613]*/
Walter Dörwald41980ca2007-08-16 21:55:45 +0000426{
Walter Dörwald41980ca2007-08-16 21:55:45 +0000427 int byteorder = -1;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300428 /* This is overwritten unless final is true. */
429 Py_ssize_t consumed = data->len;
430 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
431 errors, &byteorder,
432 final ? NULL : &consumed);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000433 return codec_tuple(decoded, consumed);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000434}
435
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300436/*[clinic input]
437_codecs.utf_32_be_decode
438 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300439 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200440 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300441 /
442[clinic start generated code]*/
Walter Dörwald41980ca2007-08-16 21:55:45 +0000443
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300444static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300445_codecs_utf_32_be_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300446 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300447/*[clinic end generated code: output=ff82bae862c92c4e input=f1ae1bbbb86648ff]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300448{
449 int byteorder = 1;
450 /* This is overwritten unless final is true. */
451 Py_ssize_t consumed = data->len;
452 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
453 errors, &byteorder,
454 final ? NULL : &consumed);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000455 return codec_tuple(decoded, consumed);
456}
457
458/* This non-standard version also provides access to the byteorder
459 parameter of the builtin UTF-32 codec.
460
461 It returns a tuple (unicode, bytesread, byteorder) with byteorder
462 being the value in effect at the end of data.
463
464*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300465/*[clinic input]
466_codecs.utf_32_ex_decode
467 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300468 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300469 byteorder: int = 0
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200470 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300471 /
472[clinic start generated code]*/
Walter Dörwald41980ca2007-08-16 21:55:45 +0000473
474static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300475_codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300476 const char *errors, int byteorder, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300477/*[clinic end generated code: output=6bfb177dceaf4848 input=e46a73bc859d0bd0]*/
Walter Dörwald41980ca2007-08-16 21:55:45 +0000478{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300479 Py_ssize_t consumed = data->len;
480 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
481 errors, &byteorder,
482 final ? NULL : &consumed);
483 if (decoded == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000484 return NULL;
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300485 return Py_BuildValue("Nni", decoded, consumed, byteorder);
Walter Dörwald41980ca2007-08-16 21:55:45 +0000486}
487
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300488/*[clinic input]
489_codecs.unicode_escape_decode
490 data: Py_buffer(accept={str, buffer})
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300491 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300492 /
493[clinic start generated code]*/
494
Walter Dörwald41980ca2007-08-16 21:55:45 +0000495static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300496_codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300497 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300498/*[clinic end generated code: output=3ca3c917176b82ab input=8328081a3a569bd6]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000499{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300500 PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len,
501 errors);
502 return codec_tuple(decoded, data->len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000503}
504
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300505/*[clinic input]
506_codecs.raw_unicode_escape_decode
507 data: Py_buffer(accept={str, buffer})
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300508 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300509 /
510[clinic start generated code]*/
511
Guido van Rossume2d67f92000-03-10 23:09:23 +0000512static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300513_codecs_raw_unicode_escape_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300514 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300515/*[clinic end generated code: output=c98eeb56028070a6 input=d2f5159ce3b3392f]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000516{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300517 PyObject *decoded = PyUnicode_DecodeRawUnicodeEscape(data->buf, data->len,
518 errors);
519 return codec_tuple(decoded, data->len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000520}
521
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300522/*[clinic input]
523_codecs.latin_1_decode
524 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300525 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300526 /
527[clinic start generated code]*/
528
Guido van Rossume2d67f92000-03-10 23:09:23 +0000529static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300530_codecs_latin_1_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300531 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300532/*[clinic end generated code: output=07f3dfa3f72c7d8f input=76ca58fd6dcd08c7]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000533{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300534 PyObject *decoded = PyUnicode_DecodeLatin1(data->buf, data->len, errors);
535 return codec_tuple(decoded, data->len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000536}
537
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300538/*[clinic input]
539_codecs.ascii_decode
540 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300541 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300542 /
543[clinic start generated code]*/
544
Guido van Rossume2d67f92000-03-10 23:09:23 +0000545static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300546_codecs_ascii_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300547 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300548/*[clinic end generated code: output=2627d72058d42429 input=e428a267a04b4481]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000549{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300550 PyObject *decoded = PyUnicode_DecodeASCII(data->buf, data->len, errors);
551 return codec_tuple(decoded, data->len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000552}
553
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300554/*[clinic input]
555_codecs.charmap_decode
556 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300557 errors: str(accept={str, NoneType}) = None
558 mapping: object = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300559 /
560[clinic start generated code]*/
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000561
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300562static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300563_codecs_charmap_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300564 const char *errors, PyObject *mapping)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300565/*[clinic end generated code: output=2c335b09778cf895 input=15b69df43458eb40]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300566{
567 PyObject *decoded;
568
Guido van Rossume2d67f92000-03-10 23:09:23 +0000569 if (mapping == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 mapping = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000571
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300572 decoded = PyUnicode_DecodeCharmap(data->buf, data->len, mapping, errors);
573 return codec_tuple(decoded, data->len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000574}
575
Steve Dowercc16be82016-09-08 10:35:16 -0700576#ifdef MS_WINDOWS
Guido van Rossum24bdb042000-03-28 20:29:59 +0000577
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300578/*[clinic input]
579_codecs.mbcs_decode
580 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300581 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200582 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300583 /
584[clinic start generated code]*/
585
Guido van Rossum24bdb042000-03-28 20:29:59 +0000586static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300587_codecs_mbcs_decode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300588 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300589/*[clinic end generated code: output=39b65b8598938c4b input=1c1d50f08fa53789]*/
Guido van Rossum24bdb042000-03-28 20:29:59 +0000590{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300591 Py_ssize_t consumed = data->len;
592 PyObject *decoded = PyUnicode_DecodeMBCSStateful(data->buf, data->len,
593 errors, final ? NULL : &consumed);
Martin v. Löwis423be952008-08-13 15:53:07 +0000594 return codec_tuple(decoded, consumed);
Guido van Rossum24bdb042000-03-28 20:29:59 +0000595}
596
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300597/*[clinic input]
Steve Dowerf5aba582016-09-06 19:42:27 -0700598_codecs.oem_decode
599 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300600 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200601 final: bool(accept={int}) = False
Steve Dowerf5aba582016-09-06 19:42:27 -0700602 /
603[clinic start generated code]*/
604
605static PyObject *
606_codecs_oem_decode_impl(PyObject *module, Py_buffer *data,
607 const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300608/*[clinic end generated code: output=da1617612f3fcad8 input=81b67cba811022e5]*/
Steve Dowerf5aba582016-09-06 19:42:27 -0700609{
610 Py_ssize_t consumed = data->len;
611 PyObject *decoded = PyUnicode_DecodeCodePageStateful(CP_OEMCP,
612 data->buf, data->len, errors, final ? NULL : &consumed);
613 return codec_tuple(decoded, consumed);
614}
615
616/*[clinic input]
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300617_codecs.code_page_decode
618 codepage: int
619 data: Py_buffer
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300620 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka202fda52017-03-12 10:10:47 +0200621 final: bool(accept={int}) = False
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300622 /
623[clinic start generated code]*/
624
Victor Stinner3a50e702011-10-18 21:21:00 +0200625static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300626_codecs_code_page_decode_impl(PyObject *module, int codepage,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300627 Py_buffer *data, const char *errors, int final)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300628/*[clinic end generated code: output=53008ea967da3fff input=c5f58d036cb63575]*/
Victor Stinner3a50e702011-10-18 21:21:00 +0200629{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300630 Py_ssize_t consumed = data->len;
Serhiy Storchaka1aa5e1d2015-05-12 14:00:22 +0300631 PyObject *decoded = PyUnicode_DecodeCodePageStateful(codepage,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300632 data->buf, data->len,
633 errors,
634 final ? NULL : &consumed);
Victor Stinner3a50e702011-10-18 21:21:00 +0200635 return codec_tuple(decoded, consumed);
636}
637
Steve Dowercc16be82016-09-08 10:35:16 -0700638#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000639
Guido van Rossume2d67f92000-03-10 23:09:23 +0000640/* --- Encoder ------------------------------------------------------------ */
641
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300642/*[clinic input]
643_codecs.readbuffer_encode
644 data: Py_buffer(accept={str, buffer})
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300645 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300646 /
647[clinic start generated code]*/
648
Guido van Rossume2d67f92000-03-10 23:09:23 +0000649static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300650_codecs_readbuffer_encode_impl(PyObject *module, Py_buffer *data,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300651 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300652/*[clinic end generated code: output=c645ea7cdb3d6e86 input=aa10cfdf252455c5]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000653{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300654 PyObject *result = PyBytes_FromStringAndSize(data->buf, data->len);
655 return codec_tuple(result, data->len);
Guido van Rossume2d67f92000-03-10 23:09:23 +0000656}
657
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300658/*[clinic input]
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300659_codecs.utf_7_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300660 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300661 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300662 /
663[clinic start generated code]*/
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000664
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300665static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300666_codecs_utf_7_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300667 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300668/*[clinic end generated code: output=0feda21ffc921bc8 input=2546dbbb3fa53114]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300669{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300670 return codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
671 PyUnicode_GET_LENGTH(str));
Marc-André Lemburgc60e6f72001-09-20 10:35:46 +0000672}
673
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300674/*[clinic input]
675_codecs.utf_8_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300676 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300677 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300678 /
679[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000680
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300681static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300682_codecs_utf_8_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300683 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300684/*[clinic end generated code: output=02bf47332b9c796c input=a3e71ae01c3f93f3]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300685{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300686 return codec_tuple(_PyUnicode_AsUTF8String(str, errors),
687 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000688}
689
690/* This version provides access to the byteorder parameter of the
691 builtin UTF-16 codecs as optional third argument. It defaults to 0
692 which means: use the native byte order and prepend the data with a
Walter Dörwald9fd115c2005-11-02 08:30:08 +0000693 BOM mark.
Guido van Rossume2d67f92000-03-10 23:09:23 +0000694
695*/
696
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300697/*[clinic input]
698_codecs.utf_16_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300699 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300700 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300701 byteorder: int = 0
702 /
703[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000704
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300705static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300706_codecs_utf_16_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300707 const char *errors, int byteorder)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300708/*[clinic end generated code: output=c654e13efa2e64e4 input=68cdc2eb8338555d]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300709{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300710 return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
711 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000712}
713
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300714/*[clinic input]
715_codecs.utf_16_le_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300716 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300717 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300718 /
719[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000720
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300721static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300722_codecs_utf_16_le_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300723 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300724/*[clinic end generated code: output=431b01e55f2d4995 input=83d042706eed6798]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300725{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300726 return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
727 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000728}
729
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300730/*[clinic input]
731_codecs.utf_16_be_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300732 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300733 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300734 /
735[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000736
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300737static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300738_codecs_utf_16_be_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300739 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300740/*[clinic end generated code: output=96886a6fd54dcae3 input=6f1e9e623b03071b]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300741{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300742 return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
743 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000744}
745
Walter Dörwald41980ca2007-08-16 21:55:45 +0000746/* This version provides access to the byteorder parameter of the
747 builtin UTF-32 codecs as optional third argument. It defaults to 0
748 which means: use the native byte order and prepend the data with a
749 BOM mark.
750
751*/
752
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300753/*[clinic input]
754_codecs.utf_32_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300755 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300756 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300757 byteorder: int = 0
758 /
759[clinic start generated code]*/
Walter Dörwald41980ca2007-08-16 21:55:45 +0000760
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300761static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300762_codecs_utf_32_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300763 const char *errors, int byteorder)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300764/*[clinic end generated code: output=5c760da0c09a8b83 input=8ec4c64d983bc52b]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300765{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300766 return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
767 PyUnicode_GET_LENGTH(str));
Walter Dörwald41980ca2007-08-16 21:55:45 +0000768}
769
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300770/*[clinic input]
771_codecs.utf_32_le_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300772 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300773 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300774 /
775[clinic start generated code]*/
Walter Dörwald41980ca2007-08-16 21:55:45 +0000776
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300777static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300778_codecs_utf_32_le_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300779 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300780/*[clinic end generated code: output=b65cd176de8e36d6 input=f0918d41de3eb1b1]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300781{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300782 return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
783 PyUnicode_GET_LENGTH(str));
Walter Dörwald41980ca2007-08-16 21:55:45 +0000784}
785
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300786/*[clinic input]
787_codecs.utf_32_be_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300788 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300789 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300790 /
791[clinic start generated code]*/
Walter Dörwald41980ca2007-08-16 21:55:45 +0000792
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300793static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300794_codecs_utf_32_be_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300795 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300796/*[clinic end generated code: output=1d9e71a9358709e9 input=967a99a95748b557]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300797{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300798 return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
799 PyUnicode_GET_LENGTH(str));
Walter Dörwald41980ca2007-08-16 21:55:45 +0000800}
801
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300802/*[clinic input]
803_codecs.unicode_escape_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300804 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300805 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300806 /
807[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000808
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300809static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300810_codecs_unicode_escape_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300811 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300812/*[clinic end generated code: output=66271b30bc4f7a3c input=8c4de07597054e33]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300813{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300814 return codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
815 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000816}
817
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300818/*[clinic input]
819_codecs.raw_unicode_escape_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300820 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300821 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300822 /
823[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000824
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300825static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300826_codecs_raw_unicode_escape_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300827 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300828/*[clinic end generated code: output=a66a806ed01c830a input=4aa6f280d78e4574]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300829{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300830 return codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
831 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000832}
833
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300834/*[clinic input]
835_codecs.latin_1_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300836 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300837 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300838 /
839[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000840
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300841static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300842_codecs_latin_1_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300843 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300844/*[clinic end generated code: output=2c28c83a27884e08 input=ec3ef74bf85c5c5d]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300845{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300846 return codec_tuple(_PyUnicode_AsLatin1String(str, errors),
847 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000848}
849
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300850/*[clinic input]
851_codecs.ascii_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300852 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300853 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300854 /
855[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000856
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300857static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300858_codecs_ascii_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300859 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300860/*[clinic end generated code: output=b5e035182d33befc input=93e6e602838bd3de]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300861{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300862 return codec_tuple(_PyUnicode_AsASCIIString(str, errors),
863 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000864}
865
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300866/*[clinic input]
867_codecs.charmap_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300868 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300869 errors: str(accept={str, NoneType}) = None
870 mapping: object = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300871 /
872[clinic start generated code]*/
Guido van Rossume2d67f92000-03-10 23:09:23 +0000873
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300874static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300875_codecs_charmap_encode_impl(PyObject *module, PyObject *str,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300876 const char *errors, PyObject *mapping)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300877/*[clinic end generated code: output=047476f48495a9e9 input=2a98feae73dadce8]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300878{
Guido van Rossume2d67f92000-03-10 23:09:23 +0000879 if (mapping == Py_None)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 mapping = NULL;
Guido van Rossume2d67f92000-03-10 23:09:23 +0000881
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300882 return codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
883 PyUnicode_GET_LENGTH(str));
Guido van Rossume2d67f92000-03-10 23:09:23 +0000884}
885
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300886/*[clinic input]
887_codecs.charmap_build
888 map: unicode
889 /
890[clinic start generated code]*/
891
892static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300893_codecs_charmap_build_impl(PyObject *module, PyObject *map)
894/*[clinic end generated code: output=bb073c27031db9ac input=d91a91d1717dbc6d]*/
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000895{
Thomas Wouters73e5a5b2006-06-08 15:35:45 +0000896 return PyUnicode_BuildEncodingMap(map);
897}
898
Steve Dowercc16be82016-09-08 10:35:16 -0700899#ifdef MS_WINDOWS
Guido van Rossum24bdb042000-03-28 20:29:59 +0000900
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300901/*[clinic input]
902_codecs.mbcs_encode
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300903 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300904 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300905 /
906[clinic start generated code]*/
Guido van Rossum24bdb042000-03-28 20:29:59 +0000907
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300908static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300909_codecs_mbcs_encode_impl(PyObject *module, PyObject *str, const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300910/*[clinic end generated code: output=76e2e170c966c080 input=2e932fc289ea5a5b]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300911{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300912 return codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
913 PyUnicode_GET_LENGTH(str));
Guido van Rossum24bdb042000-03-28 20:29:59 +0000914}
915
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300916/*[clinic input]
Steve Dowerf5aba582016-09-06 19:42:27 -0700917_codecs.oem_encode
918 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300919 errors: str(accept={str, NoneType}) = None
Steve Dowerf5aba582016-09-06 19:42:27 -0700920 /
921[clinic start generated code]*/
922
923static PyObject *
924_codecs_oem_encode_impl(PyObject *module, PyObject *str, const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300925/*[clinic end generated code: output=65d5982c737de649 input=9eac86dc21eb14f2]*/
Steve Dowerf5aba582016-09-06 19:42:27 -0700926{
927 return codec_tuple(PyUnicode_EncodeCodePage(CP_OEMCP, str, errors),
928 PyUnicode_GET_LENGTH(str));
929}
930
931/*[clinic input]
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300932_codecs.code_page_encode
933 code_page: int
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300934 str: unicode
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300935 errors: str(accept={str, NoneType}) = None
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300936 /
937[clinic start generated code]*/
Victor Stinner3a50e702011-10-18 21:21:00 +0200938
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300939static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300940_codecs_code_page_encode_impl(PyObject *module, int code_page, PyObject *str,
941 const char *errors)
Serhiy Storchaka279f4462019-09-14 12:24:05 +0300942/*[clinic end generated code: output=45673f6085657a9e input=7d18a33bc8cd0f94]*/
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300943{
Serhiy Storchaka21a663e2016-04-13 15:37:23 +0300944 return codec_tuple(PyUnicode_EncodeCodePage(code_page, str, errors),
945 PyUnicode_GET_LENGTH(str));
Victor Stinner3a50e702011-10-18 21:21:00 +0200946}
947
Steve Dowercc16be82016-09-08 10:35:16 -0700948#endif /* MS_WINDOWS */
Guido van Rossum24bdb042000-03-28 20:29:59 +0000949
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000950/* --- Error handler registry --------------------------------------------- */
951
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300952/*[clinic input]
953_codecs.register_error
954 errors: str
955 handler: object
956 /
Walter Dörwald0ae29812002-10-31 13:36:29 +0000957
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300958Register the specified error handler under the name errors.
959
960handler must be a callable object, that will be called with an exception
961instance containing information about the location of the encoding/decoding
962error and must return a (replacement, new position) tuple.
963[clinic start generated code]*/
964
965static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300966_codecs_register_error_impl(PyObject *module, const char *errors,
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300967 PyObject *handler)
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300968/*[clinic end generated code: output=fa2f7d1879b3067d input=5e6709203c2e33fe]*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000969{
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300970 if (PyCodec_RegisterError(errors, handler))
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000971 return NULL;
Thomas Wouters4d70c3d2006-06-08 14:42:34 +0000972 Py_RETURN_NONE;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000973}
974
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300975/*[clinic input]
976_codecs.lookup_error
977 name: str
978 /
Walter Dörwald0ae29812002-10-31 13:36:29 +0000979
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300980lookup_error(errors) -> handler
981
982Return the error handler for the specified error handling name or raise a
983LookupError, if no handler exists under this name.
984[clinic start generated code]*/
985
986static PyObject *
Serhiy Storchaka1a2b24f2016-07-07 17:35:15 +0300987_codecs_lookup_error_impl(PyObject *module, const char *name)
988/*[clinic end generated code: output=087f05dc0c9a98cc input=4775dd65e6235aba]*/
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000989{
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000990 return PyCodec_LookupError(name);
991}
992
Guido van Rossume2d67f92000-03-10 23:09:23 +0000993/* --- Module API --------------------------------------------------------- */
994
995static PyMethodDef _codecs_functions[] = {
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300996 _CODECS_REGISTER_METHODDEF
Hai Shid332e7b2020-09-29 05:41:11 +0800997 _CODECS_UNREGISTER_METHODDEF
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +0300998 _CODECS_LOOKUP_METHODDEF
999 _CODECS_ENCODE_METHODDEF
1000 _CODECS_DECODE_METHODDEF
1001 _CODECS_ESCAPE_ENCODE_METHODDEF
1002 _CODECS_ESCAPE_DECODE_METHODDEF
1003 _CODECS_UTF_8_ENCODE_METHODDEF
1004 _CODECS_UTF_8_DECODE_METHODDEF
1005 _CODECS_UTF_7_ENCODE_METHODDEF
1006 _CODECS_UTF_7_DECODE_METHODDEF
1007 _CODECS_UTF_16_ENCODE_METHODDEF
1008 _CODECS_UTF_16_LE_ENCODE_METHODDEF
1009 _CODECS_UTF_16_BE_ENCODE_METHODDEF
1010 _CODECS_UTF_16_DECODE_METHODDEF
1011 _CODECS_UTF_16_LE_DECODE_METHODDEF
1012 _CODECS_UTF_16_BE_DECODE_METHODDEF
1013 _CODECS_UTF_16_EX_DECODE_METHODDEF
1014 _CODECS_UTF_32_ENCODE_METHODDEF
1015 _CODECS_UTF_32_LE_ENCODE_METHODDEF
1016 _CODECS_UTF_32_BE_ENCODE_METHODDEF
1017 _CODECS_UTF_32_DECODE_METHODDEF
1018 _CODECS_UTF_32_LE_DECODE_METHODDEF
1019 _CODECS_UTF_32_BE_DECODE_METHODDEF
1020 _CODECS_UTF_32_EX_DECODE_METHODDEF
1021 _CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF
1022 _CODECS_UNICODE_ESCAPE_DECODE_METHODDEF
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001023 _CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF
1024 _CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF
1025 _CODECS_LATIN_1_ENCODE_METHODDEF
1026 _CODECS_LATIN_1_DECODE_METHODDEF
1027 _CODECS_ASCII_ENCODE_METHODDEF
1028 _CODECS_ASCII_DECODE_METHODDEF
1029 _CODECS_CHARMAP_ENCODE_METHODDEF
1030 _CODECS_CHARMAP_DECODE_METHODDEF
1031 _CODECS_CHARMAP_BUILD_METHODDEF
1032 _CODECS_READBUFFER_ENCODE_METHODDEF
1033 _CODECS_MBCS_ENCODE_METHODDEF
1034 _CODECS_MBCS_DECODE_METHODDEF
Steve Dowerf5aba582016-09-06 19:42:27 -07001035 _CODECS_OEM_ENCODE_METHODDEF
1036 _CODECS_OEM_DECODE_METHODDEF
Serhiy Storchaka0c59ff62015-05-12 13:15:57 +03001037 _CODECS_CODE_PAGE_ENCODE_METHODDEF
1038 _CODECS_CODE_PAGE_DECODE_METHODDEF
1039 _CODECS_REGISTER_ERROR_METHODDEF
1040 _CODECS_LOOKUP_ERROR_METHODDEF
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 {NULL, NULL} /* sentinel */
Guido van Rossume2d67f92000-03-10 23:09:23 +00001042};
1043
Hai Shi1ea45ae2020-02-11 05:16:38 -06001044static PyModuleDef_Slot _codecs_slots[] = {
1045 {0, NULL}
1046};
1047
Martin v. Löwis1a214512008-06-11 05:26:20 +00001048static struct PyModuleDef codecsmodule = {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 PyModuleDef_HEAD_INIT,
1050 "_codecs",
1051 NULL,
Hai Shi1ea45ae2020-02-11 05:16:38 -06001052 0,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001053 _codecs_functions,
Hai Shi1ea45ae2020-02-11 05:16:38 -06001054 _codecs_slots,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001055 NULL,
1056 NULL,
1057 NULL
Martin v. Löwis1a214512008-06-11 05:26:20 +00001058};
1059
Mark Hammondfe51c6d2002-08-02 02:27:13 +00001060PyMODINIT_FUNC
Martin v. Löwis1a214512008-06-11 05:26:20 +00001061PyInit__codecs(void)
Guido van Rossume2d67f92000-03-10 23:09:23 +00001062{
Hai Shi1ea45ae2020-02-11 05:16:38 -06001063 return PyModuleDef_Init(&codecsmodule);
Guido van Rossume2d67f92000-03-10 23:09:23 +00001064}