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

Delta Between Two Patch Sets: Modules/_codecsmodule.c

Issue 20173: Derby #4: Convert 53 sites to Argument Clinic across 5 files
Left Patch Set: Created 6 years, 1 month ago
Right 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:
Right: Side by side diff | Download
« no previous file with change/comment | « Modules/clinic/_codecsmodule.c.h ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
(no file at all)
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\ 58
59 return None, or a tuple of functions (encoder, decoder, stream_reader,\n\ 59 Register a codec search function.
60 stream_writer) (or a CodecInfo object)."); 60
61 61 Search functions are expected to take one argument, the encoding name in
62 static 62 all lower case letters, and either return None, or a tuple of functions
63 PyObject *codec_register(PyObject *self, PyObject *search_function) 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."); 80
76 81 Looks up a codec tuple in the Python codec registry and returns a CodecInfo obje ct.
77 static 82 [clinic start generated code]*/
78 PyObject *codec_lookup(PyObject *self, PyObject *args) 83
79 { 84 static PyObject *
80 char *encoding; 85 _codecs_lookup_impl(PyModuleDef *module, const char *encoding)
81 86 /*[clinic end generated code: output=798e41aff0c04ef6 input=3c572c0db3febe9c]*/
82 if (!PyArg_ParseTuple(args, "s:lookup", &encoding)) 87 {
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
97 100 different error handling scheme. Default is 'strict' meaning that encoding
98 static PyObject * 101 errors raise a ValueError. Other possible values are 'ignore', 'replace'
99 codec_encode(PyObject *self, PyObject *args, PyObject *kwargs) 102 and 'xmlcharrefreplace' as well as any other name registered with
100 { 103 codecs.register_error that can handle ValueErrors.
101 static char *kwlist[] = {"obj", "encoding", "errors", NULL}; 104 [clinic start generated code]*/
102 const char *encoding = NULL; 105
103 const char *errors = NULL; 106 static PyObject *
104 PyObject *v; 107 _codecs_encode_impl(PyModuleDef *module, PyObject *obj, const char *encoding,
105 108 const char *errors)
106 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:encode", kwlist, 109 /*[clinic end generated code: output=5c073f62249c8d7c input=cb1f62cf8d9e2a2c]*/
107 &v, &encoding, &errors)) 110 {
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
126 127 different error handling scheme. Default is 'strict' meaning that encoding
127 static PyObject * 128 errors raise a ValueError. Other possible values are 'ignore' and 'replace'
128 codec_decode(PyObject *self, PyObject *args, PyObject *kwargs) 129 as well as any other name registered with codecs.register_error that is able
129 { 130 to handle ValueErrors.
130 static char *kwlist[] = {"obj", "encoding", "errors", NULL}; 131 [clinic start generated code]*/
131 const char *encoding = NULL; 132
132 const char *errors = NULL; 133 static PyObject *
133 PyObject *v; 134 _codecs_decode_impl(PyModuleDef *module, PyObject *obj, const char *encoding,
134 135 const char *errors)
135 if (!PyArg_ParseTupleAndKeywords(args, kwargs, "O|ss:decode", kwlist, 136 /*[clinic end generated code: output=c81cbf6189a7f878 input=9d04293156021886]*/
136 &v, &encoding, &errors)) 137 {
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) 171 return NULL;
173 return NULL; 172 return Py_BuildValue("Nn", decoded, len);
174 v = Py_BuildValue("On", unicode, len);
175 Py_DECREF(unicode);
176 return v;
177 } 173 }
178 174
179 /* --- String codecs ------------------------------------------------------ */ 175 /* --- String codecs ------------------------------------------------------ */
180 static PyObject * 176 /*[clinic input]
181 escape_decode(PyObject *self, 177 _codecs.escape_decode
182 PyObject *args) 178 data: Py_buffer(types={'str', 'buffer'})
183 { 179 errors: str(nullable=True) = NULL
184 Py_buffer pbuf; 180 /
185 const char *errors = NULL; 181 [clinic start generated code]*/
186 PyObject *result; 182
187 183 static PyObject *
188 if (!PyArg_ParseTuple(args, "s*|z:escape_decode", 184 _codecs_escape_decode_impl(PyModuleDef *module, Py_buffer *data,
189 &pbuf, &errors)) 185 const char *errors)
190 return NULL; 186 /*[clinic end generated code: output=648fa3e78d03e658 input=f373087451274bbb]*/
191 result = codec_tuple( 187 {
192 PyBytes_DecodeEscape(pbuf.buf, pbuf.len, errors, 0, NULL), 188 PyObject *decoded = PyBytes_DecodeEscape(data->buf, data->len,
193 pbuf.len); 189 errors, 0, NULL);
194 PyBuffer_Release(&pbuf); 190 return codec_tuple(decoded, data->len);
195 return result; 191 }
196 } 192
197 193 /*[clinic input]
198 static PyObject * 194 _codecs.escape_encode
199 escape_encode(PyObject *self, 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!"
200 PyObject *args) 196 errors: str(nullable=True) = NULL
201 { 197 /
202 PyObject *str; 198 [clinic start generated code]*/
199
200 static PyObject *
201 _codecs_escape_encode_impl(PyModuleDef *module, PyObject *data,
202 const char *errors)
203 /*[clinic end generated code: output=fcd6f34fe4111c50 input=995ec13d87247ec7]*/
204 {
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; 207 PyObject *v;
206 PyObject *v; 208
207 209 size = PyBytes_GET_SIZE(data);
208 if (!PyArg_ParseTuple(args, "O!|z:escape_encode",
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 ------------------------------------------------------------ */
260 257 /*[clinic input]
261 static PyObject * 258 _codecs.unicode_internal_decode
262 unicode_internal_decode(PyObject *self, 259 obj: object
263 PyObject *args) 260 errors: str(nullable=True) = NULL
264 { 261 /
265 PyObject *obj; 262 [clinic start generated code]*/
266 const char *errors = NULL; 263
267 264 static PyObject *
268 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_decode", 265 _codecs_unicode_internal_decode_impl(PyModuleDef *module, PyObject *obj,
269 &obj, &errors)) 266 const char *errors)
270 return NULL; 267 /*[clinic end generated code: output=9fe47c2cd8807d92 input=71d1d53c6ea4f0fc]*/
271 268 {
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
292 static PyObject * 289 /*[clinic input]
293 utf_7_decode(PyObject *self, 290 _codecs.utf_7_decode
294 PyObject *args) 291 data: Py_buffer
295 { 292 errors: str(nullable=True) = NULL
296 Py_buffer pbuf; 293 final: int(c_default="0") = False
297 const char *errors = NULL; 294 /
298 int final = 0; 295 [clinic start generated code]*/
299 Py_ssize_t consumed; 296
300 PyObject *decoded = NULL; 297 static PyObject *
301 298 _codecs_utf_7_decode_impl(PyModuleDef *module, Py_buffer *data,
302 if (!PyArg_ParseTuple(args, "y*|zi:utf_7_decode", 299 const char *errors, int final)
303 &pbuf, &errors, &final)) 300 /*[clinic end generated code: output=ca945e907e72e827 input=8fd0241dcc559fe8]*/
304 return NULL; 301 {
305 consumed = pbuf.len; 302 Py_ssize_t consumed = data->len;
306 303 PyObject *decoded = PyUnicode_DecodeUTF7Stateful(data->buf, data->len,
307 decoded = PyUnicode_DecodeUTF7Stateful(pbuf.buf, pbuf.len, errors, 304 errors,
308 final ? NULL : &consumed); 305 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
315 static PyObject * 309 /*[clinic input]
316 utf_8_decode(PyObject *self, 310 _codecs.utf_8_decode
317 PyObject *args) 311 data: Py_buffer
318 { 312 errors: str(nullable=True) = NULL
319 Py_buffer pbuf; 313 final: int(c_default="0") = False
320 const char *errors = NULL; 314 /
321 int final = 0; 315 [clinic start generated code]*/
322 Py_ssize_t consumed; 316
323 PyObject *decoded = NULL; 317 static PyObject *
324 318 _codecs_utf_8_decode_impl(PyModuleDef *module, Py_buffer *data,
325 if (!PyArg_ParseTuple(args, "y*|zi:utf_8_decode", 319 const char *errors, int final)
326 &pbuf, &errors, &final)) 320 /*[clinic end generated code: output=7309f9ff4ef5c9b6 input=709f25fbc3cd9a3e]*/
327 return NULL; 321 {
328 consumed = pbuf.len; 322 Py_ssize_t consumed = data->len;
329 323 PyObject *decoded = PyUnicode_DecodeUTF8Stateful(data->buf, data->len,
330 decoded = PyUnicode_DecodeUTF8Stateful(pbuf.buf, pbuf.len, errors, 324 errors,
331 final ? NULL : &consumed); 325 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
338 static PyObject * 329 /*[clinic input]
339 utf_16_decode(PyObject *self, 330 _codecs.utf_16_decode
340 PyObject *args) 331 data: Py_buffer
341 { 332 errors: str(nullable=True) = NULL
342 Py_buffer pbuf; 333 final: int(c_default="0") = False
343 const char *errors = NULL; 334 /
335 [clinic start generated code]*/
336
337 static PyObject *
338 _codecs_utf_16_decode_impl(PyModuleDef *module, Py_buffer *data,
339 const char *errors, int final)
340 /*[clinic end generated code: output=8d2fa0507d9bef2c input=81678c8535585915]*/
341 {
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
361 static PyObject * 351 /*[clinic input]
362 utf_16_le_decode(PyObject *self, 352 _codecs.utf_16_le_decode
363 PyObject *args) 353 data: Py_buffer
364 { 354 errors: str(nullable=True) = NULL
365 Py_buffer pbuf; 355 final: int(c_default="0") = False
366 const char *errors = NULL; 356 /
357 [clinic start generated code]*/
358
359 static PyObject *
360 _codecs_utf_16_le_decode_impl(PyModuleDef *module, Py_buffer *data,
361 const char *errors, int final)
362 /*[clinic end generated code: output=4fd621515ef4ce18 input=006e53be083b6777]*/
363 {
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
385 static PyObject * 373 /*[clinic input]
386 utf_16_be_decode(PyObject *self, 374 _codecs.utf_16_be_decode
387 PyObject *args) 375 data: Py_buffer
388 { 376 errors: str(nullable=True) = NULL
389 Py_buffer pbuf; 377 final: int(c_default="0") = False
390 const char *errors = NULL; 378 /
379 [clinic start generated code]*/
380
381 static PyObject *
382 _codecs_utf_16_be_decode_impl(PyModuleDef *module, Py_buffer *data,
383 const char *errors, int final)
384 /*[clinic end generated code: output=792f4eacb3e1fa05 input=d9490ad0f74140f3]*/
385 {
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 */
416 402 /*[clinic input]
417 static PyObject * 403 _codecs.utf_16_ex_decode
418 utf_16_ex_decode(PyObject *self, 404 data: Py_buffer
419 PyObject *args) 405 errors: str(nullable=True) = NULL
420 { 406 byteorder: int = 0
421 Py_buffer pbuf; 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
422 const char *errors = NULL; 408 /
409 [clinic start generated code]*/
410
411 static PyObject *
412 _codecs_utf_16_ex_decode_impl(PyModuleDef *module, Py_buffer *data,
413 const char *errors, int byteorder, int final)
414 /*[clinic end generated code: output=f136a186dc2defa0 input=56a16c35944e4668]*/
415 {
416 /* This is overwritten unless final is true. */
417 Py_ssize_t consumed = data->len;
418
419 PyObject *decoded = PyUnicode_DecodeUTF16Stateful(data->buf, data->len,
420 errors, &byteorder,
421 final ? NULL : &consumed);
422 if (decoded == NULL)
423 return NULL;
424 return Py_BuildValue("Nni", decoded, consumed, byteorder);
425 }
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
435 static PyObject *
436 _codecs_utf_32_decode_impl(PyModuleDef *module, Py_buffer *data,
437 const char *errors, int final)
438 /*[clinic end generated code: output=b7635e55857e8efb input=778f09d112c84716]*/
439 {
423 int byteorder = 0; 440 int byteorder = 0;
424 PyObject *unicode, *tuple; 441 /* This is overwritten unless final is true. */
425 int final = 0; 442 Py_ssize_t consumed = data->len;
426 Py_ssize_t consumed; 443 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
427 444 errors, &byteorder,
428 if (!PyArg_ParseTuple(args, "y*|zii:utf_16_ex_decode", 445 final ? NULL : &consumed);
429 &pbuf, &errors, &byteorder, &final))
430 return NULL;
431 consumed = pbuf.len; /* This is overwritten unless final is true. */
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 }
441
442 static PyObject *
443 utf_32_decode(PyObject *self,
444 PyObject *args)
445 {
446 Py_buffer pbuf;
447 const char *errors = NULL;
448 int byteorder = 0;
449 int final = 0;
450 Py_ssize_t consumed;
451 PyObject *decoded;
452
453 if (!PyArg_ParseTuple(args, "y*|zi:utf_32_decode",
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
465 static PyObject * 449 /*[clinic input]
466 utf_32_le_decode(PyObject *self, 450 _codecs.utf_32_le_decode
467 PyObject *args) 451 data: Py_buffer
468 { 452 errors: str(nullable=True) = NULL
469 Py_buffer pbuf; 453 final: int(c_default="0") = False
470 const char *errors = NULL; 454 /
455 [clinic start generated code]*/
456
457 static PyObject *
458 _codecs_utf_32_le_decode_impl(PyModuleDef *module, Py_buffer *data,
459 const char *errors, int final)
460 /*[clinic end generated code: output=a79d1787d8ddf988 input=7a398c6cadca231f]*/
461 {
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
488 static PyObject * 471 /*[clinic input]
489 utf_32_be_decode(PyObject *self, 472 _codecs.utf_32_be_decode
490 PyObject *args) 473 data: Py_buffer
491 { 474 errors: str(nullable=True) = NULL
492 Py_buffer pbuf; 475 final: int(c_default="0") = False
493 const char *errors = NULL; 476 /
477 [clinic start generated code]*/
478
479 static PyObject *
480 _codecs_utf_32_be_decode_impl(PyModuleDef *module, Py_buffer *data,
481 const char *errors, int final)
482 /*[clinic end generated code: output=a8356b0f36779981 input=573fb3c3b8ac000d]*/
483 {
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 */
518 500 /*[clinic input]
519 static PyObject * 501 _codecs.utf_32_ex_decode
520 utf_32_ex_decode(PyObject *self, 502 data: Py_buffer
521 PyObject *args) 503 errors: str(nullable=True) = NULL
522 { 504 byteorder: int = 0
523 Py_buffer pbuf; 505 final: int(c_default="0") = False
524 const char *errors = NULL; 506 /
525 int byteorder = 0; 507 [clinic start generated code]*/
526 PyObject *unicode, *tuple; 508
527 int final = 0; 509 static PyObject *
528 Py_ssize_t consumed; 510 _codecs_utf_32_ex_decode_impl(PyModuleDef *module, Py_buffer *data,
529 511 const char *errors, int byteorder, int final)
530 if (!PyArg_ParseTuple(args, "y*|zii:utf_32_ex_decode", 512 /*[clinic end generated code: output=ab8c70977c1992f5 input=56c2fd15274e69a0]*/
531 &pbuf, &errors, &byteorder, &final)) 513 {
532 return NULL; 514 Py_ssize_t consumed = data->len;
533 consumed = pbuf.len; /* This is overwritten unless final is true. */ 515 PyObject *decoded = PyUnicode_DecodeUTF32Stateful(data->buf, data->len,
534 unicode = PyUnicode_DecodeUTF32Stateful(pbuf.buf, pbuf.len, errors, 516 errors, &byteorder,
535 &byteorder, final ? NULL : &consumed); 517 final ? NULL : &consumed);
536 PyBuffer_Release(&pbuf); 518 if (decoded == NULL)
537 if (unicode == NULL) 519 return NULL;
538 return NULL; 520 return Py_BuildValue("Nni", decoded, consumed, byteorder);
539 tuple = Py_BuildValue("Oni", unicode, consumed, byteorder); 521 }
540 Py_DECREF(unicode); 522
541 return tuple; 523 /*[clinic input]
542 } 524 _codecs.unicode_escape_decode
543 525 data: Py_buffer(types={'str', 'buffer'})
544 static PyObject * 526 errors: str(nullable=True) = NULL
545 unicode_escape_decode(PyObject *self, 527 /
546 PyObject *args) 528 [clinic start generated code]*/
547 { 529
548 Py_buffer pbuf; 530 static PyObject *
549 const char *errors = NULL; 531 _codecs_unicode_escape_decode_impl(PyModuleDef *module, Py_buffer *data,
550 PyObject *unicode; 532 const char *errors)
551 533 /*[clinic end generated code: output=d1aa63f2620c4999 input=80f2ff60014d65c4]*/
552 if (!PyArg_ParseTuple(args, "s*|z:unicode_escape_decode", 534 {
553 &pbuf, &errors)) 535 PyObject *decoded = PyUnicode_DecodeUnicodeEscape(data->buf, data->len,
554 return NULL; 536 errors);
555 537 return codec_tuple(decoded, data->len);
556 unicode = PyUnicode_DecodeUnicodeEscape(pbuf.buf, pbuf.len, errors); 538 }
557 PyBuffer_Release(&pbuf); 539
558 return codec_tuple(unicode, pbuf.len); 540 /*[clinic input]
559 } 541 _codecs.raw_unicode_escape_decode
560 542 data: Py_buffer(types={'str', 'buffer'})
561 static PyObject * 543 errors: str(nullable=True) = NULL
562 raw_unicode_escape_decode(PyObject *self, 544 /
563 PyObject *args) 545 [clinic start generated code]*/
564 { 546
565 Py_buffer pbuf; 547 static PyObject *
566 const char *errors = NULL; 548 _codecs_raw_unicode_escape_decode_impl(PyModuleDef *module, Py_buffer *data,
567 PyObject *unicode; 549 const char *errors)
568 550 /*[clinic end generated code: output=0bf96cc182d81379 input=ef77c7ab98ee2c60]*/
569 if (!PyArg_ParseTuple(args, "s*|z:raw_unicode_escape_decode", 551 {
570 &pbuf, &errors)) 552 PyObject *decoded = PyUnicode_DecodeRawUnicodeEscape(data->buf, data->len,
571 return NULL; 553 errors);
572 554 return codec_tuple(decoded, data->len);
573 unicode = PyUnicode_DecodeRawUnicodeEscape(pbuf.buf, pbuf.len, errors); 555 }
574 PyBuffer_Release(&pbuf); 556
575 return codec_tuple(unicode, pbuf.len); 557 /*[clinic input]
576 } 558 _codecs.latin_1_decode
577 559 data: Py_buffer
578 static PyObject * 560 errors: str(nullable=True) = NULL
579 latin_1_decode(PyObject *self, 561 /
580 PyObject *args) 562 [clinic start generated code]*/
581 { 563
582 Py_buffer pbuf; 564 static PyObject *
583 PyObject *unicode; 565 _codecs_latin_1_decode_impl(PyModuleDef *module, Py_buffer *data,
584 const char *errors = NULL; 566 const char *errors)
585 567 /*[clinic end generated code: output=66b916f5055aaf13 input=2ad8439a94444119]*/
586 if (!PyArg_ParseTuple(args, "y*|z:latin_1_decode", 568 {
587 &pbuf, &errors)) 569 PyObject *decoded = PyUnicode_DecodeLatin1(data->buf, data->len, errors);
588 return NULL; 570 return codec_tuple(decoded, data->len);
589 571 }
590 unicode = PyUnicode_DecodeLatin1(pbuf.buf, pbuf.len, errors); 572
591 PyBuffer_Release(&pbuf); 573 /*[clinic input]
592 return codec_tuple(unicode, pbuf.len); 574 _codecs.ascii_decode
593 } 575 data: Py_buffer
594 576 errors: str(nullable=True) = NULL
595 static PyObject * 577 /
596 ascii_decode(PyObject *self, 578 [clinic start generated code]*/
597 PyObject *args) 579
598 { 580 static PyObject *
599 Py_buffer pbuf; 581 _codecs_ascii_decode_impl(PyModuleDef *module, Py_buffer *data,
600 PyObject *unicode; 582 const char *errors)
601 const char *errors = NULL; 583 /*[clinic end generated code: output=7f213a1b5cdafc65 input=0bfd2dd60e3e0152]*/
602 584 {
603 if (!PyArg_ParseTuple(args, "y*|z:ascii_decode", 585 PyObject *decoded = PyUnicode_DecodeASCII(data->buf, data->len, errors);
604 &pbuf, &errors)) 586 return codec_tuple(decoded, data->len);
605 return NULL; 587 }
606 588
607 unicode = PyUnicode_DecodeASCII(pbuf.buf, pbuf.len, errors); 589 /*[clinic input]
608 PyBuffer_Release(&pbuf); 590 _codecs.charmap_decode
609 return codec_tuple(unicode, pbuf.len); 591 data: Py_buffer
610 } 592 errors: str(nullable=True) = NULL
611 593 mapping: object = NULL
612 static PyObject * 594 /
613 charmap_decode(PyObject *self, 595 [clinic start generated code]*/
614 PyObject *args) 596
615 { 597 static PyObject *
616 Py_buffer pbuf; 598 _codecs_charmap_decode_impl(PyModuleDef *module, Py_buffer *data,
617 PyObject *unicode; 599 const char *errors, PyObject *mapping)
618 const char *errors = NULL; 600 /*[clinic end generated code: output=87d27f365098bbae input=bf5ab0388adeac8e]*/
619 PyObject *mapping = NULL; 601 {
620 602 PyObject *decoded;
621 if (!PyArg_ParseTuple(args, "y*|zO:charmap_decode", 603
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
634 static PyObject * 613 /*[clinic input]
635 mbcs_decode(PyObject *self, 614 _codecs.mbcs_decode
636 PyObject *args) 615 data: Py_buffer
637 { 616 errors: str(nullable=True) = NULL
638 Py_buffer pbuf; 617 final: int(c_default="0") = False
639 const char *errors = NULL; 618 /
640 int final = 0; 619 [clinic start generated code]*/
641 Py_ssize_t consumed; 620
642 PyObject *decoded = NULL; 621 static PyObject *
643 622 _codecs_mbcs_decode_impl(PyModuleDef *module, Py_buffer *data,
644 if (!PyArg_ParseTuple(args, "y*|zi:mbcs_decode", 623 const char *errors, int final)
645 &pbuf, &errors, &final)) 624 /*[clinic end generated code: output=0ebaf3a5b20e53fa input=48841d508b7bacee]*/
646 return NULL; 625 {
647 consumed = pbuf.len; 626 Py_ssize_t consumed = data->len;
648 627 PyObject *decoded = PyUnicode_DecodeMBCSStateful(data->buf, data->len,
649 decoded = PyUnicode_DecodeMBCSStateful(pbuf.buf, pbuf.len, errors, 628 errors, final ? NULL : &consumed);
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
657 static PyObject * 632 /*[clinic input]
658 code_page_decode(PyObject *self, 633 _codecs.code_page_decode
659 PyObject *args) 634 codepage: int
660 { 635 data: Py_buffer
661 Py_buffer pbuf; 636 errors: str(nullable=True) = NULL
662 const char *errors = NULL; 637 final: int(c_default="0") = False
663 int final = 0; 638 /
664 Py_ssize_t consumed; 639 [clinic start generated code]*/
665 PyObject *decoded = NULL; 640
666 int code_page; 641 static PyObject *
667 642 _codecs_code_page_decode_impl(PyModuleDef *module, int codepage,
668 if (!PyArg_ParseTuple(args, "iy*|zi:code_page_decode", 643 Py_buffer *data, const char *errors, int final)
669 &code_page, &pbuf, &errors, &final)) 644 /*[clinic end generated code: output=4318e3d9971e31ba input=74f589867f3429ce]*/
670 return NULL; 645 {
671 consumed = pbuf.len; 646 Py_ssize_t consumed = data->len;
672 647 PyObject *decoded = PyUnicode_DecodeCodePageStateful(code_page,
673 decoded = PyUnicode_DecodeCodePageStateful(code_page, 648 data->buf, data->len,
674 pbuf.buf, pbuf.len, errors, 649 errors,
675 final ? NULL : &consumed); 650 final ? NULL : &consume d);
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
686 static PyObject * 658 /*[clinic input]
687 readbuffer_encode(PyObject *self, 659 _codecs.readbuffer_encode
688 PyObject *args) 660 data: Py_buffer(types={'str', 'buffer'})
689 { 661 errors: str(nullable=True) = NULL
690 Py_buffer pdata; 662 /
691 const char *data; 663 [clinic start generated code]*/
692 Py_ssize_t size; 664
693 const char *errors = NULL; 665 static PyObject *
694 PyObject *result; 666 _codecs_readbuffer_encode_impl(PyModuleDef *module, Py_buffer *data,
695 667 const char *errors)
696 if (!PyArg_ParseTuple(args, "s*|z:readbuffer_encode", 668 /*[clinic end generated code: output=319cc24083299859 input=fc35dc93718e670d]*/
697 &pdata, &errors)) 669 {
698 return NULL; 670 PyObject *result = PyBytes_FromStringAndSize(data->buf, data->len);
699 data = pdata.buf; 671 return codec_tuple(result, data->len);
700 size = pdata.len; 672 }
701 673
702 result = PyBytes_FromStringAndSize(data, size); 674 /*[clinic input]
703 PyBuffer_Release(&pdata); 675 _codecs.unicode_internal_encode
704 return codec_tuple(result, size); 676 obj: object
705 } 677 errors: str(nullable=True) = NULL
706 678 /
707 static PyObject * 679 [clinic start generated code]*/
708 unicode_internal_encode(PyObject *self, 680
709 PyObject *args) 681 static PyObject *
710 { 682 _codecs_unicode_internal_encode_impl(PyModuleDef *module, PyObject *obj,
711 PyObject *obj; 683 const char *errors)
712 const char *errors = NULL; 684 /*[clinic end generated code: output=be08457068ad503b input=61192dad1fce371f]*/
713 685 {
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;
718
719 if (!PyArg_ParseTuple(args, "O|z:unicode_internal_encode",
720 &obj, &errors))
721 return NULL; 689 return NULL;
722 690
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
750 static PyObject * 719 /*[clinic input]
751 utf_7_encode(PyObject *self, 720 _codecs.utf_7_encode
752 PyObject *args) 721 str: object
753 { 722 errors: str(nullable=True) = NULL
754 PyObject *str, *v; 723 /
755 const char *errors = NULL; 724 [clinic start generated code]*/
756 725
757 if (!PyArg_ParseTuple(args, "O|z:utf_7_encode", 726 static PyObject *
758 &str, &errors)) 727 _codecs_utf_7_encode_impl(PyModuleDef *module, PyObject *str,
759 return NULL; 728 const char *errors)
729 /*[clinic end generated code: output=a7accc496a32b759 input=802ba3f495219bbc]*/
730 {
731 PyObject *v;
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
772 static PyObject * 744 /*[clinic input]
773 utf_8_encode(PyObject *self, 745 _codecs.utf_8_encode
774 PyObject *args) 746 str: object
775 { 747 errors: str(nullable=True) = NULL
776 PyObject *str, *v; 748 /
777 const char *errors = NULL; 749 [clinic start generated code]*/
778 750
779 if (!PyArg_ParseTuple(args, "O|z:utf_8_encode", 751 static PyObject *
780 &str, &errors)) 752 _codecs_utf_8_encode_impl(PyModuleDef *module, PyObject *str,
781 return NULL; 753 const char *errors)
754 /*[clinic end generated code: output=ec831d80e7aedede input=5c283859a2a90bf7]*/
755 {
756 PyObject *v;
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
801 static PyObject * 776 /*[clinic input]
802 utf_16_encode(PyObject *self, 777 _codecs.utf_16_encode
803 PyObject *args) 778 str: object
804 { 779 errors: str(nullable=True) = NULL
805 PyObject *str, *v; 780 byteorder: int = 0
806 const char *errors = NULL; 781 /
807 int byteorder = 0; 782 [clinic start generated code]*/
808 783
809 if (!PyArg_ParseTuple(args, "O|zi:utf_16_encode", 784 static PyObject *
810 &str, &errors, &byteorder)) 785 _codecs_utf_16_encode_impl(PyModuleDef *module, PyObject *str,
811 return NULL; 786 const char *errors, int byteorder)
787 /*[clinic end generated code: output=93ac58e960a9ee4d input=ed850025591019db]*/
788 {
789 PyObject *v;
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
824 static PyObject * 802 /*[clinic input]
825 utf_16_le_encode(PyObject *self, 803 _codecs.utf_16_le_encode
826 PyObject *args) 804 str: object
827 { 805 errors: str(nullable=True) = NULL
828 PyObject *str, *v; 806 /
829 const char *errors = NULL; 807 [clinic start generated code]*/
830 808
831 if (!PyArg_ParseTuple(args, "O|z:utf_16_le_encode", 809 static PyObject *
832 &str, &errors)) 810 _codecs_utf_16_le_encode_impl(PyModuleDef *module, PyObject *str,
833 return NULL; 811 const char *errors)
812 /*[clinic end generated code: output=422bedb8da34fb66 input=950fcf919f117cb7]*/
813 {
814 PyObject *v;
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
846 static PyObject * 827 /*[clinic input]
847 utf_16_be_encode(PyObject *self, 828 _codecs.utf_16_be_encode
848 PyObject *args) 829 str: object
849 { 830 errors: str(nullable=True) = NULL
850 PyObject *str, *v; 831 /
851 const char *errors = NULL; 832 [clinic start generated code]*/
852 833
853 if (!PyArg_ParseTuple(args, "O|z:utf_16_be_encode", 834 static PyObject *
854 &str, &errors)) 835 _codecs_utf_16_be_encode_impl(PyModuleDef *module, PyObject *str,
855 return NULL; 836 const char *errors)
837 /*[clinic end generated code: output=3aa7ee9502acdd77 input=65a55a23d2a1e18d]*/
838 {
839 PyObject *v;
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
875 static PyObject * 859 /*[clinic input]
876 utf_32_encode(PyObject *self, 860 _codecs.utf_32_encode
877 PyObject *args) 861 str: object
878 { 862 errors: str(nullable=True) = NULL
879 PyObject *str, *v; 863 byteorder: int = 0
880 const char *errors = NULL; 864 /
881 int byteorder = 0; 865 [clinic start generated code]*/
882 866
883 if (!PyArg_ParseTuple(args, "O|zi:utf_32_encode", 867 static PyObject *
884 &str, &errors, &byteorder)) 868 _codecs_utf_32_encode_impl(PyModuleDef *module, PyObject *str,
885 return NULL; 869 const char *errors, int byteorder)
870 /*[clinic end generated code: output=3e7d5a003b02baed input=452ef1b8f0b8c3d5]*/
871 {
872 PyObject *v;
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
898 static PyObject * 885 /*[clinic input]
899 utf_32_le_encode(PyObject *self, 886 _codecs.utf_32_le_encode
900 PyObject *args) 887 str: object
901 { 888 errors: str(nullable=True) = NULL
902 PyObject *str, *v; 889 /
903 const char *errors = NULL; 890 [clinic start generated code]*/
904 891
905 if (!PyArg_ParseTuple(args, "O|z:utf_32_le_encode", 892 static PyObject *
906 &str, &errors)) 893 _codecs_utf_32_le_encode_impl(PyModuleDef *module, PyObject *str,
907 return NULL; 894 const char *errors)
895 /*[clinic end generated code: output=5dda641cd33dbfc2 input=a8e2cc9b4dbf40d9]*/
896 {
897 PyObject *v;
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
920 static PyObject * 910 /*[clinic input]
921 utf_32_be_encode(PyObject *self, 911 _codecs.utf_32_be_encode
922 PyObject *args) 912 str: object
923 { 913 errors: str(nullable=True) = NULL
924 PyObject *str, *v; 914 /
925 const char *errors = NULL; 915 [clinic start generated code]*/
926 916
927 if (!PyArg_ParseTuple(args, "O|z:utf_32_be_encode", 917 static PyObject *
928 &str, &errors)) 918 _codecs_utf_32_be_encode_impl(PyModuleDef *module, PyObject *str,
929 return NULL; 919 const char *errors)
920 /*[clinic end generated code: output=ccca8b44d91a7c7a input=cef51fb9de6a04b3]*/
921 {
922 PyObject *v;
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
942 static PyObject * 935 /*[clinic input]
943 unicode_escape_encode(PyObject *self, 936 _codecs.unicode_escape_encode
944 PyObject *args) 937 str: object
945 { 938 errors: str(nullable=True) = NULL
946 PyObject *str, *v; 939 /
947 const char *errors = NULL; 940 [clinic start generated code]*/
948 941
949 if (!PyArg_ParseTuple(args, "O|z:unicode_escape_encode", 942 static PyObject *
950 &str, &errors)) 943 _codecs_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str,
951 return NULL; 944 const char *errors)
945 /*[clinic end generated code: output=389f23d2b8f8d80b input=a9b4d66329b51e5b]*/
946 {
947 PyObject *v;
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
964 static PyObject * 960 /*[clinic input]
965 raw_unicode_escape_encode(PyObject *self, 961 _codecs.raw_unicode_escape_encode
966 PyObject *args) 962 str: object
967 { 963 errors: str(nullable=True) = NULL
968 PyObject *str, *v; 964 /
969 const char *errors = NULL; 965 [clinic start generated code]*/
970 966
971 if (!PyArg_ParseTuple(args, "O|z:raw_unicode_escape_encode", 967 static PyObject *
972 &str, &errors)) 968 _codecs_raw_unicode_escape_encode_impl(PyModuleDef *module, PyObject *str,
973 return NULL; 969 const char *errors)
970 /*[clinic end generated code: output=fec4e39d6ec37a62 input=022a856c6bbac275]*/
971 {
972 PyObject *v;
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
986 static PyObject * 985 /*[clinic input]
987 latin_1_encode(PyObject *self, 986 _codecs.latin_1_encode
988 PyObject *args) 987 str: object
989 { 988 errors: str(nullable=True) = NULL
990 PyObject *str, *v; 989 /
991 const char *errors = NULL; 990 [clinic start generated code]*/
992 991
993 if (!PyArg_ParseTuple(args, "O|z:latin_1_encode", 992 static PyObject *
994 &str, &errors)) 993 _codecs_latin_1_encode_impl(PyModuleDef *module, PyObject *str,
995 return NULL; 994 const char *errors)
995 /*[clinic end generated code: output=ecf00eb8e48c889c input=e2ec958e195e3f30]*/
996 {
997 PyObject *v;
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
1008 static PyObject * 1010 /*[clinic input]
1009 ascii_encode(PyObject *self, 1011 _codecs.ascii_encode
1010 PyObject *args) 1012 str: object
1011 { 1013 errors: str(nullable=True) = NULL
1012 PyObject *str, *v; 1014 /
1013 const char *errors = NULL; 1015 [clinic start generated code]*/
1014 1016
1015 if (!PyArg_ParseTuple(args, "O|z:ascii_encode", 1017 static PyObject *
1016 &str, &errors)) 1018 _codecs_ascii_encode_impl(PyModuleDef *module, PyObject *str,
1017 return NULL; 1019 const char *errors)
1020 /*[clinic end generated code: output=a9d18fc6b6b91cfb input=536afe3b176f737b]*/
1021 {
1022 PyObject *v;
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
1030 static PyObject * 1035 /*[clinic input]
1031 charmap_encode(PyObject *self, 1036 _codecs.charmap_encode
1032 PyObject *args) 1037 str: object
1033 { 1038 errors: str(nullable=True) = NULL
1034 PyObject *str, *v; 1039 mapping: object = NULL
1035 const char *errors = NULL; 1040 /
1036 PyObject *mapping = NULL; 1041 [clinic start generated code]*/
1037 1042
1038 if (!PyArg_ParseTuple(args, "O|zO:charmap_encode", 1043 static PyObject *
1039 &str, &errors, &mapping)) 1044 _codecs_charmap_encode_impl(PyModuleDef *module, PyObject *str,
1040 return NULL; 1045 const char *errors, PyObject *mapping)
1046 /*[clinic end generated code: output=14ca42b83853c643 input=6cfea1a4be31cde1]*/
1047 {
1048 PyObject *v;
1049
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
1057 { 1066 map: unicode
1058 PyObject *map; 1067 /
1059 if (!PyArg_ParseTuple(args, "U:charmap_build", &map)) 1068 [clinic start generated code]*/
1060 return NULL; 1069
1070 static PyObject *
1071 _codecs_charmap_build_impl(PyModuleDef *module, PyObject *map)
1072 /*[clinic end generated code: output=9485b58fa44afa6a input=d91a91d1717dbc6d]*/
1073 {
1061 return PyUnicode_BuildEncodingMap(map); 1074 return PyUnicode_BuildEncodingMap(map);
1062 } 1075 }
1063 1076
1064 #ifdef HAVE_MBCS 1077 #ifdef HAVE_MBCS
1065 1078
1066 static PyObject * 1079 /*[clinic input]
1067 mbcs_encode(PyObject *self, 1080 _codecs.mbcs_encode
1068 PyObject *args) 1081 str: object
1069 { 1082 errors: str(nullable=True) = NULL
1070 PyObject *str, *v; 1083 /
1071 const char *errors = NULL; 1084 [clinic start generated code]*/
1072 1085
1073 if (!PyArg_ParseTuple(args, "O|z:mbcs_encode", 1086 static PyObject *
1074 &str, &errors)) 1087 _codecs_mbcs_encode_impl(PyModuleDef *module, PyObject *str,
1075 return NULL; 1088 const char *errors)
1089 /*[clinic end generated code: output=d1a013bc68798bd7 input=30a1cf44d9315ec9]*/
1090 {
1091 PyObject *v;
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
1088 static PyObject * 1104 /*[clinic input]
1089 code_page_encode(PyObject *self, 1105 _codecs.code_page_encode
1090 PyObject *args) 1106 code_page: int
1091 { 1107 str: object
1092 PyObject *str, *v; 1108 errors: str(nullable=True) = NULL
1093 const char *errors = NULL; 1109 /
1094 int code_page; 1110 [clinic start generated code]*/
1095 1111
1096 if (!PyArg_ParseTuple(args, "iO|z:code_page_encode", 1112 static PyObject *
1097 &code_page, &str, &errors)) 1113 _codecs_code_page_encode_impl(PyModuleDef *module, int code_page,
1098 return NULL; 1114 PyObject *str, const char *errors)
1115 /*[clinic end generated code: output=3b406618dbfbce25 input=b0c72790c2e2ce28]*/
1116 {
1117 PyObject *v;
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\ 1141
1123 information about the location of the encoding/decoding\n\ 1142 Register the specified error handler under the name errors.
1124 error and must return a (replacement, new position) tuple."); 1143
1125 1144 handler must be a callable object, that will be called with an exception
1126 static PyObject *register_error(PyObject *self, PyObject *args) 1145 instance containing information about the location of the encoding/decoding
1127 { 1146 error and must return a (replacement, new position) tuple.
1128 const char *name; 1147 [clinic start generated code]*/
1129 PyObject *handler; 1148
1130 1149 static PyObject *
1131 if (!PyArg_ParseTuple(args, "sO:register_error", 1150 _codecs_register_error_impl(PyModuleDef *module, const char *errors,
1132 &name, &handler)) 1151 PyObject *handler)
1133 return NULL; 1152 /*[clinic end generated code: output=be00d3b1849ce68a input=5e6709203c2e33fe]*/
1134 if (PyCodec_RegisterError(name, handler)) 1153 {
1154 if (PyCodec_RegisterError(errors, 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."); 1163
1144 1164 lookup_error(errors) -> handler
1145 static PyObject *lookup_error(PyObject *self, PyObject *args) 1165
1146 { 1166 Return the error handler for the specified error handling name or raise a
1147 const char *name; 1167 LookupError, if no handler exists under this name.
1148 1168 [clinic start generated code]*/
1149 if (!PyArg_ParseTuple(args, "s:lookup_error", 1169
1150 &name)) 1170 static PyObject *
1151 return NULL; 1171 _codecs_lookup_error_impl(PyModuleDef *module, const char *name)
1172 /*[clinic end generated code: output=731e6df8c83c6158 input=4775dd65e6235aba]*/
1173 {
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 }
LEFTRIGHT

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