Add Py_LOCAL macros
[python.git] / Python / codecs.c
blob046abe35079af3208762ad4bea0e6dcd6a315aaa
1 /* ------------------------------------------------------------------------
3 Python Codec Registry and support functions
5 Written by Marc-Andre Lemburg (mal@lemburg.com).
7 Copyright (c) Corporation for National Research Initiatives.
9 ------------------------------------------------------------------------ */
11 #include "Python.h"
12 #include <ctype.h>
14 /* --- Codec Registry ----------------------------------------------------- */
16 /* Import the standard encodings package which will register the first
17 codec search function.
19 This is done in a lazy way so that the Unicode implementation does
20 not downgrade startup time of scripts not needing it.
22 ImportErrors are silently ignored by this function. Only one try is
23 made.
27 static int _PyCodecRegistry_Init(void); /* Forward */
29 int PyCodec_Register(PyObject *search_function)
31 PyInterpreterState *interp = PyThreadState_GET()->interp;
32 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
33 goto onError;
34 if (search_function == NULL) {
35 PyErr_BadArgument();
36 goto onError;
38 if (!PyCallable_Check(search_function)) {
39 PyErr_SetString(PyExc_TypeError, "argument must be callable");
40 goto onError;
42 return PyList_Append(interp->codec_search_path, search_function);
44 onError:
45 return -1;
48 /* Convert a string to a normalized Python string: all characters are
49 converted to lower case, spaces are replaced with underscores. */
51 static
52 PyObject *normalizestring(const char *string)
54 register size_t i;
55 size_t len = strlen(string);
56 char *p;
57 PyObject *v;
59 if (len > PY_SSIZE_T_MAX) {
60 PyErr_SetString(PyExc_OverflowError, "string is too large");
61 return NULL;
64 v = PyString_FromStringAndSize(NULL, len);
65 if (v == NULL)
66 return NULL;
67 p = PyString_AS_STRING(v);
68 for (i = 0; i < len; i++) {
69 register char ch = string[i];
70 if (ch == ' ')
71 ch = '-';
72 else
73 ch = tolower(Py_CHARMASK(ch));
74 p[i] = ch;
76 return v;
79 /* Lookup the given encoding and return a tuple providing the codec
80 facilities.
82 The encoding string is looked up converted to all lower-case
83 characters. This makes encodings looked up through this mechanism
84 effectively case-insensitive.
86 If no codec is found, a LookupError is set and NULL returned.
88 As side effect, this tries to load the encodings package, if not
89 yet done. This is part of the lazy load strategy for the encodings
90 package.
94 PyObject *_PyCodec_Lookup(const char *encoding)
96 PyInterpreterState *interp;
97 PyObject *result, *args = NULL, *v;
98 Py_ssize_t i, len;
100 if (encoding == NULL) {
101 PyErr_BadArgument();
102 goto onError;
105 interp = PyThreadState_GET()->interp;
106 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
107 goto onError;
109 /* Convert the encoding to a normalized Python string: all
110 characters are converted to lower case, spaces and hyphens are
111 replaced with underscores. */
112 v = normalizestring(encoding);
113 if (v == NULL)
114 goto onError;
115 PyString_InternInPlace(&v);
117 /* First, try to lookup the name in the registry dictionary */
118 result = PyDict_GetItem(interp->codec_search_cache, v);
119 if (result != NULL) {
120 Py_INCREF(result);
121 Py_DECREF(v);
122 return result;
125 /* Next, scan the search functions in order of registration */
126 args = PyTuple_New(1);
127 if (args == NULL)
128 goto onError;
129 PyTuple_SET_ITEM(args,0,v);
131 len = PyList_Size(interp->codec_search_path);
132 if (len < 0)
133 goto onError;
134 if (len == 0) {
135 PyErr_SetString(PyExc_LookupError,
136 "no codec search functions registered: "
137 "can't find encoding");
138 goto onError;
141 for (i = 0; i < len; i++) {
142 PyObject *func;
144 func = PyList_GetItem(interp->codec_search_path, i);
145 if (func == NULL)
146 goto onError;
147 result = PyEval_CallObject(func, args);
148 if (result == NULL)
149 goto onError;
150 if (result == Py_None) {
151 Py_DECREF(result);
152 continue;
154 if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
155 PyErr_SetString(PyExc_TypeError,
156 "codec search functions must return 4-tuples");
157 Py_DECREF(result);
158 goto onError;
160 break;
162 if (i == len) {
163 /* XXX Perhaps we should cache misses too ? */
164 PyErr_Format(PyExc_LookupError,
165 "unknown encoding: %s", encoding);
166 goto onError;
169 /* Cache and return the result */
170 PyDict_SetItem(interp->codec_search_cache, v, result);
171 Py_DECREF(args);
172 return result;
174 onError:
175 Py_XDECREF(args);
176 return NULL;
179 static
180 PyObject *args_tuple(PyObject *object,
181 const char *errors)
183 PyObject *args;
185 args = PyTuple_New(1 + (errors != NULL));
186 if (args == NULL)
187 return NULL;
188 Py_INCREF(object);
189 PyTuple_SET_ITEM(args,0,object);
190 if (errors) {
191 PyObject *v;
193 v = PyString_FromString(errors);
194 if (v == NULL) {
195 Py_DECREF(args);
196 return NULL;
198 PyTuple_SET_ITEM(args, 1, v);
200 return args;
203 /* Helper function to get a codec item */
205 static
206 PyObject *codec_getitem(const char *encoding, int index)
208 PyObject *codecs;
209 PyObject *v;
211 codecs = _PyCodec_Lookup(encoding);
212 if (codecs == NULL)
213 return NULL;
214 v = PyTuple_GET_ITEM(codecs, index);
215 Py_DECREF(codecs);
216 Py_INCREF(v);
217 return v;
220 /* Helper function to create an incremental codec. */
222 static
223 PyObject *codec_getincrementalcodec(const char *encoding,
224 const char *errors,
225 const char *attrname)
227 PyObject *codecs, *ret, *inccodec;
229 codecs = _PyCodec_Lookup(encoding);
230 if (codecs == NULL)
231 return NULL;
232 inccodec = PyObject_GetAttrString(codecs, attrname);
233 Py_DECREF(codecs);
234 if (inccodec == NULL)
235 return NULL;
236 if (errors)
237 ret = PyObject_CallFunction(inccodec, "s", errors);
238 else
239 ret = PyObject_CallFunction(inccodec, NULL);
240 Py_DECREF(inccodec);
241 return ret;
244 /* Helper function to create a stream codec. */
246 static
247 PyObject *codec_getstreamcodec(const char *encoding,
248 PyObject *stream,
249 const char *errors,
250 const int index)
252 PyObject *codecs, *streamcodec;
254 codecs = _PyCodec_Lookup(encoding);
255 if (codecs == NULL)
256 return NULL;
258 streamcodec = PyEval_CallFunction(
259 PyTuple_GET_ITEM(codecs, index), "Os", stream, errors);
260 Py_DECREF(codecs);
261 return streamcodec;
264 /* Convenience APIs to query the Codec registry.
266 All APIs return a codec object with incremented refcount.
270 PyObject *PyCodec_Encoder(const char *encoding)
272 return codec_getitem(encoding, 0);
275 PyObject *PyCodec_Decoder(const char *encoding)
277 return codec_getitem(encoding, 1);
280 PyObject *PyCodec_IncrementalEncoder(const char *encoding,
281 const char *errors)
283 return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
286 PyObject *PyCodec_IncrementalDecoder(const char *encoding,
287 const char *errors)
289 return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
292 PyObject *PyCodec_StreamReader(const char *encoding,
293 PyObject *stream,
294 const char *errors)
296 return codec_getstreamcodec(encoding, stream, errors, 2);
299 PyObject *PyCodec_StreamWriter(const char *encoding,
300 PyObject *stream,
301 const char *errors)
303 return codec_getstreamcodec(encoding, stream, errors, 3);
306 /* Encode an object (e.g. an Unicode object) using the given encoding
307 and return the resulting encoded object (usually a Python string).
309 errors is passed to the encoder factory as argument if non-NULL. */
311 PyObject *PyCodec_Encode(PyObject *object,
312 const char *encoding,
313 const char *errors)
315 PyObject *encoder = NULL;
316 PyObject *args = NULL, *result = NULL;
317 PyObject *v;
319 encoder = PyCodec_Encoder(encoding);
320 if (encoder == NULL)
321 goto onError;
323 args = args_tuple(object, errors);
324 if (args == NULL)
325 goto onError;
327 result = PyEval_CallObject(encoder,args);
328 if (result == NULL)
329 goto onError;
331 if (!PyTuple_Check(result) ||
332 PyTuple_GET_SIZE(result) != 2) {
333 PyErr_SetString(PyExc_TypeError,
334 "encoder must return a tuple (object,integer)");
335 goto onError;
337 v = PyTuple_GET_ITEM(result,0);
338 Py_INCREF(v);
339 /* We don't check or use the second (integer) entry. */
341 Py_DECREF(args);
342 Py_DECREF(encoder);
343 Py_DECREF(result);
344 return v;
346 onError:
347 Py_XDECREF(result);
348 Py_XDECREF(args);
349 Py_XDECREF(encoder);
350 return NULL;
353 /* Decode an object (usually a Python string) using the given encoding
354 and return an equivalent object (e.g. an Unicode object).
356 errors is passed to the decoder factory as argument if non-NULL. */
358 PyObject *PyCodec_Decode(PyObject *object,
359 const char *encoding,
360 const char *errors)
362 PyObject *decoder = NULL;
363 PyObject *args = NULL, *result = NULL;
364 PyObject *v;
366 decoder = PyCodec_Decoder(encoding);
367 if (decoder == NULL)
368 goto onError;
370 args = args_tuple(object, errors);
371 if (args == NULL)
372 goto onError;
374 result = PyEval_CallObject(decoder,args);
375 if (result == NULL)
376 goto onError;
377 if (!PyTuple_Check(result) ||
378 PyTuple_GET_SIZE(result) != 2) {
379 PyErr_SetString(PyExc_TypeError,
380 "decoder must return a tuple (object,integer)");
381 goto onError;
383 v = PyTuple_GET_ITEM(result,0);
384 Py_INCREF(v);
385 /* We don't check or use the second (integer) entry. */
387 Py_DECREF(args);
388 Py_DECREF(decoder);
389 Py_DECREF(result);
390 return v;
392 onError:
393 Py_XDECREF(args);
394 Py_XDECREF(decoder);
395 Py_XDECREF(result);
396 return NULL;
399 /* Register the error handling callback function error under the name
400 name. This function will be called by the codec when it encounters
401 an unencodable characters/undecodable bytes and doesn't know the
402 callback name, when name is specified as the error parameter
403 in the call to the encode/decode function.
404 Return 0 on success, -1 on error */
405 int PyCodec_RegisterError(const char *name, PyObject *error)
407 PyInterpreterState *interp = PyThreadState_GET()->interp;
408 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
409 return -1;
410 if (!PyCallable_Check(error)) {
411 PyErr_SetString(PyExc_TypeError, "handler must be callable");
412 return -1;
414 return PyDict_SetItemString(interp->codec_error_registry,
415 (char *)name, error);
418 /* Lookup the error handling callback function registered under the
419 name error. As a special case NULL can be passed, in which case
420 the error handling callback for strict encoding will be returned. */
421 PyObject *PyCodec_LookupError(const char *name)
423 PyObject *handler = NULL;
425 PyInterpreterState *interp = PyThreadState_GET()->interp;
426 if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
427 return NULL;
429 if (name==NULL)
430 name = "strict";
431 handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
432 if (!handler)
433 PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
434 else
435 Py_INCREF(handler);
436 return handler;
439 static void wrong_exception_type(PyObject *exc)
441 PyObject *type = PyObject_GetAttrString(exc, "__class__");
442 if (type != NULL) {
443 PyObject *name = PyObject_GetAttrString(type, "__name__");
444 Py_DECREF(type);
445 if (name != NULL) {
446 PyObject *string = PyObject_Str(name);
447 Py_DECREF(name);
448 if (string != NULL) {
449 PyErr_Format(PyExc_TypeError,
450 "don't know how to handle %.400s in error callback",
451 PyString_AS_STRING(string));
452 Py_DECREF(string);
458 PyObject *PyCodec_StrictErrors(PyObject *exc)
460 if (PyExceptionInstance_Check(exc))
461 PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
462 else
463 PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
464 return NULL;
468 #ifdef Py_USING_UNICODE
469 PyObject *PyCodec_IgnoreErrors(PyObject *exc)
471 Py_ssize_t end;
472 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
473 if (PyUnicodeEncodeError_GetEnd(exc, &end))
474 return NULL;
476 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
477 if (PyUnicodeDecodeError_GetEnd(exc, &end))
478 return NULL;
480 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
481 if (PyUnicodeTranslateError_GetEnd(exc, &end))
482 return NULL;
484 else {
485 wrong_exception_type(exc);
486 return NULL;
488 /* ouch: passing NULL, 0, pos gives None instead of u'' */
489 return Py_BuildValue("(u#n)", &end, 0, end);
493 PyObject *PyCodec_ReplaceErrors(PyObject *exc)
495 PyObject *restuple;
496 Py_ssize_t start;
497 Py_ssize_t end;
498 Py_ssize_t i;
500 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
501 PyObject *res;
502 Py_UNICODE *p;
503 if (PyUnicodeEncodeError_GetStart(exc, &start))
504 return NULL;
505 if (PyUnicodeEncodeError_GetEnd(exc, &end))
506 return NULL;
507 res = PyUnicode_FromUnicode(NULL, end-start);
508 if (res == NULL)
509 return NULL;
510 for (p = PyUnicode_AS_UNICODE(res), i = start;
511 i<end; ++p, ++i)
512 *p = '?';
513 restuple = Py_BuildValue("(On)", res, end);
514 Py_DECREF(res);
515 return restuple;
517 else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {
518 Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
519 if (PyUnicodeDecodeError_GetEnd(exc, &end))
520 return NULL;
521 return Py_BuildValue("(u#n)", &res, 1, end);
523 else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {
524 PyObject *res;
525 Py_UNICODE *p;
526 if (PyUnicodeTranslateError_GetStart(exc, &start))
527 return NULL;
528 if (PyUnicodeTranslateError_GetEnd(exc, &end))
529 return NULL;
530 res = PyUnicode_FromUnicode(NULL, end-start);
531 if (res == NULL)
532 return NULL;
533 for (p = PyUnicode_AS_UNICODE(res), i = start;
534 i<end; ++p, ++i)
535 *p = Py_UNICODE_REPLACEMENT_CHARACTER;
536 restuple = Py_BuildValue("(On)", res, end);
537 Py_DECREF(res);
538 return restuple;
540 else {
541 wrong_exception_type(exc);
542 return NULL;
546 PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
548 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
549 PyObject *restuple;
550 PyObject *object;
551 Py_ssize_t start;
552 Py_ssize_t end;
553 PyObject *res;
554 Py_UNICODE *p;
555 Py_UNICODE *startp;
556 Py_UNICODE *outp;
557 int ressize;
558 if (PyUnicodeEncodeError_GetStart(exc, &start))
559 return NULL;
560 if (PyUnicodeEncodeError_GetEnd(exc, &end))
561 return NULL;
562 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
563 return NULL;
564 startp = PyUnicode_AS_UNICODE(object);
565 for (p = startp+start, ressize = 0; p < startp+end; ++p) {
566 if (*p<10)
567 ressize += 2+1+1;
568 else if (*p<100)
569 ressize += 2+2+1;
570 else if (*p<1000)
571 ressize += 2+3+1;
572 else if (*p<10000)
573 ressize += 2+4+1;
574 #ifndef Py_UNICODE_WIDE
575 else
576 ressize += 2+5+1;
577 #else
578 else if (*p<100000)
579 ressize += 2+5+1;
580 else if (*p<1000000)
581 ressize += 2+6+1;
582 else
583 ressize += 2+7+1;
584 #endif
586 /* allocate replacement */
587 res = PyUnicode_FromUnicode(NULL, ressize);
588 if (res == NULL) {
589 Py_DECREF(object);
590 return NULL;
592 /* generate replacement */
593 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
594 p < startp+end; ++p) {
595 Py_UNICODE c = *p;
596 int digits;
597 int base;
598 *outp++ = '&';
599 *outp++ = '#';
600 if (*p<10) {
601 digits = 1;
602 base = 1;
604 else if (*p<100) {
605 digits = 2;
606 base = 10;
608 else if (*p<1000) {
609 digits = 3;
610 base = 100;
612 else if (*p<10000) {
613 digits = 4;
614 base = 1000;
616 #ifndef Py_UNICODE_WIDE
617 else {
618 digits = 5;
619 base = 10000;
621 #else
622 else if (*p<100000) {
623 digits = 5;
624 base = 10000;
626 else if (*p<1000000) {
627 digits = 6;
628 base = 100000;
630 else {
631 digits = 7;
632 base = 1000000;
634 #endif
635 while (digits-->0) {
636 *outp++ = '0' + c/base;
637 c %= base;
638 base /= 10;
640 *outp++ = ';';
642 restuple = Py_BuildValue("(On)", res, end);
643 Py_DECREF(res);
644 Py_DECREF(object);
645 return restuple;
647 else {
648 wrong_exception_type(exc);
649 return NULL;
653 static Py_UNICODE hexdigits[] = {
654 '0', '1', '2', '3', '4', '5', '6', '7',
655 '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
658 PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
660 if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {
661 PyObject *restuple;
662 PyObject *object;
663 Py_ssize_t start;
664 Py_ssize_t end;
665 PyObject *res;
666 Py_UNICODE *p;
667 Py_UNICODE *startp;
668 Py_UNICODE *outp;
669 int ressize;
670 if (PyUnicodeEncodeError_GetStart(exc, &start))
671 return NULL;
672 if (PyUnicodeEncodeError_GetEnd(exc, &end))
673 return NULL;
674 if (!(object = PyUnicodeEncodeError_GetObject(exc)))
675 return NULL;
676 startp = PyUnicode_AS_UNICODE(object);
677 for (p = startp+start, ressize = 0; p < startp+end; ++p) {
678 #ifdef Py_UNICODE_WIDE
679 if (*p >= 0x00010000)
680 ressize += 1+1+8;
681 else
682 #endif
683 if (*p >= 0x100) {
684 ressize += 1+1+4;
686 else
687 ressize += 1+1+2;
689 res = PyUnicode_FromUnicode(NULL, ressize);
690 if (res==NULL)
691 return NULL;
692 for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
693 p < startp+end; ++p) {
694 Py_UNICODE c = *p;
695 *outp++ = '\\';
696 #ifdef Py_UNICODE_WIDE
697 if (c >= 0x00010000) {
698 *outp++ = 'U';
699 *outp++ = hexdigits[(c>>28)&0xf];
700 *outp++ = hexdigits[(c>>24)&0xf];
701 *outp++ = hexdigits[(c>>20)&0xf];
702 *outp++ = hexdigits[(c>>16)&0xf];
703 *outp++ = hexdigits[(c>>12)&0xf];
704 *outp++ = hexdigits[(c>>8)&0xf];
706 else
707 #endif
708 if (c >= 0x100) {
709 *outp++ = 'u';
710 *outp++ = hexdigits[(c>>12)&0xf];
711 *outp++ = hexdigits[(c>>8)&0xf];
713 else
714 *outp++ = 'x';
715 *outp++ = hexdigits[(c>>4)&0xf];
716 *outp++ = hexdigits[c&0xf];
719 restuple = Py_BuildValue("(On)", res, end);
720 Py_DECREF(res);
721 Py_DECREF(object);
722 return restuple;
724 else {
725 wrong_exception_type(exc);
726 return NULL;
729 #endif
731 static PyObject *strict_errors(PyObject *self, PyObject *exc)
733 return PyCodec_StrictErrors(exc);
737 #ifdef Py_USING_UNICODE
738 static PyObject *ignore_errors(PyObject *self, PyObject *exc)
740 return PyCodec_IgnoreErrors(exc);
744 static PyObject *replace_errors(PyObject *self, PyObject *exc)
746 return PyCodec_ReplaceErrors(exc);
750 static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)
752 return PyCodec_XMLCharRefReplaceErrors(exc);
756 static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)
758 return PyCodec_BackslashReplaceErrors(exc);
760 #endif
762 static int _PyCodecRegistry_Init(void)
764 static struct {
765 char *name;
766 PyMethodDef def;
767 } methods[] =
770 "strict",
772 "strict_errors",
773 strict_errors,
774 METH_O
777 #ifdef Py_USING_UNICODE
779 "ignore",
781 "ignore_errors",
782 ignore_errors,
783 METH_O
787 "replace",
789 "replace_errors",
790 replace_errors,
791 METH_O
795 "xmlcharrefreplace",
797 "xmlcharrefreplace_errors",
798 xmlcharrefreplace_errors,
799 METH_O
803 "backslashreplace",
805 "backslashreplace_errors",
806 backslashreplace_errors,
807 METH_O
810 #endif
813 PyInterpreterState *interp = PyThreadState_GET()->interp;
814 PyObject *mod;
815 unsigned i;
817 if (interp->codec_search_path != NULL)
818 return 0;
820 interp->codec_search_path = PyList_New(0);
821 interp->codec_search_cache = PyDict_New();
822 interp->codec_error_registry = PyDict_New();
824 if (interp->codec_error_registry) {
825 for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {
826 PyObject *func = PyCFunction_New(&methods[i].def, NULL);
827 int res;
828 if (!func)
829 Py_FatalError("can't initialize codec error registry");
830 res = PyCodec_RegisterError(methods[i].name, func);
831 Py_DECREF(func);
832 if (res)
833 Py_FatalError("can't initialize codec error registry");
837 if (interp->codec_search_path == NULL ||
838 interp->codec_search_cache == NULL ||
839 interp->codec_error_registry == NULL)
840 Py_FatalError("can't initialize codec registry");
842 mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0);
843 if (mod == NULL) {
844 if (PyErr_ExceptionMatches(PyExc_ImportError)) {
845 /* Ignore ImportErrors... this is done so that
846 distributions can disable the encodings package. Note
847 that other errors are not masked, e.g. SystemErrors
848 raised to inform the user of an error in the Python
849 configuration are still reported back to the user. */
850 PyErr_Clear();
851 return 0;
853 return -1;
855 Py_DECREF(mod);
856 return 0;