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

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