blob: 1424bb5b709487e8fa71078524b4be33612d7fbb [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
14/* --- Globals ------------------------------------------------------------ */
15
16static PyObject *_PyCodec_SearchPath;
17static PyObject *_PyCodec_SearchCache;
18
19/* Flag used for lazy import of the standard encodings package */
20static int import_encodings_called = 0;
21
22/* --- Codec Registry ----------------------------------------------------- */
23
24/* Import the standard encodings package which will register the first
25 codec search function.
26
27 This is done in a lazy way so that the Unicode implementation does
28 not downgrade startup time of scripts not needing it.
29
Guido van Rossumb95de4f2000-03-31 17:25:23 +000030 ImportErrors are silently ignored by this function. Only one try is
31 made.
Guido van Rossumfeee4b92000-03-10 22:57:27 +000032
33*/
34
35static
Thomas Woutersf70ef4f2000-07-22 18:47:25 +000036int import_encodings(void)
Guido van Rossumfeee4b92000-03-10 22:57:27 +000037{
38 PyObject *mod;
39
40 import_encodings_called = 1;
41 mod = PyImport_ImportModule("encodings");
42 if (mod == NULL) {
Guido van Rossumb95de4f2000-03-31 17:25:23 +000043 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
44 /* Ignore ImportErrors... this is done so that
45 distributions can disable the encodings package. Note
46 that other errors are not masked, e.g. SystemErrors
47 raised to inform the user of an error in the Python
48 configuration are still reported back to the user. */
49 PyErr_Clear();
50 return 0;
51 }
52 return -1;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000053 }
54 Py_DECREF(mod);
Guido van Rossumb95de4f2000-03-31 17:25:23 +000055 return 0;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000056}
57
Guido van Rossumfeee4b92000-03-10 22:57:27 +000058int PyCodec_Register(PyObject *search_function)
59{
Guido van Rossumb95de4f2000-03-31 17:25:23 +000060 if (!import_encodings_called) {
61 if (import_encodings())
62 goto onError;
63 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +000064 if (search_function == NULL) {
65 PyErr_BadArgument();
Guido van Rossumb95de4f2000-03-31 17:25:23 +000066 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000067 }
68 if (!PyCallable_Check(search_function)) {
69 PyErr_SetString(PyExc_TypeError,
70 "argument must be callable");
Guido van Rossumb95de4f2000-03-31 17:25:23 +000071 goto onError;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000072 }
73 return PyList_Append(_PyCodec_SearchPath, search_function);
Guido van Rossumb95de4f2000-03-31 17:25:23 +000074
75 onError:
76 return -1;
Guido van Rossumfeee4b92000-03-10 22:57:27 +000077}
78
Guido van Rossum9e896b32000-04-05 20:11:21 +000079/* Convert a string to a normalized Python string: all characters are
80 converted to lower case, spaces are replaced with underscores. */
81
Guido van Rossumfeee4b92000-03-10 22:57:27 +000082static
Guido van Rossum9e896b32000-04-05 20:11:21 +000083PyObject *normalizestring(const char *string)
Guido van Rossumfeee4b92000-03-10 22:57:27 +000084{
Guido van Rossum33831132000-06-29 14:50:15 +000085 register size_t i;
Guido van Rossum582acec2000-06-28 22:07:35 +000086 size_t len = strlen(string);
Guido van Rossumfeee4b92000-03-10 22:57:27 +000087 char *p;
88 PyObject *v;
89
Guido van Rossum582acec2000-06-28 22:07:35 +000090 if (len > INT_MAX) {
91 PyErr_SetString(PyExc_OverflowError, "string is too large");
92 return NULL;
93 }
94
95 v = PyString_FromStringAndSize(NULL, (int)len);
Guido van Rossumfeee4b92000-03-10 22:57:27 +000096 if (v == NULL)
97 return NULL;
98 p = PyString_AS_STRING(v);
Guido van Rossum9e896b32000-04-05 20:11:21 +000099 for (i = 0; i < len; i++) {
100 register char ch = string[i];
101 if (ch == ' ')
102 ch = '-';
103 else
104 ch = tolower(ch);
105 p[i] = ch;
106 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000107 return v;
108}
109
110/* Lookup the given encoding and return a tuple providing the codec
111 facilities.
112
113 The encoding string is looked up converted to all lower-case
114 characters. This makes encodings looked up through this mechanism
115 effectively case-insensitive.
116
Fred Drake766de832000-05-09 19:55:59 +0000117 If no codec is found, a LookupError is set and NULL returned.
Guido van Rossumb95de4f2000-03-31 17:25:23 +0000118
119 As side effect, this tries to load the encodings package, if not
120 yet done. This is part of the lazy load strategy for the encodings
121 package.
122
123*/
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000124
125PyObject *_PyCodec_Lookup(const char *encoding)
126{
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000127 PyObject *result, *args = NULL, *v;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000128 int i, len;
129
Fred Drake766de832000-05-09 19:55:59 +0000130 if (encoding == NULL) {
131 PyErr_BadArgument();
132 goto onError;
133 }
Guido van Rossumb95de4f2000-03-31 17:25:23 +0000134 if (_PyCodec_SearchCache == NULL ||
135 _PyCodec_SearchPath == NULL) {
Barry Warsaw51ac5802000-03-20 16:36:48 +0000136 PyErr_SetString(PyExc_SystemError,
137 "codec module not properly initialized");
138 goto onError;
139 }
Guido van Rossumb95de4f2000-03-31 17:25:23 +0000140 if (!import_encodings_called) {
141 if (import_encodings())
142 goto onError;
143 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000144
Guido van Rossum9e896b32000-04-05 20:11:21 +0000145 /* Convert the encoding to a normalized Python string: all
Thomas Wouters7e474022000-07-16 12:04:32 +0000146 characters are converted to lower case, spaces and hyphens are
Guido van Rossum9e896b32000-04-05 20:11:21 +0000147 replaced with underscores. */
148 v = normalizestring(encoding);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000149 if (v == NULL)
150 goto onError;
151 PyString_InternInPlace(&v);
152
153 /* First, try to lookup the name in the registry dictionary */
154 result = PyDict_GetItem(_PyCodec_SearchCache, v);
155 if (result != NULL) {
156 Py_INCREF(result);
Barry Warsaw51ac5802000-03-20 16:36:48 +0000157 Py_DECREF(v);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000158 return result;
159 }
160
161 /* Next, scan the search functions in order of registration */
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000162 args = PyTuple_New(1);
163 if (args == NULL)
164 goto onError;
165 PyTuple_SET_ITEM(args,0,v);
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000166
167 len = PyList_Size(_PyCodec_SearchPath);
168 if (len < 0)
169 goto onError;
Guido van Rossumb95de4f2000-03-31 17:25:23 +0000170 if (len == 0) {
171 PyErr_SetString(PyExc_LookupError,
172 "no codec search functions registered: "
173 "can't find encoding");
174 goto onError;
175 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000176
177 for (i = 0; i < len; i++) {
178 PyObject *func;
179
180 func = PyList_GetItem(_PyCodec_SearchPath, i);
181 if (func == NULL)
182 goto onError;
Guido van Rossum5ba3c842000-03-24 20:52:23 +0000183 result = PyEval_CallObject(func, args);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000184 if (result == NULL)
185 goto onError;
186 if (result == Py_None) {
187 Py_DECREF(result);
188 continue;
189 }
190 if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
191 PyErr_SetString(PyExc_TypeError,
192 "codec search functions must return 4-tuples");
193 Py_DECREF(result);
194 goto onError;
195 }
196 break;
197 }
198 if (i == len) {
199 /* XXX Perhaps we should cache misses too ? */
Martin v. Löwiseb42b022002-09-26 16:01:24 +0000200 PyErr_Format(PyExc_LookupError,
201 "unknown encoding: %s", encoding);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000202 goto onError;
203 }
204
205 /* Cache and return the result */
206 PyDict_SetItem(_PyCodec_SearchCache, v, result);
207 Py_DECREF(args);
208 return result;
209
210 onError:
211 Py_XDECREF(args);
212 return NULL;
213}
214
215static
216PyObject *args_tuple(PyObject *object,
217 const char *errors)
218{
219 PyObject *args;
220
221 args = PyTuple_New(1 + (errors != NULL));
222 if (args == NULL)
223 return NULL;
224 Py_INCREF(object);
225 PyTuple_SET_ITEM(args,0,object);
226 if (errors) {
227 PyObject *v;
228
229 v = PyString_FromString(errors);
230 if (v == NULL) {
231 Py_DECREF(args);
232 return NULL;
233 }
234 PyTuple_SET_ITEM(args, 1, v);
235 }
236 return args;
237}
238
239/* Build a codec by calling factory(stream[,errors]) or just
240 factory(errors) depending on whether the given parameters are
241 non-NULL. */
242
243static
244PyObject *build_stream_codec(PyObject *factory,
245 PyObject *stream,
246 const char *errors)
247{
248 PyObject *args, *codec;
249
250 args = args_tuple(stream, errors);
251 if (args == NULL)
252 return NULL;
253
254 codec = PyEval_CallObject(factory, args);
255 Py_DECREF(args);
256 return codec;
257}
258
259/* Convenience APIs to query the Codec registry.
260
261 All APIs return a codec object with incremented refcount.
262
263 */
264
265PyObject *PyCodec_Encoder(const char *encoding)
266{
267 PyObject *codecs;
268 PyObject *v;
269
270 codecs = _PyCodec_Lookup(encoding);
271 if (codecs == NULL)
272 goto onError;
273 v = PyTuple_GET_ITEM(codecs,0);
Mark Hammonde21262c2002-07-18 23:06:17 +0000274 Py_DECREF(codecs);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000275 Py_INCREF(v);
276 return v;
277
278 onError:
279 return NULL;
280}
281
282PyObject *PyCodec_Decoder(const char *encoding)
283{
284 PyObject *codecs;
285 PyObject *v;
286
287 codecs = _PyCodec_Lookup(encoding);
288 if (codecs == NULL)
289 goto onError;
290 v = PyTuple_GET_ITEM(codecs,1);
Mark Hammonde21262c2002-07-18 23:06:17 +0000291 Py_DECREF(codecs);
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000292 Py_INCREF(v);
293 return v;
294
295 onError:
296 return NULL;
297}
298
299PyObject *PyCodec_StreamReader(const char *encoding,
300 PyObject *stream,
301 const char *errors)
302{
Mark Hammonde21262c2002-07-18 23:06:17 +0000303 PyObject *codecs, *ret;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000304
305 codecs = _PyCodec_Lookup(encoding);
306 if (codecs == NULL)
307 goto onError;
Mark Hammonde21262c2002-07-18 23:06:17 +0000308 ret = build_stream_codec(PyTuple_GET_ITEM(codecs,2),stream,errors);
309 Py_DECREF(codecs);
310 return ret;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000311
312 onError:
313 return NULL;
314}
315
316PyObject *PyCodec_StreamWriter(const char *encoding,
317 PyObject *stream,
318 const char *errors)
319{
Mark Hammonde21262c2002-07-18 23:06:17 +0000320 PyObject *codecs, *ret;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000321
322 codecs = _PyCodec_Lookup(encoding);
323 if (codecs == NULL)
324 goto onError;
Mark Hammonde21262c2002-07-18 23:06:17 +0000325 ret = build_stream_codec(PyTuple_GET_ITEM(codecs,3),stream,errors);
326 Py_DECREF(codecs);
327 return ret;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000328
329 onError:
330 return NULL;
331}
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,
339 const char *encoding,
340 const char *errors)
341{
342 PyObject *encoder = NULL;
343 PyObject *args = NULL, *result;
344 PyObject *v;
345
346 encoder = PyCodec_Encoder(encoding);
347 if (encoder == NULL)
348 goto onError;
349
350 args = args_tuple(object, errors);
351 if (args == NULL)
352 goto onError;
353
354 result = PyEval_CallObject(encoder,args);
355 if (result == NULL)
356 goto onError;
357
358 if (!PyTuple_Check(result) ||
359 PyTuple_GET_SIZE(result) != 2) {
360 PyErr_SetString(PyExc_TypeError,
361 "encoder must return a tuple (object,integer)");
362 goto onError;
363 }
364 v = PyTuple_GET_ITEM(result,0);
365 Py_INCREF(v);
366 /* We don't check or use the second (integer) entry. */
367
368 Py_DECREF(args);
369 Py_DECREF(encoder);
370 Py_DECREF(result);
371 return v;
372
373 onError:
374 Py_XDECREF(args);
375 Py_XDECREF(encoder);
376 return NULL;
377}
378
379/* Decode an object (usually a Python string) using the given encoding
380 and return an equivalent object (e.g. an Unicode object).
381
382 errors is passed to the decoder factory as argument if non-NULL. */
383
384PyObject *PyCodec_Decode(PyObject *object,
385 const char *encoding,
386 const char *errors)
387{
388 PyObject *decoder = NULL;
389 PyObject *args = NULL, *result = NULL;
390 PyObject *v;
391
392 decoder = PyCodec_Decoder(encoding);
393 if (decoder == NULL)
394 goto onError;
395
396 args = args_tuple(object, errors);
397 if (args == NULL)
398 goto onError;
399
400 result = PyEval_CallObject(decoder,args);
401 if (result == NULL)
402 goto onError;
403 if (!PyTuple_Check(result) ||
404 PyTuple_GET_SIZE(result) != 2) {
405 PyErr_SetString(PyExc_TypeError,
406 "decoder must return a tuple (object,integer)");
407 goto onError;
408 }
409 v = PyTuple_GET_ITEM(result,0);
410 Py_INCREF(v);
411 /* We don't check or use the second (integer) entry. */
412
413 Py_DECREF(args);
414 Py_DECREF(decoder);
415 Py_DECREF(result);
416 return v;
417
418 onError:
419 Py_XDECREF(args);
420 Py_XDECREF(decoder);
421 Py_XDECREF(result);
422 return NULL;
423}
424
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000425static PyObject *_PyCodec_ErrorRegistry;
426
427/* Register the error handling callback function error under the name
428 name. This function will be called by the codec when it encounters
429 an unencodable characters/undecodable bytes and doesn't know the
430 callback name, when name is specified as the error parameter
431 in the call to the encode/decode function.
432 Return 0 on success, -1 on error */
433int PyCodec_RegisterError(const char *name, PyObject *error)
434{
435 if (!PyCallable_Check(error)) {
436 PyErr_SetString(PyExc_TypeError, "handler must be callable");
437 return -1;
438 }
439 return PyDict_SetItemString( _PyCodec_ErrorRegistry, (char *)name, error);
440}
441
442/* Lookup the error handling callback function registered under the
443 name error. As a special case NULL can be passed, in which case
444 the error handling callback for strict encoding will be returned. */
445PyObject *PyCodec_LookupError(const char *name)
446{
447 PyObject *handler = NULL;
448
449 if (name==NULL)
450 name = "strict";
451 handler = PyDict_GetItemString(_PyCodec_ErrorRegistry, (char *)name);
452 if (!handler)
453 PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
454 else
455 Py_INCREF(handler);
456 return handler;
457}
458
459static void wrong_exception_type(PyObject *exc)
460{
461 PyObject *type = PyObject_GetAttrString(exc, "__class__");
462 if (type != NULL) {
463 PyObject *name = PyObject_GetAttrString(type, "__name__");
464 Py_DECREF(type);
465 if (name != NULL) {
466 PyObject *string = PyObject_Str(name);
467 Py_DECREF(name);
Walter Dörwaldf7bcd1d2002-09-02 18:22:32 +0000468 if (string != NULL) {
469 PyErr_Format(PyExc_TypeError,
470 "don't know how to handle %.400s in error callback",
471 PyString_AS_STRING(string));
472 Py_DECREF(string);
473 }
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000474 }
475 }
476}
477
478PyObject *PyCodec_StrictErrors(PyObject *exc)
479{
480 if (PyInstance_Check(exc))
481 PyErr_SetObject((PyObject*)((PyInstanceObject*)exc)->in_class,
482 exc);
483 else
484 PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
485 return NULL;
486}
487
488
489PyObject *PyCodec_IgnoreErrors(PyObject *exc)
490{
491 int end;
492 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
493 if (PyUnicodeEncodeError_GetEnd(exc, &end))
494 return NULL;
495 }
496 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
497 if (PyUnicodeDecodeError_GetEnd(exc, &end))
498 return NULL;
499 }
500 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
501 if (PyUnicodeTranslateError_GetEnd(exc, &end))
502 return NULL;
503 }
504 else {
505 wrong_exception_type(exc);
506 return NULL;
507 }
508 /* ouch: passing NULL, 0, pos gives None instead of u'' */
509 return Py_BuildValue("(u#i)", &end, 0, end);
510}
511
512
513PyObject *PyCodec_ReplaceErrors(PyObject *exc)
514{
515 PyObject *restuple;
516 int start;
517 int end;
518 int i;
519
520 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
521 PyObject *res;
522 Py_UNICODE *p;
523 if (PyUnicodeEncodeError_GetStart(exc, &start))
524 return NULL;
525 if (PyUnicodeEncodeError_GetEnd(exc, &end))
526 return NULL;
527 res = PyUnicode_FromUnicode(NULL, end-start);
528 if (res == NULL)
529 return NULL;
530 for (p = PyUnicode_AS_UNICODE(res), i = start;
531 i<end; ++p, ++i)
532 *p = '?';
533 restuple = Py_BuildValue("(Oi)", res, end);
534 Py_DECREF(res);
535 return restuple;
536 }
537 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
538 Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
539 if (PyUnicodeDecodeError_GetEnd(exc, &end))
540 return NULL;
541 return Py_BuildValue("(u#i)", &res, 1, end);
542 }
543 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
544 PyObject *res;
545 Py_UNICODE *p;
546 if (PyUnicodeTranslateError_GetStart(exc, &start))
547 return NULL;
548 if (PyUnicodeTranslateError_GetEnd(exc, &end))
549 return NULL;
550 res = PyUnicode_FromUnicode(NULL, end-start);
551 if (res == NULL)
552 return NULL;
553 for (p = PyUnicode_AS_UNICODE(res), i = start;
554 i<end; ++p, ++i)
555 *p = Py_UNICODE_REPLACEMENT_CHARACTER;
556 restuple = Py_BuildValue("(Oi)", res, end);
557 Py_DECREF(res);
558 return restuple;
559 }
560 else {
561 wrong_exception_type(exc);
562 return NULL;
563 }
564}
565
566PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
567{
568 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
569 PyObject *restuple;
570 PyObject *object;
571 int start;
572 int end;
573 PyObject *res;
574 Py_UNICODE *p;
575 Py_UNICODE *startp;
576 Py_UNICODE *outp;
577 int ressize;
578 if (PyUnicodeEncodeError_GetStart(exc, &start))
579 return NULL;
580 if (PyUnicodeEncodeError_GetEnd(exc, &end))
581 return NULL;
582 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
583 return NULL;
584 startp = PyUnicode_AS_UNICODE(object);
585 for (p = startp+start, ressize = 0; p < startp+end; ++p) {
586 if (*p<10)
587 ressize += 2+1+1;
588 else if (*p<100)
589 ressize += 2+2+1;
590 else if (*p<1000)
591 ressize += 2+3+1;
592 else if (*p<10000)
593 ressize += 2+4+1;
594 else if (*p<100000)
595 ressize += 2+5+1;
596 else if (*p<1000000)
597 ressize += 2+6+1;
598 else
599 ressize += 2+7+1;
600 }
601 /* allocate replacement */
602 res = PyUnicode_FromUnicode(NULL, ressize);
603 if (res == NULL) {
604 Py_DECREF(object);
605 return NULL;
606 }
607 /* generate replacement */
608 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
609 p < startp+end; ++p) {
610 Py_UNICODE c = *p;
611 int digits;
612 int base;
613 *outp++ = '&';
614 *outp++ = '#';
615 if (*p<10) {
616 digits = 1;
617 base = 1;
618 }
619 else if (*p<100) {
620 digits = 2;
621 base = 10;
622 }
623 else if (*p<1000) {
624 digits = 3;
625 base = 100;
626 }
627 else if (*p<10000) {
628 digits = 4;
629 base = 1000;
630 }
631 else if (*p<100000) {
632 digits = 5;
633 base = 10000;
634 }
635 else if (*p<1000000) {
636 digits = 6;
637 base = 100000;
638 }
639 else {
640 digits = 7;
641 base = 1000000;
642 }
643 while (digits-->0) {
644 *outp++ = '0' + c/base;
645 c %= base;
646 base /= 10;
647 }
648 *outp++ = ';';
649 }
650 restuple = Py_BuildValue("(Oi)", res, end);
651 Py_DECREF(res);
652 Py_DECREF(object);
653 return restuple;
654 }
655 else {
656 wrong_exception_type(exc);
657 return NULL;
658 }
659}
660
661static Py_UNICODE hexdigits[] = {
662 '0', '1', '2', '3', '4', '5', '6', '7',
663 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
664};
665
666PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
667{
668 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
669 PyObject *restuple;
670 PyObject *object;
671 int start;
672 int end;
673 PyObject *res;
674 Py_UNICODE *p;
675 Py_UNICODE *startp;
676 Py_UNICODE *outp;
677 int ressize;
678 if (PyUnicodeEncodeError_GetStart(exc, &start))
679 return NULL;
680 if (PyUnicodeEncodeError_GetEnd(exc, &end))
681 return NULL;
682 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
683 return NULL;
684 startp = PyUnicode_AS_UNICODE(object);
685 for (p = startp+start, ressize = 0; p < startp+end; ++p) {
686 if (*p >= 0x00010000)
687 ressize += 1+1+8;
688 else if (*p >= 0x100) {
689 ressize += 1+1+4;
690 }
691 else
692 ressize += 1+1+2;
693 }
694 res = PyUnicode_FromUnicode(NULL, ressize);
695 if (res==NULL)
696 return NULL;
697 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
698 p < startp+end; ++p) {
699 Py_UNICODE c = *p;
700 *outp++ = '\\';
701 if (c >= 0x00010000) {
702 *outp++ = 'U';
703 *outp++ = hexdigits[(c>>28)&0xf];
704 *outp++ = hexdigits[(c>>24)&0xf];
705 *outp++ = hexdigits[(c>>20)&0xf];
706 *outp++ = hexdigits[(c>>16)&0xf];
707 *outp++ = hexdigits[(c>>12)&0xf];
708 *outp++ = hexdigits[(c>>8)&0xf];
709 }
710 else if (c >= 0x100) {
711 *outp++ = 'u';
712 *outp++ = hexdigits[(c>>12)&0xf];
713 *outp++ = hexdigits[(c>>8)&0xf];
714 }
715 else
716 *outp++ = 'x';
717 *outp++ = hexdigits[(c>>4)&0xf];
718 *outp++ = hexdigits[c&0xf];
719 }
720
721 restuple = Py_BuildValue("(Oi)", res, end);
722 Py_DECREF(res);
723 Py_DECREF(object);
724 return restuple;
725 }
726 else {
727 wrong_exception_type(exc);
728 return NULL;
729 }
730}
731
732static PyObject *strict_errors(PyObject *self, PyObject *exc)
733{
734 return PyCodec_StrictErrors(exc);
735}
736
737
738static PyObject *ignore_errors(PyObject *self, PyObject *exc)
739{
740 return PyCodec_IgnoreErrors(exc);
741}
742
743
744static PyObject *replace_errors(PyObject *self, PyObject *exc)
745{
746 return PyCodec_ReplaceErrors(exc);
747}
748
749
750static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)
751{
752 return PyCodec_XMLCharRefReplaceErrors(exc);
753}
754
755
756static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)
757{
758 return PyCodec_BackslashReplaceErrors(exc);
759}
760
761
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000762void _PyCodecRegistry_Init(void)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000763{
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000764 static struct {
765 char *name;
766 PyMethodDef def;
767 } methods[] =
768 {
769 {
770 "strict",
771 {
772 "strict_errors",
773 strict_errors,
774 METH_O
775 }
776 },
777 {
778 "ignore",
779 {
780 "ignore_errors",
781 ignore_errors,
782 METH_O
783 }
784 },
785 {
786 "replace",
787 {
788 "replace_errors",
789 replace_errors,
790 METH_O
791 }
792 },
793 {
794 "xmlcharrefreplace",
795 {
796 "xmlcharrefreplace_errors",
797 xmlcharrefreplace_errors,
798 METH_O
799 }
800 },
801 {
802 "backslashreplace",
803 {
804 "backslashreplace_errors",
805 backslashreplace_errors,
806 METH_O
807 }
808 }
809 };
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000810 if (_PyCodec_SearchPath == NULL)
811 _PyCodec_SearchPath = PyList_New(0);
812 if (_PyCodec_SearchCache == NULL)
813 _PyCodec_SearchCache = PyDict_New();
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000814 if (_PyCodec_ErrorRegistry == NULL) {
815 int i;
816 _PyCodec_ErrorRegistry = PyDict_New();
817
818 if (_PyCodec_ErrorRegistry) {
819 for (i = 0; i < 5; ++i) {
820 PyObject *func = PyCFunction_New(&methods[i].def, NULL);
821 int res;
822 if (!func)
823 Py_FatalError("can't initialize codec error registry");
824 res = PyCodec_RegisterError(methods[i].name, func);
825 Py_DECREF(func);
826 if (res)
827 Py_FatalError("can't initialize codec error registry");
828 }
829 }
830 }
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000831 if (_PyCodec_SearchPath == NULL ||
832 _PyCodec_SearchCache == NULL)
Thomas Wouters7e474022000-07-16 12:04:32 +0000833 Py_FatalError("can't initialize codec registry");
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000834}
835
Thomas Woutersf70ef4f2000-07-22 18:47:25 +0000836void _PyCodecRegistry_Fini(void)
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000837{
838 Py_XDECREF(_PyCodec_SearchPath);
Barry Warsaw51ac5802000-03-20 16:36:48 +0000839 _PyCodec_SearchPath = NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000840 Py_XDECREF(_PyCodec_SearchCache);
Barry Warsaw51ac5802000-03-20 16:36:48 +0000841 _PyCodec_SearchCache = NULL;
Walter Dörwald3aeb6322002-09-02 13:14:32 +0000842 Py_XDECREF(_PyCodec_ErrorRegistry);
843 _PyCodec_ErrorRegistry = NULL;
Guido van Rossumfeee4b92000-03-10 22:57:27 +0000844}