blob: eca2b88bfbdeb690fd0a354dcccf7a100b5fd0e4 [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{
Guido van Rossum33831132000-06-29 14:50:15 +000056 register 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)
68 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +000069 for (i = 0; i < len; i++) {
70 register char ch = string[i];
71 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
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000188/* Codec registry encoding check API. */
189
190int PyCodec_KnownEncoding(const char *encoding)
191{
192 PyObject *codecs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000193
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000194 codecs = _PyCodec_Lookup(encoding);
195 if (!codecs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000196 PyErr_Clear();
197 return 0;
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000198 }
199 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000200 Py_DECREF(codecs);
201 return 1;
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000202 }
203}
204
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000205static
206PyObject *args_tuple(PyObject *object,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000207 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000208{
209 PyObject *args;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000210
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000211 args = PyTuple_New(1 + (errors != NULL));
212 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000213 return NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000214 Py_INCREF(object);
215 PyTuple_SET_ITEM(args,0,object);
216 if (errors) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 PyObject *v;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000218
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000219 v = PyUnicode_FromString(errors);
220 if (v == NULL) {
221 Py_DECREF(args);
222 return NULL;
223 }
224 PyTuple_SET_ITEM(args, 1, v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000225 }
226 return args;
227}
228
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000229/* Helper function to get a codec item */
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000230
231static
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232PyObject *codec_getitem(const char *encoding, int index)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000233{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000234 PyObject *codecs;
235 PyObject *v;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000236
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000237 codecs = _PyCodec_Lookup(encoding);
238 if (codecs == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000239 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000240 v = PyTuple_GET_ITEM(codecs, index);
241 Py_DECREF(codecs);
242 Py_INCREF(v);
243 return v;
244}
245
246/* Helper function to create an incremental codec. */
247
248static
249PyObject *codec_getincrementalcodec(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000250 const char *errors,
251 const char *attrname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000252{
253 PyObject *codecs, *ret, *inccodec;
254
255 codecs = _PyCodec_Lookup(encoding);
256 if (codecs == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000257 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000258 inccodec = PyObject_GetAttrString(codecs, attrname);
259 Py_DECREF(codecs);
260 if (inccodec == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000262 if (errors)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 ret = PyObject_CallFunction(inccodec, "s", errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000265 ret = PyObject_CallFunction(inccodec, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000266 Py_DECREF(inccodec);
267 return ret;
268}
269
270/* Helper function to create a stream codec. */
271
272static
273PyObject *codec_getstreamcodec(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000274 PyObject *stream,
275 const char *errors,
276 const int index)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000277{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000278 PyObject *codecs, *streamcodec, *codeccls;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000279
280 codecs = _PyCodec_Lookup(encoding);
281 if (codecs == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000282 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000283
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000284 codeccls = PyTuple_GET_ITEM(codecs, index);
285 if (errors != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000287 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000288 streamcodec = PyObject_CallFunction(codeccls, "O", stream);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000289 Py_DECREF(codecs);
290 return streamcodec;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000291}
292
Guido van Rossum98297ee2007-11-06 21:34:58 +0000293/* Convenience APIs to query the Codec registry.
294
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000295 All APIs return a codec object with incremented refcount.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000296
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000297 */
298
299PyObject *PyCodec_Encoder(const char *encoding)
300{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000301 return codec_getitem(encoding, 0);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000302}
303
304PyObject *PyCodec_Decoder(const char *encoding)
305{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000306 return codec_getitem(encoding, 1);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000307}
308
Thomas Woutersa9773292006-04-21 09:43:23 +0000309PyObject *PyCodec_IncrementalEncoder(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000310 const char *errors)
Thomas Woutersa9773292006-04-21 09:43:23 +0000311{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000312 return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
Thomas Woutersa9773292006-04-21 09:43:23 +0000313}
314
315PyObject *PyCodec_IncrementalDecoder(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000316 const char *errors)
Thomas Woutersa9773292006-04-21 09:43:23 +0000317{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000318 return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
Thomas Woutersa9773292006-04-21 09:43:23 +0000319}
320
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000321PyObject *PyCodec_StreamReader(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000322 PyObject *stream,
323 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000324{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000325 return codec_getstreamcodec(encoding, stream, errors, 2);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000326}
327
328PyObject *PyCodec_StreamWriter(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000329 PyObject *stream,
330 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000331{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000332 return codec_getstreamcodec(encoding, stream, errors, 3);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000333}
334
335/* Encode an object (e.g. an Unicode object) using the given encoding
336 and return the resulting encoded object (usually a Python string).
337
338 errors is passed to the encoder factory as argument if non-NULL. */
339
340PyObject *PyCodec_Encode(PyObject *object,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000341 const char *encoding,
342 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000343{
344 PyObject *encoder = NULL;
Neal Norwitz3715c3e2005-11-24 22:09:18 +0000345 PyObject *args = NULL, *result = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000346 PyObject *v = NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000347
348 encoder = PyCodec_Encoder(encoding);
349 if (encoder == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000350 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000351
352 args = args_tuple(object, errors);
353 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000354 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000355
356 result = PyEval_CallObject(encoder, args);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000357 if (result == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000358 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000359
Guido van Rossum98297ee2007-11-06 21:34:58 +0000360 if (!PyTuple_Check(result) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000361 PyTuple_GET_SIZE(result) != 2) {
362 PyErr_SetString(PyExc_TypeError,
363 "encoder must return a tuple (object, integer)");
364 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000365 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000366 v = PyTuple_GET_ITEM(result,0);
367 Py_INCREF(v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000368 /* We don't check or use the second (integer) entry. */
369
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000370 Py_DECREF(args);
371 Py_DECREF(encoder);
372 Py_DECREF(result);
373 return v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000374
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000375 onError:
Neal Norwitz3715c3e2005-11-24 22:09:18 +0000376 Py_XDECREF(result);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000377 Py_XDECREF(args);
378 Py_XDECREF(encoder);
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000379 return NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000380}
381
382/* Decode an object (usually a Python string) using the given encoding
383 and return an equivalent object (e.g. an Unicode object).
384
385 errors is passed to the decoder factory as argument if non-NULL. */
386
387PyObject *PyCodec_Decode(PyObject *object,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000388 const char *encoding,
389 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000390{
391 PyObject *decoder = NULL;
392 PyObject *args = NULL, *result = NULL;
393 PyObject *v;
394
395 decoder = PyCodec_Decoder(encoding);
396 if (decoder == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000397 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000398
399 args = args_tuple(object, errors);
400 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000401 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000402
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000403 result = PyEval_CallObject(decoder,args);
404 if (result == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000406 if (!PyTuple_Check(result) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000407 PyTuple_GET_SIZE(result) != 2) {
408 PyErr_SetString(PyExc_TypeError,
409 "decoder must return a tuple (object,integer)");
410 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000411 }
412 v = PyTuple_GET_ITEM(result,0);
413 Py_INCREF(v);
414 /* We don't check or use the second (integer) entry. */
415
416 Py_DECREF(args);
417 Py_DECREF(decoder);
418 Py_DECREF(result);
419 return v;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000420
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000421 onError:
422 Py_XDECREF(args);
423 Py_XDECREF(decoder);
424 Py_XDECREF(result);
425 return NULL;
426}
427
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000428/* Register the error handling callback function error under the name
429 name. This function will be called by the codec when it encounters
430 an unencodable characters/undecodable bytes and doesn't know the
431 callback name, when name is specified as the error parameter
432 in the call to the encode/decode function.
433 Return 0 on success, -1 on error */
434int PyCodec_RegisterError(const char *name, PyObject *error)
435{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000436 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000437 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000439 if (!PyCallable_Check(error)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000440 PyErr_SetString(PyExc_TypeError, "handler must be callable");
441 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000442 }
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000443 return PyDict_SetItemString(interp->codec_error_registry,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000444 (char *)name, error);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000445}
446
447/* Lookup the error handling callback function registered under the
448 name error. As a special case NULL can be passed, in which case
449 the error handling callback for strict encoding will be returned. */
450PyObject *PyCodec_LookupError(const char *name)
451{
452 PyObject *handler = NULL;
453
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000454 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000455 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000456 return NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000457
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000458 if (name==NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000459 name = "strict";
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000460 handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000461 if (!handler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000463 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000464 Py_INCREF(handler);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000465 return handler;
466}
467
468static void wrong_exception_type(PyObject *exc)
469{
Martin v. Löwisbd928fe2011-10-14 10:20:37 +0200470 _Py_IDENTIFIER(__class__);
471 _Py_IDENTIFIER(__name__);
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200472 PyObject *type = _PyObject_GetAttrId(exc, &PyId___class__);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000473 if (type != NULL) {
Martin v. Löwis1ee1b6f2011-10-10 18:11:30 +0200474 PyObject *name = _PyObject_GetAttrId(type, &PyId___name__);
Walter Dörwald573c08c2007-05-25 15:46:59 +0000475 Py_DECREF(type);
476 if (name != NULL) {
477 PyErr_Format(PyExc_TypeError,
478 "don't know how to handle %S in error callback", name);
479 Py_DECREF(name);
480 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000481 }
482}
483
484PyObject *PyCodec_StrictErrors(PyObject *exc)
485{
Brett Cannonbf364092006-03-01 04:25:17 +0000486 if (PyExceptionInstance_Check(exc))
487 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000488 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000489 PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000490 return NULL;
491}
492
493
494PyObject *PyCodec_IgnoreErrors(PyObject *exc)
495{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000496 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000497 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (PyUnicodeEncodeError_GetEnd(exc, &end))
499 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000500 }
501 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (PyUnicodeDecodeError_GetEnd(exc, &end))
503 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000504 }
505 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 if (PyUnicodeTranslateError_GetEnd(exc, &end))
507 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000508 }
509 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000510 wrong_exception_type(exc);
511 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000512 }
513 /* ouch: passing NULL, 0, pos gives None instead of u'' */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000514 return Py_BuildValue("(u#n)", &end, 0, end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000515}
516
517
518PyObject *PyCodec_ReplaceErrors(PyObject *exc)
519{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200520 Py_ssize_t start, end, i, len;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000521
522 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000523 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200524 int kind;
525 void *data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000526 if (PyUnicodeEncodeError_GetStart(exc, &start))
527 return NULL;
528 if (PyUnicodeEncodeError_GetEnd(exc, &end))
529 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200530 len = end - start;
531 res = PyUnicode_New(len, '?');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000532 if (res == NULL)
533 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200534 kind = PyUnicode_KIND(res);
535 data = PyUnicode_DATA(res);
536 for (i = 0; i < len; ++i)
537 PyUnicode_WRITE(kind, data, i, '?');
538 return Py_BuildValue("(Nn)", res, end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000539 }
540 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000541 if (PyUnicodeDecodeError_GetEnd(exc, &end))
542 return NULL;
Victor Stinner1a15aba2011-10-02 19:00:15 +0200543 return Py_BuildValue("(Cn)",
544 (int)Py_UNICODE_REPLACEMENT_CHARACTER,
545 end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000546 }
547 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000548 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200549 int kind;
550 void *data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000551 if (PyUnicodeTranslateError_GetStart(exc, &start))
552 return NULL;
553 if (PyUnicodeTranslateError_GetEnd(exc, &end))
554 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200555 len = end - start;
556 res = PyUnicode_New(len, Py_UNICODE_REPLACEMENT_CHARACTER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000557 if (res == NULL)
558 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200559 kind = PyUnicode_KIND(res);
560 data = PyUnicode_DATA(res);
561 for (i=0; i < len; i++)
562 PyUnicode_WRITE(kind, data, i, Py_UNICODE_REPLACEMENT_CHARACTER);
563 return Py_BuildValue("(Nn)", res, end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000564 }
565 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000566 wrong_exception_type(exc);
567 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000568 }
569}
570
571PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
572{
573 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000574 PyObject *restuple;
575 PyObject *object;
576 Py_ssize_t start;
577 Py_ssize_t end;
578 PyObject *res;
579 Py_UNICODE *p;
580 Py_UNICODE *startp;
581 Py_UNICODE *outp;
582 int ressize;
583 if (PyUnicodeEncodeError_GetStart(exc, &start))
584 return NULL;
585 if (PyUnicodeEncodeError_GetEnd(exc, &end))
586 return NULL;
587 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
588 return NULL;
589 startp = PyUnicode_AS_UNICODE(object);
590 for (p = startp+start, ressize = 0; p < startp+end; ++p) {
591 if (*p<10)
592 ressize += 2+1+1;
593 else if (*p<100)
594 ressize += 2+2+1;
595 else if (*p<1000)
596 ressize += 2+3+1;
597 else if (*p<10000)
598 ressize += 2+4+1;
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000599#ifndef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000600 else
601 ressize += 2+5+1;
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000602#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000603 else if (*p<100000)
604 ressize += 2+5+1;
605 else if (*p<1000000)
606 ressize += 2+6+1;
607 else
608 ressize += 2+7+1;
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000609#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000610 }
611 /* allocate replacement */
612 res = PyUnicode_FromUnicode(NULL, ressize);
613 if (res == NULL) {
614 Py_DECREF(object);
615 return NULL;
616 }
617 /* generate replacement */
618 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
619 p < startp+end; ++p) {
620 Py_UNICODE c = *p;
621 int digits;
622 int base;
623 *outp++ = '&';
624 *outp++ = '#';
625 if (*p<10) {
626 digits = 1;
627 base = 1;
628 }
629 else if (*p<100) {
630 digits = 2;
631 base = 10;
632 }
633 else if (*p<1000) {
634 digits = 3;
635 base = 100;
636 }
637 else if (*p<10000) {
638 digits = 4;
639 base = 1000;
640 }
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000641#ifndef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000642 else {
643 digits = 5;
644 base = 10000;
645 }
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000646#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000647 else if (*p<100000) {
648 digits = 5;
649 base = 10000;
650 }
651 else if (*p<1000000) {
652 digits = 6;
653 base = 100000;
654 }
655 else {
656 digits = 7;
657 base = 1000000;
658 }
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000659#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000660 while (digits-->0) {
661 *outp++ = '0' + c/base;
662 c %= base;
663 base /= 10;
664 }
665 *outp++ = ';';
666 }
667 restuple = Py_BuildValue("(On)", res, end);
668 Py_DECREF(res);
669 Py_DECREF(object);
670 return restuple;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000671 }
672 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000673 wrong_exception_type(exc);
674 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000675 }
676}
677
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000678PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
679{
Antoine Pitroue4a18922010-09-09 20:30:23 +0000680#ifndef Py_UNICODE_WIDE
681#define IS_SURROGATE_PAIR(p, end) \
682 (*p >= 0xD800 && *p <= 0xDBFF && (p + 1) < end && \
683 *(p + 1) >= 0xDC00 && *(p + 1) <= 0xDFFF)
684#else
685#define IS_SURROGATE_PAIR(p, end) 0
686#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000687 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000688 PyObject *restuple;
689 PyObject *object;
690 Py_ssize_t start;
691 Py_ssize_t end;
692 PyObject *res;
693 Py_UNICODE *p;
694 Py_UNICODE *startp;
695 Py_UNICODE *outp;
696 int ressize;
697 if (PyUnicodeEncodeError_GetStart(exc, &start))
698 return NULL;
699 if (PyUnicodeEncodeError_GetEnd(exc, &end))
700 return NULL;
701 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
702 return NULL;
703 startp = PyUnicode_AS_UNICODE(object);
704 for (p = startp+start, ressize = 0; p < startp+end; ++p) {
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000705#ifdef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000706 if (*p >= 0x00010000)
707 ressize += 1+1+8;
708 else
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000709#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000710 if (*p >= 0x100) {
Antoine Pitroue4a18922010-09-09 20:30:23 +0000711 if (IS_SURROGATE_PAIR(p, startp+end)) {
712 ressize += 1+1+8;
713 ++p;
714 }
715 else
716 ressize += 1+1+4;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000717 }
718 else
719 ressize += 1+1+2;
720 }
721 res = PyUnicode_FromUnicode(NULL, ressize);
722 if (res==NULL)
723 return NULL;
724 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
725 p < startp+end; ++p) {
Antoine Pitroue4a18922010-09-09 20:30:23 +0000726 Py_UCS4 c = (Py_UCS4) *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000727 *outp++ = '\\';
Antoine Pitroue4a18922010-09-09 20:30:23 +0000728 if (IS_SURROGATE_PAIR(p, startp+end)) {
729 c = ((*p & 0x3FF) << 10) + (*(p + 1) & 0x3FF) + 0x10000;
730 ++p;
731 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000732 if (c >= 0x00010000) {
733 *outp++ = 'U';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200734 *outp++ = Py_hexdigits[(c>>28)&0xf];
735 *outp++ = Py_hexdigits[(c>>24)&0xf];
736 *outp++ = Py_hexdigits[(c>>20)&0xf];
737 *outp++ = Py_hexdigits[(c>>16)&0xf];
738 *outp++ = Py_hexdigits[(c>>12)&0xf];
739 *outp++ = Py_hexdigits[(c>>8)&0xf];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 }
Antoine Pitroue4a18922010-09-09 20:30:23 +0000741 else if (c >= 0x100) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000742 *outp++ = 'u';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200743 *outp++ = Py_hexdigits[(c>>12)&0xf];
744 *outp++ = Py_hexdigits[(c>>8)&0xf];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000745 }
746 else
747 *outp++ = 'x';
Victor Stinnerf5cff562011-10-14 02:13:11 +0200748 *outp++ = Py_hexdigits[(c>>4)&0xf];
749 *outp++ = Py_hexdigits[c&0xf];
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000751
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000752 restuple = Py_BuildValue("(On)", res, end);
753 Py_DECREF(res);
754 Py_DECREF(object);
755 return restuple;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000756 }
757 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000758 wrong_exception_type(exc);
759 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000760 }
Antoine Pitroue4a18922010-09-09 20:30:23 +0000761#undef IS_SURROGATE_PAIR
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000762}
763
Martin v. Löwisaef3fb02009-05-02 19:27:30 +0000764/* This handler is declared static until someone demonstrates
765 a need to call it directly. */
766static PyObject *
Martin v. Löwise0a2b722009-05-10 08:08:56 +0000767PyCodec_SurrogatePassErrors(PyObject *exc)
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000768{
769 PyObject *restuple;
770 PyObject *object;
771 Py_ssize_t start;
772 Py_ssize_t end;
773 PyObject *res;
774 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000775 Py_UNICODE *p;
776 Py_UNICODE *startp;
777 char *outp;
778 if (PyUnicodeEncodeError_GetStart(exc, &start))
779 return NULL;
780 if (PyUnicodeEncodeError_GetEnd(exc, &end))
781 return NULL;
782 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
783 return NULL;
784 startp = PyUnicode_AS_UNICODE(object);
785 res = PyBytes_FromStringAndSize(NULL, 3*(end-start));
786 if (!res) {
787 Py_DECREF(object);
788 return NULL;
789 }
790 outp = PyBytes_AsString(res);
791 for (p = startp+start; p < startp+end; p++) {
792 Py_UNICODE ch = *p;
793 if (ch < 0xd800 || ch > 0xdfff) {
794 /* Not a surrogate, fail with original exception */
795 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
796 Py_DECREF(res);
797 Py_DECREF(object);
798 return NULL;
799 }
800 *outp++ = (char)(0xe0 | (ch >> 12));
801 *outp++ = (char)(0x80 | ((ch >> 6) & 0x3f));
802 *outp++ = (char)(0x80 | (ch & 0x3f));
803 }
804 restuple = Py_BuildValue("(On)", res, end);
805 Py_DECREF(res);
806 Py_DECREF(object);
807 return restuple;
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000808 }
809 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000810 unsigned char *p;
811 Py_UNICODE ch = 0;
812 if (PyUnicodeDecodeError_GetStart(exc, &start))
813 return NULL;
814 if (!(object = PyUnicodeDecodeError_GetObject(exc)))
815 return NULL;
816 if (!(p = (unsigned char*)PyBytes_AsString(object))) {
817 Py_DECREF(object);
818 return NULL;
819 }
820 /* Try decoding a single surrogate character. If
821 there are more, let the codec call us again. */
822 p += start;
823 if ((p[0] & 0xf0) == 0xe0 ||
824 (p[1] & 0xc0) == 0x80 ||
825 (p[2] & 0xc0) == 0x80) {
826 /* it's a three-byte code */
827 ch = ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + (p[2] & 0x3f);
828 if (ch < 0xd800 || ch > 0xdfff)
829 /* it's not a surrogate - fail */
830 ch = 0;
831 }
832 Py_DECREF(object);
833 if (ch == 0) {
834 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
835 return NULL;
836 }
837 return Py_BuildValue("(u#n)", &ch, 1, start+3);
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000838 }
839 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000840 wrong_exception_type(exc);
841 return NULL;
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000842 }
843}
844
Martin v. Löwis011e8422009-05-05 04:43:17 +0000845static PyObject *
Martin v. Löwis43c57782009-05-10 08:15:24 +0000846PyCodec_SurrogateEscapeErrors(PyObject *exc)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000847{
848 PyObject *restuple;
849 PyObject *object;
850 Py_ssize_t start;
851 Py_ssize_t end;
852 PyObject *res;
853 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000854 Py_UNICODE *p;
855 Py_UNICODE *startp;
856 char *outp;
857 if (PyUnicodeEncodeError_GetStart(exc, &start))
858 return NULL;
859 if (PyUnicodeEncodeError_GetEnd(exc, &end))
860 return NULL;
861 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
862 return NULL;
863 startp = PyUnicode_AS_UNICODE(object);
864 res = PyBytes_FromStringAndSize(NULL, end-start);
865 if (!res) {
866 Py_DECREF(object);
867 return NULL;
868 }
869 outp = PyBytes_AsString(res);
870 for (p = startp+start; p < startp+end; p++) {
871 Py_UNICODE ch = *p;
872 if (ch < 0xdc80 || ch > 0xdcff) {
873 /* Not a UTF-8b surrogate, fail with original exception */
874 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
875 Py_DECREF(res);
876 Py_DECREF(object);
877 return NULL;
878 }
879 *outp++ = ch - 0xdc00;
880 }
881 restuple = Py_BuildValue("(On)", res, end);
882 Py_DECREF(res);
883 Py_DECREF(object);
884 return restuple;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000885 }
886 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000887 unsigned char *p;
888 Py_UNICODE ch[4]; /* decode up to 4 bad bytes. */
889 int consumed = 0;
890 if (PyUnicodeDecodeError_GetStart(exc, &start))
891 return NULL;
892 if (PyUnicodeDecodeError_GetEnd(exc, &end))
893 return NULL;
894 if (!(object = PyUnicodeDecodeError_GetObject(exc)))
895 return NULL;
896 if (!(p = (unsigned char*)PyBytes_AsString(object))) {
897 Py_DECREF(object);
898 return NULL;
899 }
900 while (consumed < 4 && consumed < end-start) {
901 /* Refuse to escape ASCII bytes. */
902 if (p[start+consumed] < 128)
903 break;
904 ch[consumed] = 0xdc00 + p[start+consumed];
905 consumed++;
906 }
907 Py_DECREF(object);
908 if (!consumed) {
909 /* codec complained about ASCII byte. */
910 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
911 return NULL;
912 }
913 return Py_BuildValue("(u#n)", ch, consumed, start+consumed);
Martin v. Löwis011e8422009-05-05 04:43:17 +0000914 }
915 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000916 wrong_exception_type(exc);
917 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000918 }
919}
920
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000921
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000922static PyObject *strict_errors(PyObject *self, PyObject *exc)
923{
924 return PyCodec_StrictErrors(exc);
925}
926
927
928static PyObject *ignore_errors(PyObject *self, PyObject *exc)
929{
930 return PyCodec_IgnoreErrors(exc);
931}
932
933
934static PyObject *replace_errors(PyObject *self, PyObject *exc)
935{
936 return PyCodec_ReplaceErrors(exc);
937}
938
939
940static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)
941{
942 return PyCodec_XMLCharRefReplaceErrors(exc);
943}
944
945
946static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)
947{
948 return PyCodec_BackslashReplaceErrors(exc);
949}
950
Martin v. Löwise0a2b722009-05-10 08:08:56 +0000951static PyObject *surrogatepass_errors(PyObject *self, PyObject *exc)
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000952{
Martin v. Löwise0a2b722009-05-10 08:08:56 +0000953 return PyCodec_SurrogatePassErrors(exc);
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000954}
955
Martin v. Löwis43c57782009-05-10 08:15:24 +0000956static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000957{
Martin v. Löwis43c57782009-05-10 08:15:24 +0000958 return PyCodec_SurrogateEscapeErrors(exc);
Martin v. Löwis011e8422009-05-05 04:43:17 +0000959}
960
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000961static int _PyCodecRegistry_Init(void)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000962{
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000963 static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000964 char *name;
965 PyMethodDef def;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000966 } methods[] =
967 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000968 {
969 "strict",
970 {
971 "strict_errors",
972 strict_errors,
973 METH_O,
974 PyDoc_STR("Implements the 'strict' error handling, which "
975 "raises a UnicodeError on coding errors.")
976 }
977 },
978 {
979 "ignore",
980 {
981 "ignore_errors",
982 ignore_errors,
983 METH_O,
984 PyDoc_STR("Implements the 'ignore' error handling, which "
985 "ignores malformed data and continues.")
986 }
987 },
988 {
989 "replace",
990 {
991 "replace_errors",
992 replace_errors,
993 METH_O,
994 PyDoc_STR("Implements the 'replace' error handling, which "
995 "replaces malformed data with a replacement marker.")
996 }
997 },
998 {
999 "xmlcharrefreplace",
1000 {
1001 "xmlcharrefreplace_errors",
1002 xmlcharrefreplace_errors,
1003 METH_O,
1004 PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
1005 "which replaces an unencodable character with the "
1006 "appropriate XML character reference.")
1007 }
1008 },
1009 {
1010 "backslashreplace",
1011 {
1012 "backslashreplace_errors",
1013 backslashreplace_errors,
1014 METH_O,
1015 PyDoc_STR("Implements the 'backslashreplace' error handling, "
1016 "which replaces an unencodable character with a "
1017 "backslashed escape sequence.")
1018 }
1019 },
1020 {
1021 "surrogatepass",
1022 {
1023 "surrogatepass",
1024 surrogatepass_errors,
1025 METH_O
1026 }
1027 },
1028 {
1029 "surrogateescape",
1030 {
1031 "surrogateescape",
1032 surrogateescape_errors,
1033 METH_O
1034 }
1035 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001036 };
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001037
Nicholas Bastine5662ae2004-03-24 22:22:12 +00001038 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001039 PyObject *mod;
Neal Norwitz739a8f82004-07-08 01:55:58 +00001040 unsigned i;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001041
1042 if (interp->codec_search_path != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001043 return 0;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001044
1045 interp->codec_search_path = PyList_New(0);
1046 interp->codec_search_cache = PyDict_New();
1047 interp->codec_error_registry = PyDict_New();
1048
1049 if (interp->codec_error_registry) {
Victor Stinner63941882011-09-29 00:42:28 +02001050 for (i = 0; i < Py_ARRAY_LENGTH(methods); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001051 PyObject *func = PyCFunction_New(&methods[i].def, NULL);
1052 int res;
1053 if (!func)
1054 Py_FatalError("can't initialize codec error registry");
1055 res = PyCodec_RegisterError(methods[i].name, func);
1056 Py_DECREF(func);
1057 if (res)
1058 Py_FatalError("can't initialize codec error registry");
1059 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001060 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +00001061
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001062 if (interp->codec_search_path == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001063 interp->codec_search_cache == NULL ||
1064 interp->codec_error_registry == NULL)
1065 Py_FatalError("can't initialize codec registry");
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001066
Christian Heimes819b8bf2008-01-03 23:05:47 +00001067 mod = PyImport_ImportModuleNoBlock("encodings");
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001068 if (mod == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001069 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1070 /* Ignore ImportErrors... this is done so that
1071 distributions can disable the encodings package. Note
1072 that other errors are not masked, e.g. SystemErrors
1073 raised to inform the user of an error in the Python
1074 configuration are still reported back to the user. */
1075 PyErr_Clear();
1076 return 0;
1077 }
1078 return -1;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001079 }
1080 Py_DECREF(mod);
Christian Heimes6a27efa2008-10-30 21:48:26 +00001081 interp->codecs_initialized = 1;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001082 return 0;
Guido van Rossumfeee4b92000-03-10 22:57:27 +00001083}