1 | n/a | /* ------------------------------------------------------------------------ |
---|
2 | n/a | |
---|
3 | n/a | _codecs -- Provides access to the codec registry and the builtin |
---|
4 | n/a | codecs. |
---|
5 | n/a | |
---|
6 | n/a | This module should never be imported directly. The standard library |
---|
7 | n/a | module "codecs" wraps this builtin module for use within Python. |
---|
8 | n/a | |
---|
9 | n/a | The codec registry is accessible via: |
---|
10 | n/a | |
---|
11 | n/a | register(search_function) -> None |
---|
12 | n/a | |
---|
13 | n/a | lookup(encoding) -> CodecInfo object |
---|
14 | n/a | |
---|
15 | n/a | The builtin Unicode codecs use the following interface: |
---|
16 | n/a | |
---|
17 | n/a | <encoding>_encode(Unicode_object[,errors='strict']) -> |
---|
18 | n/a | (string object, bytes consumed) |
---|
19 | n/a | |
---|
20 | n/a | <encoding>_decode(char_buffer_obj[,errors='strict']) -> |
---|
21 | n/a | (Unicode object, bytes consumed) |
---|
22 | n/a | |
---|
23 | n/a | These <encoding>s are available: utf_8, unicode_escape, |
---|
24 | n/a | raw_unicode_escape, unicode_internal, latin_1, ascii (7-bit), |
---|
25 | n/a | mbcs (on win32). |
---|
26 | n/a | |
---|
27 | n/a | |
---|
28 | n/a | Written by Marc-Andre Lemburg (mal@lemburg.com). |
---|
29 | n/a | |
---|
30 | n/a | Copyright (c) Corporation for National Research Initiatives. |
---|
31 | n/a | |
---|
32 | n/a | ------------------------------------------------------------------------ */ |
---|
33 | n/a | |
---|
34 | n/a | #define PY_SSIZE_T_CLEAN |
---|
35 | n/a | #include "Python.h" |
---|
36 | n/a | |
---|
37 | n/a | #ifdef MS_WINDOWS |
---|
38 | n/a | #include <windows.h> |
---|
39 | n/a | #endif |
---|
40 | n/a | |
---|
41 | n/a | /*[clinic input] |
---|
42 | n/a | module _codecs |
---|
43 | n/a | [clinic start generated code]*/ |
---|
44 | n/a | /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/ |
---|
45 | n/a | |
---|
46 | n/a | #include "clinic/_codecsmodule.c.h" |
---|
47 | n/a | |
---|
48 | n/a | /* --- Registry ----------------------------------------------------------- */ |
---|
49 | n/a | |
---|
50 | n/a | /*[clinic input] |
---|
51 | n/a | _codecs.register |
---|
52 | n/a | search_function: object |
---|
53 | n/a | / |
---|
54 | n/a | |
---|
55 | n/a | Register a codec search function. |
---|
56 | n/a | |
---|
57 | n/a | Search functions are expected to take one argument, the encoding name in |
---|
58 | n/a | all lower case letters, and either return None, or a tuple of functions |
---|
59 | n/a | (encoder, decoder, stream_reader, stream_writer) (or a CodecInfo object). |
---|
60 | n/a | [clinic start generated code]*/ |
---|
61 | n/a | |
---|
62 | n/a | static PyObject * |
---|
63 | n/a | _codecs_register(PyObject *module, PyObject *search_function) |
---|
64 | n/a | /*[clinic end generated code: output=d1bf21e99db7d6d3 input=369578467955cae4]*/ |
---|
65 | n/a | { |
---|
66 | n/a | if (PyCodec_Register(search_function)) |
---|
67 | n/a | return NULL; |
---|
68 | n/a | |
---|
69 | n/a | Py_RETURN_NONE; |
---|
70 | n/a | } |
---|
71 | n/a | |
---|
72 | n/a | /*[clinic input] |
---|
73 | n/a | _codecs.lookup |
---|
74 | n/a | encoding: str |
---|
75 | n/a | / |
---|
76 | n/a | |
---|
77 | n/a | Looks up a codec tuple in the Python codec registry and returns a CodecInfo object. |
---|
78 | n/a | [clinic start generated code]*/ |
---|
79 | n/a | |
---|
80 | n/a | static PyObject * |
---|
81 | n/a | _codecs_lookup_impl(PyObject *module, const char *encoding) |
---|
82 | n/a | /*[clinic end generated code: output=9f0afa572080c36d input=3c572c0db3febe9c]*/ |
---|
83 | n/a | { |
---|
84 | n/a | return _PyCodec_Lookup(encoding); |
---|
85 | n/a | } |
---|
86 | n/a | |
---|
87 | n/a | /*[clinic input] |
---|
88 | n/a | _codecs.encode |
---|
89 | n/a | obj: object |
---|
90 | n/a | encoding: str(c_default="NULL") = "utf-8" |
---|
91 | n/a | errors: str(c_default="NULL") = "strict" |
---|
92 | n/a | |
---|
93 | n/a | Encodes obj using the codec registered for encoding. |
---|
94 | n/a | |
---|
95 | n/a | The default encoding is 'utf-8'. errors may be given to set a |
---|
96 | n/a | different error handling scheme. Default is 'strict' meaning that encoding |
---|
97 | n/a | errors raise a ValueError. Other possible values are 'ignore', 'replace' |
---|
98 | n/a | and 'backslashreplace' as well as any other name registered with |
---|
99 | n/a | codecs.register_error that can handle ValueErrors. |
---|
100 | n/a | [clinic start generated code]*/ |
---|
101 | n/a | |
---|
102 | n/a | static PyObject * |
---|
103 | n/a | _codecs_encode_impl(PyObject *module, PyObject *obj, const char *encoding, |
---|
104 | n/a | const char *errors) |
---|
105 | n/a | /*[clinic end generated code: output=385148eb9a067c86 input=cd5b685040ff61f0]*/ |
---|
106 | n/a | { |
---|
107 | n/a | if (encoding == NULL) |
---|
108 | n/a | encoding = PyUnicode_GetDefaultEncoding(); |
---|
109 | n/a | |
---|
110 | n/a | /* Encode via the codec registry */ |
---|
111 | n/a | return PyCodec_Encode(obj, encoding, errors); |
---|
112 | n/a | } |
---|
113 | n/a | |
---|
114 | n/a | /*[clinic input] |
---|
115 | n/a | _codecs.decode |
---|
116 | n/a | obj: object |
---|
117 | n/a | encoding: str(c_default="NULL") = "utf-8" |
---|
118 | n/a | errors: str(c_default="NULL") = "strict" |
---|
119 | n/a | |
---|
120 | n/a | Decodes obj using the codec registered for encoding. |
---|
121 | n/a | |
---|
122 | n/a | Default encoding is 'utf-8'. errors may be given to set a |
---|
123 | n/a | different error handling scheme. Default is 'strict' meaning that encoding |
---|
124 | n/a | errors raise a ValueError. Other possible values are 'ignore', 'replace' |
---|
125 | n/a | and 'backslashreplace' as well as any other name registered with |
---|
126 | n/a | codecs.register_error that can handle ValueErrors. |
---|
127 | n/a | [clinic start generated code]*/ |
---|
128 | n/a | |
---|
129 | n/a | static PyObject * |
---|
130 | n/a | _codecs_decode_impl(PyObject *module, PyObject *obj, const char *encoding, |
---|
131 | n/a | const char *errors) |
---|
132 | n/a | /*[clinic end generated code: output=679882417dc3a0bd input=7702c0cc2fa1add6]*/ |
---|
133 | n/a | { |
---|
134 | n/a | if (encoding == NULL) |
---|
135 | n/a | encoding = PyUnicode_GetDefaultEncoding(); |
---|
136 | n/a | |
---|
137 | n/a | /* Decode via the codec registry */ |
---|
138 | n/a | return PyCodec_Decode(obj, encoding, errors); |
---|
139 | n/a | } |
---|
140 | n/a | |
---|
141 | n/a | /* --- Helpers ------------------------------------------------------------ */ |
---|
142 | n/a | |
---|
143 | n/a | /*[clinic input] |
---|
144 | n/a | _codecs._forget_codec |
---|
145 | n/a | |
---|
146 | n/a | encoding: str |
---|
147 | n/a | / |
---|
148 | n/a | |
---|
149 | n/a | Purge the named codec from the internal codec lookup cache |
---|
150 | n/a | [clinic start generated code]*/ |
---|
151 | n/a | |
---|
152 | n/a | static PyObject * |
---|
153 | n/a | _codecs__forget_codec_impl(PyObject *module, const char *encoding) |
---|
154 | n/a | /*[clinic end generated code: output=0bde9f0a5b084aa2 input=18d5d92d0e386c38]*/ |
---|
155 | n/a | { |
---|
156 | n/a | if (_PyCodec_Forget(encoding) < 0) { |
---|
157 | n/a | return NULL; |
---|
158 | n/a | }; |
---|
159 | n/a | Py_RETURN_NONE; |
---|
160 | n/a | } |
---|
161 | n/a | |
---|
162 | n/a | static |
---|
163 | n/a | PyObject *codec_tuple(PyObject *decoded, |
---|
164 | n/a | Py_ssize_t len) |
---|
165 | n/a | { |
---|
166 | n/a | if (decoded == NULL) |
---|
167 | n/a | return NULL; |
---|
168 | n/a | return Py_BuildValue("Nn", decoded, len); |
---|
169 | n/a | } |
---|
170 | n/a | |
---|
171 | n/a | /* --- String codecs ------------------------------------------------------ */ |
---|
172 | n/a | /*[clinic input] |
---|
173 | n/a | _codecs.escape_decode |
---|
174 | n/a | data: Py_buffer(accept={str, buffer}) |
---|
175 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
176 | n/a | / |
---|
177 | n/a | [clinic start generated code]*/ |
---|
178 | n/a | |
---|
179 | n/a | static PyObject * |
---|
180 | n/a | _codecs_escape_decode_impl(PyObject *module, Py_buffer *data, |
---|
181 | n/a | const char *errors) |
---|
182 | n/a | /*[clinic end generated code: output=505200ba8056979a input=0018edfd99db714d]*/ |
---|
183 | n/a | { |
---|
184 | n/a | PyObject *decoded = PyBytes_DecodeEscape(data->buf, data->len, |
---|
185 | n/a | errors, 0, NULL); |
---|
186 | n/a | return codec_tuple(decoded, data->len); |
---|
187 | n/a | } |
---|
188 | n/a | |
---|
189 | n/a | /*[clinic input] |
---|
190 | n/a | _codecs.escape_encode |
---|
191 | n/a | data: object(subclass_of='&PyBytes_Type') |
---|
192 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
193 | n/a | / |
---|
194 | n/a | [clinic start generated code]*/ |
---|
195 | n/a | |
---|
196 | n/a | static PyObject * |
---|
197 | n/a | _codecs_escape_encode_impl(PyObject *module, PyObject *data, |
---|
198 | n/a | const char *errors) |
---|
199 | n/a | /*[clinic end generated code: output=4af1d477834bab34 input=da9ded00992f32f2]*/ |
---|
200 | n/a | { |
---|
201 | n/a | Py_ssize_t size; |
---|
202 | n/a | Py_ssize_t newsize; |
---|
203 | n/a | PyObject *v; |
---|
204 | n/a | |
---|
205 | n/a | size = PyBytes_GET_SIZE(data); |
---|
206 | n/a | if (size > PY_SSIZE_T_MAX / 4) { |
---|
207 | n/a | PyErr_SetString(PyExc_OverflowError, |
---|
208 | n/a | "string is too large to encode"); |
---|
209 | n/a | return NULL; |
---|
210 | n/a | } |
---|
211 | n/a | newsize = 4*size; |
---|
212 | n/a | v = PyBytes_FromStringAndSize(NULL, newsize); |
---|
213 | n/a | |
---|
214 | n/a | if (v == NULL) { |
---|
215 | n/a | return NULL; |
---|
216 | n/a | } |
---|
217 | n/a | else { |
---|
218 | n/a | Py_ssize_t i; |
---|
219 | n/a | char c; |
---|
220 | n/a | char *p = PyBytes_AS_STRING(v); |
---|
221 | n/a | |
---|
222 | n/a | for (i = 0; i < size; i++) { |
---|
223 | n/a | /* There's at least enough room for a hex escape */ |
---|
224 | n/a | assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4); |
---|
225 | n/a | c = PyBytes_AS_STRING(data)[i]; |
---|
226 | n/a | if (c == '\'' || c == '\\') |
---|
227 | n/a | *p++ = '\\', *p++ = c; |
---|
228 | n/a | else if (c == '\t') |
---|
229 | n/a | *p++ = '\\', *p++ = 't'; |
---|
230 | n/a | else if (c == '\n') |
---|
231 | n/a | *p++ = '\\', *p++ = 'n'; |
---|
232 | n/a | else if (c == '\r') |
---|
233 | n/a | *p++ = '\\', *p++ = 'r'; |
---|
234 | n/a | else if (c < ' ' || c >= 0x7f) { |
---|
235 | n/a | *p++ = '\\'; |
---|
236 | n/a | *p++ = 'x'; |
---|
237 | n/a | *p++ = Py_hexdigits[(c & 0xf0) >> 4]; |
---|
238 | n/a | *p++ = Py_hexdigits[c & 0xf]; |
---|
239 | n/a | } |
---|
240 | n/a | else |
---|
241 | n/a | *p++ = c; |
---|
242 | n/a | } |
---|
243 | n/a | *p = '\0'; |
---|
244 | n/a | if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) { |
---|
245 | n/a | return NULL; |
---|
246 | n/a | } |
---|
247 | n/a | } |
---|
248 | n/a | |
---|
249 | n/a | return codec_tuple(v, size); |
---|
250 | n/a | } |
---|
251 | n/a | |
---|
252 | n/a | /* --- Decoder ------------------------------------------------------------ */ |
---|
253 | n/a | /*[clinic input] |
---|
254 | n/a | _codecs.unicode_internal_decode |
---|
255 | n/a | obj: object |
---|
256 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
257 | n/a | / |
---|
258 | n/a | [clinic start generated code]*/ |
---|
259 | n/a | |
---|
260 | n/a | static PyObject * |
---|
261 | n/a | _codecs_unicode_internal_decode_impl(PyObject *module, PyObject *obj, |
---|
262 | n/a | const char *errors) |
---|
263 | n/a | /*[clinic end generated code: output=edbfe175e09eff9a input=8d57930aeda170c6]*/ |
---|
264 | n/a | { |
---|
265 | n/a | if (PyUnicode_Check(obj)) { |
---|
266 | n/a | if (PyUnicode_READY(obj) < 0) |
---|
267 | n/a | return NULL; |
---|
268 | n/a | Py_INCREF(obj); |
---|
269 | n/a | return codec_tuple(obj, PyUnicode_GET_LENGTH(obj)); |
---|
270 | n/a | } |
---|
271 | n/a | else { |
---|
272 | n/a | Py_buffer view; |
---|
273 | n/a | PyObject *result; |
---|
274 | n/a | if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) |
---|
275 | n/a | return NULL; |
---|
276 | n/a | |
---|
277 | n/a | result = codec_tuple( |
---|
278 | n/a | _PyUnicode_DecodeUnicodeInternal(view.buf, view.len, errors), |
---|
279 | n/a | view.len); |
---|
280 | n/a | PyBuffer_Release(&view); |
---|
281 | n/a | return result; |
---|
282 | n/a | } |
---|
283 | n/a | } |
---|
284 | n/a | |
---|
285 | n/a | /*[clinic input] |
---|
286 | n/a | _codecs.utf_7_decode |
---|
287 | n/a | data: Py_buffer |
---|
288 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
289 | n/a | final: int(c_default="0") = False |
---|
290 | n/a | / |
---|
291 | n/a | [clinic start generated code]*/ |
---|
292 | n/a | |
---|
293 | n/a | static PyObject * |
---|
294 | n/a | _codecs_utf_7_decode_impl(PyObject *module, Py_buffer *data, |
---|
295 | n/a | const char *errors, int final) |
---|
296 | n/a | /*[clinic end generated code: output=0cd3a944a32a4089 input=bc4d6247ecdb01e6]*/ |
---|
297 | n/a | { |
---|
298 | n/a | Py_ssize_t consumed = data->len; |
---|
299 | n/a | PyObject *decoded = PyUnicode_DecodeUTF7Stateful(data->buf, data->len, |
---|
300 | n/a | errors, |
---|
301 | n/a | final ? NULL : &consumed); |
---|
302 | n/a | return codec_tuple(decoded, consumed); |
---|
303 | n/a | } |
---|
304 | n/a | |
---|
305 | n/a | /*[clinic input] |
---|
306 | n/a | _codecs.utf_8_decode |
---|
307 | n/a | data: Py_buffer |
---|
308 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
309 | n/a | final: int(c_default="0") = False |
---|
310 | n/a | / |
---|
311 | n/a | [clinic start generated code]*/ |
---|
312 | n/a | |
---|
313 | n/a | static PyObject * |
---|
314 | n/a | _codecs_utf_8_decode_impl(PyObject *module, Py_buffer *data, |
---|
315 | n/a | const char *errors, int final) |
---|
316 | n/a | /*[clinic end generated code: output=10f74dec8d9bb8bf input=39161d71e7422ee2]*/ |
---|
317 | n/a | { |
---|
318 | n/a | Py_ssize_t consumed = data->len; |
---|
319 | n/a | PyObject *decoded = PyUnicode_DecodeUTF8Stateful(data->buf, data->len, |
---|
320 | n/a | errors, |
---|
321 | n/a | final ? NULL : &consumed); |
---|
322 | n/a | return codec_tuple(decoded, consumed); |
---|
323 | n/a | } |
---|
324 | n/a | |
---|
325 | n/a | /*[clinic input] |
---|
326 | n/a | _codecs.utf_16_decode |
---|
327 | n/a | data: Py_buffer |
---|
328 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
329 | n/a | final: int(c_default="0") = False |
---|
330 | n/a | / |
---|
331 | n/a | [clinic start generated code]*/ |
---|
332 | n/a | |
---|
333 | n/a | static PyObject * |
---|
334 | n/a | _codecs_utf_16_decode_impl(PyObject *module, Py_buffer *data, |
---|
335 | n/a | const char *errors, int final) |
---|
336 | n/a | /*[clinic end generated code: output=783b442abcbcc2d0 input=f3cf01d1461007ce]*/ |
---|
337 | n/a | { |
---|
338 | n/a | int byteorder = 0; |
---|
339 | n/a | /* This is overwritten unless final is true. */ |
---|
340 | n/a | Py_ssize_t consumed = data->len; |
---|
341 | n/a | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
---|
342 | n/a | errors, &byteorder, |
---|
343 | n/a | final ? NULL : &consumed); |
---|
344 | n/a | return codec_tuple(decoded, consumed); |
---|
345 | n/a | } |
---|
346 | n/a | |
---|
347 | n/a | /*[clinic input] |
---|
348 | n/a | _codecs.utf_16_le_decode |
---|
349 | n/a | data: Py_buffer |
---|
350 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
351 | n/a | final: int(c_default="0") = False |
---|
352 | n/a | / |
---|
353 | n/a | [clinic start generated code]*/ |
---|
354 | n/a | |
---|
355 | n/a | static PyObject * |
---|
356 | n/a | _codecs_utf_16_le_decode_impl(PyObject *module, Py_buffer *data, |
---|
357 | n/a | const char *errors, int final) |
---|
358 | n/a | /*[clinic end generated code: output=899b9e6364379dcd input=a77e3bf97335d94e]*/ |
---|
359 | n/a | { |
---|
360 | n/a | int byteorder = -1; |
---|
361 | n/a | /* This is overwritten unless final is true. */ |
---|
362 | n/a | Py_ssize_t consumed = data->len; |
---|
363 | n/a | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
---|
364 | n/a | errors, &byteorder, |
---|
365 | n/a | final ? NULL : &consumed); |
---|
366 | n/a | return codec_tuple(decoded, consumed); |
---|
367 | n/a | } |
---|
368 | n/a | |
---|
369 | n/a | /*[clinic input] |
---|
370 | n/a | _codecs.utf_16_be_decode |
---|
371 | n/a | data: Py_buffer |
---|
372 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
373 | n/a | final: int(c_default="0") = False |
---|
374 | n/a | / |
---|
375 | n/a | [clinic start generated code]*/ |
---|
376 | n/a | |
---|
377 | n/a | static PyObject * |
---|
378 | n/a | _codecs_utf_16_be_decode_impl(PyObject *module, Py_buffer *data, |
---|
379 | n/a | const char *errors, int final) |
---|
380 | n/a | /*[clinic end generated code: output=49f6465ea07669c8 input=606f69fae91b5563]*/ |
---|
381 | n/a | { |
---|
382 | n/a | int byteorder = 1; |
---|
383 | n/a | /* This is overwritten unless final is true. */ |
---|
384 | n/a | Py_ssize_t consumed = data->len; |
---|
385 | n/a | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
---|
386 | n/a | errors, &byteorder, |
---|
387 | n/a | final ? NULL : &consumed); |
---|
388 | n/a | return codec_tuple(decoded, consumed); |
---|
389 | n/a | } |
---|
390 | n/a | |
---|
391 | n/a | /* This non-standard version also provides access to the byteorder |
---|
392 | n/a | parameter of the builtin UTF-16 codec. |
---|
393 | n/a | |
---|
394 | n/a | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
---|
395 | n/a | being the value in effect at the end of data. |
---|
396 | n/a | |
---|
397 | n/a | */ |
---|
398 | n/a | /*[clinic input] |
---|
399 | n/a | _codecs.utf_16_ex_decode |
---|
400 | n/a | data: Py_buffer |
---|
401 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
402 | n/a | byteorder: int = 0 |
---|
403 | n/a | final: int(c_default="0") = False |
---|
404 | n/a | / |
---|
405 | n/a | [clinic start generated code]*/ |
---|
406 | n/a | |
---|
407 | n/a | static PyObject * |
---|
408 | n/a | _codecs_utf_16_ex_decode_impl(PyObject *module, Py_buffer *data, |
---|
409 | n/a | const char *errors, int byteorder, int final) |
---|
410 | n/a | /*[clinic end generated code: output=0f385f251ecc1988 input=f6e7f697658c013e]*/ |
---|
411 | n/a | { |
---|
412 | n/a | /* This is overwritten unless final is true. */ |
---|
413 | n/a | Py_ssize_t consumed = data->len; |
---|
414 | n/a | |
---|
415 | n/a | PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len, |
---|
416 | n/a | errors, &byteorder, |
---|
417 | n/a | final ? NULL : &consumed); |
---|
418 | n/a | if (decoded == NULL) |
---|
419 | n/a | return NULL; |
---|
420 | n/a | return Py_BuildValue("Nni", decoded, consumed, byteorder); |
---|
421 | n/a | } |
---|
422 | n/a | |
---|
423 | n/a | /*[clinic input] |
---|
424 | n/a | _codecs.utf_32_decode |
---|
425 | n/a | data: Py_buffer |
---|
426 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
427 | n/a | final: int(c_default="0") = False |
---|
428 | n/a | / |
---|
429 | n/a | [clinic start generated code]*/ |
---|
430 | n/a | |
---|
431 | n/a | static PyObject * |
---|
432 | n/a | _codecs_utf_32_decode_impl(PyObject *module, Py_buffer *data, |
---|
433 | n/a | const char *errors, int final) |
---|
434 | n/a | /*[clinic end generated code: output=2fc961807f7b145f input=86d4f41c6c2e763d]*/ |
---|
435 | n/a | { |
---|
436 | n/a | int byteorder = 0; |
---|
437 | n/a | /* This is overwritten unless final is true. */ |
---|
438 | n/a | Py_ssize_t consumed = data->len; |
---|
439 | n/a | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
---|
440 | n/a | errors, &byteorder, |
---|
441 | n/a | final ? NULL : &consumed); |
---|
442 | n/a | return codec_tuple(decoded, consumed); |
---|
443 | n/a | } |
---|
444 | n/a | |
---|
445 | n/a | /*[clinic input] |
---|
446 | n/a | _codecs.utf_32_le_decode |
---|
447 | n/a | data: Py_buffer |
---|
448 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
449 | n/a | final: int(c_default="0") = False |
---|
450 | n/a | / |
---|
451 | n/a | [clinic start generated code]*/ |
---|
452 | n/a | |
---|
453 | n/a | static PyObject * |
---|
454 | n/a | _codecs_utf_32_le_decode_impl(PyObject *module, Py_buffer *data, |
---|
455 | n/a | const char *errors, int final) |
---|
456 | n/a | /*[clinic end generated code: output=ec8f46b67a94f3e6 input=d18b650772d188ba]*/ |
---|
457 | n/a | { |
---|
458 | n/a | int byteorder = -1; |
---|
459 | n/a | /* This is overwritten unless final is true. */ |
---|
460 | n/a | Py_ssize_t consumed = data->len; |
---|
461 | n/a | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
---|
462 | n/a | errors, &byteorder, |
---|
463 | n/a | final ? NULL : &consumed); |
---|
464 | n/a | return codec_tuple(decoded, consumed); |
---|
465 | n/a | } |
---|
466 | n/a | |
---|
467 | n/a | /*[clinic input] |
---|
468 | n/a | _codecs.utf_32_be_decode |
---|
469 | n/a | data: Py_buffer |
---|
470 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
471 | n/a | final: int(c_default="0") = False |
---|
472 | n/a | / |
---|
473 | n/a | [clinic start generated code]*/ |
---|
474 | n/a | |
---|
475 | n/a | static PyObject * |
---|
476 | n/a | _codecs_utf_32_be_decode_impl(PyObject *module, Py_buffer *data, |
---|
477 | n/a | const char *errors, int final) |
---|
478 | n/a | /*[clinic end generated code: output=ff82bae862c92c4e input=19c271b5d34926d8]*/ |
---|
479 | n/a | { |
---|
480 | n/a | int byteorder = 1; |
---|
481 | n/a | /* This is overwritten unless final is true. */ |
---|
482 | n/a | Py_ssize_t consumed = data->len; |
---|
483 | n/a | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
---|
484 | n/a | errors, &byteorder, |
---|
485 | n/a | final ? NULL : &consumed); |
---|
486 | n/a | return codec_tuple(decoded, consumed); |
---|
487 | n/a | } |
---|
488 | n/a | |
---|
489 | n/a | /* This non-standard version also provides access to the byteorder |
---|
490 | n/a | parameter of the builtin UTF-32 codec. |
---|
491 | n/a | |
---|
492 | n/a | It returns a tuple (unicode, bytesread, byteorder) with byteorder |
---|
493 | n/a | being the value in effect at the end of data. |
---|
494 | n/a | |
---|
495 | n/a | */ |
---|
496 | n/a | /*[clinic input] |
---|
497 | n/a | _codecs.utf_32_ex_decode |
---|
498 | n/a | data: Py_buffer |
---|
499 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
500 | n/a | byteorder: int = 0 |
---|
501 | n/a | final: int(c_default="0") = False |
---|
502 | n/a | / |
---|
503 | n/a | [clinic start generated code]*/ |
---|
504 | n/a | |
---|
505 | n/a | static PyObject * |
---|
506 | n/a | _codecs_utf_32_ex_decode_impl(PyObject *module, Py_buffer *data, |
---|
507 | n/a | const char *errors, int byteorder, int final) |
---|
508 | n/a | /*[clinic end generated code: output=6bfb177dceaf4848 input=4af3e6ccfe34a076]*/ |
---|
509 | n/a | { |
---|
510 | n/a | Py_ssize_t consumed = data->len; |
---|
511 | n/a | PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len, |
---|
512 | n/a | errors, &byteorder, |
---|
513 | n/a | final ? NULL : &consumed); |
---|
514 | n/a | if (decoded == NULL) |
---|
515 | n/a | return NULL; |
---|
516 | n/a | return Py_BuildValue("Nni", decoded, consumed, byteorder); |
---|
517 | n/a | } |
---|
518 | n/a | |
---|
519 | n/a | /*[clinic input] |
---|
520 | n/a | _codecs.unicode_escape_decode |
---|
521 | n/a | data: Py_buffer(accept={str, buffer}) |
---|
522 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
523 | n/a | / |
---|
524 | n/a | [clinic start generated code]*/ |
---|
525 | n/a | |
---|
526 | n/a | static PyObject * |
---|
527 | n/a | _codecs_unicode_escape_decode_impl(PyObject *module, Py_buffer *data, |
---|
528 | n/a | const char *errors) |
---|
529 | n/a | /*[clinic end generated code: output=3ca3c917176b82ab input=49fd27d06813a7f5]*/ |
---|
530 | n/a | { |
---|
531 | n/a | PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len, |
---|
532 | n/a | errors); |
---|
533 | n/a | return codec_tuple(decoded, data->len); |
---|
534 | n/a | } |
---|
535 | n/a | |
---|
536 | n/a | /*[clinic input] |
---|
537 | n/a | _codecs.raw_unicode_escape_decode |
---|
538 | n/a | data: Py_buffer(accept={str, buffer}) |
---|
539 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
540 | n/a | / |
---|
541 | n/a | [clinic start generated code]*/ |
---|
542 | n/a | |
---|
543 | n/a | static PyObject * |
---|
544 | n/a | _codecs_raw_unicode_escape_decode_impl(PyObject *module, Py_buffer *data, |
---|
545 | n/a | const char *errors) |
---|
546 | n/a | /*[clinic end generated code: output=c98eeb56028070a6 input=770903a211434ebc]*/ |
---|
547 | n/a | { |
---|
548 | n/a | PyObject *decoded = PyUnicode_DecodeRawUnicodeEscape(data->buf, data->len, |
---|
549 | n/a | errors); |
---|
550 | n/a | return codec_tuple(decoded, data->len); |
---|
551 | n/a | } |
---|
552 | n/a | |
---|
553 | n/a | /*[clinic input] |
---|
554 | n/a | _codecs.latin_1_decode |
---|
555 | n/a | data: Py_buffer |
---|
556 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
557 | n/a | / |
---|
558 | n/a | [clinic start generated code]*/ |
---|
559 | n/a | |
---|
560 | n/a | static PyObject * |
---|
561 | n/a | _codecs_latin_1_decode_impl(PyObject *module, Py_buffer *data, |
---|
562 | n/a | const char *errors) |
---|
563 | n/a | /*[clinic end generated code: output=07f3dfa3f72c7d8f input=5cad0f1759c618ec]*/ |
---|
564 | n/a | { |
---|
565 | n/a | PyObject *decoded = PyUnicode_DecodeLatin1(data->buf, data->len, errors); |
---|
566 | n/a | return codec_tuple(decoded, data->len); |
---|
567 | n/a | } |
---|
568 | n/a | |
---|
569 | n/a | /*[clinic input] |
---|
570 | n/a | _codecs.ascii_decode |
---|
571 | n/a | data: Py_buffer |
---|
572 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
573 | n/a | / |
---|
574 | n/a | [clinic start generated code]*/ |
---|
575 | n/a | |
---|
576 | n/a | static PyObject * |
---|
577 | n/a | _codecs_ascii_decode_impl(PyObject *module, Py_buffer *data, |
---|
578 | n/a | const char *errors) |
---|
579 | n/a | /*[clinic end generated code: output=2627d72058d42429 input=ad1106f64037bd16]*/ |
---|
580 | n/a | { |
---|
581 | n/a | PyObject *decoded = PyUnicode_DecodeASCII(data->buf, data->len, errors); |
---|
582 | n/a | return codec_tuple(decoded, data->len); |
---|
583 | n/a | } |
---|
584 | n/a | |
---|
585 | n/a | /*[clinic input] |
---|
586 | n/a | _codecs.charmap_decode |
---|
587 | n/a | data: Py_buffer |
---|
588 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
589 | n/a | mapping: object = NULL |
---|
590 | n/a | / |
---|
591 | n/a | [clinic start generated code]*/ |
---|
592 | n/a | |
---|
593 | n/a | static PyObject * |
---|
594 | n/a | _codecs_charmap_decode_impl(PyObject *module, Py_buffer *data, |
---|
595 | n/a | const char *errors, PyObject *mapping) |
---|
596 | n/a | /*[clinic end generated code: output=2c335b09778cf895 input=19712ca35c5a80e2]*/ |
---|
597 | n/a | { |
---|
598 | n/a | PyObject *decoded; |
---|
599 | n/a | |
---|
600 | n/a | if (mapping == Py_None) |
---|
601 | n/a | mapping = NULL; |
---|
602 | n/a | |
---|
603 | n/a | decoded = PyUnicode_DecodeCharmap(data->buf, data->len, mapping, errors); |
---|
604 | n/a | return codec_tuple(decoded, data->len); |
---|
605 | n/a | } |
---|
606 | n/a | |
---|
607 | n/a | #ifdef MS_WINDOWS |
---|
608 | n/a | |
---|
609 | n/a | /*[clinic input] |
---|
610 | n/a | _codecs.mbcs_decode |
---|
611 | n/a | data: Py_buffer |
---|
612 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
613 | n/a | final: int(c_default="0") = False |
---|
614 | n/a | / |
---|
615 | n/a | [clinic start generated code]*/ |
---|
616 | n/a | |
---|
617 | n/a | static PyObject * |
---|
618 | n/a | _codecs_mbcs_decode_impl(PyObject *module, Py_buffer *data, |
---|
619 | n/a | const char *errors, int final) |
---|
620 | n/a | /*[clinic end generated code: output=39b65b8598938c4b input=d492c1ca64f4fa8a]*/ |
---|
621 | n/a | { |
---|
622 | n/a | Py_ssize_t consumed = data->len; |
---|
623 | n/a | PyObject *decoded = PyUnicode_DecodeMBCSStateful(data->buf, data->len, |
---|
624 | n/a | errors, final ? NULL : &consumed); |
---|
625 | n/a | return codec_tuple(decoded, consumed); |
---|
626 | n/a | } |
---|
627 | n/a | |
---|
628 | n/a | /*[clinic input] |
---|
629 | n/a | _codecs.oem_decode |
---|
630 | n/a | data: Py_buffer |
---|
631 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
632 | n/a | final: int(c_default="0") = False |
---|
633 | n/a | / |
---|
634 | n/a | [clinic start generated code]*/ |
---|
635 | n/a | |
---|
636 | n/a | static PyObject * |
---|
637 | n/a | _codecs_oem_decode_impl(PyObject *module, Py_buffer *data, |
---|
638 | n/a | const char *errors, int final) |
---|
639 | n/a | /*[clinic end generated code: output=da1617612f3fcad8 input=95b8a92c446b03cd]*/ |
---|
640 | n/a | { |
---|
641 | n/a | Py_ssize_t consumed = data->len; |
---|
642 | n/a | PyObject *decoded = PyUnicode_DecodeCodePageStateful(CP_OEMCP, |
---|
643 | n/a | data->buf, data->len, errors, final ? NULL : &consumed); |
---|
644 | n/a | return codec_tuple(decoded, consumed); |
---|
645 | n/a | } |
---|
646 | n/a | |
---|
647 | n/a | /*[clinic input] |
---|
648 | n/a | _codecs.code_page_decode |
---|
649 | n/a | codepage: int |
---|
650 | n/a | data: Py_buffer |
---|
651 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
652 | n/a | final: int(c_default="0") = False |
---|
653 | n/a | / |
---|
654 | n/a | [clinic start generated code]*/ |
---|
655 | n/a | |
---|
656 | n/a | static PyObject * |
---|
657 | n/a | _codecs_code_page_decode_impl(PyObject *module, int codepage, |
---|
658 | n/a | Py_buffer *data, const char *errors, int final) |
---|
659 | n/a | /*[clinic end generated code: output=53008ea967da3fff input=4f3152a304e21d51]*/ |
---|
660 | n/a | { |
---|
661 | n/a | Py_ssize_t consumed = data->len; |
---|
662 | n/a | PyObject *decoded = PyUnicode_DecodeCodePageStateful(codepage, |
---|
663 | n/a | data->buf, data->len, |
---|
664 | n/a | errors, |
---|
665 | n/a | final ? NULL : &consumed); |
---|
666 | n/a | return codec_tuple(decoded, consumed); |
---|
667 | n/a | } |
---|
668 | n/a | |
---|
669 | n/a | #endif /* MS_WINDOWS */ |
---|
670 | n/a | |
---|
671 | n/a | /* --- Encoder ------------------------------------------------------------ */ |
---|
672 | n/a | |
---|
673 | n/a | /*[clinic input] |
---|
674 | n/a | _codecs.readbuffer_encode |
---|
675 | n/a | data: Py_buffer(accept={str, buffer}) |
---|
676 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
677 | n/a | / |
---|
678 | n/a | [clinic start generated code]*/ |
---|
679 | n/a | |
---|
680 | n/a | static PyObject * |
---|
681 | n/a | _codecs_readbuffer_encode_impl(PyObject *module, Py_buffer *data, |
---|
682 | n/a | const char *errors) |
---|
683 | n/a | /*[clinic end generated code: output=c645ea7cdb3d6e86 input=b7c322b89d4ab923]*/ |
---|
684 | n/a | { |
---|
685 | n/a | PyObject *result = PyBytes_FromStringAndSize(data->buf, data->len); |
---|
686 | n/a | return codec_tuple(result, data->len); |
---|
687 | n/a | } |
---|
688 | n/a | |
---|
689 | n/a | /*[clinic input] |
---|
690 | n/a | _codecs.unicode_internal_encode |
---|
691 | n/a | obj: object |
---|
692 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
693 | n/a | / |
---|
694 | n/a | [clinic start generated code]*/ |
---|
695 | n/a | |
---|
696 | n/a | static PyObject * |
---|
697 | n/a | _codecs_unicode_internal_encode_impl(PyObject *module, PyObject *obj, |
---|
698 | n/a | const char *errors) |
---|
699 | n/a | /*[clinic end generated code: output=a72507dde4ea558f input=8628f0280cf5ba61]*/ |
---|
700 | n/a | { |
---|
701 | n/a | if (PyErr_WarnEx(PyExc_DeprecationWarning, |
---|
702 | n/a | "unicode_internal codec has been deprecated", |
---|
703 | n/a | 1)) |
---|
704 | n/a | return NULL; |
---|
705 | n/a | |
---|
706 | n/a | if (PyUnicode_Check(obj)) { |
---|
707 | n/a | Py_UNICODE *u; |
---|
708 | n/a | Py_ssize_t len, size; |
---|
709 | n/a | |
---|
710 | n/a | if (PyUnicode_READY(obj) < 0) |
---|
711 | n/a | return NULL; |
---|
712 | n/a | |
---|
713 | n/a | u = PyUnicode_AsUnicodeAndSize(obj, &len); |
---|
714 | n/a | if (u == NULL) |
---|
715 | n/a | return NULL; |
---|
716 | n/a | if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) |
---|
717 | n/a | return PyErr_NoMemory(); |
---|
718 | n/a | size = len * sizeof(Py_UNICODE); |
---|
719 | n/a | return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size), |
---|
720 | n/a | PyUnicode_GET_LENGTH(obj)); |
---|
721 | n/a | } |
---|
722 | n/a | else { |
---|
723 | n/a | Py_buffer view; |
---|
724 | n/a | PyObject *result; |
---|
725 | n/a | if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) |
---|
726 | n/a | return NULL; |
---|
727 | n/a | result = codec_tuple(PyBytes_FromStringAndSize(view.buf, view.len), |
---|
728 | n/a | view.len); |
---|
729 | n/a | PyBuffer_Release(&view); |
---|
730 | n/a | return result; |
---|
731 | n/a | } |
---|
732 | n/a | } |
---|
733 | n/a | |
---|
734 | n/a | /*[clinic input] |
---|
735 | n/a | _codecs.utf_7_encode |
---|
736 | n/a | str: unicode |
---|
737 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
738 | n/a | / |
---|
739 | n/a | [clinic start generated code]*/ |
---|
740 | n/a | |
---|
741 | n/a | static PyObject * |
---|
742 | n/a | _codecs_utf_7_encode_impl(PyObject *module, PyObject *str, |
---|
743 | n/a | const char *errors) |
---|
744 | n/a | /*[clinic end generated code: output=0feda21ffc921bc8 input=d1a47579e79cbe15]*/ |
---|
745 | n/a | { |
---|
746 | n/a | return codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors), |
---|
747 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
748 | n/a | } |
---|
749 | n/a | |
---|
750 | n/a | /*[clinic input] |
---|
751 | n/a | _codecs.utf_8_encode |
---|
752 | n/a | str: unicode |
---|
753 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
754 | n/a | / |
---|
755 | n/a | [clinic start generated code]*/ |
---|
756 | n/a | |
---|
757 | n/a | static PyObject * |
---|
758 | n/a | _codecs_utf_8_encode_impl(PyObject *module, PyObject *str, |
---|
759 | n/a | const char *errors) |
---|
760 | n/a | /*[clinic end generated code: output=02bf47332b9c796c input=42e3ba73c4392eef]*/ |
---|
761 | n/a | { |
---|
762 | n/a | return codec_tuple(_PyUnicode_AsUTF8String(str, errors), |
---|
763 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
764 | n/a | } |
---|
765 | n/a | |
---|
766 | n/a | /* This version provides access to the byteorder parameter of the |
---|
767 | n/a | builtin UTF-16 codecs as optional third argument. It defaults to 0 |
---|
768 | n/a | which means: use the native byte order and prepend the data with a |
---|
769 | n/a | BOM mark. |
---|
770 | n/a | |
---|
771 | n/a | */ |
---|
772 | n/a | |
---|
773 | n/a | /*[clinic input] |
---|
774 | n/a | _codecs.utf_16_encode |
---|
775 | n/a | str: unicode |
---|
776 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
777 | n/a | byteorder: int = 0 |
---|
778 | n/a | / |
---|
779 | n/a | [clinic start generated code]*/ |
---|
780 | n/a | |
---|
781 | n/a | static PyObject * |
---|
782 | n/a | _codecs_utf_16_encode_impl(PyObject *module, PyObject *str, |
---|
783 | n/a | const char *errors, int byteorder) |
---|
784 | n/a | /*[clinic end generated code: output=c654e13efa2e64e4 input=ff46416b04edb944]*/ |
---|
785 | n/a | { |
---|
786 | n/a | return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder), |
---|
787 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
788 | n/a | } |
---|
789 | n/a | |
---|
790 | n/a | /*[clinic input] |
---|
791 | n/a | _codecs.utf_16_le_encode |
---|
792 | n/a | str: unicode |
---|
793 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
794 | n/a | / |
---|
795 | n/a | [clinic start generated code]*/ |
---|
796 | n/a | |
---|
797 | n/a | static PyObject * |
---|
798 | n/a | _codecs_utf_16_le_encode_impl(PyObject *module, PyObject *str, |
---|
799 | n/a | const char *errors) |
---|
800 | n/a | /*[clinic end generated code: output=431b01e55f2d4995 input=cb385455ea8f2fe0]*/ |
---|
801 | n/a | { |
---|
802 | n/a | return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1), |
---|
803 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
804 | n/a | } |
---|
805 | n/a | |
---|
806 | n/a | /*[clinic input] |
---|
807 | n/a | _codecs.utf_16_be_encode |
---|
808 | n/a | str: unicode |
---|
809 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
810 | n/a | / |
---|
811 | n/a | [clinic start generated code]*/ |
---|
812 | n/a | |
---|
813 | n/a | static PyObject * |
---|
814 | n/a | _codecs_utf_16_be_encode_impl(PyObject *module, PyObject *str, |
---|
815 | n/a | const char *errors) |
---|
816 | n/a | /*[clinic end generated code: output=96886a6fd54dcae3 input=9119997066bdaefd]*/ |
---|
817 | n/a | { |
---|
818 | n/a | return codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1), |
---|
819 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
820 | n/a | } |
---|
821 | n/a | |
---|
822 | n/a | /* This version provides access to the byteorder parameter of the |
---|
823 | n/a | builtin UTF-32 codecs as optional third argument. It defaults to 0 |
---|
824 | n/a | which means: use the native byte order and prepend the data with a |
---|
825 | n/a | BOM mark. |
---|
826 | n/a | |
---|
827 | n/a | */ |
---|
828 | n/a | |
---|
829 | n/a | /*[clinic input] |
---|
830 | n/a | _codecs.utf_32_encode |
---|
831 | n/a | str: unicode |
---|
832 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
833 | n/a | byteorder: int = 0 |
---|
834 | n/a | / |
---|
835 | n/a | [clinic start generated code]*/ |
---|
836 | n/a | |
---|
837 | n/a | static PyObject * |
---|
838 | n/a | _codecs_utf_32_encode_impl(PyObject *module, PyObject *str, |
---|
839 | n/a | const char *errors, int byteorder) |
---|
840 | n/a | /*[clinic end generated code: output=5c760da0c09a8b83 input=c5e77da82fbe5c2a]*/ |
---|
841 | n/a | { |
---|
842 | n/a | return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder), |
---|
843 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
844 | n/a | } |
---|
845 | n/a | |
---|
846 | n/a | /*[clinic input] |
---|
847 | n/a | _codecs.utf_32_le_encode |
---|
848 | n/a | str: unicode |
---|
849 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
850 | n/a | / |
---|
851 | n/a | [clinic start generated code]*/ |
---|
852 | n/a | |
---|
853 | n/a | static PyObject * |
---|
854 | n/a | _codecs_utf_32_le_encode_impl(PyObject *module, PyObject *str, |
---|
855 | n/a | const char *errors) |
---|
856 | n/a | /*[clinic end generated code: output=b65cd176de8e36d6 input=9993b25fe0877848]*/ |
---|
857 | n/a | { |
---|
858 | n/a | return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1), |
---|
859 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
860 | n/a | } |
---|
861 | n/a | |
---|
862 | n/a | /*[clinic input] |
---|
863 | n/a | _codecs.utf_32_be_encode |
---|
864 | n/a | str: unicode |
---|
865 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
866 | n/a | / |
---|
867 | n/a | [clinic start generated code]*/ |
---|
868 | n/a | |
---|
869 | n/a | static PyObject * |
---|
870 | n/a | _codecs_utf_32_be_encode_impl(PyObject *module, PyObject *str, |
---|
871 | n/a | const char *errors) |
---|
872 | n/a | /*[clinic end generated code: output=1d9e71a9358709e9 input=d3e0ccaa02920431]*/ |
---|
873 | n/a | { |
---|
874 | n/a | return codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1), |
---|
875 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
876 | n/a | } |
---|
877 | n/a | |
---|
878 | n/a | /*[clinic input] |
---|
879 | n/a | _codecs.unicode_escape_encode |
---|
880 | n/a | str: unicode |
---|
881 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
882 | n/a | / |
---|
883 | n/a | [clinic start generated code]*/ |
---|
884 | n/a | |
---|
885 | n/a | static PyObject * |
---|
886 | n/a | _codecs_unicode_escape_encode_impl(PyObject *module, PyObject *str, |
---|
887 | n/a | const char *errors) |
---|
888 | n/a | /*[clinic end generated code: output=66271b30bc4f7a3c input=65d9eefca65b455a]*/ |
---|
889 | n/a | { |
---|
890 | n/a | return codec_tuple(PyUnicode_AsUnicodeEscapeString(str), |
---|
891 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
892 | n/a | } |
---|
893 | n/a | |
---|
894 | n/a | /*[clinic input] |
---|
895 | n/a | _codecs.raw_unicode_escape_encode |
---|
896 | n/a | str: unicode |
---|
897 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
898 | n/a | / |
---|
899 | n/a | [clinic start generated code]*/ |
---|
900 | n/a | |
---|
901 | n/a | static PyObject * |
---|
902 | n/a | _codecs_raw_unicode_escape_encode_impl(PyObject *module, PyObject *str, |
---|
903 | n/a | const char *errors) |
---|
904 | n/a | /*[clinic end generated code: output=a66a806ed01c830a input=5aa33e4a133391ab]*/ |
---|
905 | n/a | { |
---|
906 | n/a | return codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str), |
---|
907 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
908 | n/a | } |
---|
909 | n/a | |
---|
910 | n/a | /*[clinic input] |
---|
911 | n/a | _codecs.latin_1_encode |
---|
912 | n/a | str: unicode |
---|
913 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
914 | n/a | / |
---|
915 | n/a | [clinic start generated code]*/ |
---|
916 | n/a | |
---|
917 | n/a | static PyObject * |
---|
918 | n/a | _codecs_latin_1_encode_impl(PyObject *module, PyObject *str, |
---|
919 | n/a | const char *errors) |
---|
920 | n/a | /*[clinic end generated code: output=2c28c83a27884e08 input=30b11c9e49a65150]*/ |
---|
921 | n/a | { |
---|
922 | n/a | return codec_tuple(_PyUnicode_AsLatin1String(str, errors), |
---|
923 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
924 | n/a | } |
---|
925 | n/a | |
---|
926 | n/a | /*[clinic input] |
---|
927 | n/a | _codecs.ascii_encode |
---|
928 | n/a | str: unicode |
---|
929 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
930 | n/a | / |
---|
931 | n/a | [clinic start generated code]*/ |
---|
932 | n/a | |
---|
933 | n/a | static PyObject * |
---|
934 | n/a | _codecs_ascii_encode_impl(PyObject *module, PyObject *str, |
---|
935 | n/a | const char *errors) |
---|
936 | n/a | /*[clinic end generated code: output=b5e035182d33befc input=843a1d268e6dfa8e]*/ |
---|
937 | n/a | { |
---|
938 | n/a | return codec_tuple(_PyUnicode_AsASCIIString(str, errors), |
---|
939 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
940 | n/a | } |
---|
941 | n/a | |
---|
942 | n/a | /*[clinic input] |
---|
943 | n/a | _codecs.charmap_encode |
---|
944 | n/a | str: unicode |
---|
945 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
946 | n/a | mapping: object = NULL |
---|
947 | n/a | / |
---|
948 | n/a | [clinic start generated code]*/ |
---|
949 | n/a | |
---|
950 | n/a | static PyObject * |
---|
951 | n/a | _codecs_charmap_encode_impl(PyObject *module, PyObject *str, |
---|
952 | n/a | const char *errors, PyObject *mapping) |
---|
953 | n/a | /*[clinic end generated code: output=047476f48495a9e9 input=0752cde07a6d6d00]*/ |
---|
954 | n/a | { |
---|
955 | n/a | if (mapping == Py_None) |
---|
956 | n/a | mapping = NULL; |
---|
957 | n/a | |
---|
958 | n/a | return codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors), |
---|
959 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
960 | n/a | } |
---|
961 | n/a | |
---|
962 | n/a | /*[clinic input] |
---|
963 | n/a | _codecs.charmap_build |
---|
964 | n/a | map: unicode |
---|
965 | n/a | / |
---|
966 | n/a | [clinic start generated code]*/ |
---|
967 | n/a | |
---|
968 | n/a | static PyObject * |
---|
969 | n/a | _codecs_charmap_build_impl(PyObject *module, PyObject *map) |
---|
970 | n/a | /*[clinic end generated code: output=bb073c27031db9ac input=d91a91d1717dbc6d]*/ |
---|
971 | n/a | { |
---|
972 | n/a | return PyUnicode_BuildEncodingMap(map); |
---|
973 | n/a | } |
---|
974 | n/a | |
---|
975 | n/a | #ifdef MS_WINDOWS |
---|
976 | n/a | |
---|
977 | n/a | /*[clinic input] |
---|
978 | n/a | _codecs.mbcs_encode |
---|
979 | n/a | str: unicode |
---|
980 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
981 | n/a | / |
---|
982 | n/a | [clinic start generated code]*/ |
---|
983 | n/a | |
---|
984 | n/a | static PyObject * |
---|
985 | n/a | _codecs_mbcs_encode_impl(PyObject *module, PyObject *str, const char *errors) |
---|
986 | n/a | /*[clinic end generated code: output=76e2e170c966c080 input=de471e0815947553]*/ |
---|
987 | n/a | { |
---|
988 | n/a | return codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors), |
---|
989 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
990 | n/a | } |
---|
991 | n/a | |
---|
992 | n/a | /*[clinic input] |
---|
993 | n/a | _codecs.oem_encode |
---|
994 | n/a | str: unicode |
---|
995 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
996 | n/a | / |
---|
997 | n/a | [clinic start generated code]*/ |
---|
998 | n/a | |
---|
999 | n/a | static PyObject * |
---|
1000 | n/a | _codecs_oem_encode_impl(PyObject *module, PyObject *str, const char *errors) |
---|
1001 | n/a | /*[clinic end generated code: output=65d5982c737de649 input=3fc5f0028aad3cda]*/ |
---|
1002 | n/a | { |
---|
1003 | n/a | return codec_tuple(PyUnicode_EncodeCodePage(CP_OEMCP, str, errors), |
---|
1004 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
1005 | n/a | } |
---|
1006 | n/a | |
---|
1007 | n/a | /*[clinic input] |
---|
1008 | n/a | _codecs.code_page_encode |
---|
1009 | n/a | code_page: int |
---|
1010 | n/a | str: unicode |
---|
1011 | n/a | errors: str(accept={str, NoneType}) = NULL |
---|
1012 | n/a | / |
---|
1013 | n/a | [clinic start generated code]*/ |
---|
1014 | n/a | |
---|
1015 | n/a | static PyObject * |
---|
1016 | n/a | _codecs_code_page_encode_impl(PyObject *module, int code_page, PyObject *str, |
---|
1017 | n/a | const char *errors) |
---|
1018 | n/a | /*[clinic end generated code: output=45673f6085657a9e input=786421ae617d680b]*/ |
---|
1019 | n/a | { |
---|
1020 | n/a | return codec_tuple(PyUnicode_EncodeCodePage(code_page, str, errors), |
---|
1021 | n/a | PyUnicode_GET_LENGTH(str)); |
---|
1022 | n/a | } |
---|
1023 | n/a | |
---|
1024 | n/a | #endif /* MS_WINDOWS */ |
---|
1025 | n/a | |
---|
1026 | n/a | /* --- Error handler registry --------------------------------------------- */ |
---|
1027 | n/a | |
---|
1028 | n/a | /*[clinic input] |
---|
1029 | n/a | _codecs.register_error |
---|
1030 | n/a | errors: str |
---|
1031 | n/a | handler: object |
---|
1032 | n/a | / |
---|
1033 | n/a | |
---|
1034 | n/a | Register the specified error handler under the name errors. |
---|
1035 | n/a | |
---|
1036 | n/a | handler must be a callable object, that will be called with an exception |
---|
1037 | n/a | instance containing information about the location of the encoding/decoding |
---|
1038 | n/a | error and must return a (replacement, new position) tuple. |
---|
1039 | n/a | [clinic start generated code]*/ |
---|
1040 | n/a | |
---|
1041 | n/a | static PyObject * |
---|
1042 | n/a | _codecs_register_error_impl(PyObject *module, const char *errors, |
---|
1043 | n/a | PyObject *handler) |
---|
1044 | n/a | /*[clinic end generated code: output=fa2f7d1879b3067d input=5e6709203c2e33fe]*/ |
---|
1045 | n/a | { |
---|
1046 | n/a | if (PyCodec_RegisterError(errors, handler)) |
---|
1047 | n/a | return NULL; |
---|
1048 | n/a | Py_RETURN_NONE; |
---|
1049 | n/a | } |
---|
1050 | n/a | |
---|
1051 | n/a | /*[clinic input] |
---|
1052 | n/a | _codecs.lookup_error |
---|
1053 | n/a | name: str |
---|
1054 | n/a | / |
---|
1055 | n/a | |
---|
1056 | n/a | lookup_error(errors) -> handler |
---|
1057 | n/a | |
---|
1058 | n/a | Return the error handler for the specified error handling name or raise a |
---|
1059 | n/a | LookupError, if no handler exists under this name. |
---|
1060 | n/a | [clinic start generated code]*/ |
---|
1061 | n/a | |
---|
1062 | n/a | static PyObject * |
---|
1063 | n/a | _codecs_lookup_error_impl(PyObject *module, const char *name) |
---|
1064 | n/a | /*[clinic end generated code: output=087f05dc0c9a98cc input=4775dd65e6235aba]*/ |
---|
1065 | n/a | { |
---|
1066 | n/a | return PyCodec_LookupError(name); |
---|
1067 | n/a | } |
---|
1068 | n/a | |
---|
1069 | n/a | /* --- Module API --------------------------------------------------------- */ |
---|
1070 | n/a | |
---|
1071 | n/a | static PyMethodDef _codecs_functions[] = { |
---|
1072 | n/a | _CODECS_REGISTER_METHODDEF |
---|
1073 | n/a | _CODECS_LOOKUP_METHODDEF |
---|
1074 | n/a | _CODECS_ENCODE_METHODDEF |
---|
1075 | n/a | _CODECS_DECODE_METHODDEF |
---|
1076 | n/a | _CODECS_ESCAPE_ENCODE_METHODDEF |
---|
1077 | n/a | _CODECS_ESCAPE_DECODE_METHODDEF |
---|
1078 | n/a | _CODECS_UTF_8_ENCODE_METHODDEF |
---|
1079 | n/a | _CODECS_UTF_8_DECODE_METHODDEF |
---|
1080 | n/a | _CODECS_UTF_7_ENCODE_METHODDEF |
---|
1081 | n/a | _CODECS_UTF_7_DECODE_METHODDEF |
---|
1082 | n/a | _CODECS_UTF_16_ENCODE_METHODDEF |
---|
1083 | n/a | _CODECS_UTF_16_LE_ENCODE_METHODDEF |
---|
1084 | n/a | _CODECS_UTF_16_BE_ENCODE_METHODDEF |
---|
1085 | n/a | _CODECS_UTF_16_DECODE_METHODDEF |
---|
1086 | n/a | _CODECS_UTF_16_LE_DECODE_METHODDEF |
---|
1087 | n/a | _CODECS_UTF_16_BE_DECODE_METHODDEF |
---|
1088 | n/a | _CODECS_UTF_16_EX_DECODE_METHODDEF |
---|
1089 | n/a | _CODECS_UTF_32_ENCODE_METHODDEF |
---|
1090 | n/a | _CODECS_UTF_32_LE_ENCODE_METHODDEF |
---|
1091 | n/a | _CODECS_UTF_32_BE_ENCODE_METHODDEF |
---|
1092 | n/a | _CODECS_UTF_32_DECODE_METHODDEF |
---|
1093 | n/a | _CODECS_UTF_32_LE_DECODE_METHODDEF |
---|
1094 | n/a | _CODECS_UTF_32_BE_DECODE_METHODDEF |
---|
1095 | n/a | _CODECS_UTF_32_EX_DECODE_METHODDEF |
---|
1096 | n/a | _CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF |
---|
1097 | n/a | _CODECS_UNICODE_ESCAPE_DECODE_METHODDEF |
---|
1098 | n/a | _CODECS_UNICODE_INTERNAL_ENCODE_METHODDEF |
---|
1099 | n/a | _CODECS_UNICODE_INTERNAL_DECODE_METHODDEF |
---|
1100 | n/a | _CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF |
---|
1101 | n/a | _CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF |
---|
1102 | n/a | _CODECS_LATIN_1_ENCODE_METHODDEF |
---|
1103 | n/a | _CODECS_LATIN_1_DECODE_METHODDEF |
---|
1104 | n/a | _CODECS_ASCII_ENCODE_METHODDEF |
---|
1105 | n/a | _CODECS_ASCII_DECODE_METHODDEF |
---|
1106 | n/a | _CODECS_CHARMAP_ENCODE_METHODDEF |
---|
1107 | n/a | _CODECS_CHARMAP_DECODE_METHODDEF |
---|
1108 | n/a | _CODECS_CHARMAP_BUILD_METHODDEF |
---|
1109 | n/a | _CODECS_READBUFFER_ENCODE_METHODDEF |
---|
1110 | n/a | _CODECS_MBCS_ENCODE_METHODDEF |
---|
1111 | n/a | _CODECS_MBCS_DECODE_METHODDEF |
---|
1112 | n/a | _CODECS_OEM_ENCODE_METHODDEF |
---|
1113 | n/a | _CODECS_OEM_DECODE_METHODDEF |
---|
1114 | n/a | _CODECS_CODE_PAGE_ENCODE_METHODDEF |
---|
1115 | n/a | _CODECS_CODE_PAGE_DECODE_METHODDEF |
---|
1116 | n/a | _CODECS_REGISTER_ERROR_METHODDEF |
---|
1117 | n/a | _CODECS_LOOKUP_ERROR_METHODDEF |
---|
1118 | n/a | _CODECS__FORGET_CODEC_METHODDEF |
---|
1119 | n/a | {NULL, NULL} /* sentinel */ |
---|
1120 | n/a | }; |
---|
1121 | n/a | |
---|
1122 | n/a | static struct PyModuleDef codecsmodule = { |
---|
1123 | n/a | PyModuleDef_HEAD_INIT, |
---|
1124 | n/a | "_codecs", |
---|
1125 | n/a | NULL, |
---|
1126 | n/a | -1, |
---|
1127 | n/a | _codecs_functions, |
---|
1128 | n/a | NULL, |
---|
1129 | n/a | NULL, |
---|
1130 | n/a | NULL, |
---|
1131 | n/a | NULL |
---|
1132 | n/a | }; |
---|
1133 | n/a | |
---|
1134 | n/a | PyMODINIT_FUNC |
---|
1135 | n/a | PyInit__codecs(void) |
---|
1136 | n/a | { |
---|
1137 | n/a | return PyModule_Create(&codecsmodule); |
---|
1138 | n/a | } |
---|