blob: 0008bdcbdca76dc3f7e2258e555bc6a91856e08c [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
Guido van Rossumfeee4b92000-03-10 22:57:27 +000014/* --- Codec Registry ----------------------------------------------------- */
15
16/* Import the standard encodings package which will register the first
Guido van Rossum98297ee2007-11-06 21:34:58 +000017 codec search function.
Guido van Rossumfeee4b92000-03-10 22:57:27 +000018
19 This is done in a lazy way so that the Unicode implementation does
20 not downgrade startup time of scripts not needing it.
21
Guido van Rossumb95de4f2000-03-31 17:25:23 +000022 ImportErrors are silently ignored by this function. Only one try is
23 made.
Guido van Rossumfeee4b92000-03-10 22:57:27 +000024
25*/
26
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000027static int _PyCodecRegistry_Init(void); /* Forward */
Guido van Rossumfeee4b92000-03-10 22:57:27 +000028
Guido van Rossumfeee4b92000-03-10 22:57:27 +000029int PyCodec_Register(PyObject *search_function)
30{
Nicholas Bastine5662ae2004-03-24 22:22:12 +000031 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000032 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000033 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000034 if (search_function == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000035 PyErr_BadArgument();
36 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000037 }
38 if (!PyCallable_Check(search_function)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000039 PyErr_SetString(PyExc_TypeError, "argument must be callable");
40 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000041 }
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +000042 return PyList_Append(interp->codec_search_path, search_function);
Guido van Rossumb95de4f2000-03-31 17:25:23 +000043
44 onError:
45 return -1;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000046}
47
Guido van Rossum9e896b32000-04-05 20:11:21 +000048/* Convert a string to a normalized Python string: all characters are
49 converted to lower case, spaces are replaced with underscores. */
50
Guido van Rossumfeee4b92000-03-10 22:57:27 +000051static
Guido van Rossum9e896b32000-04-05 20:11:21 +000052PyObject *normalizestring(const char *string)
Guido van Rossumfeee4b92000-03-10 22:57:27 +000053{
Guido van Rossum33831132000-06-29 14:50:15 +000054 register size_t i;
Guido van Rossum582acec2000-06-28 22:07:35 +000055 size_t len = strlen(string);
Guido van Rossumfeee4b92000-03-10 22:57:27 +000056 char *p;
57 PyObject *v;
Guido van Rossum21431e82007-10-19 21:48:41 +000058
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000059 if (len > PY_SSIZE_T_MAX) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000060 PyErr_SetString(PyExc_OverflowError, "string is too large");
61 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +000062 }
Guido van Rossum21431e82007-10-19 21:48:41 +000063
64 p = PyMem_Malloc(len + 1);
65 if (p == NULL)
66 return NULL;
Guido van Rossum9e896b32000-04-05 20:11:21 +000067 for (i = 0; i < len; i++) {
68 register char ch = string[i];
69 if (ch == ' ')
70 ch = '-';
71 else
Antoine Pitroucf9d3c02011-07-24 02:27:04 +020072 ch = Py_TOLOWER(Py_CHARMASK(ch));
Antoine Pitrouf95a1b32010-05-09 15:52:27 +000073 p[i] = ch;
Guido van Rossum9e896b32000-04-05 20:11:21 +000074 }
Guido van Rossum21431e82007-10-19 21:48:41 +000075 p[i] = '\0';
76 v = PyUnicode_FromString(p);
77 if (v == NULL)
78 return NULL;
79 PyMem_Free(p);
Guido van Rossumfeee4b92000-03-10 22:57:27 +000080 return v;
81}
82
83/* Lookup the given encoding and return a tuple providing the codec
84 facilities.
85
86 The encoding string is looked up converted to all lower-case
87 characters. This makes encodings looked up through this mechanism
88 effectively case-insensitive.
89
Guido van Rossum98297ee2007-11-06 21:34:58 +000090 If no codec is found, a LookupError is set and NULL returned.
Guido van Rossumb95de4f2000-03-31 17:25:23 +000091
92 As side effect, this tries to load the encodings package, if not
93 yet done. This is part of the lazy load strategy for the encodings
94 package.
95
96*/
Guido van Rossumfeee4b92000-03-10 22:57:27 +000097
98PyObject *_PyCodec_Lookup(const char *encoding)
99{
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000100 PyInterpreterState *interp;
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000101 PyObject *result, *args = NULL, *v;
Thomas Wouters477c8d52006-05-27 19:21:47 +0000102 Py_ssize_t i, len;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000103
Fred Drake766de832000-05-09 19:55:59 +0000104 if (encoding == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000105 PyErr_BadArgument();
106 goto onError;
Fred Drake766de832000-05-09 19:55:59 +0000107 }
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000108
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000109 interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000110 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000111 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000112
Guido van Rossum9e896b32000-04-05 20:11:21 +0000113 /* Convert the encoding to a normalized Python string: all
Thomas Wouters7e474022000-07-16 12:04:32 +0000114 characters are converted to lower case, spaces and hyphens are
Guido van Rossum9e896b32000-04-05 20:11:21 +0000115 replaced with underscores. */
116 v = normalizestring(encoding);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000117 if (v == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000118 goto onError;
Guido van Rossum21431e82007-10-19 21:48:41 +0000119 PyUnicode_InternInPlace(&v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000120
121 /* First, try to lookup the name in the registry dictionary */
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000122 result = PyDict_GetItem(interp->codec_search_cache, v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000123 if (result != NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000124 Py_INCREF(result);
125 Py_DECREF(v);
126 return result;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000127 }
Guido van Rossum98297ee2007-11-06 21:34:58 +0000128
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000129 /* Next, scan the search functions in order of registration */
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000130 args = PyTuple_New(1);
131 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000132 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000133 PyTuple_SET_ITEM(args,0,v);
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000134
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000135 len = PyList_Size(interp->codec_search_path);
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000136 if (len < 0)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000137 goto onError;
Guido van Rossumb95de4f2000-03-31 17:25:23 +0000138 if (len == 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000139 PyErr_SetString(PyExc_LookupError,
140 "no codec search functions registered: "
141 "can't find encoding");
142 goto onError;
Guido van Rossumb95de4f2000-03-31 17:25:23 +0000143 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000144
145 for (i = 0; i < len; i++) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000146 PyObject *func;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000147
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000148 func = PyList_GetItem(interp->codec_search_path, i);
149 if (func == NULL)
150 goto onError;
151 result = PyEval_CallObject(func, args);
152 if (result == NULL)
153 goto onError;
154 if (result == Py_None) {
155 Py_DECREF(result);
156 continue;
157 }
158 if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
159 PyErr_SetString(PyExc_TypeError,
160 "codec search functions must return 4-tuples");
161 Py_DECREF(result);
162 goto onError;
163 }
164 break;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000165 }
166 if (i == len) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000167 /* XXX Perhaps we should cache misses too ? */
168 PyErr_Format(PyExc_LookupError,
Martin v. Löwiseb42b022002-09-26 16:01:24 +0000169 "unknown encoding: %s", encoding);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000170 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000171 }
172
173 /* Cache and return the result */
Neal Norwitz9edcc2e2007-08-11 04:58:26 +0000174 if (PyDict_SetItem(interp->codec_search_cache, v, result) < 0) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000175 Py_DECREF(result);
176 goto onError;
Neal Norwitz9edcc2e2007-08-11 04:58:26 +0000177 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000178 Py_DECREF(args);
179 return result;
180
181 onError:
182 Py_XDECREF(args);
183 return NULL;
184}
185
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000186/* Codec registry encoding check API. */
187
188int PyCodec_KnownEncoding(const char *encoding)
189{
190 PyObject *codecs;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000191
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000192 codecs = _PyCodec_Lookup(encoding);
193 if (!codecs) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000194 PyErr_Clear();
195 return 0;
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000196 }
197 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000198 Py_DECREF(codecs);
199 return 1;
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000200 }
201}
202
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000203static
204PyObject *args_tuple(PyObject *object,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000205 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000206{
207 PyObject *args;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000208
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000209 args = PyTuple_New(1 + (errors != NULL));
210 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000211 return NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000212 Py_INCREF(object);
213 PyTuple_SET_ITEM(args,0,object);
214 if (errors) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000215 PyObject *v;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000216
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000217 v = PyUnicode_FromString(errors);
218 if (v == NULL) {
219 Py_DECREF(args);
220 return NULL;
221 }
222 PyTuple_SET_ITEM(args, 1, v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000223 }
224 return args;
225}
226
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000227/* Helper function to get a codec item */
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000228
229static
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000230PyObject *codec_getitem(const char *encoding, int index)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000231{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000232 PyObject *codecs;
233 PyObject *v;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000234
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000235 codecs = _PyCodec_Lookup(encoding);
236 if (codecs == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000237 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000238 v = PyTuple_GET_ITEM(codecs, index);
239 Py_DECREF(codecs);
240 Py_INCREF(v);
241 return v;
242}
243
244/* Helper function to create an incremental codec. */
245
246static
247PyObject *codec_getincrementalcodec(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000248 const char *errors,
249 const char *attrname)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000250{
251 PyObject *codecs, *ret, *inccodec;
252
253 codecs = _PyCodec_Lookup(encoding);
254 if (codecs == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000255 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000256 inccodec = PyObject_GetAttrString(codecs, attrname);
257 Py_DECREF(codecs);
258 if (inccodec == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000259 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000260 if (errors)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000261 ret = PyObject_CallFunction(inccodec, "s", errors);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000262 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000263 ret = PyObject_CallFunction(inccodec, NULL);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000264 Py_DECREF(inccodec);
265 return ret;
266}
267
268/* Helper function to create a stream codec. */
269
270static
271PyObject *codec_getstreamcodec(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000272 PyObject *stream,
273 const char *errors,
274 const int index)
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000275{
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000276 PyObject *codecs, *streamcodec, *codeccls;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000277
278 codecs = _PyCodec_Lookup(encoding);
279 if (codecs == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000280 return NULL;
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000281
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000282 codeccls = PyTuple_GET_ITEM(codecs, index);
283 if (errors != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000284 streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
Thomas Wouters0e3f5912006-08-11 14:57:12 +0000285 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000286 streamcodec = PyObject_CallFunction(codeccls, "O", stream);
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000287 Py_DECREF(codecs);
288 return streamcodec;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000289}
290
Guido van Rossum98297ee2007-11-06 21:34:58 +0000291/* Convenience APIs to query the Codec registry.
292
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000293 All APIs return a codec object with incremented refcount.
Guido van Rossum98297ee2007-11-06 21:34:58 +0000294
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000295 */
296
297PyObject *PyCodec_Encoder(const char *encoding)
298{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000299 return codec_getitem(encoding, 0);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000300}
301
302PyObject *PyCodec_Decoder(const char *encoding)
303{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000304 return codec_getitem(encoding, 1);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000305}
306
Thomas Woutersa9773292006-04-21 09:43:23 +0000307PyObject *PyCodec_IncrementalEncoder(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000308 const char *errors)
Thomas Woutersa9773292006-04-21 09:43:23 +0000309{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000310 return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
Thomas Woutersa9773292006-04-21 09:43:23 +0000311}
312
313PyObject *PyCodec_IncrementalDecoder(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000314 const char *errors)
Thomas Woutersa9773292006-04-21 09:43:23 +0000315{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000316 return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
Thomas Woutersa9773292006-04-21 09:43:23 +0000317}
318
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000319PyObject *PyCodec_StreamReader(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000320 PyObject *stream,
321 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000322{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000323 return codec_getstreamcodec(encoding, stream, errors, 2);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000324}
325
326PyObject *PyCodec_StreamWriter(const char *encoding,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000327 PyObject *stream,
328 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000329{
Thomas Wouters49fd7fa2006-04-21 10:40:58 +0000330 return codec_getstreamcodec(encoding, stream, errors, 3);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000331}
332
333/* Encode an object (e.g. an Unicode object) using the given encoding
334 and return the resulting encoded object (usually a Python string).
335
336 errors is passed to the encoder factory as argument if non-NULL. */
337
338PyObject *PyCodec_Encode(PyObject *object,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000339 const char *encoding,
340 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000341{
342 PyObject *encoder = NULL;
Neal Norwitz3715c3e2005-11-24 22:09:18 +0000343 PyObject *args = NULL, *result = NULL;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000344 PyObject *v = NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000345
346 encoder = PyCodec_Encoder(encoding);
347 if (encoder == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000348 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000349
350 args = args_tuple(object, errors);
351 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000352 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000353
354 result = PyEval_CallObject(encoder, args);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000355 if (result == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000356 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000357
Guido van Rossum98297ee2007-11-06 21:34:58 +0000358 if (!PyTuple_Check(result) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000359 PyTuple_GET_SIZE(result) != 2) {
360 PyErr_SetString(PyExc_TypeError,
361 "encoder must return a tuple (object, integer)");
362 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000363 }
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000364 v = PyTuple_GET_ITEM(result,0);
365 Py_INCREF(v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000366 /* We don't check or use the second (integer) entry. */
367
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000368 Py_DECREF(args);
369 Py_DECREF(encoder);
370 Py_DECREF(result);
371 return v;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000372
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000373 onError:
Neal Norwitz3715c3e2005-11-24 22:09:18 +0000374 Py_XDECREF(result);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000375 Py_XDECREF(args);
376 Py_XDECREF(encoder);
Marc-André Lemburgb2750b52008-06-06 12:18:17 +0000377 return NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000378}
379
380/* Decode an object (usually a Python string) using the given encoding
381 and return an equivalent object (e.g. an Unicode object).
382
383 errors is passed to the decoder factory as argument if non-NULL. */
384
385PyObject *PyCodec_Decode(PyObject *object,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000386 const char *encoding,
387 const char *errors)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000388{
389 PyObject *decoder = NULL;
390 PyObject *args = NULL, *result = NULL;
391 PyObject *v;
392
393 decoder = PyCodec_Decoder(encoding);
394 if (decoder == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000395 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000396
397 args = args_tuple(object, errors);
398 if (args == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000399 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000400
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000401 result = PyEval_CallObject(decoder,args);
402 if (result == NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000403 goto onError;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000404 if (!PyTuple_Check(result) ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000405 PyTuple_GET_SIZE(result) != 2) {
406 PyErr_SetString(PyExc_TypeError,
407 "decoder must return a tuple (object,integer)");
408 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000409 }
410 v = PyTuple_GET_ITEM(result,0);
411 Py_INCREF(v);
412 /* We don't check or use the second (integer) entry. */
413
414 Py_DECREF(args);
415 Py_DECREF(decoder);
416 Py_DECREF(result);
417 return v;
Guido van Rossum98297ee2007-11-06 21:34:58 +0000418
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000419 onError:
420 Py_XDECREF(args);
421 Py_XDECREF(decoder);
422 Py_XDECREF(result);
423 return NULL;
424}
425
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000426/* Register the error handling callback function error under the name
427 name. This function will be called by the codec when it encounters
428 an unencodable characters/undecodable bytes and doesn't know the
429 callback name, when name is specified as the error parameter
430 in the call to the encode/decode function.
431 Return 0 on success, -1 on error */
432int PyCodec_RegisterError(const char *name, PyObject *error)
433{
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000434 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000435 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000436 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000437 if (!PyCallable_Check(error)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000438 PyErr_SetString(PyExc_TypeError, "handler must be callable");
439 return -1;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000440 }
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000441 return PyDict_SetItemString(interp->codec_error_registry,
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000442 (char *)name, error);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000443}
444
445/* Lookup the error handling callback function registered under the
446 name error. As a special case NULL can be passed, in which case
447 the error handling callback for strict encoding will be returned. */
448PyObject *PyCodec_LookupError(const char *name)
449{
450 PyObject *handler = NULL;
451
Nicholas Bastine5662ae2004-03-24 22:22:12 +0000452 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000453 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000454 return NULL;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000455
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000456 if (name==NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000457 name = "strict";
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000458 handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000459 if (!handler)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000460 PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000461 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000462 Py_INCREF(handler);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000463 return handler;
464}
465
466static void wrong_exception_type(PyObject *exc)
467{
468 PyObject *type = PyObject_GetAttrString(exc, "__class__");
469 if (type != NULL) {
Walter Dörwald573c08c2007-05-25 15:46:59 +0000470 PyObject *name = PyObject_GetAttrString(type, "__name__");
471 Py_DECREF(type);
472 if (name != NULL) {
473 PyErr_Format(PyExc_TypeError,
474 "don't know how to handle %S in error callback", name);
475 Py_DECREF(name);
476 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000477 }
478}
479
480PyObject *PyCodec_StrictErrors(PyObject *exc)
481{
Brett Cannonbf364092006-03-01 04:25:17 +0000482 if (PyExceptionInstance_Check(exc))
483 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000484 else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000485 PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000486 return NULL;
487}
488
489
490PyObject *PyCodec_IgnoreErrors(PyObject *exc)
491{
Martin v. Löwis18e16552006-02-15 17:27:45 +0000492 Py_ssize_t end;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000493 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000494 if (PyUnicodeEncodeError_GetEnd(exc, &end))
495 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000496 }
497 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000498 if (PyUnicodeDecodeError_GetEnd(exc, &end))
499 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000500 }
501 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000502 if (PyUnicodeTranslateError_GetEnd(exc, &end))
503 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000504 }
505 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000506 wrong_exception_type(exc);
507 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000508 }
509 /* ouch: passing NULL, 0, pos gives None instead of u'' */
Martin v. Löwis18e16552006-02-15 17:27:45 +0000510 return Py_BuildValue("(u#n)", &end, 0, end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000511}
512
513
514PyObject *PyCodec_ReplaceErrors(PyObject *exc)
515{
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200516 Py_ssize_t start, end, i, len;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000517
518 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000519 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200520 int kind;
521 void *data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000522 if (PyUnicodeEncodeError_GetStart(exc, &start))
523 return NULL;
524 if (PyUnicodeEncodeError_GetEnd(exc, &end))
525 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200526 len = end - start;
527 res = PyUnicode_New(len, '?');
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000528 if (res == NULL)
529 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200530 kind = PyUnicode_KIND(res);
531 data = PyUnicode_DATA(res);
532 for (i = 0; i < len; ++i)
533 PyUnicode_WRITE(kind, data, i, '?');
534 return Py_BuildValue("(Nn)", res, end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000535 }
536 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000537 if (PyUnicodeDecodeError_GetEnd(exc, &end))
538 return NULL;
Victor Stinner1a15aba2011-10-02 19:00:15 +0200539 return Py_BuildValue("(Cn)",
540 (int)Py_UNICODE_REPLACEMENT_CHARACTER,
541 end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000542 }
543 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000544 PyObject *res;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200545 int kind;
546 void *data;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000547 if (PyUnicodeTranslateError_GetStart(exc, &start))
548 return NULL;
549 if (PyUnicodeTranslateError_GetEnd(exc, &end))
550 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200551 len = end - start;
552 res = PyUnicode_New(len, Py_UNICODE_REPLACEMENT_CHARACTER);
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000553 if (res == NULL)
554 return NULL;
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200555 kind = PyUnicode_KIND(res);
556 data = PyUnicode_DATA(res);
557 for (i=0; i < len; i++)
558 PyUnicode_WRITE(kind, data, i, Py_UNICODE_REPLACEMENT_CHARACTER);
559 return Py_BuildValue("(Nn)", res, end);
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000560 }
561 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000562 wrong_exception_type(exc);
563 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000564 }
565}
566
567PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
568{
569 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000570 PyObject *restuple;
571 PyObject *object;
572 Py_ssize_t start;
573 Py_ssize_t end;
574 PyObject *res;
575 Py_UNICODE *p;
576 Py_UNICODE *startp;
577 Py_UNICODE *outp;
578 int ressize;
579 if (PyUnicodeEncodeError_GetStart(exc, &start))
580 return NULL;
581 if (PyUnicodeEncodeError_GetEnd(exc, &end))
582 return NULL;
583 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
584 return NULL;
585 startp = PyUnicode_AS_UNICODE(object);
586 for (p = startp+start, ressize = 0; p < startp+end; ++p) {
587 if (*p<10)
588 ressize += 2+1+1;
589 else if (*p<100)
590 ressize += 2+2+1;
591 else if (*p<1000)
592 ressize += 2+3+1;
593 else if (*p<10000)
594 ressize += 2+4+1;
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000595#ifndef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000596 else
597 ressize += 2+5+1;
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000598#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000599 else if (*p<100000)
600 ressize += 2+5+1;
601 else if (*p<1000000)
602 ressize += 2+6+1;
603 else
604 ressize += 2+7+1;
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000605#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000606 }
607 /* allocate replacement */
608 res = PyUnicode_FromUnicode(NULL, ressize);
609 if (res == NULL) {
610 Py_DECREF(object);
611 return NULL;
612 }
613 /* generate replacement */
614 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
615 p < startp+end; ++p) {
616 Py_UNICODE c = *p;
617 int digits;
618 int base;
619 *outp++ = '&';
620 *outp++ = '#';
621 if (*p<10) {
622 digits = 1;
623 base = 1;
624 }
625 else if (*p<100) {
626 digits = 2;
627 base = 10;
628 }
629 else if (*p<1000) {
630 digits = 3;
631 base = 100;
632 }
633 else if (*p<10000) {
634 digits = 4;
635 base = 1000;
636 }
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000637#ifndef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000638 else {
639 digits = 5;
640 base = 10000;
641 }
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000642#else
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000643 else if (*p<100000) {
644 digits = 5;
645 base = 10000;
646 }
647 else if (*p<1000000) {
648 digits = 6;
649 base = 100000;
650 }
651 else {
652 digits = 7;
653 base = 1000000;
654 }
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000655#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000656 while (digits-->0) {
657 *outp++ = '0' + c/base;
658 c %= base;
659 base /= 10;
660 }
661 *outp++ = ';';
662 }
663 restuple = Py_BuildValue("(On)", res, end);
664 Py_DECREF(res);
665 Py_DECREF(object);
666 return restuple;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000667 }
668 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000669 wrong_exception_type(exc);
670 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000671 }
672}
673
Martin v. Löwisd63a3b82011-09-28 07:41:54 +0200674static const char *hexdigits = "0123456789abcdef";
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000675
676PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
677{
Antoine Pitroue4a18922010-09-09 20:30:23 +0000678#ifndef Py_UNICODE_WIDE
679#define IS_SURROGATE_PAIR(p, end) \
680 (*p >= 0xD800 && *p <= 0xDBFF && (p + 1) < end && \
681 *(p + 1) >= 0xDC00 && *(p + 1) <= 0xDFFF)
682#else
683#define IS_SURROGATE_PAIR(p, end) 0
684#endif
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000685 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000686 PyObject *restuple;
687 PyObject *object;
688 Py_ssize_t start;
689 Py_ssize_t end;
690 PyObject *res;
691 Py_UNICODE *p;
692 Py_UNICODE *startp;
693 Py_UNICODE *outp;
694 int ressize;
695 if (PyUnicodeEncodeError_GetStart(exc, &start))
696 return NULL;
697 if (PyUnicodeEncodeError_GetEnd(exc, &end))
698 return NULL;
699 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
700 return NULL;
701 startp = PyUnicode_AS_UNICODE(object);
702 for (p = startp+start, ressize = 0; p < startp+end; ++p) {
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000703#ifdef Py_UNICODE_WIDE
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000704 if (*p >= 0x00010000)
705 ressize += 1+1+8;
706 else
Hye-Shik Chang7db07e62003-12-29 01:36:01 +0000707#endif
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000708 if (*p >= 0x100) {
Antoine Pitroue4a18922010-09-09 20:30:23 +0000709 if (IS_SURROGATE_PAIR(p, startp+end)) {
710 ressize += 1+1+8;
711 ++p;
712 }
713 else
714 ressize += 1+1+4;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000715 }
716 else
717 ressize += 1+1+2;
718 }
719 res = PyUnicode_FromUnicode(NULL, ressize);
720 if (res==NULL)
721 return NULL;
722 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
723 p < startp+end; ++p) {
Antoine Pitroue4a18922010-09-09 20:30:23 +0000724 Py_UCS4 c = (Py_UCS4) *p;
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000725 *outp++ = '\\';
Antoine Pitroue4a18922010-09-09 20:30:23 +0000726 if (IS_SURROGATE_PAIR(p, startp+end)) {
727 c = ((*p & 0x3FF) << 10) + (*(p + 1) & 0x3FF) + 0x10000;
728 ++p;
729 }
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000730 if (c >= 0x00010000) {
731 *outp++ = 'U';
732 *outp++ = hexdigits[(c>>28)&0xf];
733 *outp++ = hexdigits[(c>>24)&0xf];
734 *outp++ = hexdigits[(c>>20)&0xf];
735 *outp++ = hexdigits[(c>>16)&0xf];
736 *outp++ = hexdigits[(c>>12)&0xf];
737 *outp++ = hexdigits[(c>>8)&0xf];
738 }
Antoine Pitroue4a18922010-09-09 20:30:23 +0000739 else if (c >= 0x100) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000740 *outp++ = 'u';
741 *outp++ = hexdigits[(c>>12)&0xf];
742 *outp++ = hexdigits[(c>>8)&0xf];
743 }
744 else
745 *outp++ = 'x';
746 *outp++ = hexdigits[(c>>4)&0xf];
747 *outp++ = hexdigits[c&0xf];
748 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000749
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000750 restuple = Py_BuildValue("(On)", res, end);
751 Py_DECREF(res);
752 Py_DECREF(object);
753 return restuple;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000754 }
755 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000756 wrong_exception_type(exc);
757 return NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000758 }
Antoine Pitroue4a18922010-09-09 20:30:23 +0000759#undef IS_SURROGATE_PAIR
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000760}
761
Martin v. Löwisaef3fb02009-05-02 19:27:30 +0000762/* This handler is declared static until someone demonstrates
763 a need to call it directly. */
764static PyObject *
Martin v. Löwise0a2b722009-05-10 08:08:56 +0000765PyCodec_SurrogatePassErrors(PyObject *exc)
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000766{
767 PyObject *restuple;
768 PyObject *object;
769 Py_ssize_t start;
770 Py_ssize_t end;
771 PyObject *res;
772 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000773 Py_UNICODE *p;
774 Py_UNICODE *startp;
775 char *outp;
776 if (PyUnicodeEncodeError_GetStart(exc, &start))
777 return NULL;
778 if (PyUnicodeEncodeError_GetEnd(exc, &end))
779 return NULL;
780 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
781 return NULL;
782 startp = PyUnicode_AS_UNICODE(object);
783 res = PyBytes_FromStringAndSize(NULL, 3*(end-start));
784 if (!res) {
785 Py_DECREF(object);
786 return NULL;
787 }
788 outp = PyBytes_AsString(res);
789 for (p = startp+start; p < startp+end; p++) {
790 Py_UNICODE ch = *p;
791 if (ch < 0xd800 || ch > 0xdfff) {
792 /* Not a surrogate, fail with original exception */
793 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
794 Py_DECREF(res);
795 Py_DECREF(object);
796 return NULL;
797 }
798 *outp++ = (char)(0xe0 | (ch >> 12));
799 *outp++ = (char)(0x80 | ((ch >> 6) & 0x3f));
800 *outp++ = (char)(0x80 | (ch & 0x3f));
801 }
802 restuple = Py_BuildValue("(On)", res, end);
803 Py_DECREF(res);
804 Py_DECREF(object);
805 return restuple;
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000806 }
807 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000808 unsigned char *p;
809 Py_UNICODE ch = 0;
810 if (PyUnicodeDecodeError_GetStart(exc, &start))
811 return NULL;
812 if (!(object = PyUnicodeDecodeError_GetObject(exc)))
813 return NULL;
814 if (!(p = (unsigned char*)PyBytes_AsString(object))) {
815 Py_DECREF(object);
816 return NULL;
817 }
818 /* Try decoding a single surrogate character. If
819 there are more, let the codec call us again. */
820 p += start;
821 if ((p[0] & 0xf0) == 0xe0 ||
822 (p[1] & 0xc0) == 0x80 ||
823 (p[2] & 0xc0) == 0x80) {
824 /* it's a three-byte code */
825 ch = ((p[0] & 0x0f) << 12) + ((p[1] & 0x3f) << 6) + (p[2] & 0x3f);
826 if (ch < 0xd800 || ch > 0xdfff)
827 /* it's not a surrogate - fail */
828 ch = 0;
829 }
830 Py_DECREF(object);
831 if (ch == 0) {
832 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
833 return NULL;
834 }
835 return Py_BuildValue("(u#n)", &ch, 1, start+3);
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000836 }
837 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000838 wrong_exception_type(exc);
839 return NULL;
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000840 }
841}
842
Martin v. Löwis011e8422009-05-05 04:43:17 +0000843static PyObject *
Martin v. Löwis43c57782009-05-10 08:15:24 +0000844PyCodec_SurrogateEscapeErrors(PyObject *exc)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000845{
846 PyObject *restuple;
847 PyObject *object;
848 Py_ssize_t start;
849 Py_ssize_t end;
850 PyObject *res;
851 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000852 Py_UNICODE *p;
853 Py_UNICODE *startp;
854 char *outp;
855 if (PyUnicodeEncodeError_GetStart(exc, &start))
856 return NULL;
857 if (PyUnicodeEncodeError_GetEnd(exc, &end))
858 return NULL;
859 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
860 return NULL;
861 startp = PyUnicode_AS_UNICODE(object);
862 res = PyBytes_FromStringAndSize(NULL, end-start);
863 if (!res) {
864 Py_DECREF(object);
865 return NULL;
866 }
867 outp = PyBytes_AsString(res);
868 for (p = startp+start; p < startp+end; p++) {
869 Py_UNICODE ch = *p;
870 if (ch < 0xdc80 || ch > 0xdcff) {
871 /* Not a UTF-8b surrogate, fail with original exception */
872 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
873 Py_DECREF(res);
874 Py_DECREF(object);
875 return NULL;
876 }
877 *outp++ = ch - 0xdc00;
878 }
879 restuple = Py_BuildValue("(On)", res, end);
880 Py_DECREF(res);
881 Py_DECREF(object);
882 return restuple;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000883 }
884 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000885 unsigned char *p;
886 Py_UNICODE ch[4]; /* decode up to 4 bad bytes. */
887 int consumed = 0;
888 if (PyUnicodeDecodeError_GetStart(exc, &start))
889 return NULL;
890 if (PyUnicodeDecodeError_GetEnd(exc, &end))
891 return NULL;
892 if (!(object = PyUnicodeDecodeError_GetObject(exc)))
893 return NULL;
894 if (!(p = (unsigned char*)PyBytes_AsString(object))) {
895 Py_DECREF(object);
896 return NULL;
897 }
898 while (consumed < 4 && consumed < end-start) {
899 /* Refuse to escape ASCII bytes. */
900 if (p[start+consumed] < 128)
901 break;
902 ch[consumed] = 0xdc00 + p[start+consumed];
903 consumed++;
904 }
905 Py_DECREF(object);
906 if (!consumed) {
907 /* codec complained about ASCII byte. */
908 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
909 return NULL;
910 }
911 return Py_BuildValue("(u#n)", ch, consumed, start+consumed);
Martin v. Löwis011e8422009-05-05 04:43:17 +0000912 }
913 else {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000914 wrong_exception_type(exc);
915 return NULL;
Martin v. Löwis011e8422009-05-05 04:43:17 +0000916 }
917}
918
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000919
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000920static PyObject *strict_errors(PyObject *self, PyObject *exc)
921{
922 return PyCodec_StrictErrors(exc);
923}
924
925
926static PyObject *ignore_errors(PyObject *self, PyObject *exc)
927{
928 return PyCodec_IgnoreErrors(exc);
929}
930
931
932static PyObject *replace_errors(PyObject *self, PyObject *exc)
933{
934 return PyCodec_ReplaceErrors(exc);
935}
936
937
938static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)
939{
940 return PyCodec_XMLCharRefReplaceErrors(exc);
941}
942
943
944static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)
945{
946 return PyCodec_BackslashReplaceErrors(exc);
947}
948
Martin v. Löwise0a2b722009-05-10 08:08:56 +0000949static PyObject *surrogatepass_errors(PyObject *self, PyObject *exc)
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000950{
Martin v. Löwise0a2b722009-05-10 08:08:56 +0000951 return PyCodec_SurrogatePassErrors(exc);
Martin v. Löwisdb12d452009-05-02 18:52:14 +0000952}
953
Martin v. Löwis43c57782009-05-10 08:15:24 +0000954static PyObject *surrogateescape_errors(PyObject *self, PyObject *exc)
Martin v. Löwis011e8422009-05-05 04:43:17 +0000955{
Martin v. Löwis43c57782009-05-10 08:15:24 +0000956 return PyCodec_SurrogateEscapeErrors(exc);
Martin v. Löwis011e8422009-05-05 04:43:17 +0000957}
958
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +0000959static int _PyCodecRegistry_Init(void)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000960{
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000961 static struct {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000962 char *name;
963 PyMethodDef def;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000964 } methods[] =
965 {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +0000966 {
967 "strict",
968 {
969 "strict_errors",
970 strict_errors,
971 METH_O,
972 PyDoc_STR("Implements the 'strict' error handling, which "
973 "raises a UnicodeError on coding errors.")
974 }
975 },
976 {
977 "ignore",
978 {
979 "ignore_errors",
980 ignore_errors,
981 METH_O,
982 PyDoc_STR("Implements the 'ignore' error handling, which "
983 "ignores malformed data and continues.")
984 }
985 },
986 {
987 "replace",
988 {
989 "replace_errors",
990 replace_errors,
991 METH_O,
992 PyDoc_STR("Implements the 'replace' error handling, which "
993 "replaces malformed data with a replacement marker.")
994 }
995 },
996 {
997 "xmlcharrefreplace",
998 {
999 "xmlcharrefreplace_errors",
1000 xmlcharrefreplace_errors,
1001 METH_O,
1002 PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
1003 "which replaces an unencodable character with the "
1004 "appropriate XML character reference.")
1005 }
1006 },
1007 {
1008 "backslashreplace",
1009 {
1010 "backslashreplace_errors",
1011 backslashreplace_errors,
1012 METH_O,
1013 PyDoc_STR("Implements the 'backslashreplace' error handling, "
1014 "which replaces an unencodable character with a "
1015 "backslashed escape sequence.")
1016 }
1017 },
1018 {
1019 "surrogatepass",
1020 {
1021 "surrogatepass",
1022 surrogatepass_errors,
1023 METH_O
1024 }
1025 },
1026 {
1027 "surrogateescape",
1028 {
1029 "surrogateescape",
1030 surrogateescape_errors,
1031 METH_O
1032 }
1033 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001034 };
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001035
Nicholas Bastine5662ae2004-03-24 22:22:12 +00001036 PyInterpreterState *interp = PyThreadState_GET()->interp;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001037 PyObject *mod;
Neal Norwitz739a8f82004-07-08 01:55:58 +00001038 unsigned i;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001039
1040 if (interp->codec_search_path != NULL)
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001041 return 0;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001042
1043 interp->codec_search_path = PyList_New(0);
1044 interp->codec_search_cache = PyDict_New();
1045 interp->codec_error_registry = PyDict_New();
1046
1047 if (interp->codec_error_registry) {
Victor Stinner63941882011-09-29 00:42:28 +02001048 for (i = 0; i < Py_ARRAY_LENGTH(methods); ++i) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001049 PyObject *func = PyCFunction_New(&methods[i].def, NULL);
1050 int res;
1051 if (!func)
1052 Py_FatalError("can't initialize codec error registry");
1053 res = PyCodec_RegisterError(methods[i].name, func);
1054 Py_DECREF(func);
1055 if (res)
1056 Py_FatalError("can't initialize codec error registry");
1057 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +00001058 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +00001059
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001060 if (interp->codec_search_path == NULL ||
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001061 interp->codec_search_cache == NULL ||
1062 interp->codec_error_registry == NULL)
1063 Py_FatalError("can't initialize codec registry");
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001064
Christian Heimes819b8bf2008-01-03 23:05:47 +00001065 mod = PyImport_ImportModuleNoBlock("encodings");
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001066 if (mod == NULL) {
Antoine Pitrouf95a1b32010-05-09 15:52:27 +00001067 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
1068 /* Ignore ImportErrors... this is done so that
1069 distributions can disable the encodings package. Note
1070 that other errors are not masked, e.g. SystemErrors
1071 raised to inform the user of an error in the Python
1072 configuration are still reported back to the user. */
1073 PyErr_Clear();
1074 return 0;
1075 }
1076 return -1;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001077 }
1078 Py_DECREF(mod);
Christian Heimes6a27efa2008-10-30 21:48:26 +00001079 interp->codecs_initialized = 1;
Gustavo Niemeyer5ddd4c32003-03-19 00:35:36 +00001080 return 0;
Guido van Rossumfeee4b92000-03-10 22:57:27 +00001081}