Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code | Sign in
(17)

Side by Side Diff: Modules/_codecsmodule.c

Issue 20173: Derby #4: Convert 53 sites to Argument Clinic across 5 files
Patch Set: Created 4 years, 12 months ago
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments. Please Sign in to add in-line comments.
Jump to:
View unified diff | Download patch
« no previous file with comments | « Modules/clinic/_codecsmodule.c.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* ------------------------------------------------------------------------ 1 /* ------------------------------------------------------------------------
2 2
3 _codecs -- Provides access to the codec registry and the builtin 3 _codecs -- Provides access to the codec registry and the builtin
4 codecs. 4 codecs.
5 5
6 This module should never be imported directly. The standard library 6 This module should never be imported directly. The standard library
7 module "codecs" wraps this builtin module for use within Python. 7 module "codecs" wraps this builtin module for use within Python.
8 8
9 The codec registry is accessible via: 9 The codec registry is accessible via:
10 10
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 44
45 /*[clinic input] 45 /*[clinic input]
46 module _codecs 46 module _codecs
47 [clinic start generated code]*/ 47 [clinic start generated code]*/
48 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/ 48 /*[clinic end generated code: output=da39a3ee5e6b4b0d input=e1390e3da3cb9deb]*/
49 49
50 #include "clinic/_codecsmodule.c.h" 50 #include "clinic/_codecsmodule.c.h"
51 51
52 /* --- Registry ----------------------------------------------------------- */ 52 /* --- Registry ----------------------------------------------------------- */
53 53
54 PyDoc_STRVAR(register__doc__, 54 /*[clinic input]
55 "register(search_function)\n\ 55 _codecs.register
56 \n\ 56 search_function: object
57 Register a codec search function. Search functions are expected to take\n\ 57 /
58 one argument, the encoding name in all lower case letters, and either\n\
59 return None, or a tuple of functions (encoder, decoder, stream_reader,\n\
60 stream_writer) (or a CodecInfo object).");
61 58
62 static 59 Register a codec search function.
63 PyObject *codec_register(PyObject *self, PyObject *search_function) 60
61 Search functions are expected to take one argument, the encoding name in
62 all lower case letters, and either return None, or a tuple of functions
63 (encoder, decoder, stream_reader, stream_writer) (or a CodecInfo object).
64 [clinic start generated code]*/
65
66 static PyObject *
67 _codecs_register(PyModuleDef *module, PyObject *search_function)
68 /*[clinic end generated code: output=d17608b6ad380eb8 input=369578467955cae4]*/
64 { 69 {
65 if (PyCodec_Register(search_function)) 70 if (PyCodec_Register(search_function))
66 return NULL; 71 return NULL;
67 72
68 Py_RETURN_NONE; 73 Py_RETURN_NONE;
69 } 74 }
70 75
71 PyDoc_STRVAR(lookup__doc__, 76 /*[clinic input]
72 "lookup(encoding) -> CodecInfo\n\ 77 _codecs.lookup
73 \n\ 78 encoding: str
74 Looks up a codec tuple in the Python codec registry and returns\n\ 79 /
75 a CodecInfo object.");
76 80
77 static 81 Looks up a codec tuple in the Python codec registry and returns a CodecInfo obje ct.
78 PyObject *codec_lookup(PyObject *self, PyObject *args) 82 [clinic start generated code]*/
83
84 static PyObject *
85 _codecs_lookup_impl(PyModuleDef *module, const char *encoding)
86 /*[clinic end generated code: output=798e41aff0c04ef6 input=3c572c0db3febe9c]*/
79 { 87 {
80 char *encoding;
81
82 if (!PyArg_ParseTuple(args, "s:lookup", &encoding))
83 return NULL;
84
85 return _PyCodec_Lookup(encoding); 88 return _PyCodec_Lookup(encoding);
86 } 89 }
87 90
88 PyDoc_STRVAR(encode__doc__, 91 /*[clinic input]
89 "encode(obj, [encoding[,errors]]) -> object\n\ 92 _codecs.encode
90 \n\ 93 obj: object
91 Encodes obj using the codec registered for encoding. encoding defaults\n\ 94 encoding: str(c_default="NULL") = "utf-8"
92 to the default encoding. errors may be given to set a different error\n\ 95 errors: str(c_default="NULL") = "strict"
93 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ 96
94 a ValueError. Other possible values are 'ignore', 'replace' and\n\ 97 Encodes obj using the codec registered for encoding.
95 'xmlcharrefreplace' as well as any other name registered with\n\ 98
96 codecs.register_error that can handle ValueErrors."); 99 encoding defaults to the default encoding. errors may be given to set a
100 different error handling scheme. Default is 'strict' meaning that encoding
101 errors raise a ValueError. Other possible values are 'ignore', 'replace'
102 and 'xmlcharrefreplace' as well as any other name registered with
103 codecs.register_error that can handle ValueErrors.
104 [clinic start generated code]*/
97 105
98 static PyObject * 106 static PyObject *
99 codec_encode(PyObject *self, PyObject *args, PyObject *kwargs) 107 _codecs_encode_impl(PyModuleDef *module, PyObject *obj, const char *encoding, co nst char *errors)
108 /*[clinic end generated code: output=0662bb9f1c3506e5 input=cb1f62cf8d9e2a2c]*/
100 { 109 {
101 static char *kwlist[] = {"obj", "encoding", "errors", NULL};
102 const char *encoding = NULL;
103 const char *errors = NULL;
104 PyObject *v;
105
106 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:encode", kwlist,
107 &v, &encoding, &errors))
108 return NULL;
109
110 if (encoding == NULL) 110 if (encoding == NULL)
111 encoding = PyUnicode_GetDefaultEncoding(); 111 encoding = PyUnicode_GetDefaultEncoding();
112 112
113 /* Encode via the codec registry */ 113 /* Encode via the codec registry */
114 return PyCodec_Encode(v, encoding, errors); 114 return PyCodec_Encode(obj, encoding, errors);
115 } 115 }
116 116
117 PyDoc_STRVAR(decode__doc__, 117 /*[clinic input]
118 "decode(obj, [encoding[,errors]]) -> object\n\ 118 _codecs.decode
119 \n\ 119 obj: object
120 Decodes obj using the codec registered for encoding. encoding defaults\n\ 120 encoding: str(c_default="NULL") = "utf-8"
121 to the default encoding. errors may be given to set a different error\n\ 121 errors: str(c_default="NULL") = "strict"
122 handling scheme. Default is 'strict' meaning that encoding errors raise\n\ 122
123 a ValueError. Other possible values are 'ignore' and 'replace'\n\ 123 Decodes obj using the codec registered for encoding.
124 as well as any other name registered with codecs.register_error that is\n\ 124
125 able to handle ValueErrors."); 125 encoding defaults to the default encoding. errors may be given to set a
126 different error handling scheme. Default is 'strict' meaning that encoding
127 errors raise a ValueError. Other possible values are 'ignore' and 'replace'
128 as well as any other name registered with codecs.register_error that is able
129 to handle ValueErrors.
130 [clinic start generated code]*/
126 131
127 static PyObject * 132 static PyObject *
128 codec_decode(PyObject *self, PyObject *args, PyObject *kwargs) 133 _codecs_decode_impl(PyModuleDef *module, PyObject *obj, const char *encoding, co nst char *errors)
134 /*[clinic end generated code: output=ba495029b811ab57 input=6e095980ea74fd98]*/
129 { 135 {
130 static char *kwlist[] = {"obj", "encoding", "errors", NULL};
131 const char *encoding = NULL;
132 const char *errors = NULL;
133 PyObject *v;
134
135 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:decode", kwlist,
136 &v, &encoding, &errors))
137 return NULL;
138
139 if (encoding == NULL) 136 if (encoding == NULL)
140 encoding = PyUnicode_GetDefaultEncoding(); 137 encoding = PyUnicode_GetDefaultEncoding();
141 138
142 /* Decode via the codec registry */ 139 /* Decode via the codec registry */
143 return PyCodec_Decode(v, encoding, errors); 140 return PyCodec_Decode(obj, encoding, errors);
144 } 141 }
145 142
146 /* --- Helpers ------------------------------------------------------------ */ 143 /* --- Helpers ------------------------------------------------------------ */
147 144
148 /*[clinic input] 145 /*[clinic input]
149 _codecs._forget_codec 146 _codecs._forget_codec
150 147
151 encoding: str 148 encoding: str
152 / 149 /
153 150
154 Purge the named codec from the internal codec lookup cache 151 Purge the named codec from the internal codec lookup cache
155 [clinic start generated code]*/ 152 [clinic start generated code]*/
156 153
157 static PyObject * 154 static PyObject *
158 _codecs__forget_codec_impl(PyModuleDef *module, const char *encoding) 155 _codecs__forget_codec_impl(PyModuleDef *module, const char *encoding)
159 /*[clinic end generated code: output=b56a9b99d2d28080 input=18d5d92d0e386c38]*/ 156 /*[clinic end generated code: output=b56a9b99d2d28080 input=18d5d92d0e386c38]*/
160 { 157 {
161 if (_PyCodec_Forget(encoding) < 0) { 158 if (_PyCodec_Forget(encoding) < 0) {
162 return NULL; 159 return NULL;
163 }; 160 };
164 Py_RETURN_NONE; 161 Py_RETURN_NONE;
165 } 162 }
166 163
167 static 164 static
168 PyObject *codec_tuple(PyObject *unicode, 165 PyObject *codec_tuple(PyObject *decoded,
169 Py_ssize_t len) 166 Py_ssize_t len)
170 { 167 {
171 PyObject *v; 168 if (decoded == NULL)
172 if (unicode == NULL)
173 return NULL; 169 return NULL;
174 v = Py_BuildValue("On", unicode, len); 170 return Py_BuildValue("Nn", decoded, len);
175 Py_DECREF(unicode);
176 return v;
177 } 171 }
178 172
179 /* --- String codecs ------------------------------------------------------ */ 173 /* --- String codecs ------------------------------------------------------ */
174 /*[clinic input]
175 _codecs.escape_decode
176 data: Py_buffer(types='str bytes bytearray buffer')
177 errors: str(nullable=True) = NULL
178 /
179 [clinic start generated code]*/
180
180 static PyObject * 181 static PyObject *
181 escape_decode(PyObject *self, 182 _codecs_escape_decode_impl(PyModuleDef *module, Py_buffer *data, const char *err ors)
182 PyObject *args) 183 /*[clinic end generated code: output=300a12d7135db0d6 input=40afdd7eb681507a]*/
183 { 184 {
184 Py_buffer pbuf; 185 PyObject *decoded = PyBytes_DecodeEscape(data->buf, data->len,
185 const char *errors = NULL; 186 errors, 0, NULL);
186 PyObject *result; 187 return codec_tuple(decoded, data->len);
187
188 if (!PyArg_ParseTuple(args, "s*|z:escape_decode",
189 &pbuf, &errors))
190 return NULL;
191 result = codec_tuple(
192 PyBytes_DecodeEscape(pbuf.buf, pbuf.len, errors, 0, NULL),
193 pbuf.len);
194 PyBuffer_Release(&pbuf);
195 return result;
196 } 188 }
197 189
190 /*[clinic input]
191 _codecs.escape_encode
192 data: object(subclass_of='&PyBytes_Type')
193 errors: str(nullable=True) = NULL
194 /
195 [clinic start generated code]*/
196
198 static PyObject * 197 static PyObject *
199 escape_encode(PyObject *self, 198 _codecs_escape_encode_impl(PyModuleDef *module, PyObject *data, const char *erro rs)
200 PyObject *args) 199 /*[clinic end generated code: output=97ec47579ed9dd40 input=995ec13d87247ec7]*/
201 { 200 {
202 PyObject *str;
203 Py_ssize_t size; 201 Py_ssize_t size;
204 Py_ssize_t newsize; 202 Py_ssize_t newsize;
205 const char *errors = NULL;
206 PyObject *v; 203 PyObject *v;
207 204
208 if (!PyArg_ParseTuple(args, "O!|z:escape_encode", 205 size = PyBytes_GET_SIZE(data);
209 &PyBytes_Type, &str, &errors))
210 return NULL;
211
212 size = PyBytes_GET_SIZE(str);
213 if (size > PY_SSIZE_T_MAX / 4) { 206 if (size > PY_SSIZE_T_MAX / 4) {
214 PyErr_SetString(PyExc_OverflowError, 207 PyErr_SetString(PyExc_OverflowError,
215 "string is too large to encode"); 208 "string is too large to encode");
216 return NULL; 209 return NULL;
217 } 210 }
218 newsize = 4*size; 211 newsize = 4*size;
219 v = PyBytes_FromStringAndSize(NULL, newsize); 212 v = PyBytes_FromStringAndSize(NULL, newsize);
220 213
221 if (v == NULL) { 214 if (v == NULL) {
222 return NULL; 215 return NULL;
223 } 216 }
224 else { 217 else {
225 Py_ssize_t i; 218 Py_ssize_t i;
226 char c; 219 char c;
227 char *p = PyBytes_AS_STRING(v); 220 char *p = PyBytes_AS_STRING(v);
228 221
229 for (i = 0; i < size; i++) { 222 for (i = 0; i < size; i++) {
230 /* There's at least enough room for a hex escape */ 223 /* There's at least enough room for a hex escape */
231 assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4); 224 assert(newsize - (p - PyBytes_AS_STRING(v)) >= 4);
232 c = PyBytes_AS_STRING(str)[i]; 225 c = PyBytes_AS_STRING(data)[i];
233 if (c == '\'' || c == '\\') 226 if (c == '\'' || c == '\\')
234 *p++ = '\\', *p++ = c; 227 *p++ = '\\', *p++ = c;
235 else if (c == '\t') 228 else if (c == '\t')
236 *p++ = '\\', *p++ = 't'; 229 *p++ = '\\', *p++ = 't';
237 else if (c == '\n') 230 else if (c == '\n')
238 *p++ = '\\', *p++ = 'n'; 231 *p++ = '\\', *p++ = 'n';
239 else if (c == '\r') 232 else if (c == '\r')
240 *p++ = '\\', *p++ = 'r'; 233 *p++ = '\\', *p++ = 'r';
241 else if (c < ' ' || c >= 0x7f) { 234 else if (c < ' ' || c >= 0x7f) {
242 *p++ = '\\'; 235 *p++ = '\\';
243 *p++ = 'x'; 236 *p++ = 'x';
244 *p++ = Py_hexdigits[(c & 0xf0) >> 4]; 237 *p++ = Py_hexdigits[(c & 0xf0) >> 4];
245 *p++ = Py_hexdigits[c & 0xf]; 238 *p++ = Py_hexdigits[c & 0xf];
246 } 239 }
247 else 240 else
248 *p++ = c; 241 *p++ = c;
249 } 242 }
250 *p = '\0'; 243 *p = '\0';
251 if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) { 244 if (_PyBytes_Resize(&v, (p - PyBytes_AS_STRING(v)))) {
252 return NULL; 245 return NULL;
253 } 246 }
254 } 247 }
255 248
256 return codec_tuple(v, size); 249 return codec_tuple(v, size);
257 } 250 }
258 251
259 /* --- Decoder ------------------------------------------------------------ */ 252 /* --- Decoder ------------------------------------------------------------ */
253 /*[clinic input]
254 _codecs.unicode_internal_decode
255 obj: object
256 errors: str(nullable=True) = NULL
257 /
258 [clinic start generated code]*/
260 259
261 static PyObject * 260 static PyObject *
262 unicode_internal_decode(PyObject *self, 261 _codecs_unicode_internal_decode_impl(PyModuleDef *module, PyObject *obj, const c har *errors)
263 PyObject *args) 262 /*[clinic end generated code: output=bd707b3de38091d9 input=71d1d53c6ea4f0fc]*/
264 { 263 {
265 PyObject *obj;
266 const char *errors = NULL;
267
268 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode",
269 &obj, &errors))
270 return NULL;
271
272 if (PyUnicode_Check(obj)) { 264 if (PyUnicode_Check(obj)) {
273 if (PyUnicode_READY(obj) < 0) 265 if (PyUnicode_READY(obj) < 0)
274 return NULL; 266 return NULL;
275 Py_INCREF(obj); 267 Py_INCREF(obj);
276 return codec_tuple(obj, PyUnicode_GET_LENGTH(obj)); 268 return codec_tuple(obj, PyUnicode_GET_LENGTH(obj));
277 } 269 }
278 else { 270 else {
279 Py_buffer view; 271 Py_buffer view;
280 PyObject *result; 272 PyObject *result;
281 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) 273 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
282 return NULL; 274 return NULL;
283 275
284 result = codec_tuple( 276 result = codec_tuple(
285 _PyUnicode_DecodeUnicodeInternal(view.buf, view.len, errors), 277 _PyUnicode_DecodeUnicodeInternal(view.buf, view.len, errors),
286 view.len); 278 view.len);
287 PyBuffer_Release(&view); 279 PyBuffer_Release(&view);
288 return result; 280 return result;
289 } 281 }
290 } 282 }
291 283
284 /*[clinic input]
285 _codecs.utf_7_decode
286 data: Py_buffer
287 errors: str(nullable=True) = NULL
288 final: int(c_default="0") = False
289 /
290 [clinic start generated code]*/
291
292 static PyObject * 292 static PyObject *
293 utf_7_decode(PyObject *self, 293 _codecs_utf_7_decode_impl(PyModuleDef *module, Py_buffer *data, const char *erro rs, int final)
294 PyObject *args) 294 /*[clinic end generated code: output=43e1112766a1e987 input=8fd0241dcc559fe8]*/
295 { 295 {
296 Py_buffer pbuf; 296 Py_ssize_t consumed = data->len;
297 const char *errors = NULL; 297 PyObject *decoded = PyUnicode_DecodeUTF7Stateful(data->buf, data->len,
298 int final = 0; 298 errors,
299 Py_ssize_t consumed; 299 final ? NULL : &consumed);
300 PyObject *decoded = NULL;
301
302 if (!PyArg_ParseTuple(args, "y*|zi:utf_7_decode",
303 &pbuf, &errors, &final))
304 return NULL;
305 consumed = pbuf.len;
306
307 decoded = PyUnicode_DecodeUTF7Stateful(pbuf.buf, pbuf.len, errors,
308 final ? NULL : &consumed);
309 PyBuffer_Release(&pbuf);
310 if (decoded == NULL)
311 return NULL;
312 return codec_tuple(decoded, consumed); 300 return codec_tuple(decoded, consumed);
313 } 301 }
314 302
303 /*[clinic input]
304 _codecs.utf_8_decode
305 data: Py_buffer
306 errors: str(nullable=True) = NULL
307 final: int(c_default="0") = False
308 /
309 [clinic start generated code]*/
310
315 static PyObject * 311 static PyObject *
316 utf_8_decode(PyObject *self, 312 _codecs_utf_8_decode_impl(PyModuleDef *module, Py_buffer *data, const char *erro rs, int final)
317 PyObject *args) 313 /*[clinic end generated code: output=4c66b62c7ce685a7 input=709f25fbc3cd9a3e]*/
318 { 314 {
319 Py_buffer pbuf; 315 Py_ssize_t consumed = data->len;
320 const char *errors = NULL; 316 PyObject *decoded = PyUnicode_DecodeUTF8Stateful(data->buf, data->len,
321 int final = 0; 317 errors,
322 Py_ssize_t consumed; 318 final ? NULL : &consumed);
323 PyObject *decoded = NULL;
324
325 if (!PyArg_ParseTuple(args, "y*|zi:utf_8_decode",
326 &pbuf, &errors, &final))
327 return NULL;
328 consumed = pbuf.len;
329
330 decoded = PyUnicode_DecodeUTF8Stateful(pbuf.buf, pbuf.len, errors,
331 final ? NULL : &consumed);
332 PyBuffer_Release(&pbuf);
333 if (decoded == NULL)
334 return NULL;
335 return codec_tuple(decoded, consumed); 319 return codec_tuple(decoded, consumed);
336 } 320 }
337 321
322 /*[clinic input]
323 _codecs.utf_16_decode
324 data: Py_buffer
325 errors: str(nullable=True) = NULL
326 final: int(c_default="0") = False
327 /
328 [clinic start generated code]*/
329
338 static PyObject * 330 static PyObject *
339 utf_16_decode(PyObject *self, 331 _codecs_utf_16_decode_impl(PyModuleDef *module, Py_buffer *data, const char *err ors, int final)
340 PyObject *args) 332 /*[clinic end generated code: output=cb95e943c37604ed input=81678c8535585915]*/
341 { 333 {
342 Py_buffer pbuf;
343 const char *errors = NULL;
344 int byteorder = 0; 334 int byteorder = 0;
345 int final = 0; 335 /* This is overwritten unless final is true. */
346 Py_ssize_t consumed; 336 Py_ssize_t consumed = data->len;
347 PyObject *decoded; 337 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
348 338 errors, &byteorder,
349 if (!PyArg_ParseTuple(args, "y*|zi:utf_16_decode", 339 final ? NULL : &consumed);
350 &pbuf, &errors, &final))
351 return NULL;
352 consumed = pbuf.len; /* This is overwritten unless final is true. */
353 decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
354 &byteorder, final ? NULL : &consumed);
355 PyBuffer_Release(&pbuf);
356 if (decoded == NULL)
357 return NULL;
358 return codec_tuple(decoded, consumed); 340 return codec_tuple(decoded, consumed);
359 } 341 }
360 342
343 /*[clinic input]
344 _codecs.utf_16_le_decode
345 data: Py_buffer
346 errors: str(nullable=True) = NULL
347 final: int(c_default="0") = False
348 /
349 [clinic start generated code]*/
350
361 static PyObject * 351 static PyObject *
362 utf_16_le_decode(PyObject *self, 352 _codecs_utf_16_le_decode_impl(PyModuleDef *module, Py_buffer *data, const char * errors, int final)
363 PyObject *args) 353 /*[clinic end generated code: output=b8066ebafa08b9b8 input=006e53be083b6777]*/
364 { 354 {
365 Py_buffer pbuf;
366 const char *errors = NULL;
367 int byteorder = -1; 355 int byteorder = -1;
368 int final = 0; 356 /* This is overwritten unless final is true. */
369 Py_ssize_t consumed; 357 Py_ssize_t consumed = data->len;
370 PyObject *decoded = NULL; 358 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
371 359 errors, &byteorder,
372 if (!PyArg_ParseTuple(args, "y*|zi:utf_16_le_decode", 360 final ? NULL : &consumed);
373 &pbuf, &errors, &final))
374 return NULL;
375
376 consumed = pbuf.len; /* This is overwritten unless final is true. */
377 decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
378 &byteorder, final ? NULL : &consumed);
379 PyBuffer_Release(&pbuf);
380 if (decoded == NULL)
381 return NULL;
382 return codec_tuple(decoded, consumed); 361 return codec_tuple(decoded, consumed);
383 } 362 }
384 363
364 /*[clinic input]
365 _codecs.utf_16_be_decode
366 data: Py_buffer
367 errors: str(nullable=True) = NULL
368 final: int(c_default="0") = False
369 /
370 [clinic start generated code]*/
371
385 static PyObject * 372 static PyObject *
386 utf_16_be_decode(PyObject *self, 373 _codecs_utf_16_be_decode_impl(PyModuleDef *module, Py_buffer *data, const char * errors, int final)
387 PyObject *args) 374 /*[clinic end generated code: output=f685253b80a5555b input=d9490ad0f74140f3]*/
388 { 375 {
389 Py_buffer pbuf;
390 const char *errors = NULL;
391 int byteorder = 1; 376 int byteorder = 1;
392 int final = 0; 377 /* This is overwritten unless final is true. */
393 Py_ssize_t consumed; 378 Py_ssize_t consumed = data->len;
394 PyObject *decoded = NULL; 379 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
395 380 errors, &byteorder,
396 if (!PyArg_ParseTuple(args, "y*|zi:utf_16_be_decode", 381 final ? NULL : &consumed);
397 &pbuf, &errors, &final))
398 return NULL;
399
400 consumed = pbuf.len; /* This is overwritten unless final is true. */
401 decoded = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
402 &byteorder, final ? NULL : &consumed);
403 PyBuffer_Release(&pbuf);
404 if (decoded == NULL)
405 return NULL;
406 return codec_tuple(decoded, consumed); 382 return codec_tuple(decoded, consumed);
407 } 383 }
408 384
409 /* This non-standard version also provides access to the byteorder 385 /* This non-standard version also provides access to the byteorder
410 parameter of the builtin UTF-16 codec. 386 parameter of the builtin UTF-16 codec.
411 387
412 It returns a tuple (unicode, bytesread, byteorder) with byteorder 388 It returns a tuple (unicode, bytesread, byteorder) with byteorder
413 being the value in effect at the end of data. 389 being the value in effect at the end of data.
414 390
415 */ 391 */
392 /*[clinic input]
393 _codecs.utf_16_ex_decode
394 data: Py_buffer
395 errors: str(nullable=True) = NULL
396 byteorder: int = 0
397 final: int(c_default="0") = False
398 /
399 [clinic start generated code]*/
416 400
417 static PyObject * 401 static PyObject *
418 utf_16_ex_decode(PyObject *self, 402 _codecs_utf_16_ex_decode_impl(PyModuleDef *module, Py_buffer *data, const char * errors, int byteorder, int final)
419 PyObject *args) 403 /*[clinic end generated code: output=16c608e33b828a6c input=56a16c35944e4668]*/
420 { 404 {
421 Py_buffer pbuf; 405 /* This is overwritten unless final is true. */
422 const char *errors = NULL; 406 Py_ssize_t consumed = data->len;
423 int byteorder = 0;
424 PyObject *unicode, *tuple;
425 int final = 0;
426 Py_ssize_t consumed;
427 407
428 if (!PyArg_ParseTuple(args, "y*|zii:utf_16_ex_decode", 408 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
429 &pbuf, &errors, &byteorder, &final)) 409 errors, &byteorder,
410 final ? NULL : &consumed);
411 if (decoded == NULL)
430 return NULL; 412 return NULL;
431 consumed = pbuf.len; /* This is overwritten unless final is true. */ 413 return Py_BuildValue("Nni", decoded, consumed, byteorder);
432 unicode = PyUnicode_DecodeUTF16Stateful(pbuf.buf, pbuf.len, errors,
433 &byteorder, final ? NULL : &consumed);
434 PyBuffer_Release(&pbuf);
435 if (unicode == NULL)
436 return NULL;
437 tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
438 Py_DECREF(unicode);
439 return tuple;
440 } 414 }
441 415
416 /*[clinic input]
417 _codecs.utf_32_decode
418 data: Py_buffer
419 errors: str(nullable=True) = NULL
420 final: int(c_default="0") = False
421 /
422 [clinic start generated code]*/
423
442 static PyObject * 424 static PyObject *
443 utf_32_decode(PyObject *self, 425 _codecs_utf_32_decode_impl(PyModuleDef *module, Py_buffer *data, const char *err ors, int final)
444 PyObject *args) 426 /*[clinic end generated code: output=f0c809ec525c8d69 input=778f09d112c84716]*/
445 { 427 {
446 Py_buffer pbuf;
447 const char *errors = NULL;
448 int byteorder = 0; 428 int byteorder = 0;
449 int final = 0; 429 /* This is overwritten unless final is true. */
450 Py_ssize_t consumed; 430 Py_ssize_t consumed = data->len;
451 PyObject *decoded; 431 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
452 432 errors, &byteorder,
453 if (!PyArg_ParseTuple(args, "y*|zi:utf_32_decode", 433 final ? NULL : &consumed);
454 &pbuf, &errors, &final))
455 return NULL;
456 consumed = pbuf.len; /* This is overwritten unless final is true. */
457 decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
458 &byteorder, final ? NULL : &consumed);
459 PyBuffer_Release(&pbuf);
460 if (decoded == NULL)
461 return NULL;
462 return codec_tuple(decoded, consumed); 434 return codec_tuple(decoded, consumed);
463 } 435 }
464 436
437 /*[clinic input]
438 _codecs.utf_32_le_decode
439 data: Py_buffer
440 errors: str(nullable=True) = NULL
441 final: int(c_default="0") = False
442 /
443 [clinic start generated code]*/
444
465 static PyObject * 445 static PyObject *
466 utf_32_le_decode(PyObject *self, 446 _codecs_utf_32_le_decode_impl(PyModuleDef *module, Py_buffer *data, const char * errors, int final)
467 PyObject *args) 447 /*[clinic end generated code: output=405dd1513f8c8d0b input=7a398c6cadca231f]*/
468 { 448 {
469 Py_buffer pbuf;
470 const char *errors = NULL;
471 int byteorder = -1; 449 int byteorder = -1;
472 int final = 0; 450 /* This is overwritten unless final is true. */
473 Py_ssize_t consumed; 451 Py_ssize_t consumed = data->len;
474 PyObject *decoded; 452 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
475 453 errors, &byteorder,
476 if (!PyArg_ParseTuple(args, "y*|zi:utf_32_le_decode", 454 final ? NULL : &consumed);
477 &pbuf, &errors, &final))
478 return NULL;
479 consumed = pbuf.len; /* This is overwritten unless final is true. */
480 decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
481 &byteorder, final ? NULL : &consumed);
482 PyBuffer_Release(&pbuf);
483 if (decoded == NULL)
484 return NULL;
485 return codec_tuple(decoded, consumed); 455 return codec_tuple(decoded, consumed);
486 } 456 }
487 457
458 /*[clinic input]
459 _codecs.utf_32_be_decode
460 data: Py_buffer
461 errors: str(nullable=True) = NULL
462 final: int(c_default="0") = False
463 /
464 [clinic start generated code]*/
465
488 static PyObject * 466 static PyObject *
489 utf_32_be_decode(PyObject *self, 467 _codecs_utf_32_be_decode_impl(PyModuleDef *module, Py_buffer *data, const char * errors, int final)
490 PyObject *args) 468 /*[clinic end generated code: output=24febf529888e0b8 input=573fb3c3b8ac000d]*/
491 { 469 {
492 Py_buffer pbuf;
493 const char *errors = NULL;
494 int byteorder = 1; 470 int byteorder = 1;
495 int final = 0; 471 /* This is overwritten unless final is true. */
496 Py_ssize_t consumed; 472 Py_ssize_t consumed = data->len;
497 PyObject *decoded; 473 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
498 474 errors, &byteorder,
499 if (!PyArg_ParseTuple(args, "y*|zi:utf_32_be_decode", 475 final ? NULL : &consumed);
500 &pbuf, &errors, &final))
501 return NULL;
502 consumed = pbuf.len; /* This is overwritten unless final is true. */
503 decoded = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
504 &byteorder, final ? NULL : &consumed);
505 PyBuffer_Release(&pbuf);
506 if (decoded == NULL)
507 return NULL;
508 return codec_tuple(decoded, consumed); 476 return codec_tuple(decoded, consumed);
509 } 477 }
510 478
511 /* This non-standard version also provides access to the byteorder 479 /* This non-standard version also provides access to the byteorder
512 parameter of the builtin UTF-32 codec. 480 parameter of the builtin UTF-32 codec.
513 481
514 It returns a tuple (unicode, bytesread, byteorder) with byteorder 482 It returns a tuple (unicode, bytesread, byteorder) with byteorder
515 being the value in effect at the end of data. 483 being the value in effect at the end of data.
516 484
517 */ 485 */
486 /*[clinic input]
487 _codecs.utf_32_ex_decode
488 data: Py_buffer
489 errors: str(nullable=True) = NULL
490 byteorder: int = 0
491 final: int(c_default="0") = False
492 /
493 [clinic start generated code]*/
518 494
519 static PyObject * 495 static PyObject *
520 utf_32_ex_decode(PyObject *self, 496 _codecs_utf_32_ex_decode_impl(PyModuleDef *module, Py_buffer *data, const char * errors, int byteorder, int final)
521 PyObject *args) 497 /*[clinic end generated code: output=3aa867c23cbf1e72 input=56c2fd15274e69a0]*/
522 { 498 {
523 Py_buffer pbuf; 499 Py_ssize_t consumed = data->len;
524 const char *errors = NULL; 500 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
525 int byteorder = 0; 501 errors, &byteorder,
526 PyObject *unicode, *tuple; 502 final ? NULL : &consumed);
527 int final = 0; 503 if (decoded == NULL)
528 Py_ssize_t consumed;
529
530 if (!PyArg_ParseTuple(args, "y*|zii:utf_32_ex_decode",
531 &pbuf, &errors, &byteorder, &final))
532 return NULL; 504 return NULL;
533 consumed = pbuf.len; /* This is overwritten unless final is true. */ 505 return Py_BuildValue("Nni", decoded, consumed, byteorder);
534 unicode = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors,
535 &byteorder, final ? NULL : &consumed);
536 PyBuffer_Release(&pbuf);
537 if (unicode == NULL)
538 return NULL;
539 tuple = Py_BuildValue("Oni", unicode, consumed, byteorder);
540 Py_DECREF(unicode);
541 return tuple;
542 } 506 }
543 507
508 /*[clinic input]
509 _codecs.unicode_escape_decode
510 data: Py_buffer(types='str bytes bytearray buffer')
511 errors: str(nullable=True) = NULL
512 /
513 [clinic start generated code]*/
514
544 static PyObject * 515 static PyObject *
545 unicode_escape_decode(PyObject *self, 516 _codecs_unicode_escape_decode_impl(PyModuleDef *module, Py_buffer *data, const c har *errors)
546 PyObject *args) 517 /*[clinic end generated code: output=84def09dc1178ab4 input=e6cf9b15356033bf]*/
547 { 518 {
548 Py_buffer pbuf; 519 PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len,
549 const char *errors = NULL; 520 errors);
550 PyObject *unicode; 521 return codec_tuple(decoded, data->len);
551
552 if (!PyArg_ParseTuple(args, "s*|z:unicode_escape_decode",
553 &pbuf, &errors))
554 return NULL;
555
556 unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors);
557 PyBuffer_Release(&pbuf);
558 return codec_tuple(unicode, pbuf.len);
559 } 522 }
560 523
524 /*[clinic input]
525 _codecs.raw_unicode_escape_decode
526 data: Py_buffer(types='str bytes bytearray buffer')
527 errors: str(nullable=True) = NULL
528 /
529 [clinic start generated code]*/
530
561 static PyObject * 531 static PyObject *
562 raw_unicode_escape_decode(PyObject *self, 532 _codecs_raw_unicode_escape_decode_impl(PyModuleDef *module, Py_buffer *data, con st char *errors)
563 PyObject *args) 533 /*[clinic end generated code: output=ea5de256e10fad48 input=8ac951eae7f7f989]*/
564 { 534 {
565 Py_buffer pbuf; 535 PyObject *decoded = PyUnicode_DecodeRawUnicodeEscape(data->buf, data->len,
566 const char *errors = NULL; 536 errors);
567 PyObject *unicode; 537 return codec_tuple(decoded, data->len);
568
569 if (!PyArg_ParseTuple(args, "s*|z:raw_unicode_escape_decode",
570 &pbuf, &errors))
571 return NULL;
572
573 unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors);
574 PyBuffer_Release(&pbuf);
575 return codec_tuple(unicode, pbuf.len);
576 } 538 }
577 539
540 /*[clinic input]
541 _codecs.latin_1_decode
542 data: Py_buffer
543 errors: str(nullable=True) = NULL
544 /
545 [clinic start generated code]*/
546
578 static PyObject * 547 static PyObject *
579 latin_1_decode(PyObject *self, 548 _codecs_latin_1_decode_impl(PyModuleDef *module, Py_buffer *data, const char *er rors)
580 PyObject *args) 549 /*[clinic end generated code: output=9f460c6755979903 input=2ad8439a94444119]*/
581 { 550 {
582 Py_buffer pbuf; 551 PyObject *decoded = PyUnicode_DecodeLatin1(data->buf, data->len, errors);
583 PyObject *unicode; 552 return codec_tuple(decoded, data->len);
584 const char *errors = NULL;
585
586 if (!PyArg_ParseTuple(args, "y*|z:latin_1_decode",
587 &pbuf, &errors))
588 return NULL;
589
590 unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors);
591 PyBuffer_Release(&pbuf);
592 return codec_tuple(unicode, pbuf.len);
593 } 553 }
594 554
555 /*[clinic input]
556 _codecs.ascii_decode
557 data: Py_buffer
558 errors: str(nullable=True) = NULL
559 /
560 [clinic start generated code]*/
561
595 static PyObject * 562 static PyObject *
596 ascii_decode(PyObject *self, 563 _codecs_ascii_decode_impl(PyModuleDef *module, Py_buffer *data, const char *erro rs)
597 PyObject *args) 564 /*[clinic end generated code: output=e57a04a8833400cc input=0bfd2dd60e3e0152]*/
598 { 565 {
599 Py_buffer pbuf; 566 PyObject *decoded = PyUnicode_DecodeASCII(data->buf, data->len, errors);
600 PyObject *unicode; 567 return codec_tuple(decoded, data->len);
601 const char *errors = NULL;
602
603 if (!PyArg_ParseTuple(args, "y*|z:ascii_decode",
604 &pbuf, &errors))
605 return NULL;
606
607 unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors);
608 PyBuffer_Release(&pbuf);
609 return codec_tuple(unicode, pbuf.len);
610 } 568 }
611 569
570 /*[clinic input]
571 _codecs.charmap_decode
572 data: Py_buffer
573 errors: str(nullable=True) = NULL
574 mapping: object = NULL
575 /
576 [clinic start generated code]*/
577
612 static PyObject * 578 static PyObject *
613 charmap_decode(PyObject *self, 579 _codecs_charmap_decode_impl(PyModuleDef *module, Py_buffer *data, const char *er rors, PyObject *mapping)
614 PyObject *args) 580 /*[clinic end generated code: output=2a3b7be5ed83a5d8 input=bf5ab0388adeac8e]*/
615 { 581 {
616 Py_buffer pbuf; 582 PyObject *decoded;
617 PyObject *unicode;
618 const char *errors = NULL;
619 PyObject *mapping = NULL;
620 583
621 if (!PyArg_ParseTuple(args, "y*|zO:charmap_decode",
622 &pbuf, &errors, &mapping))
623 return NULL;
624 if (mapping == Py_None) 584 if (mapping == Py_None)
625 mapping = NULL; 585 mapping = NULL;
626 586
627 unicode = PyUnicode_DecodeCharmap(pbuf.buf, pbuf.len, mapping, errors); 587 decoded = PyUnicode_DecodeCharmap(data->buf, data->len, mapping, errors);
628 PyBuffer_Release(&pbuf); 588 return codec_tuple(decoded, data->len);
629 return codec_tuple(unicode, pbuf.len);
630 } 589 }
631 590
632 #ifdef HAVE_MBCS 591 #ifdef HAVE_MBCS
633 592
593 /*[clinic input]
594 _codecs.mbcs_decode
595 data: Py_buffer
596 errors: str(nullable=True) = NULL
597 final: int(c_default="0") = False
598 /
599 [clinic start generated code]*/
600
634 static PyObject * 601 static PyObject *
635 mbcs_decode(PyObject *self, 602 _codecs_mbcs_decode_impl(PyModuleDef *module, Py_buffer *data, const char *error s, int final)
636 PyObject *args) 603 /*[clinic end generated code: output=aeb7d16803a466ce input=48841d508b7bacee]*/
637 { 604 {
638 Py_buffer pbuf; 605 Py_ssize_t consumed = data->len;
639 const char *errors = NULL; 606 PyObject *decoded = PyUnicode_DecodeMBCSStateful(data->buf, data->len,
640 int final = 0; 607 errors, final ? NULL : &consumed);
641 Py_ssize_t consumed;
642 PyObject *decoded = NULL;
643
644 if (!PyArg_ParseTuple(args, "y*|zi:mbcs_decode",
645 &pbuf, &errors, &final))
646 return NULL;
647 consumed = pbuf.len;
648
649 decoded = PyUnicode_DecodeMBCSStateful(pbuf.buf, pbuf.len, errors,
650 final ? NULL : &consumed);
651 PyBuffer_Release(&pbuf);
652 if (decoded == NULL)
653 return NULL;
654 return codec_tuple(decoded, consumed); 608 return codec_tuple(decoded, consumed);
655 } 609 }
656 610
611 /*[clinic input]
612 _codecs.code_page_decode
613 codepage: int
614 data: Py_buffer
615 errors: str(nullable=True) = NULL
616 final: int(c_default="0") = False
617 /
618 [clinic start generated code]*/
619
657 static PyObject * 620 static PyObject *
658 code_page_decode(PyObject *self, 621 _codecs_code_page_decode_impl(PyModuleDef *module, int codepage, Py_buffer *data , const char *errors, int final)
659 PyObject *args) 622 /*[clinic end generated code: output=419f9cef0a3bb271 input=74f589867f3429ce]*/
660 { 623 {
661 Py_buffer pbuf; 624 Py_ssize_t consumed = data->len;
662 const char *errors = NULL; 625 PyObject *decoded = PyUnicode_DecodeCodePageStateful(code_page,
663 int final = 0; 626 data->buf, data->len,
664 Py_ssize_t consumed; 627 errors,
665 PyObject *decoded = NULL; 628 final ? NULL : &consume d);
666 int code_page;
667
668 if (!PyArg_ParseTuple(args, "iy*|zi:code_page_decode",
669 &code_page, &pbuf, &errors, &final))
670 return NULL;
671 consumed = pbuf.len;
672
673 decoded = PyUnicode_DecodeCodePageStateful(code_page,
674 pbuf.buf, pbuf.len, errors,
675 final ? NULL : &consumed);
676 PyBuffer_Release(&pbuf);
677 if (decoded == NULL)
678 return NULL;
679 return codec_tuple(decoded, consumed); 629 return codec_tuple(decoded, consumed);
680 } 630 }
681 631
682 #endif /* HAVE_MBCS */ 632 #endif /* HAVE_MBCS */
683 633
684 /* --- Encoder ------------------------------------------------------------ */ 634 /* --- Encoder ------------------------------------------------------------ */
685 635
636 /*[clinic input]
637 _codecs.readbuffer_encode
638 data: Py_buffer(types='str bytes bytearray buffer')
639 errors: str(nullable=True) = NULL
640 /
641 [clinic start generated code]*/
642
686 static PyObject * 643 static PyObject *
687 readbuffer_encode(PyObject *self, 644 _codecs_readbuffer_encode_impl(PyModuleDef *module, Py_buffer *data, const char *errors)
688 PyObject *args) 645 /*[clinic end generated code: output=add770b377370b6e input=2ab3d649599cec57]*/
689 { 646 {
690 Py_buffer pdata; 647 PyObject *result = PyBytes_FromStringAndSize(data->buf, data->len);
691 const char *data; 648 return codec_tuple(result, data->len);
692 Py_ssize_t size;
693 const char *errors = NULL;
694 PyObject *result;
695
696 if (!PyArg_ParseTuple(args, "s*|z:readbuffer_encode",
697 &pdata, &errors))
698 return NULL;
699 data = pdata.buf;
700 size = pdata.len;
701
702 result = PyBytes_FromStringAndSize(data, size);
703 PyBuffer_Release(&pdata);
704 return codec_tuple(result, size);
705 } 649 }
706 650
651 /*[clinic input]
652 _codecs.unicode_internal_encode
653 obj: object
654 errors: str(nullable=True) = NULL
655 /
656 [clinic start generated code]*/
657
707 static PyObject * 658 static PyObject *
708 unicode_internal_encode(PyObject *self, 659 _codecs_unicode_internal_encode_impl(PyModuleDef *module, PyObject *obj, const c har *errors)
709 PyObject *args) 660 /*[clinic end generated code: output=880a7415536bff52 input=61192dad1fce371f]*/
710 { 661 {
711 PyObject *obj;
712 const char *errors = NULL;
713
714 if (PyErr_WarnEx(PyExc_DeprecationWarning, 662 if (PyErr_WarnEx(PyExc_DeprecationWarning,
715 "unicode_internal codec has been deprecated", 663 "unicode_internal codec has been deprecated",
716 1)) 664 1))
717 return NULL; 665 return NULL;
718 666
719 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
720 &obj, &errors))
721 return NULL;
722
723 if (PyUnicode_Check(obj)) { 667 if (PyUnicode_Check(obj)) {
724 Py_UNICODE *u; 668 Py_UNICODE *u;
725 Py_ssize_t len, size; 669 Py_ssize_t len, size;
726 670
727 if (PyUnicode_READY(obj) < 0) 671 if (PyUnicode_READY(obj) < 0)
728 return NULL; 672 return NULL;
729 673
730 u = PyUnicode_AsUnicodeAndSize(obj, &len); 674 u = PyUnicode_AsUnicodeAndSize(obj, &len);
731 if (u == NULL) 675 if (u == NULL)
732 return NULL; 676 return NULL;
733 if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE)) 677 if ((size_t)len > (size_t)PY_SSIZE_T_MAX / sizeof(Py_UNICODE))
734 return PyErr_NoMemory(); 678 return PyErr_NoMemory();
735 size = len * sizeof(Py_UNICODE); 679 size = len * sizeof(Py_UNICODE);
736 return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size), 680 return codec_tuple(PyBytes_FromStringAndSize((const char*)u, size),
737 PyUnicode_GET_LENGTH(obj)); 681 PyUnicode_GET_LENGTH(obj));
738 } 682 }
739 else { 683 else {
740 Py_buffer view; 684 Py_buffer view;
741 PyObject *result; 685 PyObject *result;
742 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0) 686 if (PyObject_GetBuffer(obj, &view, PyBUF_SIMPLE) != 0)
743 return NULL; 687 return NULL;
744 result = codec_tuple(PyBytes_FromStringAndSize(view.buf, view.len), view .len); 688 result = codec_tuple(PyBytes_FromStringAndSize(view.buf, view.len),
689 view.len);
745 PyBuffer_Release(&view); 690 PyBuffer_Release(&view);
746 return result; 691 return result;
747 } 692 }
748 } 693 }
749 694
695 /*[clinic input]
696 _codecs.utf_7_encode
697 str: object
698 errors: str(nullable=True) = NULL
699 /
700 [clinic start generated code]*/
701
750 static PyObject * 702 static PyObject *
751 utf_7_encode(PyObject *self, 703 _codecs_utf_7_encode_impl(PyModuleDef *module, PyObject *str, const char *errors )
752 PyObject *args) 704 /*[clinic end generated code: output=061f316ae18c1427 input=802ba3f495219bbc]*/
753 { 705 {
754 PyObject *str, *v; 706 PyObject *v;
755 const char *errors = NULL;
756
757 if (!PyArg_ParseTuple(args, "O|z:utf_7_encode",
758 &str, &errors))
759 return NULL;
760 707
761 str = PyUnicode_FromObject(str); 708 str = PyUnicode_FromObject(str);
762 if (str == NULL || PyUnicode_READY(str) < 0) { 709 if (str == NULL || PyUnicode_READY(str) < 0) {
763 Py_XDECREF(str); 710 Py_XDECREF(str);
764 return NULL; 711 return NULL;
765 } 712 }
766 v = codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors), 713 v = codec_tuple(_PyUnicode_EncodeUTF7(str, 0, 0, errors),
767 PyUnicode_GET_LENGTH(str)); 714 PyUnicode_GET_LENGTH(str));
768 Py_DECREF(str); 715 Py_DECREF(str);
769 return v; 716 return v;
770 } 717 }
771 718
719 /*[clinic input]
720 _codecs.utf_8_encode
721 str: object
722 errors: str(nullable=True) = NULL
723 /
724 [clinic start generated code]*/
725
772 static PyObject * 726 static PyObject *
773 utf_8_encode(PyObject *self, 727 _codecs_utf_8_encode_impl(PyModuleDef *module, PyObject *str, const char *errors )
774 PyObject *args) 728 /*[clinic end generated code: output=0842e04321f57d0f input=5c283859a2a90bf7]*/
775 { 729 {
776 PyObject *str, *v; 730 PyObject *v;
777 const char *errors = NULL;
778
779 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode",
780 &str, &errors))
781 return NULL;
782 731
783 str = PyUnicode_FromObject(str); 732 str = PyUnicode_FromObject(str);
784 if (str == NULL || PyUnicode_READY(str) < 0) { 733 if (str == NULL || PyUnicode_READY(str) < 0) {
785 Py_XDECREF(str); 734 Py_XDECREF(str);
786 return NULL; 735 return NULL;
787 } 736 }
788 v = codec_tuple(PyUnicode_AsEncodedString(str, "utf-8", errors), 737 v = codec_tuple(PyUnicode_AsEncodedString(str, "utf-8", errors),
789 PyUnicode_GET_LENGTH(str)); 738 PyUnicode_GET_LENGTH(str));
790 Py_DECREF(str); 739 Py_DECREF(str);
791 return v; 740 return v;
792 } 741 }
793 742
794 /* This version provides access to the byteorder parameter of the 743 /* This version provides access to the byteorder parameter of the
795 builtin UTF-16 codecs as optional third argument. It defaults to 0 744 builtin UTF-16 codecs as optional third argument. It defaults to 0
796 which means: use the native byte order and prepend the data with a 745 which means: use the native byte order and prepend the data with a
797 BOM mark. 746 BOM mark.
798 747
799 */ 748 */
800 749
750 /*[clinic input]
751 _codecs.utf_16_encode
752 str: object
753 errors: str(nullable=True) = NULL
754 byteorder: int = 0
755 /
756 [clinic start generated code]*/
757
801 static PyObject * 758 static PyObject *
802 utf_16_encode(PyObject *self, 759 _codecs_utf_16_encode_impl(PyModuleDef *module, PyObject *str, const char *error s, int byteorder)
803 PyObject *args) 760 /*[clinic end generated code: output=a5bb3352c1825683 input=ed850025591019db]*/
804 { 761 {
805 PyObject *str, *v; 762 PyObject *v;
806 const char *errors = NULL;
807 int byteorder = 0;
808
809 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode",
810 &str, &errors, &byteorder))
811 return NULL;
812 763
813 str = PyUnicode_FromObject(str); 764 str = PyUnicode_FromObject(str);
814 if (str == NULL || PyUnicode_READY(str) < 0) { 765 if (str == NULL || PyUnicode_READY(str) < 0) {
815 Py_XDECREF(str); 766 Py_XDECREF(str);
816 return NULL; 767 return NULL;
817 } 768 }
818 v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder), 769 v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, byteorder),
819 PyUnicode_GET_LENGTH(str)); 770 PyUnicode_GET_LENGTH(str));
820 Py_DECREF(str); 771 Py_DECREF(str);
821 return v; 772 return v;
822 } 773 }
823 774
775 /*[clinic input]
776 _codecs.utf_16_le_encode
777 str: object
778 errors: str(nullable=True) = NULL
779 /
780 [clinic start generated code]*/
781
824 static PyObject * 782 static PyObject *
825 utf_16_le_encode(PyObject *self, 783 _codecs_utf_16_le_encode_impl(PyModuleDef *module, PyObject *str, const char *er rors)
826 PyObject *args) 784 /*[clinic end generated code: output=0c526b4d37936db1 input=950fcf919f117cb7]*/
827 { 785 {
828 PyObject *str, *v; 786 PyObject *v;
829 const char *errors = NULL;
830
831 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode",
832 &str, &errors))
833 return NULL;
834 787
835 str = PyUnicode_FromObject(str); 788 str = PyUnicode_FromObject(str);
836 if (str == NULL || PyUnicode_READY(str) < 0) { 789 if (str == NULL || PyUnicode_READY(str) < 0) {
837 Py_XDECREF(str); 790 Py_XDECREF(str);
838 return NULL; 791 return NULL;
839 } 792 }
840 v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1), 793 v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, -1),
841 PyUnicode_GET_LENGTH(str)); 794 PyUnicode_GET_LENGTH(str));
842 Py_DECREF(str); 795 Py_DECREF(str);
843 return v; 796 return v;
844 } 797 }
845 798
799 /*[clinic input]
800 _codecs.utf_16_be_encode
801 str: object
802 errors: str(nullable=True) = NULL
803 /
804 [clinic start generated code]*/
805
846 static PyObject * 806 static PyObject *
847 utf_16_be_encode(PyObject *self, 807 _codecs_utf_16_be_encode_impl(PyModuleDef *module, PyObject *str, const char *er rors)
848 PyObject *args) 808 /*[clinic end generated code: output=817e6bba3d9f99fe input=65a55a23d2a1e18d]*/
849 { 809 {
850 PyObject *str, *v; 810 PyObject *v;
851 const char *errors = NULL;
852
853 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode",
854 &str, &errors))
855 return NULL;
856 811
857 str = PyUnicode_FromObject(str); 812 str = PyUnicode_FromObject(str);
858 if (str == NULL || PyUnicode_READY(str) < 0) { 813 if (str == NULL || PyUnicode_READY(str) < 0) {
859 Py_XDECREF(str); 814 Py_XDECREF(str);
860 return NULL; 815 return NULL;
861 } 816 }
862 v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1), 817 v = codec_tuple(_PyUnicode_EncodeUTF16(str, errors, +1),
863 PyUnicode_GET_LENGTH(str)); 818 PyUnicode_GET_LENGTH(str));
864 Py_DECREF(str); 819 Py_DECREF(str);
865 return v; 820 return v;
866 } 821 }
867 822
868 /* This version provides access to the byteorder parameter of the 823 /* This version provides access to the byteorder parameter of the
869 builtin UTF-32 codecs as optional third argument. It defaults to 0 824 builtin UTF-32 codecs as optional third argument. It defaults to 0
870 which means: use the native byte order and prepend the data with a 825 which means: use the native byte order and prepend the data with a
871 BOM mark. 826 BOM mark.
872 827
873 */ 828 */
874 829
830 /*[clinic input]
831 _codecs.utf_32_encode
832 str: object
833 errors: str(nullable=True) = NULL
834 byteorder: int = 0
835 /
836 [clinic start generated code]*/
837
875 static PyObject * 838 static PyObject *
876 utf_32_encode(PyObject *self, 839 _codecs_utf_32_encode_impl(PyModuleDef *module, PyObject *str, const char *error s, int byteorder)
877 PyObject *args) 840 /*[clinic end generated code: output=2ed436c6159df625 input=452ef1b8f0b8c3d5]*/
878 { 841 {
879 PyObject *str, *v; 842 PyObject *v;
880 const char *errors = NULL;
881 int byteorder = 0;
882
883 if (!PyArg_ParseTuple(args, "O|zi:utf_32_encode",
884 &str, &errors, &byteorder))
885 return NULL;
886 843
887 str = PyUnicode_FromObject(str); 844 str = PyUnicode_FromObject(str);
888 if (str == NULL || PyUnicode_READY(str) < 0) { 845 if (str == NULL || PyUnicode_READY(str) < 0) {
889 Py_XDECREF(str); 846 Py_XDECREF(str);
890 return NULL; 847 return NULL;
891 } 848 }
892 v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder), 849 v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, byteorder),
893 PyUnicode_GET_LENGTH(str)); 850 PyUnicode_GET_LENGTH(str));
894 Py_DECREF(str); 851 Py_DECREF(str);
895 return v; 852 return v;
896 } 853 }
897 854
855 /*[clinic input]
856 _codecs.utf_32_le_encode
857 str: object
858 errors: str(nullable=True) = NULL
859 /
860 [clinic start generated code]*/
861
898 static PyObject * 862 static PyObject *
899 utf_32_le_encode(PyObject *self, 863 _codecs_utf_32_le_encode_impl(PyModuleDef *module, PyObject *str, const char *er rors)
900 PyObject *args) 864 /*[clinic end generated code: output=f1de99147eb63a8b input=a8e2cc9b4dbf40d9]*/
901 { 865 {
902 PyObject *str, *v; 866 PyObject *v;
903 const char *errors = NULL;
904
905 if (!PyArg_ParseTuple(args, "O|z:utf_32_le_encode",
906 &str, &errors))
907 return NULL;
908 867
909 str = PyUnicode_FromObject(str); 868 str = PyUnicode_FromObject(str);
910 if (str == NULL || PyUnicode_READY(str) < 0) { 869 if (str == NULL || PyUnicode_READY(str) < 0) {
911 Py_XDECREF(str); 870 Py_XDECREF(str);
912 return NULL; 871 return NULL;
913 } 872 }
914 v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1), 873 v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, -1),
915 PyUnicode_GET_LENGTH(str)); 874 PyUnicode_GET_LENGTH(str));
916 Py_DECREF(str); 875 Py_DECREF(str);
917 return v; 876 return v;
918 } 877 }
919 878
879 /*[clinic input]
880 _codecs.utf_32_be_encode
881 str: object
882 errors: str(nullable=True) = NULL
883 /
884 [clinic start generated code]*/
885
920 static PyObject * 886 static PyObject *
921 utf_32_be_encode(PyObject *self, 887 _codecs_utf_32_be_encode_impl(PyModuleDef *module, PyObject *str, const char *er rors)
922 PyObject *args) 888 /*[clinic end generated code: output=a8a3276bc70f5543 input=cef51fb9de6a04b3]*/
923 { 889 {
924 PyObject *str, *v; 890 PyObject *v;
925 const char *errors = NULL;
926
927 if (!PyArg_ParseTuple(args, "O|z:utf_32_be_encode",
928 &str, &errors))
929 return NULL;
930 891
931 str = PyUnicode_FromObject(str); 892 str = PyUnicode_FromObject(str);
932 if (str == NULL || PyUnicode_READY(str) < 0) { 893 if (str == NULL || PyUnicode_READY(str) < 0) {
933 Py_XDECREF(str); 894 Py_XDECREF(str);
934 return NULL; 895 return NULL;
935 } 896 }
936 v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1), 897 v = codec_tuple(_PyUnicode_EncodeUTF32(str, errors, +1),
937 PyUnicode_GET_LENGTH(str)); 898 PyUnicode_GET_LENGTH(str));
938 Py_DECREF(str); 899 Py_DECREF(str);
939 return v; 900 return v;
940 } 901 }
941 902
903 /*[clinic input]
904 _codecs.unicode_escape_encode
905 str: object
906 errors: str(nullable=True) = NULL
907 /
908 [clinic start generated code]*/
909
942 static PyObject * 910 static PyObject *
943 unicode_escape_encode(PyObject *self, 911 _codecs_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str, const cha r *errors)
944 PyObject *args) 912 /*[clinic end generated code: output=b966c98c8f66f373 input=a9b4d66329b51e5b]*/
945 { 913 {
946 PyObject *str, *v; 914 PyObject *v;
947 const char *errors = NULL;
948
949 if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode",
950 &str, &errors))
951 return NULL;
952 915
953 str = PyUnicode_FromObject(str); 916 str = PyUnicode_FromObject(str);
954 if (str == NULL || PyUnicode_READY(str) < 0) { 917 if (str == NULL || PyUnicode_READY(str) < 0) {
955 Py_XDECREF(str); 918 Py_XDECREF(str);
956 return NULL; 919 return NULL;
957 } 920 }
958 v = codec_tuple(PyUnicode_AsUnicodeEscapeString(str), 921 v = codec_tuple(PyUnicode_AsUnicodeEscapeString(str),
959 PyUnicode_GET_LENGTH(str)); 922 PyUnicode_GET_LENGTH(str));
960 Py_DECREF(str); 923 Py_DECREF(str);
961 return v; 924 return v;
962 } 925 }
963 926
927 /*[clinic input]
928 _codecs.raw_unicode_escape_encode
929 str: object
930 errors: str(nullable=True) = NULL
931 /
932 [clinic start generated code]*/
933
964 static PyObject * 934 static PyObject *
965 raw_unicode_escape_encode(PyObject *self, 935 _codecs_raw_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str, const char *errors)
966 PyObject *args) 936 /*[clinic end generated code: output=bd93a964bfc0066c input=022a856c6bbac275]*/
967 { 937 {
968 PyObject *str, *v; 938 PyObject *v;
969 const char *errors = NULL;
970
971 if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode",
972 &str, &errors))
973 return NULL;
974 939
975 str = PyUnicode_FromObject(str); 940 str = PyUnicode_FromObject(str);
976 if (str == NULL || PyUnicode_READY(str) < 0) { 941 if (str == NULL || PyUnicode_READY(str) < 0) {
977 Py_XDECREF(str); 942 Py_XDECREF(str);
978 return NULL; 943 return NULL;
979 } 944 }
980 v = codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str), 945 v = codec_tuple(PyUnicode_AsRawUnicodeEscapeString(str),
981 PyUnicode_GET_LENGTH(str)); 946 PyUnicode_GET_LENGTH(str));
982 Py_DECREF(str); 947 Py_DECREF(str);
983 return v; 948 return v;
984 } 949 }
985 950
951 /*[clinic input]
952 _codecs.latin_1_encode
953 str: object
954 errors: str(nullable=True) = NULL
955 /
956 [clinic start generated code]*/
957
986 static PyObject * 958 static PyObject *
987 latin_1_encode(PyObject *self, 959 _codecs_latin_1_encode_impl(PyModuleDef *module, PyObject *str, const char *erro rs)
988 PyObject *args) 960 /*[clinic end generated code: output=77df1557422a9c94 input=e2ec958e195e3f30]*/
989 { 961 {
990 PyObject *str, *v; 962 PyObject *v;
991 const char *errors = NULL;
992
993 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode",
994 &str, &errors))
995 return NULL;
996 963
997 str = PyUnicode_FromObject(str); 964 str = PyUnicode_FromObject(str);
998 if (str == NULL || PyUnicode_READY(str) < 0) { 965 if (str == NULL || PyUnicode_READY(str) < 0) {
999 Py_XDECREF(str); 966 Py_XDECREF(str);
1000 return NULL; 967 return NULL;
1001 } 968 }
1002 v = codec_tuple(_PyUnicode_AsLatin1String(str, errors), 969 v = codec_tuple(_PyUnicode_AsLatin1String(str, errors),
1003 PyUnicode_GET_LENGTH(str)); 970 PyUnicode_GET_LENGTH(str));
1004 Py_DECREF(str); 971 Py_DECREF(str);
1005 return v; 972 return v;
1006 } 973 }
1007 974
975 /*[clinic input]
976 _codecs.ascii_encode
977 str: object
978 errors: str(nullable=True) = NULL
979 /
980 [clinic start generated code]*/
981
1008 static PyObject * 982 static PyObject *
1009 ascii_encode(PyObject *self, 983 _codecs_ascii_encode_impl(PyModuleDef *module, PyObject *str, const char *errors )
1010 PyObject *args) 984 /*[clinic end generated code: output=8dde6ff249762a68 input=536afe3b176f737b]*/
1011 { 985 {
1012 PyObject *str, *v; 986 PyObject *v;
1013 const char *errors = NULL;
1014
1015 if (!PyArg_ParseTuple(args, "O|z:ascii_encode",
1016 &str, &errors))
1017 return NULL;
1018 987
1019 str = PyUnicode_FromObject(str); 988 str = PyUnicode_FromObject(str);
1020 if (str == NULL || PyUnicode_READY(str) < 0) { 989 if (str == NULL || PyUnicode_READY(str) < 0) {
1021 Py_XDECREF(str); 990 Py_XDECREF(str);
1022 return NULL; 991 return NULL;
1023 } 992 }
1024 v = codec_tuple(_PyUnicode_AsASCIIString(str, errors), 993 v = codec_tuple(_PyUnicode_AsASCIIString(str, errors),
1025 PyUnicode_GET_LENGTH(str)); 994 PyUnicode_GET_LENGTH(str));
1026 Py_DECREF(str); 995 Py_DECREF(str);
1027 return v; 996 return v;
1028 } 997 }
1029 998
999 /*[clinic input]
1000 _codecs.charmap_encode
1001 str: object
1002 errors: str(nullable=True) = NULL
1003 mapping: object = NULL
1004 /
1005 [clinic start generated code]*/
1006
1030 static PyObject * 1007 static PyObject *
1031 charmap_encode(PyObject *self, 1008 _codecs_charmap_encode_impl(PyModuleDef *module, PyObject *str, const char *erro rs, PyObject *mapping)
1032 PyObject *args) 1009 /*[clinic end generated code: output=5df643ee90dda64b input=6cfea1a4be31cde1]*/
1033 { 1010 {
1034 PyObject *str, *v; 1011 PyObject *v;
1035 const char *errors = NULL;
1036 PyObject *mapping = NULL;
1037 1012
1038 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode",
1039 &str, &errors, &mapping))
1040 return NULL;
1041 if (mapping == Py_None) 1013 if (mapping == Py_None)
1042 mapping = NULL; 1014 mapping = NULL;
1043 1015
1044 str = PyUnicode_FromObject(str); 1016 str = PyUnicode_FromObject(str);
1045 if (str == NULL || PyUnicode_READY(str) < 0) { 1017 if (str == NULL || PyUnicode_READY(str) < 0) {
1046 Py_XDECREF(str); 1018 Py_XDECREF(str);
1047 return NULL; 1019 return NULL;
1048 } 1020 }
1049 v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors), 1021 v = codec_tuple(_PyUnicode_EncodeCharmap(str, mapping, errors),
1050 PyUnicode_GET_LENGTH(str)); 1022 PyUnicode_GET_LENGTH(str));
1051 Py_DECREF(str); 1023 Py_DECREF(str);
1052 return v; 1024 return v;
1053 } 1025 }
1054 1026
1055 static PyObject* 1027 /*[clinic input]
1056 charmap_build(PyObject *self, PyObject *args) 1028 _codecs.charmap_build
1029 map: unicode
1030 /
1031 [clinic start generated code]*/
1032
1033 static PyObject *
1034 _codecs_charmap_build_impl(PyModuleDef *module, PyObject *map)
1035 /*[clinic end generated code: output=9485b58fa44afa6a input=d91a91d1717dbc6d]*/
1057 { 1036 {
1058 PyObject *map;
1059 if (!PyArg_ParseTuple(args, "U:charmap_build", &map))
1060 return NULL;
1061 return PyUnicode_BuildEncodingMap(map); 1037 return PyUnicode_BuildEncodingMap(map);
1062 } 1038 }
1063 1039
1064 #ifdef HAVE_MBCS 1040 #ifdef HAVE_MBCS
1065 1041
1042 /*[clinic input]
1043 _codecs.mbcs_encode
1044 str: object
1045 errors: str(nullable=True) = NULL
1046 /
1047 [clinic start generated code]*/
1048
1066 static PyObject * 1049 static PyObject *
1067 mbcs_encode(PyObject *self, 1050 _codecs_mbcs_encode_impl(PyModuleDef *module, PyObject *str, const char *errors)
1068 PyObject *args) 1051 /*[clinic end generated code: output=1abdbfcca74446cd input=30a1cf44d9315ec9]*/
1069 { 1052 {
1070 PyObject *str, *v; 1053 PyObject *v;
1071 const char *errors = NULL;
1072
1073 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode",
1074 &str, &errors))
1075 return NULL;
1076 1054
1077 str = PyUnicode_FromObject(str); 1055 str = PyUnicode_FromObject(str);
1078 if (str == NULL || PyUnicode_READY(str) < 0) { 1056 if (str == NULL || PyUnicode_READY(str) < 0) {
1079 Py_XDECREF(str); 1057 Py_XDECREF(str);
1080 return NULL; 1058 return NULL;
1081 } 1059 }
1082 v = codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors), 1060 v = codec_tuple(PyUnicode_EncodeCodePage(CP_ACP, str, errors),
1083 PyUnicode_GET_LENGTH(str)); 1061 PyUnicode_GET_LENGTH(str));
1084 Py_DECREF(str); 1062 Py_DECREF(str);
1085 return v; 1063 return v;
1086 } 1064 }
1087 1065
1066 /*[clinic input]
1067 _codecs.code_page_encode
1068 code_page: int
1069 str: object
1070 errors: str(nullable=True) = NULL
1071 /
1072 [clinic start generated code]*/
1073
1088 static PyObject * 1074 static PyObject *
1089 code_page_encode(PyObject *self, 1075 _codecs_code_page_encode_impl(PyModuleDef *module, int code_page, PyObject *str, const char *errors)
1090 PyObject *args) 1076 /*[clinic end generated code: output=dda783c0f0d377cf input=b0c72790c2e2ce28]*/
1091 { 1077 {
1092 PyObject *str, *v; 1078 PyObject *v;
1093 const char *errors = NULL;
1094 int code_page;
1095
1096 if (!PyArg_ParseTuple(args, "iO|z:code_page_encode",
1097 &code_page, &str, &errors))
1098 return NULL;
1099 1079
1100 str = PyUnicode_FromObject(str); 1080 str = PyUnicode_FromObject(str);
1101 if (str == NULL || PyUnicode_READY(str) < 0) { 1081 if (str == NULL || PyUnicode_READY(str) < 0) {
1102 Py_XDECREF(str); 1082 Py_XDECREF(str);
1103 return NULL; 1083 return NULL;
1104 } 1084 }
1105 v = codec_tuple(PyUnicode_EncodeCodePage(code_page, 1085 v = codec_tuple(PyUnicode_EncodeCodePage(code_page,
1106 str, 1086 str,
1107 errors), 1087 errors),
1108 PyUnicode_GET_LENGTH(str)); 1088 PyUnicode_GET_LENGTH(str));
1109 Py_DECREF(str); 1089 Py_DECREF(str);
1110 return v; 1090 return v;
1111 } 1091 }
1112 1092
1113 #endif /* HAVE_MBCS */ 1093 #endif /* HAVE_MBCS */
1114 1094
1115 /* --- Error handler registry --------------------------------------------- */ 1095 /* --- Error handler registry --------------------------------------------- */
1116 1096
1117 PyDoc_STRVAR(register_error__doc__, 1097 /*[clinic input]
1118 "register_error(errors, handler)\n\ 1098 _codecs.register_error
1119 \n\ 1099 errors: str
1120 Register the specified error handler under the name\n\ 1100 handler: object
1121 errors. handler must be a callable object, that\n\ 1101 /
1122 will be called with an exception instance containing\n\
1123 information about the location of the encoding/decoding\n\
1124 error and must return a (replacement, new position) tuple.");
1125 1102
1126 static PyObject *register_error(PyObject *self, PyObject *args) 1103 Register the specified error handler under the name errors.
1104
1105 handler must be a callable object, that will be called with an exception
1106 instance containing information about the location of the encoding/decoding
1107 error and must return a (replacement, new position) tuple.
1108 [clinic start generated code]*/
1109
1110 static PyObject *
1111 _codecs_register_error_impl(PyModuleDef *module, const char *errors, PyObject *h andler)
1112 /*[clinic end generated code: output=ed559ea4a806f2ad input=5e6709203c2e33fe]*/
1127 { 1113 {
1128 const char *name; 1114 if (PyCodec_RegisterError(errors, handler))
1129 PyObject *handler;
1130
1131 if (!PyArg_ParseTuple(args, "sO:register_error",
1132 &name, &handler))
1133 return NULL;
1134 if (PyCodec_RegisterError(name, handler))
1135 return NULL; 1115 return NULL;
1136 Py_RETURN_NONE; 1116 Py_RETURN_NONE;
1137 } 1117 }
1138 1118
1139 PyDoc_STRVAR(lookup_error__doc__, 1119 /*[clinic input]
1140 "lookup_error(errors) -> handler\n\ 1120 _codecs.lookup_error
1141 \n\ 1121 name: str
1142 Return the error handler for the specified error handling name\n\ 1122 /
1143 or raise a LookupError, if no handler exists under this name.");
1144 1123
1145 static PyObject *lookup_error(PyObject *self, PyObject *args) 1124 lookup_error(errors) -> handler
1125
1126 Return the error handler for the specified error handling name or raise a
1127 LookupError, if no handler exists under this name.
1128 [clinic start generated code]*/
1129
1130 static PyObject *
1131 _codecs_lookup_error_impl(PyModuleDef *module, const char *name)
1132 /*[clinic end generated code: output=731e6df8c83c6158 input=4775dd65e6235aba]*/
1146 { 1133 {
1147 const char *name;
1148
1149 if (!PyArg_ParseTuple(args, "s:lookup_error",
1150 &name))
1151 return NULL;
1152 return PyCodec_LookupError(name); 1134 return PyCodec_LookupError(name);
1153 } 1135 }
1154 1136
1155 /* --- Module API --------------------------------------------------------- */ 1137 /* --- Module API --------------------------------------------------------- */
1156 1138
1157 static PyMethodDef _codecs_functions[] = { 1139 static PyMethodDef _codecs_functions[] = {
1158 {"register", codec_register, METH_O, 1140 _CODECS_REGISTER_METHODDEF
1159 register__doc__}, 1141 _CODECS_LOOKUP_METHODDEF
1160 {"lookup", codec_lookup, METH_VARARGS, 1142 _CODECS_ENCODE_METHODDEF
1161 lookup__doc__}, 1143 _CODECS_DECODE_METHODDEF
1162 {"encode", (PyCFunction)codec_encode, METH_VARARGS|METH_KEYWORDS, 1144 _CODECS_ESCAPE_ENCODE_METHODDEF
1163 encode__doc__}, 1145 _CODECS_ESCAPE_DECODE_METHODDEF
1164 {"decode", (PyCFunction)codec_decode, METH_VARARGS|METH_KEYWORDS, 1146 _CODECS_UTF_8_ENCODE_METHODDEF
1165 decode__doc__}, 1147 _CODECS_UTF_8_DECODE_METHODDEF
1166 {"escape_encode", escape_encode, METH_VARARGS}, 1148 _CODECS_UTF_7_ENCODE_METHODDEF
1167 {"escape_decode", escape_decode, METH_VARARGS}, 1149 _CODECS_UTF_7_DECODE_METHODDEF
1168 {"utf_8_encode", utf_8_encode, METH_VARARGS}, 1150 _CODECS_UTF_16_ENCODE_METHODDEF
1169 {"utf_8_decode", utf_8_decode, METH_VARARGS}, 1151 _CODECS_UTF_16_LE_ENCODE_METHODDEF
1170 {"utf_7_encode", utf_7_encode, METH_VARARGS}, 1152 _CODECS_UTF_16_BE_ENCODE_METHODDEF
1171 {"utf_7_decode", utf_7_decode, METH_VARARGS}, 1153 _CODECS_UTF_16_DECODE_METHODDEF
1172 {"utf_16_encode", utf_16_encode, METH_VARARGS}, 1154 _CODECS_UTF_16_LE_DECODE_METHODDEF
1173 {"utf_16_le_encode", utf_16_le_encode, METH_VARARGS}, 1155 _CODECS_UTF_16_BE_DECODE_METHODDEF
1174 {"utf_16_be_encode", utf_16_be_encode, METH_VARARGS}, 1156 _CODECS_UTF_16_EX_DECODE_METHODDEF
1175 {"utf_16_decode", utf_16_decode, METH_VARARGS}, 1157 _CODECS_UTF_32_ENCODE_METHODDEF
1176 {"utf_16_le_decode", utf_16_le_decode, METH_VARARGS}, 1158 _CODECS_UTF_32_LE_ENCODE_METHODDEF
1177 {"utf_16_be_decode", utf_16_be_decode, METH_VARARGS}, 1159 _CODECS_UTF_32_BE_ENCODE_METHODDEF
1178 {"utf_16_ex_decode", utf_16_ex_decode, METH_VARARGS}, 1160 _CODECS_UTF_32_DECODE_METHODDEF
1179 {"utf_32_encode", utf_32_encode, METH_VARARGS}, 1161 _CODECS_UTF_32_LE_DECODE_METHODDEF
1180 {"utf_32_le_encode", utf_32_le_encode, METH_VARARGS}, 1162 _CODECS_UTF_32_BE_DECODE_METHODDEF
1181 {"utf_32_be_encode", utf_32_be_encode, METH_VARARGS}, 1163 _CODECS_UTF_32_EX_DECODE_METHODDEF
1182 {"utf_32_decode", utf_32_decode, METH_VARARGS}, 1164 _CODECS_UNICODE_ESCAPE_ENCODE_METHODDEF
1183 {"utf_32_le_decode", utf_32_le_decode, METH_VARARGS}, 1165 _CODECS_UNICODE_ESCAPE_DECODE_METHODDEF
1184 {"utf_32_be_decode", utf_32_be_decode, METH_VARARGS}, 1166 _CODECS_UNICODE_INTERNAL_ENCODE_METHODDEF
1185 {"utf_32_ex_decode", utf_32_ex_decode, METH_VARARGS}, 1167 _CODECS_UNICODE_INTERNAL_DECODE_METHODDEF
1186 {"unicode_escape_encode", unicode_escape_encode, METH_VARARGS}, 1168 _CODECS_RAW_UNICODE_ESCAPE_ENCODE_METHODDEF
1187 {"unicode_escape_decode", unicode_escape_decode, METH_VARARGS}, 1169 _CODECS_RAW_UNICODE_ESCAPE_DECODE_METHODDEF
1188 {"unicode_internal_encode", unicode_internal_encode, METH_VARARGS}, 1170 _CODECS_LATIN_1_ENCODE_METHODDEF
1189 {"unicode_internal_decode", unicode_internal_decode, METH_VARARGS}, 1171 _CODECS_LATIN_1_DECODE_METHODDEF
1190 {"raw_unicode_escape_encode", raw_unicode_escape_encode, METH_VARARGS}, 1172 _CODECS_ASCII_ENCODE_METHODDEF
1191 {"raw_unicode_escape_decode", raw_unicode_escape_decode, METH_VARARGS}, 1173 _CODECS_ASCII_DECODE_METHODDEF
1192 {"latin_1_encode", latin_1_encode, METH_VARARGS}, 1174 _CODECS_CHARMAP_ENCODE_METHODDEF
1193 {"latin_1_decode", latin_1_decode, METH_VARARGS}, 1175 _CODECS_CHARMAP_DECODE_METHODDEF
1194 {"ascii_encode", ascii_encode, METH_VARARGS}, 1176 _CODECS_CHARMAP_BUILD_METHODDEF
1195 {"ascii_decode", ascii_decode, METH_VARARGS}, 1177 _CODECS_READBUFFER_ENCODE_METHODDEF
1196 {"charmap_encode", charmap_encode, METH_VARARGS}, 1178 _CODECS_MBCS_ENCODE_METHODDEF
1197 {"charmap_decode", charmap_decode, METH_VARARGS}, 1179 _CODECS_MBCS_DECODE_METHODDEF
1198 {"charmap_build", charmap_build, METH_VARARGS}, 1180 _CODECS_CODE_PAGE_ENCODE_METHODDEF
1199 {"readbuffer_encode", readbuffer_encode, METH_VARARGS}, 1181 _CODECS_CODE_PAGE_DECODE_METHODDEF
1200 #ifdef HAVE_MBCS 1182 _CODECS_REGISTER_ERROR_METHODDEF
1201 {"mbcs_encode", mbcs_encode, METH_VARARGS}, 1183 _CODECS_LOOKUP_ERROR_METHODDEF
1202 {"mbcs_decode", mbcs_decode, METH_VARARGS},
1203 {"code_page_encode", code_page_encode, METH_VARARGS},
1204 {"code_page_decode", code_page_decode, METH_VARARGS},
1205 #endif
1206 {"register_error", register_error, METH_VARARGS,
1207 register_error__doc__},
1208 {"lookup_error", lookup_error, METH_VARARGS,
1209 lookup_error__doc__},
1210 _CODECS__FORGET_CODEC_METHODDEF 1184 _CODECS__FORGET_CODEC_METHODDEF
1211 {NULL, NULL} /* sentinel */ 1185 {NULL, NULL} /* sentinel */
1212 }; 1186 };
1213 1187
1214 static struct PyModuleDef codecsmodule = { 1188 static struct PyModuleDef codecsmodule = {
1215 PyModuleDef_HEAD_INIT, 1189 PyModuleDef_HEAD_INIT,
1216 "_codecs", 1190 "_codecs",
1217 NULL, 1191 NULL,
1218 -1, 1192 -1,
1219 _codecs_functions, 1193 _codecs_functions,
1220 NULL, 1194 NULL,
1221 NULL, 1195 NULL,
1222 NULL, 1196 NULL,
1223 NULL 1197 NULL
1224 }; 1198 };
1225 1199
1226 PyMODINIT_FUNC 1200 PyMODINIT_FUNC
1227 PyInit__codecs(void) 1201 PyInit__codecs(void)
1228 { 1202 {
1229 return PyModule_Create(&codecsmodule); 1203 return PyModule_Create(&codecsmodule);
1230 } 1204 }
OLDNEW
« no previous file with comments | « Modules/clinic/_codecsmodule.c.h ('k') | no next file » | no next file with comments »

RSS Feeds Recent Issues | This issue
This is Rietveld 894c83f36cb7+