blob: 74445b03dc58793b91751cfc435e04bfed5078cd [file] [log] [blame]
Guido van Rossumfeee4b92000-03-10 22:57:27 +00001/* ------------------------------------------------------------------------
2
3 Python Codec Registry and support functions
4
5Written by Marc-Andre Lemburg (mal@lemburg.com).
6
Guido van Rossum16b1ad92000-08-03 16:24:25 +00007Copyright (c) Corporation for National Research Initiatives.
Guido van Rossumfeee4b92000-03-10 22:57:27 +00008
9 ------------------------------------------------------------------------ */
10
11#include "Python.h"
12#include <ctype.h>
13
Victor Stinnerf5cff562011-10-14 02:13:11 +020014const char *Py_hexdigits = "0123456789abcdef";
15
Guido van Rossumfeee4b92000-03-10 22:57:27 +000016/* --- Codec Registry ----------------------------------------------------- */
17
18/* Import the standard encodings package which will register the first
Guido van Rossum98297ee2007-11-06 21:34:58 +000019 codec search function.
Guido van Rossumfeee4b92000-03-10 22:57:27 +000020
21 This is done in a lazy way so that the Unicode implementation does
22 not downgrade startup time of scripts not needing it.
23
Guido van Rossumb95de4f2000-03-31 17:25:23 +000024 ImportErrors are silently ignored by this function. Only one try is
25 made.
Guido van Rossumfeee4b92000-03-10 22:57:27 +000026
27*/
28
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000029static int _PyCodecRegistry_Init(void); /* Forward */
Guido van Rossumfeee4b92000-03-10 22:57:27 +000030
Guido van Rossumfeee4b92000-03-10 22:57:27 +000031int PyCodec_Register(PyObject *search_function)
32{
Nicholas Bastine5662ae2004-03-24 22:22:12 +000033 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000034 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000036 if (search_function == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000037 PyErr_BadArgument();
38 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000039 }
40 if (!PyCallable_Check(search_function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000041 PyErr_SetString(PyExc_TypeError, "argument must be callable");
42 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000043 }
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000044 return PyList_Append(interp->codec_search_path, search_function);
Guido van Rossumb95de4f2000-03-31 17:25:23 +000045
46 onError:
47 return -1;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000048}
49
Guido van Rossum9e896b32000-04-05 20:11:21 +000050/* Convert a string to a normalized Python string: all characters are
51 converted to lower case, spaces are replaced with underscores. */
52
Guido van Rossumfeee4b92000-03-10 22:57:27 +000053static
Guido van Rossum9e896b32000-04-05 20:11:21 +000054PyObject *normalizestring(const char *string)
Guido van Rossumfeee4b92000-03-10 22:57:27 +000055{
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020056 size_t i;
Guido van Rossum582acec2000-06-28 22:07:35 +000057 size_t len = strlen(string);
Guido van Rossumfeee4b92000-03-10 22:57:27 +000058 char *p;
59 PyObject *v;
Guido van Rossum21431e82007-10-19 21:48:41 +000060
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000061 if (len > PY_SSIZE_T_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000062 PyErr_SetString(PyExc_OverflowError, "string is too large");
63 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000064 }
Guido van Rossum21431e82007-10-19 21:48:41 +000065
66 p = PyMem_Malloc(len + 1);
67 if (p == NULL)
Victor Stinnercc351592013-07-12 00:02:55 +020068 return PyErr_NoMemory();
Guido van Rossum9e896b32000-04-05 20:11:21 +000069 for (i = 0; i < len; i++) {
Antoine Pitrou9ed5f272013-08-13 20:18:52 +020070 char ch = string[i];
Guido van Rossum9e896b32000-04-05 20:11:21 +000071 if (ch == ' ')
72 ch = '-';
73 else
Antoine Pitroucf9d3c02011-07-24 02:27:04 +020074 ch = Py_TOLOWER(Py_CHARMASK(ch));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000075 p[i] = ch;
Guido van Rossum9e896b32000-04-05 20:11:21 +000076 }
Guido van Rossum21431e82007-10-19 21:48:41 +000077 p[i] = '\0';
78 v = PyUnicode_FromString(p);
79 if (v == NULL)
80 return NULL;
81 PyMem_Free(p);
Guido van Rossumfeee4b92000-03-10 22:57:27 +000082 return v;
83}
84
85/* Lookup the given encoding and return a tuple providing the codec
86 facilities.
87
88 The encoding string is looked up converted to all lower-case
89 characters. This makes encodings looked up through this mechanism
90 effectively case-insensitive.
91
Guido van Rossum98297ee2007-11-06 21:34:58 +000092 If no codec is found, a LookupError is set and NULL returned.
Guido van Rossumb95de4f2000-03-31 17:25:23 +000093
94 As side effect, this tries to load the encodings package, if not
95 yet done. This is part of the lazy load strategy for the encodings
96 package.
97
98*/
Guido van Rossumfeee4b92000-03-10 22:57:27 +000099
100PyObject *_PyCodec_Lookup(const char *encoding)
101{
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000102 PyInterpreterState *interp;
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000103 PyObject *result, *args = NULL, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000104 Py_ssize_t i, len;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000105
Fred Drake766de832000-05-09 19:55:59 +0000106 if (encoding == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000107 PyErr_BadArgument();
108 goto onError;
Fred Drake766de832000-05-09 19:55:59 +0000109 }
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000110
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000111 interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000112 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000113 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000114
Guido van Rossum9e896b32000-04-05 20:11:21 +0000115 /* Convert the encoding to a normalized Python string: all
Thomas Wouters7e474022000-07-16 12:04:32 +0000116 characters are converted to lower case, spaces and hyphens are
Guido van Rossum9e896b32000-04-05 20:11:21 +0000117 replaced with underscores. */
118 v = normalizestring(encoding);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000119 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000120 goto onError;
Guido van Rossum21431e82007-10-19 21:48:41 +0000121 PyUnicode_InternInPlace(&v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000122
123 /* First, try to lookup the name in the registry dictionary */
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000124 result = PyDict_GetItem(interp->codec_search_cache, v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000125 if (result != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000126 Py_INCREF(result);
127 Py_DECREF(v);
128 return result;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000129 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000130
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000131 /* Next, scan the search functions in order of registration */
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000132 args = PyTuple_New(1);
133 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000134 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000135 PyTuple_SET_ITEM(args,0,v);
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000136
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000137 len = PyList_Size(interp->codec_search_path);
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000138 if (len < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 goto onError;
Guido van Rossumb95de4f2000-03-31 17:25:23 +0000140 if (len == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000141 PyErr_SetString(PyExc_LookupError,
142 "no codec search functions registered: "
143 "can't find encoding");
144 goto onError;
Guido van Rossumb95de4f2000-03-31 17:25:23 +0000145 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000146
147 for (i = 0; i < len; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 PyObject *func;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000149
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000150 func = PyList_GetItem(interp->codec_search_path, i);
151 if (func == NULL)
152 goto onError;
153 result = PyEval_CallObject(func, args);
154 if (result == NULL)
155 goto onError;
156 if (result == Py_None) {
157 Py_DECREF(result);
158 continue;
159 }
160 if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
161 PyErr_SetString(PyExc_TypeError,
162 "codec search functions must return 4-tuples");
163 Py_DECREF(result);
164 goto onError;
165 }
166 break;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000167 }
168 if (i == len) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000169 /* XXX Perhaps we should cache misses too ? */
170 PyErr_Format(PyExc_LookupError,
Martin v. Löwiseb42b022002-09-26 16:01:24 +0000171 "unknown encoding: %s", encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000172 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000173 }
174
175 /* Cache and return the result */
Neal Norwitz9edcc2e2007-08-11 04:58:26 +0000176 if (PyDict_SetItem(interp->codec_search_cache, v, result) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000177 Py_DECREF(result);
178 goto onError;
Neal Norwitz9edcc2e2007-08-11 04:58:26 +0000179 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000180 Py_DECREF(args);
181 return result;
182
183 onError:
184 Py_XDECREF(args);
185 return NULL;
186}
187
Nick Coghlan8fad1672014-09-15 23:50:44 +1200188int _PyCodec_Forget(const char *encoding)
189{
190 PyInterpreterState *interp;
191 PyObject *v;
192 int result;
193
194 interp = PyThreadState_GET()->interp;
195 if (interp->codec_search_path == NULL) {
196 return -1;
197 }
198
199 /* Convert the encoding to a normalized Python string: all
200 characters are converted to lower case, spaces and hyphens are
201 replaced with underscores. */
202 v = normalizestring(encoding);
203 if (v == NULL) {
204 return -1;
205 }
206
207 /* Drop the named codec from the internal cache */
208 result = PyDict_DelItem(interp->codec_search_cache, v);
209 Py_DECREF(v);
210
211 return result;
212}
213
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000214/* Codec registry encoding check API. */
215
216int PyCodec_KnownEncoding(const char *encoding)
217{
218 PyObject *codecs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000220 codecs = _PyCodec_Lookup(encoding);
221 if (!codecs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000222 PyErr_Clear();
223 return 0;
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000224 }
225 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000226 Py_DECREF(codecs);
227 return 1;
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000228 }
229}
230
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000231static
232PyObject *args_tuple(PyObject *object,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000233 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000234{
235 PyObject *args;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000236
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000237 args = PyTuple_New(1 + (errors != NULL));
238 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 return NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000240 Py_INCREF(object);
241 PyTuple_SET_ITEM(args,0,object);
242 if (errors) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000243 PyObject *v;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000244
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000245 v = PyUnicode_FromString(errors);
246 if (v == NULL) {
247 Py_DECREF(args);
248 return NULL;
249 }
250 PyTuple_SET_ITEM(args, 1, v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000251 }
252 return args;
253}
254
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000255/* Helper function to get a codec item */
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000256
257static
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000258PyObject *codec_getitem(const char *encoding, int index)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000259{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260 PyObject *codecs;
261 PyObject *v;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000262
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000263 codecs = _PyCodec_Lookup(encoding);
264 if (codecs == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266 v = PyTuple_GET_ITEM(codecs, index);
267 Py_DECREF(codecs);
268 Py_INCREF(v);
269 return v;
270}
271
Nick Coghlana9b15242014-02-04 22:11:18 +1000272/* Helper functions to create an incremental codec. */
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000273static
Nick Coghlana9b15242014-02-04 22:11:18 +1000274PyObject *codec_makeincrementalcodec(PyObject *codec_info,
275 const char *errors,
276 const char *attrname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000277{
Nick Coghlana9b15242014-02-04 22:11:18 +1000278 PyObject *ret, *inccodec;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000279
Nick Coghlana9b15242014-02-04 22:11:18 +1000280 inccodec = PyObject_GetAttrString(codec_info, attrname);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281 if (inccodec == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283 if (errors)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 ret = PyObject_CallFunction(inccodec, "s", errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000285 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 ret = PyObject_CallFunction(inccodec, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 Py_DECREF(inccodec);
288 return ret;
289}
290
Nick Coghlana9b15242014-02-04 22:11:18 +1000291static
292PyObject *codec_getincrementalcodec(const char *encoding,
293 const char *errors,
294 const char *attrname)
295{
296 PyObject *codec_info, *ret;
297
298 codec_info = _PyCodec_Lookup(encoding);
299 if (codec_info == NULL)
300 return NULL;
301 ret = codec_makeincrementalcodec(codec_info, errors, attrname);
302 Py_DECREF(codec_info);
303 return ret;
304}
305
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000306/* Helper function to create a stream codec. */
307
308static
309PyObject *codec_getstreamcodec(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 PyObject *stream,
311 const char *errors,
312 const int index)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000313{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000314 PyObject *codecs, *streamcodec, *codeccls;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000315
316 codecs = _PyCodec_Lookup(encoding);
317 if (codecs == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000318 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000319
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000320 codeccls = PyTuple_GET_ITEM(codecs, index);
321 if (errors != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000323 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000324 streamcodec = PyObject_CallFunction(codeccls, "O", stream);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 Py_DECREF(codecs);
326 return streamcodec;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000327}
328
Nick Coghlana9b15242014-02-04 22:11:18 +1000329/* Helpers to work with the result of _PyCodec_Lookup
330
331 */
332PyObject *_PyCodecInfo_GetIncrementalDecoder(PyObject *codec_info,
333 const char *errors)
334{
335 return codec_makeincrementalcodec(codec_info, errors,
336 "incrementaldecoder");
337}
338
339PyObject *_PyCodecInfo_GetIncrementalEncoder(PyObject *codec_info,
340 const char *errors)
341{
342 return codec_makeincrementalcodec(codec_info, errors,
343 "incrementalencoder");
344}
345
346
Guido van Rossum98297ee2007-11-06 21:34:58 +0000347/* Convenience APIs to query the Codec registry.
348
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000349 All APIs return a codec object with incremented refcount.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000350
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000351 */
352
353PyObject *PyCodec_Encoder(const char *encoding)
354{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000355 return codec_getitem(encoding, 0);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000356}
357
358PyObject *PyCodec_Decoder(const char *encoding)
359{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000360 return codec_getitem(encoding, 1);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000361}
362
Thomas Woutersa9773292006-04-21 09:43:23 +0000363PyObject *PyCodec_IncrementalEncoder(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000364 const char *errors)
Thomas Woutersa9773292006-04-21 09:43:23 +0000365{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000366 return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
Thomas Woutersa9773292006-04-21 09:43:23 +0000367}
368
369PyObject *PyCodec_IncrementalDecoder(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000370 const char *errors)
Thomas Woutersa9773292006-04-21 09:43:23 +0000371{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000372 return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
Thomas Woutersa9773292006-04-21 09:43:23 +0000373}
374
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000375PyObject *PyCodec_StreamReader(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000376 PyObject *stream,
377 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000378{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000379 return codec_getstreamcodec(encoding, stream, errors, 2);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000380}
381
382PyObject *PyCodec_StreamWriter(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000383 PyObject *stream,
384 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000385{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000386 return codec_getstreamcodec(encoding, stream, errors, 3);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000387}
388
Nick Coghlan8b097b42013-11-13 23:49:21 +1000389/* Helper that tries to ensure the reported exception chain indicates the
390 * codec that was invoked to trigger the failure without changing the type
391 * of the exception raised.
392 */
393static void
394wrap_codec_error(const char *operation,
395 const char *encoding)
396{
397 /* TrySetFromCause will replace the active exception with a suitably
398 * updated clone if it can, otherwise it will leave the original
399 * exception alone.
400 */
401 _PyErr_TrySetFromCause("%s with '%s' codec failed",
402 operation, encoding);
403}
404
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000405/* Encode an object (e.g. an Unicode object) using the given encoding
406 and return the resulting encoded object (usually a Python string).
407
408 errors is passed to the encoder factory as argument if non-NULL. */
409
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000410static PyObject *
411_PyCodec_EncodeInternal(PyObject *object,
412 PyObject *encoder,
413 const char *encoding,
414 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000415{
Neal Norwitz3715c3e2005-11-24 22:09:18 +0000416 PyObject *args = NULL, *result = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000417 PyObject *v = NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000418
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000419 args = args_tuple(object, errors);
420 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000421 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000422
423 result = PyEval_CallObject(encoder, args);
Nick Coghlanc4c25802013-11-15 21:47:37 +1000424 if (result == NULL) {
425 wrap_codec_error("encoding", encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000426 goto onError;
Nick Coghlanc4c25802013-11-15 21:47:37 +1000427 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000428
Guido van Rossum98297ee2007-11-06 21:34:58 +0000429 if (!PyTuple_Check(result) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000430 PyTuple_GET_SIZE(result) != 2) {
431 PyErr_SetString(PyExc_TypeError,
432 "encoder must return a tuple (object, integer)");
433 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000434 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000435 v = PyTuple_GET_ITEM(result,0);
436 Py_INCREF(v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000437 /* We don't check or use the second (integer) entry. */
438
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000439 Py_DECREF(args);
440 Py_DECREF(encoder);
441 Py_DECREF(result);
442 return v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000443
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000444 onError:
Neal Norwitz3715c3e2005-11-24 22:09:18 +0000445 Py_XDECREF(result);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000446 Py_XDECREF(args);
447 Py_XDECREF(encoder);
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000448 return NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000449}
450
451/* Decode an object (usually a Python string) using the given encoding
452 and return an equivalent object (e.g. an Unicode object).
453
454 errors is passed to the decoder factory as argument if non-NULL. */
455
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000456static PyObject *
457_PyCodec_DecodeInternal(PyObject *object,
458 PyObject *decoder,
459 const char *encoding,
460 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000461{
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000462 PyObject *args = NULL, *result = NULL;
463 PyObject *v;
464
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000465 args = args_tuple(object, errors);
466 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000467 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000468
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000469 result = PyEval_CallObject(decoder,args);
Nick Coghlanc4c25802013-11-15 21:47:37 +1000470 if (result == NULL) {
471 wrap_codec_error("decoding", encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000472 goto onError;
Nick Coghlanc4c25802013-11-15 21:47:37 +1000473 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000474 if (!PyTuple_Check(result) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000475 PyTuple_GET_SIZE(result) != 2) {
476 PyErr_SetString(PyExc_TypeError,
477 "decoder must return a tuple (object,integer)");
478 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000479 }
480 v = PyTuple_GET_ITEM(result,0);
481 Py_INCREF(v);
482 /* We don't check or use the second (integer) entry. */
483
484 Py_DECREF(args);
485 Py_DECREF(decoder);
486 Py_DECREF(result);
487 return v;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000488
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000489 onError:
490 Py_XDECREF(args);
491 Py_XDECREF(decoder);
492 Py_XDECREF(result);
493 return NULL;
494}
495
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000496/* Generic encoding/decoding API */
497PyObject *PyCodec_Encode(PyObject *object,
498 const char *encoding,
499 const char *errors)
500{
501 PyObject *encoder;
502
503 encoder = PyCodec_Encoder(encoding);
504 if (encoder == NULL)
505 return NULL;
506
507 return _PyCodec_EncodeInternal(object, encoder, encoding, errors);
508}
509
510PyObject *PyCodec_Decode(PyObject *object,
511 const char *encoding,
512 const char *errors)
513{
514 PyObject *decoder;
515
516 decoder = PyCodec_Decoder(encoding);
517 if (decoder == NULL)
518 return NULL;
519
520 return _PyCodec_DecodeInternal(object, decoder, encoding, errors);
521}
522
523/* Text encoding/decoding API */
Nick Coghlana9b15242014-02-04 22:11:18 +1000524PyObject * _PyCodec_LookupTextEncoding(const char *encoding,
525 const char *alternate_command)
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000526{
527 _Py_IDENTIFIER(_is_text_encoding);
528 PyObject *codec;
529 PyObject *attr;
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000530 int is_text_codec;
531
532 codec = _PyCodec_Lookup(encoding);
533 if (codec == NULL)
534 return NULL;
535
536 /* Backwards compatibility: assume any raw tuple describes a text
537 * encoding, and the same for anything lacking the private
538 * attribute.
539 */
540 if (!PyTuple_CheckExact(codec)) {
541 attr = _PyObject_GetAttrId(codec, &PyId__is_text_encoding);
542 if (attr == NULL) {
543 if (PyErr_ExceptionMatches(PyExc_AttributeError)) {
544 PyErr_Clear();
545 } else {
546 Py_DECREF(codec);
547 return NULL;
548 }
549 } else {
550 is_text_codec = PyObject_IsTrue(attr);
551 Py_DECREF(attr);
552 if (!is_text_codec) {
553 Py_DECREF(codec);
554 PyErr_Format(PyExc_LookupError,
555 "'%.400s' is not a text encoding; "
Nick Coghlana9b15242014-02-04 22:11:18 +1000556 "use %s to handle arbitrary codecs",
557 encoding, alternate_command);
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000558 return NULL;
559 }
560 }
561 }
562
Nick Coghlana9b15242014-02-04 22:11:18 +1000563 /* This appears to be a valid text encoding */
564 return codec;
565}
566
567
568static
569PyObject *codec_getitem_checked(const char *encoding,
570 const char *alternate_command,
571 int index)
572{
573 PyObject *codec;
574 PyObject *v;
575
576 codec = _PyCodec_LookupTextEncoding(encoding, alternate_command);
577 if (codec == NULL)
578 return NULL;
579
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000580 v = PyTuple_GET_ITEM(codec, index);
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000581 Py_INCREF(v);
Nick Coghlana9b15242014-02-04 22:11:18 +1000582 Py_DECREF(codec);
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000583 return v;
584}
585
586static PyObject * _PyCodec_TextEncoder(const char *encoding)
587{
Nick Coghlana9b15242014-02-04 22:11:18 +1000588 return codec_getitem_checked(encoding, "codecs.encode()", 0);
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000589}
590
591static PyObject * _PyCodec_TextDecoder(const char *encoding)
592{
Nick Coghlana9b15242014-02-04 22:11:18 +1000593 return codec_getitem_checked(encoding, "codecs.decode()", 1);
Nick Coghlanc72e4e62013-11-22 22:39:36 +1000594}
595
596PyObject *_PyCodec_EncodeText(PyObject *object,
597 const char *encoding,
598 const char *errors)
599{
600 PyObject *encoder;
601
602 encoder = _PyCodec_TextEncoder(encoding);
603 if (encoder == NULL)
604 return NULL;
605
606 return _PyCodec_EncodeInternal(object, encoder, encoding, errors);
607}
608
609PyObject *_PyCodec_DecodeText(PyObject *object,
610 const char *encoding,
611 const char *errors)
612{
613 PyObject *decoder;
614
615 decoder = _PyCodec_TextDecoder(encoding);
616 if (decoder == NULL)
617 return NULL;
618
619 return _PyCodec_DecodeInternal(object, decoder, encoding, errors);
620}
621
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000622/* Register the error handling callback function error under the name
623 name. This function will be called by the codec when it encounters
624 an unencodable characters/undecodable bytes and doesn't know the
625 callback name, when name is specified as the error parameter
626 in the call to the encode/decode function.
627 Return 0 on success, -1 on error */
628int PyCodec_RegisterError(const char *name, PyObject *error)
629{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000630 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000631 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000632 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000633 if (!PyCallable_Check(error)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000634 PyErr_SetString(PyExc_TypeError, "handler must be callable");
635 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000636 }
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000637 return PyDict_SetItemString(interp->codec_error_registry,
Serhiy Storchakac6792272013-10-19 21:03:34 +0300638 name, error);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000639}
640
641/* Lookup the error handling callback function registered under the
642 name error. As a special case NULL can be passed, in which case
643 the error handling callback for strict encoding will be returned. */
644PyObject *PyCodec_LookupError(const char *name)
645{
646 PyObject *handler = NULL;
647
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000648 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000649 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000650 return NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000651
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000652 if (name==NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000653 name = "strict";
Serhiy Storchakac6792272013-10-19 21:03:34 +0300654 handler = PyDict_GetItemString(interp->codec_error_registry, name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000655 if (!handler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000657 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000658 Py_INCREF(handler);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000659 return handler;
660}
661
662static void wrong_exception_type(PyObject *exc)
663{
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300664 PyErr_Format(PyExc_TypeError,
665 "don't know how to handle %.200s in error callback",
666 exc->ob_type->tp_name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000667}
668
669PyObject *PyCodec_StrictErrors(PyObject *exc)
670{
Brett Cannonbf364092006-03-01 04:25:17 +0000671 if (PyExceptionInstance_Check(exc))
672 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000673 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000674 PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000675 return NULL;
676}
677
678
679PyObject *PyCodec_IgnoreErrors(PyObject *exc)
680{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000681 Py_ssize_t end;
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300682
683 if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000684 if (PyUnicodeEncodeError_GetEnd(exc, &end))
685 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000686 }
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300687 else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 if (PyUnicodeDecodeError_GetEnd(exc, &end))
689 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000690 }
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300691 else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000692 if (PyUnicodeTranslateError_GetEnd(exc, &end))
693 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000694 }
695 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000696 wrong_exception_type(exc);
697 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000698 }
Victor Stinneree450092011-12-01 02:52:11 +0100699 return Py_BuildValue("(Nn)", PyUnicode_New(0, 0), end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000700}
701
702
703PyObject *PyCodec_ReplaceErrors(PyObject *exc)
704{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200705 Py_ssize_t start, end, i, len;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000706
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300707 if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200709 int kind;
710 void *data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000711 if (PyUnicodeEncodeError_GetStart(exc, &start))
712 return NULL;
713 if (PyUnicodeEncodeError_GetEnd(exc, &end))
714 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200715 len = end - start;
716 res = PyUnicode_New(len, '?');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 if (res == NULL)
718 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200719 kind = PyUnicode_KIND(res);
720 data = PyUnicode_DATA(res);
721 for (i = 0; i < len; ++i)
722 PyUnicode_WRITE(kind, data, i, '?');
Victor Stinner8f825062012-04-27 13:55:39 +0200723 assert(_PyUnicode_CheckConsistency(res, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200724 return Py_BuildValue("(Nn)", res, end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000725 }
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300726 else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 if (PyUnicodeDecodeError_GetEnd(exc, &end))
728 return NULL;
Victor Stinner1a15aba2011-10-02 19:00:15 +0200729 return Py_BuildValue("(Cn)",
730 (int)Py_UNICODE_REPLACEMENT_CHARACTER,
731 end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000732 }
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300733 else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000734 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200735 int kind;
736 void *data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000737 if (PyUnicodeTranslateError_GetStart(exc, &start))
738 return NULL;
739 if (PyUnicodeTranslateError_GetEnd(exc, &end))
740 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200741 len = end - start;
742 res = PyUnicode_New(len, Py_UNICODE_REPLACEMENT_CHARACTER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000743 if (res == NULL)
744 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200745 kind = PyUnicode_KIND(res);
746 data = PyUnicode_DATA(res);
747 for (i=0; i < len; i++)
748 PyUnicode_WRITE(kind, data, i, Py_UNICODE_REPLACEMENT_CHARACTER);
Victor Stinner8f825062012-04-27 13:55:39 +0200749 assert(_PyUnicode_CheckConsistency(res, 1));
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200750 return Py_BuildValue("(Nn)", res, end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000751 }
752 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000753 wrong_exception_type(exc);
754 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000755 }
756}
757
758PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
759{
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300760 if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000761 PyObject *restuple;
762 PyObject *object;
Victor Stinnerb31f1bc2011-11-04 21:29:10 +0100763 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000764 Py_ssize_t start;
765 Py_ssize_t end;
766 PyObject *res;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100767 unsigned char *outp;
Serhiy Storchaka2e374092014-10-04 14:15:49 +0300768 Py_ssize_t ressize;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100769 Py_UCS4 ch;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000770 if (PyUnicodeEncodeError_GetStart(exc, &start))
771 return NULL;
772 if (PyUnicodeEncodeError_GetEnd(exc, &end))
773 return NULL;
774 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
775 return NULL;
Serhiy Storchaka2e374092014-10-04 14:15:49 +0300776 if (end - start > PY_SSIZE_T_MAX / (2+7+1))
777 end = start + PY_SSIZE_T_MAX / (2+7+1);
Martin v. Löwisb09af032011-11-04 11:16:41 +0100778 for (i = start, ressize = 0; i < end; ++i) {
779 /* object is guaranteed to be "ready" */
780 ch = PyUnicode_READ_CHAR(object, i);
781 if (ch<10)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000782 ressize += 2+1+1;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100783 else if (ch<100)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000784 ressize += 2+2+1;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100785 else if (ch<1000)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000786 ressize += 2+3+1;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100787 else if (ch<10000)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000788 ressize += 2+4+1;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100789 else if (ch<100000)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000790 ressize += 2+5+1;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100791 else if (ch<1000000)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000792 ressize += 2+6+1;
793 else
794 ressize += 2+7+1;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000795 }
796 /* allocate replacement */
Martin v. Löwisb09af032011-11-04 11:16:41 +0100797 res = PyUnicode_New(ressize, 127);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000798 if (res == NULL) {
799 Py_DECREF(object);
800 return NULL;
801 }
Martin v. Löwisb09af032011-11-04 11:16:41 +0100802 outp = PyUnicode_1BYTE_DATA(res);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000803 /* generate replacement */
Victor Stinnerb31f1bc2011-11-04 21:29:10 +0100804 for (i = start; i < end; ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000805 int digits;
806 int base;
Martin v. Löwis8ba79302011-11-04 12:26:49 +0100807 ch = PyUnicode_READ_CHAR(object, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 *outp++ = '&';
809 *outp++ = '#';
Martin v. Löwisb09af032011-11-04 11:16:41 +0100810 if (ch<10) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000811 digits = 1;
812 base = 1;
813 }
Martin v. Löwisb09af032011-11-04 11:16:41 +0100814 else if (ch<100) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000815 digits = 2;
816 base = 10;
817 }
Martin v. Löwisb09af032011-11-04 11:16:41 +0100818 else if (ch<1000) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000819 digits = 3;
820 base = 100;
821 }
Martin v. Löwisb09af032011-11-04 11:16:41 +0100822 else if (ch<10000) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000823 digits = 4;
824 base = 1000;
825 }
Martin v. Löwisb09af032011-11-04 11:16:41 +0100826 else if (ch<100000) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000827 digits = 5;
828 base = 10000;
829 }
Martin v. Löwisb09af032011-11-04 11:16:41 +0100830 else if (ch<1000000) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000831 digits = 6;
832 base = 100000;
833 }
834 else {
835 digits = 7;
836 base = 1000000;
837 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 while (digits-->0) {
Martin v. Löwisb09af032011-11-04 11:16:41 +0100839 *outp++ = '0' + ch/base;
840 ch %= base;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000841 base /= 10;
842 }
843 *outp++ = ';';
844 }
Victor Stinner8f825062012-04-27 13:55:39 +0200845 assert(_PyUnicode_CheckConsistency(res, 1));
846 restuple = Py_BuildValue("(Nn)", res, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000847 Py_DECREF(object);
848 return restuple;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000849 }
850 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000851 wrong_exception_type(exc);
852 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000853 }
854}
855
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000856PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
857{
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +0300858 if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000859 PyObject *restuple;
860 PyObject *object;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100861 Py_ssize_t i;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000862 Py_ssize_t start;
863 Py_ssize_t end;
864 PyObject *res;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100865 unsigned char *outp;
Serhiy Storchaka2e374092014-10-04 14:15:49 +0300866 Py_ssize_t ressize;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100867 Py_UCS4 c;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000868 if (PyUnicodeEncodeError_GetStart(exc, &start))
869 return NULL;
870 if (PyUnicodeEncodeError_GetEnd(exc, &end))
871 return NULL;
872 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
873 return NULL;
Serhiy Storchaka2e374092014-10-04 14:15:49 +0300874 if (end - start > PY_SSIZE_T_MAX / (1+1+8))
875 end = start + PY_SSIZE_T_MAX / (1+1+8);
Martin v. Löwisb09af032011-11-04 11:16:41 +0100876 for (i = start, ressize = 0; i < end; ++i) {
877 /* object is guaranteed to be "ready" */
878 c = PyUnicode_READ_CHAR(object, i);
879 if (c >= 0x10000) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000880 ressize += 1+1+8;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100881 }
882 else if (c >= 0x100) {
883 ressize += 1+1+4;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000884 }
885 else
886 ressize += 1+1+2;
887 }
Martin v. Löwisb09af032011-11-04 11:16:41 +0100888 res = PyUnicode_New(ressize, 127);
Serhiy Storchaka8aa8c472014-09-23 19:59:09 +0300889 if (res == NULL) {
890 Py_DECREF(object);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000891 return NULL;
Serhiy Storchaka8aa8c472014-09-23 19:59:09 +0300892 }
Martin v. Löwisb09af032011-11-04 11:16:41 +0100893 for (i = start, outp = PyUnicode_1BYTE_DATA(res);
894 i < end; ++i) {
895 c = PyUnicode_READ_CHAR(object, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000896 *outp++ = '\\';
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000897 if (c >= 0x00010000) {
898 *outp++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200899 *outp++ = Py_hexdigits[(c>>28)&0xf];
900 *outp++ = Py_hexdigits[(c>>24)&0xf];
901 *outp++ = Py_hexdigits[(c>>20)&0xf];
902 *outp++ = Py_hexdigits[(c>>16)&0xf];
903 *outp++ = Py_hexdigits[(c>>12)&0xf];
904 *outp++ = Py_hexdigits[(c>>8)&0xf];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000905 }
Antoine Pitroue4a18922010-09-09 20:30:23 +0000906 else if (c >= 0x100) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000907 *outp++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200908 *outp++ = Py_hexdigits[(c>>12)&0xf];
909 *outp++ = Py_hexdigits[(c>>8)&0xf];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000910 }
911 else
912 *outp++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200913 *outp++ = Py_hexdigits[(c>>4)&0xf];
914 *outp++ = Py_hexdigits[c&0xf];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000915 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000916
Victor Stinner8f825062012-04-27 13:55:39 +0200917 assert(_PyUnicode_CheckConsistency(res, 1));
918 restuple = Py_BuildValue("(Nn)", res, end);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919 Py_DECREF(object);
920 return restuple;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000921 }
922 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000923 wrong_exception_type(exc);
924 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000925 }
926}
927
Serhiy Storchaka58cf6072013-11-19 11:32:41 +0200928#define ENC_UTF8 0
929#define ENC_UTF16BE 1
930#define ENC_UTF16LE 2
931#define ENC_UTF32BE 3
932#define ENC_UTF32LE 4
933
934static int
935get_standard_encoding(const char *encoding, int *bytelength)
936{
937 if (Py_TOLOWER(encoding[0]) == 'u' &&
938 Py_TOLOWER(encoding[1]) == 't' &&
939 Py_TOLOWER(encoding[2]) == 'f') {
940 encoding += 3;
941 if (*encoding == '-' || *encoding == '_' )
942 encoding++;
943 if (encoding[0] == '1' && encoding[1] == '6') {
944 encoding += 2;
945 *bytelength = 2;
946 if (*encoding == '\0') {
947#ifdef WORDS_BIGENDIAN
948 return ENC_UTF16BE;
949#else
950 return ENC_UTF16LE;
951#endif
952 }
953 if (*encoding == '-' || *encoding == '_' )
954 encoding++;
955 if (Py_TOLOWER(encoding[1]) == 'e' && encoding[2] == '\0') {
956 if (Py_TOLOWER(encoding[0]) == 'b')
957 return ENC_UTF16BE;
958 if (Py_TOLOWER(encoding[0]) == 'l')
959 return ENC_UTF16LE;
960 }
961 }
962 else if (encoding[0] == '3' && encoding[1] == '2') {
963 encoding += 2;
964 *bytelength = 4;
965 if (*encoding == '\0') {
966#ifdef WORDS_BIGENDIAN
967 return ENC_UTF32BE;
968#else
969 return ENC_UTF32LE;
970#endif
971 }
972 if (*encoding == '-' || *encoding == '_' )
973 encoding++;
974 if (Py_TOLOWER(encoding[1]) == 'e' && encoding[2] == '\0') {
975 if (Py_TOLOWER(encoding[0]) == 'b')
976 return ENC_UTF32BE;
977 if (Py_TOLOWER(encoding[0]) == 'l')
978 return ENC_UTF32LE;
979 }
980 }
981 }
982 /* utf-8 */
983 *bytelength = 3;
984 return ENC_UTF8;
985}
986
Martin v. Löwisaef3fb02009-05-02 19:27:30 +0000987/* This handler is declared static until someone demonstrates
988 a need to call it directly. */
989static PyObject *
Martin v. Löwise0a2b722009-05-10 08:08:56 +0000990PyCodec_SurrogatePassErrors(PyObject *exc)
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000991{
992 PyObject *restuple;
993 PyObject *object;
Serhiy Storchaka58cf6072013-11-19 11:32:41 +0200994 PyObject *encode;
995 char *encoding;
996 int code;
997 int bytelength;
Martin v. Löwisb09af032011-11-04 11:16:41 +0100998 Py_ssize_t i;
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000999 Py_ssize_t start;
1000 Py_ssize_t end;
1001 PyObject *res;
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +03001002
1003 if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001004 unsigned char *outp;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001005 if (PyUnicodeEncodeError_GetStart(exc, &start))
1006 return NULL;
1007 if (PyUnicodeEncodeError_GetEnd(exc, &end))
1008 return NULL;
1009 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
1010 return NULL;
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001011 if (!(encode = PyUnicodeEncodeError_GetEncoding(exc))) {
1012 Py_DECREF(object);
1013 return NULL;
1014 }
1015 if (!(encoding = PyUnicode_AsUTF8(encode))) {
1016 Py_DECREF(object);
1017 Py_DECREF(encode);
1018 return NULL;
1019 }
1020 code = get_standard_encoding(encoding, &bytelength);
1021 Py_DECREF(encode);
1022
Serhiy Storchaka2e374092014-10-04 14:15:49 +03001023 if (end - start > PY_SSIZE_T_MAX / bytelength)
1024 end = start + PY_SSIZE_T_MAX / bytelength;
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001025 res = PyBytes_FromStringAndSize(NULL, bytelength*(end-start));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001026 if (!res) {
1027 Py_DECREF(object);
1028 return NULL;
1029 }
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001030 outp = (unsigned char*)PyBytes_AsString(res);
Martin v. Löwisb09af032011-11-04 11:16:41 +01001031 for (i = start; i < end; i++) {
1032 /* object is guaranteed to be "ready" */
1033 Py_UCS4 ch = PyUnicode_READ_CHAR(object, i);
Victor Stinner76df43d2012-10-30 01:42:39 +01001034 if (!Py_UNICODE_IS_SURROGATE(ch)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001035 /* Not a surrogate, fail with original exception */
1036 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
1037 Py_DECREF(res);
1038 Py_DECREF(object);
1039 return NULL;
1040 }
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001041 switch (code) {
1042 case ENC_UTF8:
1043 *outp++ = (unsigned char)(0xe0 | (ch >> 12));
1044 *outp++ = (unsigned char)(0x80 | ((ch >> 6) & 0x3f));
1045 *outp++ = (unsigned char)(0x80 | (ch & 0x3f));
1046 break;
1047 case ENC_UTF16LE:
1048 *outp++ = (unsigned char) ch;
1049 *outp++ = (unsigned char)(ch >> 8);
1050 break;
1051 case ENC_UTF16BE:
1052 *outp++ = (unsigned char)(ch >> 8);
1053 *outp++ = (unsigned char) ch;
1054 break;
1055 case ENC_UTF32LE:
1056 *outp++ = (unsigned char) ch;
1057 *outp++ = (unsigned char)(ch >> 8);
1058 *outp++ = (unsigned char)(ch >> 16);
1059 *outp++ = (unsigned char)(ch >> 24);
1060 break;
1061 case ENC_UTF32BE:
1062 *outp++ = (unsigned char)(ch >> 24);
1063 *outp++ = (unsigned char)(ch >> 16);
1064 *outp++ = (unsigned char)(ch >> 8);
1065 *outp++ = (unsigned char) ch;
1066 break;
1067 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001068 }
1069 restuple = Py_BuildValue("(On)", res, end);
1070 Py_DECREF(res);
1071 Py_DECREF(object);
1072 return restuple;
Martin v. Löwisdb12d452009-05-02 18:52:14 +00001073 }
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +03001074 else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001075 unsigned char *p;
Victor Stinnerc06bb7a2011-11-04 21:36:35 +01001076 Py_UCS4 ch = 0;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001077 if (PyUnicodeDecodeError_GetStart(exc, &start))
1078 return NULL;
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001079 if (PyUnicodeDecodeError_GetEnd(exc, &end))
1080 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001081 if (!(object = PyUnicodeDecodeError_GetObject(exc)))
1082 return NULL;
1083 if (!(p = (unsigned char*)PyBytes_AsString(object))) {
1084 Py_DECREF(object);
1085 return NULL;
1086 }
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001087 if (!(encode = PyUnicodeDecodeError_GetEncoding(exc))) {
1088 Py_DECREF(object);
1089 return NULL;
1090 }
1091 if (!(encoding = PyUnicode_AsUTF8(encode))) {
1092 Py_DECREF(object);
1093 Py_DECREF(encode);
1094 return NULL;
1095 }
1096 code = get_standard_encoding(encoding, &bytelength);
1097 Py_DECREF(encode);
1098
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001099 /* Try decoding a single surrogate character. If
1100 there are more, let the codec call us again. */
1101 p += start;
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001102 if (PyBytes_GET_SIZE(object) - start >= bytelength) {
1103 switch (code) {
1104 case ENC_UTF8:
1105 if ((p[0] & 0xf0) == 0xe0 &&
1106 (p[1] & 0xc0) == 0x80 &&
1107 (p[2] & 0xc0) == 0x80) {
1108 /* it's a three-byte code */
1109 ch = ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + (p[2] & 0x3f);
1110 }
1111 break;
1112 case ENC_UTF16LE:
1113 ch = p[1] << 8 | p[0];
1114 break;
1115 case ENC_UTF16BE:
1116 ch = p[0] << 8 | p[1];
1117 break;
1118 case ENC_UTF32LE:
1119 ch = (p[3] << 24) | (p[2] << 16) | (p[1] << 8) | p[0];
1120 break;
1121 case ENC_UTF32BE:
1122 ch = (p[0] << 24) | (p[1] << 16) | (p[2] << 8) | p[3];
1123 break;
1124 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001125 }
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001126
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001127 Py_DECREF(object);
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001128 if (!Py_UNICODE_IS_SURROGATE(ch)) {
1129 /* it's not a surrogate - fail */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001130 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
1131 return NULL;
1132 }
Victor Stinnerc06bb7a2011-11-04 21:36:35 +01001133 res = PyUnicode_FromOrdinal(ch);
1134 if (res == NULL)
1135 return NULL;
Serhiy Storchaka58cf6072013-11-19 11:32:41 +02001136 return Py_BuildValue("(Nn)", res, start + bytelength);
Martin v. Löwisdb12d452009-05-02 18:52:14 +00001137 }
1138 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001139 wrong_exception_type(exc);
1140 return NULL;
Martin v. Löwisdb12d452009-05-02 18:52:14 +00001141 }
1142}
1143
Martin v. Löwis011e8422009-05-05 04:43:17 +00001144static PyObject *
Martin v. Löwis43c57782009-05-10 08:15:24 +00001145PyCodec_SurrogateEscapeErrors(PyObject *exc)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001146{
1147 PyObject *restuple;
1148 PyObject *object;
Martin v. Löwisb09af032011-11-04 11:16:41 +01001149 Py_ssize_t i;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001150 Py_ssize_t start;
1151 Py_ssize_t end;
1152 PyObject *res;
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +03001153
1154 if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001155 char *outp;
1156 if (PyUnicodeEncodeError_GetStart(exc, &start))
1157 return NULL;
1158 if (PyUnicodeEncodeError_GetEnd(exc, &end))
1159 return NULL;
1160 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
1161 return NULL;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001162 res = PyBytes_FromStringAndSize(NULL, end-start);
1163 if (!res) {
1164 Py_DECREF(object);
1165 return NULL;
1166 }
1167 outp = PyBytes_AsString(res);
Martin v. Löwisb09af032011-11-04 11:16:41 +01001168 for (i = start; i < end; i++) {
1169 /* object is guaranteed to be "ready" */
1170 Py_UCS4 ch = PyUnicode_READ_CHAR(object, i);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001171 if (ch < 0xdc80 || ch > 0xdcff) {
1172 /* Not a UTF-8b surrogate, fail with original exception */
1173 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
1174 Py_DECREF(res);
1175 Py_DECREF(object);
1176 return NULL;
1177 }
1178 *outp++ = ch - 0xdc00;
1179 }
1180 restuple = Py_BuildValue("(On)", res, end);
1181 Py_DECREF(res);
1182 Py_DECREF(object);
1183 return restuple;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001184 }
Serhiy Storchakaca7fecb2015-05-18 16:08:52 +03001185 else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
Victor Stinnerc06bb7a2011-11-04 21:36:35 +01001186 PyObject *str;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001187 unsigned char *p;
Victor Stinnerc06bb7a2011-11-04 21:36:35 +01001188 Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001189 int consumed = 0;
1190 if (PyUnicodeDecodeError_GetStart(exc, &start))
1191 return NULL;
1192 if (PyUnicodeDecodeError_GetEnd(exc, &end))
1193 return NULL;
1194 if (!(object = PyUnicodeDecodeError_GetObject(exc)))
1195 return NULL;
1196 if (!(p = (unsigned char*)PyBytes_AsString(object))) {
1197 Py_DECREF(object);
1198 return NULL;
1199 }
1200 while (consumed < 4 && consumed < end-start) {
1201 /* Refuse to escape ASCII bytes. */
1202 if (p[start+consumed] < 128)
1203 break;
1204 ch[consumed] = 0xdc00 + p[start+consumed];
1205 consumed++;
1206 }
1207 Py_DECREF(object);
1208 if (!consumed) {
1209 /* codec complained about ASCII byte. */
1210 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
1211 return NULL;
1212 }
Victor Stinnerc06bb7a2011-11-04 21:36:35 +01001213 str = PyUnicode_FromKindAndData(PyUnicode_2BYTE_KIND, ch, consumed);
1214 if (str == NULL)
1215 return NULL;
1216 return Py_BuildValue("(Nn)", str, start+consumed);
Martin v. Löwis011e8422009-05-05 04:43:17 +00001217 }
1218 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001219 wrong_exception_type(exc);
1220 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +00001221 }
1222}
1223
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001224
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001225static PyObject *strict_errors(PyObject *self, PyObject *exc)
1226{
1227 return PyCodec_StrictErrors(exc);
1228}
1229
1230
1231static PyObject *ignore_errors(PyObject *self, PyObject *exc)
1232{
1233 return PyCodec_IgnoreErrors(exc);
1234}
1235
1236
1237static PyObject *replace_errors(PyObject *self, PyObject *exc)
1238{
1239 return PyCodec_ReplaceErrors(exc);
1240}
1241
1242
1243static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)
1244{
1245 return PyCodec_XMLCharRefReplaceErrors(exc);
1246}
1247
1248
1249static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)
1250{
1251 return PyCodec_BackslashReplaceErrors(exc);
1252}
1253
Martin v. Löwise0a2b722009-05-10 08:08:56 +00001254static PyObject *surrogatepass_errors(PyObject *self, PyObject *exc)
Martin v. Löwisdb12d452009-05-02 18:52:14 +00001255{
Martin v. Löwise0a2b722009-05-10 08:08:56 +00001256 return PyCodec_SurrogatePassErrors(exc);
Martin v. Löwisdb12d452009-05-02 18:52:14 +00001257}
1258
Martin v. Löwis43c57782009-05-10 08:15:24 +00001259static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc)
Martin v. Löwis011e8422009-05-05 04:43:17 +00001260{
Martin v. Löwis43c57782009-05-10 08:15:24 +00001261 return PyCodec_SurrogateEscapeErrors(exc);
Martin v. Löwis011e8422009-05-05 04:43:17 +00001262}
1263
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001264static int _PyCodecRegistry_Init(void)
Guido van Rossumfeee4b92000-03-10 22:57:27 +00001265{
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001266 static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001267 char *name;
1268 PyMethodDef def;
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001269 } methods[] =
1270 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001271 {
1272 "strict",
1273 {
1274 "strict_errors",
1275 strict_errors,
1276 METH_O,
1277 PyDoc_STR("Implements the 'strict' error handling, which "
1278 "raises a UnicodeError on coding errors.")
1279 }
1280 },
1281 {
1282 "ignore",
1283 {
1284 "ignore_errors",
1285 ignore_errors,
1286 METH_O,
1287 PyDoc_STR("Implements the 'ignore' error handling, which "
1288 "ignores malformed data and continues.")
1289 }
1290 },
1291 {
1292 "replace",
1293 {
1294 "replace_errors",
1295 replace_errors,
1296 METH_O,
1297 PyDoc_STR("Implements the 'replace' error handling, which "
1298 "replaces malformed data with a replacement marker.")
1299 }
1300 },
1301 {
1302 "xmlcharrefreplace",
1303 {
1304 "xmlcharrefreplace_errors",
1305 xmlcharrefreplace_errors,
1306 METH_O,
1307 PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
1308 "which replaces an unencodable character with the "
1309 "appropriate XML character reference.")
1310 }
1311 },
1312 {
1313 "backslashreplace",
1314 {
1315 "backslashreplace_errors",
1316 backslashreplace_errors,
1317 METH_O,
1318 PyDoc_STR("Implements the 'backslashreplace' error handling, "
1319 "which replaces an unencodable character with a "
1320 "backslashed escape sequence.")
1321 }
1322 },
1323 {
1324 "surrogatepass",
1325 {
1326 "surrogatepass",
1327 surrogatepass_errors,
1328 METH_O
1329 }
1330 },
1331 {
1332 "surrogateescape",
1333 {
1334 "surrogateescape",
1335 surrogateescape_errors,
1336 METH_O
1337 }
1338 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001339 };
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001340
Nicholas Bastine5662ae2004-03-24 22:22:12 +00001341 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001342 PyObject *mod;
Neal Norwitz739a8f82004-07-08 01:55:58 +00001343 unsigned i;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001344
1345 if (interp->codec_search_path != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001346 return 0;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001347
1348 interp->codec_search_path = PyList_New(0);
1349 interp->codec_search_cache = PyDict_New();
1350 interp->codec_error_registry = PyDict_New();
1351
1352 if (interp->codec_error_registry) {
Victor Stinner63941882011-09-29 00:42:28 +02001353 for (i = 0; i < Py_ARRAY_LENGTH(methods); ++i) {
Andrew Svetlov3ba3a3e2012-12-25 13:32:35 +02001354 PyObject *func = PyCFunction_NewEx(&methods[i].def, NULL, NULL);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001355 int res;
1356 if (!func)
1357 Py_FatalError("can't initialize codec error registry");
1358 res = PyCodec_RegisterError(methods[i].name, func);
1359 Py_DECREF(func);
1360 if (res)
1361 Py_FatalError("can't initialize codec error registry");
1362 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001363 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +00001364
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001365 if (interp->codec_search_path == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001366 interp->codec_search_cache == NULL ||
1367 interp->codec_error_registry == NULL)
1368 Py_FatalError("can't initialize codec registry");
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001369
Christian Heimes819b8bf2008-01-03 23:05:47 +00001370 mod = PyImport_ImportModuleNoBlock("encodings");
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001371 if (mod == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001372 return -1;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001373 }
1374 Py_DECREF(mod);
Christian Heimes6a27efa2008-10-30 21:48:26 +00001375 interp->codecs_initialized = 1;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001376 return 0;
Guido van Rossumfeee4b92000-03-10 22:57:27 +00001377}