2000-03-10 18:57:27 -04:00
|
|
|
/* ------------------------------------------------------------------------
|
|
|
|
|
|
|
|
Python Codec Registry and support functions
|
|
|
|
|
|
|
|
Written by Marc-Andre Lemburg (mal@lemburg.com).
|
|
|
|
|
2000-08-03 13:24:25 -03:00
|
|
|
Copyright (c) Corporation for National Research Initiatives.
|
2000-03-10 18:57:27 -04:00
|
|
|
|
|
|
|
------------------------------------------------------------------------ */
|
|
|
|
|
|
|
|
#include "Python.h"
|
|
|
|
#include <ctype.h>
|
|
|
|
|
|
|
|
/* --- Codec Registry ----------------------------------------------------- */
|
|
|
|
|
|
|
|
/* Import the standard encodings package which will register the first
|
2010-05-09 11:46:46 -03:00
|
|
|
codec search function.
|
2000-03-10 18:57:27 -04:00
|
|
|
|
|
|
|
This is done in a lazy way so that the Unicode implementation does
|
|
|
|
not downgrade startup time of scripts not needing it.
|
|
|
|
|
2000-03-31 13:25:23 -04:00
|
|
|
ImportErrors are silently ignored by this function. Only one try is
|
|
|
|
made.
|
2000-03-10 18:57:27 -04:00
|
|
|
|
|
|
|
*/
|
|
|
|
|
2003-03-18 20:35:36 -04:00
|
|
|
static int _PyCodecRegistry_Init(void); /* Forward */
|
2000-03-10 18:57:27 -04:00
|
|
|
|
|
|
|
int PyCodec_Register(PyObject *search_function)
|
|
|
|
{
|
2004-03-24 18:22:12 -04:00
|
|
|
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
2003-03-18 20:35:36 -04:00
|
|
|
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
if (search_function == NULL) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_BadArgument();
|
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
if (!PyCallable_Check(search_function)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "argument must be callable");
|
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
2003-03-18 20:35:36 -04:00
|
|
|
return PyList_Append(interp->codec_search_path, search_function);
|
2000-03-31 13:25:23 -04:00
|
|
|
|
|
|
|
onError:
|
|
|
|
return -1;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
|
2000-04-05 17:11:21 -03:00
|
|
|
/* Convert a string to a normalized Python string: all characters are
|
|
|
|
converted to lower case, spaces are replaced with underscores. */
|
|
|
|
|
2000-03-10 18:57:27 -04:00
|
|
|
static
|
2000-04-05 17:11:21 -03:00
|
|
|
PyObject *normalizestring(const char *string)
|
2000-03-10 18:57:27 -04:00
|
|
|
{
|
2000-06-29 11:50:15 -03:00
|
|
|
register size_t i;
|
2000-06-28 19:07:35 -03:00
|
|
|
size_t len = strlen(string);
|
2000-03-10 18:57:27 -04:00
|
|
|
char *p;
|
|
|
|
PyObject *v;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2006-04-13 04:52:27 -03:00
|
|
|
if (len > PY_SSIZE_T_MAX) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_OverflowError, "string is too large");
|
|
|
|
return NULL;
|
2006-04-13 04:52:27 -03:00
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2008-06-09 01:58:54 -03:00
|
|
|
v = PyString_FromStringAndSize(NULL, len);
|
2000-03-10 18:57:27 -04:00
|
|
|
if (v == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
2008-06-09 01:58:54 -03:00
|
|
|
p = PyString_AS_STRING(v);
|
2000-04-05 17:11:21 -03:00
|
|
|
for (i = 0; i < len; i++) {
|
|
|
|
register char ch = string[i];
|
|
|
|
if (ch == ' ')
|
|
|
|
ch = '-';
|
|
|
|
else
|
2011-07-23 21:51:01 -03:00
|
|
|
ch = Py_TOLOWER(Py_CHARMASK(ch));
|
2010-05-09 11:46:46 -03:00
|
|
|
p[i] = ch;
|
2000-04-05 17:11:21 -03:00
|
|
|
}
|
2000-03-10 18:57:27 -04:00
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup the given encoding and return a tuple providing the codec
|
|
|
|
facilities.
|
|
|
|
|
|
|
|
The encoding string is looked up converted to all lower-case
|
|
|
|
characters. This makes encodings looked up through this mechanism
|
|
|
|
effectively case-insensitive.
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
If no codec is found, a LookupError is set and NULL returned.
|
2000-03-31 13:25:23 -04:00
|
|
|
|
|
|
|
As side effect, this tries to load the encodings package, if not
|
|
|
|
yet done. This is part of the lazy load strategy for the encodings
|
|
|
|
package.
|
|
|
|
|
|
|
|
*/
|
2000-03-10 18:57:27 -04:00
|
|
|
|
|
|
|
PyObject *_PyCodec_Lookup(const char *encoding)
|
|
|
|
{
|
2003-03-18 20:35:36 -04:00
|
|
|
PyInterpreterState *interp;
|
2000-03-24 16:52:23 -04:00
|
|
|
PyObject *result, *args = NULL, *v;
|
2006-04-22 08:40:03 -03:00
|
|
|
Py_ssize_t i, len;
|
2000-03-10 18:57:27 -04:00
|
|
|
|
2000-05-09 16:55:59 -03:00
|
|
|
if (encoding == NULL) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_BadArgument();
|
|
|
|
goto onError;
|
2000-05-09 16:55:59 -03:00
|
|
|
}
|
2003-03-18 20:35:36 -04:00
|
|
|
|
2004-03-24 18:22:12 -04:00
|
|
|
interp = PyThreadState_GET()->interp;
|
2003-03-18 20:35:36 -04:00
|
|
|
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
|
2000-04-05 17:11:21 -03:00
|
|
|
/* Convert the encoding to a normalized Python string: all
|
2000-07-16 09:04:32 -03:00
|
|
|
characters are converted to lower case, spaces and hyphens are
|
2000-04-05 17:11:21 -03:00
|
|
|
replaced with underscores. */
|
|
|
|
v = normalizestring(encoding);
|
2000-03-10 18:57:27 -04:00
|
|
|
if (v == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
2008-06-09 01:58:54 -03:00
|
|
|
PyString_InternInPlace(&v);
|
2000-03-10 18:57:27 -04:00
|
|
|
|
|
|
|
/* First, try to lookup the name in the registry dictionary */
|
2003-03-18 20:35:36 -04:00
|
|
|
result = PyDict_GetItem(interp->codec_search_cache, v);
|
2000-03-10 18:57:27 -04:00
|
|
|
if (result != NULL) {
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(result);
|
|
|
|
Py_DECREF(v);
|
|
|
|
return result;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2000-03-10 18:57:27 -04:00
|
|
|
/* Next, scan the search functions in order of registration */
|
|
|
|
args = PyTuple_New(1);
|
|
|
|
if (args == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
PyTuple_SET_ITEM(args,0,v);
|
2000-03-24 16:52:23 -04:00
|
|
|
|
2003-03-18 20:35:36 -04:00
|
|
|
len = PyList_Size(interp->codec_search_path);
|
2000-03-24 16:52:23 -04:00
|
|
|
if (len < 0)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
2000-03-31 13:25:23 -04:00
|
|
|
if (len == 0) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_LookupError,
|
|
|
|
"no codec search functions registered: "
|
|
|
|
"can't find encoding");
|
|
|
|
goto onError;
|
2000-03-31 13:25:23 -04:00
|
|
|
}
|
2000-03-10 18:57:27 -04:00
|
|
|
|
|
|
|
for (i = 0; i < len; i++) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *func;
|
|
|
|
|
|
|
|
func = PyList_GetItem(interp->codec_search_path, i);
|
|
|
|
if (func == NULL)
|
|
|
|
goto onError;
|
|
|
|
result = PyEval_CallObject(func, args);
|
|
|
|
if (result == NULL)
|
|
|
|
goto onError;
|
|
|
|
if (result == Py_None) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if (!PyTuple_Check(result) || PyTuple_GET_SIZE(result) != 4) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"codec search functions must return 4-tuples");
|
|
|
|
Py_DECREF(result);
|
|
|
|
goto onError;
|
|
|
|
}
|
|
|
|
break;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
if (i == len) {
|
2010-05-09 11:46:46 -03:00
|
|
|
/* XXX Perhaps we should cache misses too ? */
|
|
|
|
PyErr_Format(PyExc_LookupError,
|
2002-09-26 13:01:24 -03:00
|
|
|
"unknown encoding: %s", encoding);
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Cache and return the result */
|
2003-03-18 20:35:36 -04:00
|
|
|
PyDict_SetItem(interp->codec_search_cache, v, result);
|
2000-03-10 18:57:27 -04:00
|
|
|
Py_DECREF(args);
|
|
|
|
return result;
|
|
|
|
|
|
|
|
onError:
|
|
|
|
Py_XDECREF(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
PyObject *args_tuple(PyObject *object,
|
2010-05-09 11:46:46 -03:00
|
|
|
const char *errors)
|
2000-03-10 18:57:27 -04:00
|
|
|
{
|
|
|
|
PyObject *args;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2000-03-10 18:57:27 -04:00
|
|
|
args = PyTuple_New(1 + (errors != NULL));
|
|
|
|
if (args == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
2000-03-10 18:57:27 -04:00
|
|
|
Py_INCREF(object);
|
|
|
|
PyTuple_SET_ITEM(args,0,object);
|
|
|
|
if (errors) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
v = PyString_FromString(errors);
|
|
|
|
if (v == NULL) {
|
|
|
|
Py_DECREF(args);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyTuple_SET_ITEM(args, 1, v);
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
return args;
|
|
|
|
}
|
|
|
|
|
2006-03-16 17:46:40 -04:00
|
|
|
/* Helper function to get a codec item */
|
2000-03-10 18:57:27 -04:00
|
|
|
|
|
|
|
static
|
2006-03-16 17:46:40 -04:00
|
|
|
PyObject *codec_getitem(const char *encoding, int index)
|
2000-03-10 18:57:27 -04:00
|
|
|
{
|
|
|
|
PyObject *codecs;
|
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
codecs = _PyCodec_Lookup(encoding);
|
|
|
|
if (codecs == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
2006-03-16 17:46:40 -04:00
|
|
|
v = PyTuple_GET_ITEM(codecs, index);
|
2002-07-18 20:06:17 -03:00
|
|
|
Py_DECREF(codecs);
|
2000-03-10 18:57:27 -04:00
|
|
|
Py_INCREF(v);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:21:00 -03:00
|
|
|
/* Helper functions to create an incremental codec. */
|
2006-03-16 17:46:40 -04:00
|
|
|
static
|
2015-05-31 14:21:00 -03:00
|
|
|
PyObject *codec_makeincrementalcodec(PyObject *codec_info,
|
|
|
|
const char *errors,
|
|
|
|
const char *attrname)
|
2006-03-15 07:35:15 -04:00
|
|
|
{
|
2015-05-31 14:21:00 -03:00
|
|
|
PyObject *ret, *inccodec;
|
2006-03-15 07:35:15 -04:00
|
|
|
|
2015-05-31 14:21:00 -03:00
|
|
|
inccodec = PyObject_GetAttrString(codec_info, attrname);
|
2006-03-18 10:05:43 -04:00
|
|
|
if (inccodec == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
2006-03-15 07:35:15 -04:00
|
|
|
if (errors)
|
2010-05-09 11:46:46 -03:00
|
|
|
ret = PyObject_CallFunction(inccodec, "s", errors);
|
2006-03-15 07:35:15 -04:00
|
|
|
else
|
2010-05-09 11:46:46 -03:00
|
|
|
ret = PyObject_CallFunction(inccodec, NULL);
|
2006-03-16 17:46:40 -04:00
|
|
|
Py_DECREF(inccodec);
|
2006-03-15 07:35:15 -04:00
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:21:00 -03:00
|
|
|
static
|
|
|
|
PyObject *codec_getincrementalcodec(const char *encoding,
|
|
|
|
const char *errors,
|
|
|
|
const char *attrname)
|
|
|
|
{
|
|
|
|
PyObject *codec_info, *ret;
|
|
|
|
|
|
|
|
codec_info = _PyCodec_Lookup(encoding);
|
|
|
|
if (codec_info == NULL)
|
|
|
|
return NULL;
|
|
|
|
ret = codec_makeincrementalcodec(codec_info, errors, attrname);
|
|
|
|
Py_DECREF(codec_info);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2006-03-16 17:46:40 -04:00
|
|
|
/* Helper function to create a stream codec. */
|
|
|
|
|
|
|
|
static
|
|
|
|
PyObject *codec_getstreamcodec(const char *encoding,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *stream,
|
|
|
|
const char *errors,
|
|
|
|
const int index)
|
2006-03-15 07:35:15 -04:00
|
|
|
{
|
2006-06-23 18:16:18 -03:00
|
|
|
PyObject *codecs, *streamcodec, *codeccls;
|
2006-03-15 07:35:15 -04:00
|
|
|
|
|
|
|
codecs = _PyCodec_Lookup(encoding);
|
|
|
|
if (codecs == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
2006-03-16 17:46:40 -04:00
|
|
|
|
2006-06-23 18:16:18 -03:00
|
|
|
codeccls = PyTuple_GET_ITEM(codecs, index);
|
|
|
|
if (errors != NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
streamcodec = PyObject_CallFunction(codeccls, "Os", stream, errors);
|
2006-06-23 18:16:18 -03:00
|
|
|
else
|
2010-05-09 11:46:46 -03:00
|
|
|
streamcodec = PyObject_CallFunction(codeccls, "O", stream);
|
2006-03-15 07:35:15 -04:00
|
|
|
Py_DECREF(codecs);
|
2006-03-16 17:46:40 -04:00
|
|
|
return streamcodec;
|
|
|
|
}
|
2006-03-15 07:35:15 -04:00
|
|
|
|
2015-05-31 14:21:00 -03:00
|
|
|
/* Helpers to work with the result of _PyCodec_Lookup
|
|
|
|
|
|
|
|
*/
|
|
|
|
PyObject *_PyCodecInfo_GetIncrementalDecoder(PyObject *codec_info,
|
|
|
|
const char *errors)
|
|
|
|
{
|
|
|
|
return codec_makeincrementalcodec(codec_info, errors,
|
|
|
|
"incrementaldecoder");
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *_PyCodecInfo_GetIncrementalEncoder(PyObject *codec_info,
|
|
|
|
const char *errors)
|
|
|
|
{
|
|
|
|
return codec_makeincrementalcodec(codec_info, errors,
|
|
|
|
"incrementalencoder");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
/* Convenience APIs to query the Codec registry.
|
|
|
|
|
2006-03-16 17:46:40 -04:00
|
|
|
All APIs return a codec object with incremented refcount.
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2006-03-16 17:46:40 -04:00
|
|
|
*/
|
|
|
|
|
|
|
|
PyObject *PyCodec_Encoder(const char *encoding)
|
|
|
|
{
|
|
|
|
return codec_getitem(encoding, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *PyCodec_Decoder(const char *encoding)
|
|
|
|
{
|
|
|
|
return codec_getitem(encoding, 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *PyCodec_IncrementalEncoder(const char *encoding,
|
2010-05-09 11:46:46 -03:00
|
|
|
const char *errors)
|
2006-03-16 17:46:40 -04:00
|
|
|
{
|
|
|
|
return codec_getincrementalcodec(encoding, errors, "incrementalencoder");
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *PyCodec_IncrementalDecoder(const char *encoding,
|
2010-05-09 11:46:46 -03:00
|
|
|
const char *errors)
|
2006-03-16 17:46:40 -04:00
|
|
|
{
|
|
|
|
return codec_getincrementalcodec(encoding, errors, "incrementaldecoder");
|
2006-03-15 07:35:15 -04:00
|
|
|
}
|
|
|
|
|
2000-03-10 18:57:27 -04:00
|
|
|
PyObject *PyCodec_StreamReader(const char *encoding,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *stream,
|
|
|
|
const char *errors)
|
2000-03-10 18:57:27 -04:00
|
|
|
{
|
2006-03-16 17:46:40 -04:00
|
|
|
return codec_getstreamcodec(encoding, stream, errors, 2);
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *PyCodec_StreamWriter(const char *encoding,
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *stream,
|
|
|
|
const char *errors)
|
2000-03-10 18:57:27 -04:00
|
|
|
{
|
2006-03-16 17:46:40 -04:00
|
|
|
return codec_getstreamcodec(encoding, stream, errors, 3);
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Encode an object (e.g. an Unicode object) using the given encoding
|
|
|
|
and return the resulting encoded object (usually a Python string).
|
|
|
|
|
|
|
|
errors is passed to the encoder factory as argument if non-NULL. */
|
|
|
|
|
2015-05-31 14:21:00 -03:00
|
|
|
static PyObject *
|
|
|
|
_PyCodec_EncodeInternal(PyObject *object,
|
|
|
|
PyObject *encoder,
|
|
|
|
const char *encoding,
|
|
|
|
const char *errors)
|
2000-03-10 18:57:27 -04:00
|
|
|
{
|
2005-11-24 18:09:18 -04:00
|
|
|
PyObject *args = NULL, *result = NULL;
|
2000-03-10 18:57:27 -04:00
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
args = args_tuple(object, errors);
|
|
|
|
if (args == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
|
|
|
|
2000-03-10 18:57:27 -04:00
|
|
|
result = PyEval_CallObject(encoder,args);
|
|
|
|
if (result == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
|
2010-05-09 11:46:46 -03:00
|
|
|
if (!PyTuple_Check(result) ||
|
|
|
|
PyTuple_GET_SIZE(result) != 2) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"encoder must return a tuple (object,integer)");
|
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
v = PyTuple_GET_ITEM(result,0);
|
|
|
|
Py_INCREF(v);
|
|
|
|
/* We don't check or use the second (integer) entry. */
|
|
|
|
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(encoder);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return v;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2000-03-10 18:57:27 -04:00
|
|
|
onError:
|
2005-11-24 18:09:18 -04:00
|
|
|
Py_XDECREF(result);
|
2000-03-10 18:57:27 -04:00
|
|
|
Py_XDECREF(args);
|
|
|
|
Py_XDECREF(encoder);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Decode an object (usually a Python string) using the given encoding
|
|
|
|
and return an equivalent object (e.g. an Unicode object).
|
|
|
|
|
|
|
|
errors is passed to the decoder factory as argument if non-NULL. */
|
|
|
|
|
2015-05-31 14:21:00 -03:00
|
|
|
static PyObject *
|
|
|
|
_PyCodec_DecodeInternal(PyObject *object,
|
|
|
|
PyObject *decoder,
|
|
|
|
const char *encoding,
|
|
|
|
const char *errors)
|
2000-03-10 18:57:27 -04:00
|
|
|
{
|
|
|
|
PyObject *args = NULL, *result = NULL;
|
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
args = args_tuple(object, errors);
|
|
|
|
if (args == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
|
|
|
|
2000-03-10 18:57:27 -04:00
|
|
|
result = PyEval_CallObject(decoder,args);
|
|
|
|
if (result == NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
goto onError;
|
|
|
|
if (!PyTuple_Check(result) ||
|
|
|
|
PyTuple_GET_SIZE(result) != 2) {
|
|
|
|
PyErr_SetString(PyExc_TypeError,
|
|
|
|
"decoder must return a tuple (object,integer)");
|
|
|
|
goto onError;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|
|
|
|
v = PyTuple_GET_ITEM(result,0);
|
|
|
|
Py_INCREF(v);
|
|
|
|
/* We don't check or use the second (integer) entry. */
|
|
|
|
|
|
|
|
Py_DECREF(args);
|
|
|
|
Py_DECREF(decoder);
|
|
|
|
Py_DECREF(result);
|
|
|
|
return v;
|
2010-05-09 11:46:46 -03:00
|
|
|
|
2000-03-10 18:57:27 -04:00
|
|
|
onError:
|
|
|
|
Py_XDECREF(args);
|
|
|
|
Py_XDECREF(decoder);
|
|
|
|
Py_XDECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-05-31 14:21:00 -03:00
|
|
|
/* Generic encoding/decoding API */
|
|
|
|
PyObject *PyCodec_Encode(PyObject *object,
|
|
|
|
const char *encoding,
|
|
|
|
const char *errors)
|
|
|
|
{
|
|
|
|
PyObject *encoder;
|
|
|
|
|
|
|
|
encoder = PyCodec_Encoder(encoding);
|
|
|
|
if (encoder == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return _PyCodec_EncodeInternal(object, encoder, encoding, errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *PyCodec_Decode(PyObject *object,
|
|
|
|
const char *encoding,
|
|
|
|
const char *errors)
|
|
|
|
{
|
|
|
|
PyObject *decoder;
|
|
|
|
|
|
|
|
decoder = PyCodec_Decoder(encoding);
|
|
|
|
if (decoder == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return _PyCodec_DecodeInternal(object, decoder, encoding, errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Text encoding/decoding API */
|
|
|
|
PyObject * _PyCodec_LookupTextEncoding(const char *encoding,
|
|
|
|
const char *alternate_command)
|
|
|
|
{
|
|
|
|
PyObject *codec;
|
|
|
|
PyObject *attr;
|
|
|
|
int is_text_codec;
|
|
|
|
|
|
|
|
codec = _PyCodec_Lookup(encoding);
|
|
|
|
if (codec == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
/* Backwards compatibility: assume any raw tuple describes a text
|
|
|
|
* encoding, and the same for anything lacking the private
|
|
|
|
* attribute.
|
|
|
|
*/
|
|
|
|
if (Py_Py3kWarningFlag && !PyTuple_CheckExact(codec)) {
|
|
|
|
attr = PyObject_GetAttrString(codec, "_is_text_encoding");
|
|
|
|
if (attr == NULL) {
|
|
|
|
if (!PyErr_ExceptionMatches(PyExc_AttributeError))
|
|
|
|
goto onError;
|
|
|
|
PyErr_Clear();
|
|
|
|
} else {
|
|
|
|
is_text_codec = PyObject_IsTrue(attr);
|
|
|
|
Py_DECREF(attr);
|
|
|
|
if (is_text_codec < 0)
|
|
|
|
goto onError;
|
|
|
|
if (!is_text_codec) {
|
|
|
|
PyObject *msg = PyString_FromFormat(
|
|
|
|
"'%.400s' is not a text encoding; "
|
|
|
|
"use %s to handle arbitrary codecs",
|
|
|
|
encoding, alternate_command);
|
|
|
|
if (msg == NULL)
|
|
|
|
goto onError;
|
|
|
|
if (PyErr_WarnPy3k(PyString_AS_STRING(msg), 1) < 0) {
|
|
|
|
Py_DECREF(msg);
|
|
|
|
goto onError;
|
|
|
|
}
|
|
|
|
Py_DECREF(msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This appears to be a valid text encoding */
|
|
|
|
return codec;
|
|
|
|
|
|
|
|
onError:
|
|
|
|
Py_DECREF(codec);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static
|
|
|
|
PyObject *codec_getitem_checked(const char *encoding,
|
|
|
|
const char *alternate_command,
|
|
|
|
int index)
|
|
|
|
{
|
|
|
|
PyObject *codec;
|
|
|
|
PyObject *v;
|
|
|
|
|
|
|
|
codec = _PyCodec_LookupTextEncoding(encoding, alternate_command);
|
|
|
|
if (codec == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
v = PyTuple_GET_ITEM(codec, index);
|
|
|
|
Py_INCREF(v);
|
|
|
|
Py_DECREF(codec);
|
|
|
|
return v;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject * _PyCodec_TextEncoder(const char *encoding)
|
|
|
|
{
|
|
|
|
return codec_getitem_checked(encoding, "codecs.encode()", 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyObject * _PyCodec_TextDecoder(const char *encoding)
|
|
|
|
{
|
|
|
|
return codec_getitem_checked(encoding, "codecs.decode()", 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *_PyCodec_EncodeText(PyObject *object,
|
|
|
|
const char *encoding,
|
|
|
|
const char *errors)
|
|
|
|
{
|
|
|
|
PyObject *encoder;
|
|
|
|
|
|
|
|
encoder = _PyCodec_TextEncoder(encoding);
|
|
|
|
if (encoder == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return _PyCodec_EncodeInternal(object, encoder, encoding, errors);
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *_PyCodec_DecodeText(PyObject *object,
|
|
|
|
const char *encoding,
|
|
|
|
const char *errors)
|
|
|
|
{
|
|
|
|
PyObject *decoder;
|
|
|
|
|
|
|
|
decoder = _PyCodec_TextDecoder(encoding);
|
|
|
|
if (decoder == NULL)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return _PyCodec_DecodeInternal(object, decoder, encoding, errors);
|
|
|
|
}
|
|
|
|
|
2002-09-02 10:14:32 -03:00
|
|
|
/* Register the error handling callback function error under the name
|
|
|
|
name. This function will be called by the codec when it encounters
|
|
|
|
an unencodable characters/undecodable bytes and doesn't know the
|
|
|
|
callback name, when name is specified as the error parameter
|
|
|
|
in the call to the encode/decode function.
|
|
|
|
Return 0 on success, -1 on error */
|
|
|
|
int PyCodec_RegisterError(const char *name, PyObject *error)
|
|
|
|
{
|
2004-03-24 18:22:12 -04:00
|
|
|
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
2003-03-18 20:35:36 -04:00
|
|
|
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
|
2010-05-09 11:46:46 -03:00
|
|
|
return -1;
|
2002-09-02 10:14:32 -03:00
|
|
|
if (!PyCallable_Check(error)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "handler must be callable");
|
|
|
|
return -1;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
2003-03-18 20:35:36 -04:00
|
|
|
return PyDict_SetItemString(interp->codec_error_registry,
|
2010-05-09 11:46:46 -03:00
|
|
|
(char *)name, error);
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Lookup the error handling callback function registered under the
|
|
|
|
name error. As a special case NULL can be passed, in which case
|
|
|
|
the error handling callback for strict encoding will be returned. */
|
|
|
|
PyObject *PyCodec_LookupError(const char *name)
|
|
|
|
{
|
|
|
|
PyObject *handler = NULL;
|
|
|
|
|
2004-03-24 18:22:12 -04:00
|
|
|
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
2003-03-18 20:35:36 -04:00
|
|
|
if (interp->codec_search_path == NULL && _PyCodecRegistry_Init())
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
2003-03-18 20:35:36 -04:00
|
|
|
|
2002-09-02 10:14:32 -03:00
|
|
|
if (name==NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
name = "strict";
|
2003-03-18 20:35:36 -04:00
|
|
|
handler = PyDict_GetItemString(interp->codec_error_registry, (char *)name);
|
2002-09-02 10:14:32 -03:00
|
|
|
if (!handler)
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_Format(PyExc_LookupError, "unknown error handler name '%.400s'", name);
|
2002-09-02 10:14:32 -03:00
|
|
|
else
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_INCREF(handler);
|
2002-09-02 10:14:32 -03:00
|
|
|
return handler;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void wrong_exception_type(PyObject *exc)
|
|
|
|
{
|
|
|
|
PyObject *type = PyObject_GetAttrString(exc, "__class__");
|
|
|
|
if (type != NULL) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *name = PyObject_GetAttrString(type, "__name__");
|
|
|
|
Py_DECREF(type);
|
|
|
|
if (name != NULL) {
|
|
|
|
PyObject *string = PyObject_Str(name);
|
|
|
|
Py_DECREF(name);
|
|
|
|
if (string != NULL) {
|
|
|
|
PyErr_Format(PyExc_TypeError,
|
|
|
|
"don't know how to handle %.400s in error callback",
|
|
|
|
PyString_AS_STRING(string));
|
|
|
|
Py_DECREF(string);
|
|
|
|
}
|
|
|
|
}
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *PyCodec_StrictErrors(PyObject *exc)
|
|
|
|
{
|
2006-03-01 00:25:17 -04:00
|
|
|
if (PyExceptionInstance_Check(exc))
|
|
|
|
PyErr_SetObject(PyExceptionInstance_Class(exc), exc);
|
2002-09-02 10:14:32 -03:00
|
|
|
else
|
2010-05-09 11:46:46 -03:00
|
|
|
PyErr_SetString(PyExc_TypeError, "codec must pass exception instance");
|
2002-09-02 10:14:32 -03:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-21 16:08:33 -04:00
|
|
|
#ifdef Py_USING_UNICODE
|
2002-09-02 10:14:32 -03:00
|
|
|
PyObject *PyCodec_IgnoreErrors(PyObject *exc)
|
|
|
|
{
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t end;
|
2015-05-18 10:08:38 -03:00
|
|
|
|
|
|
|
if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (PyUnicodeEncodeError_GetEnd(exc, &end))
|
|
|
|
return NULL;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
2015-05-18 10:08:38 -03:00
|
|
|
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (PyUnicodeDecodeError_GetEnd(exc, &end))
|
|
|
|
return NULL;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
2015-05-18 10:08:38 -03:00
|
|
|
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (PyUnicodeTranslateError_GetEnd(exc, &end))
|
|
|
|
return NULL;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 11:46:46 -03:00
|
|
|
wrong_exception_type(exc);
|
|
|
|
return NULL;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
/* ouch: passing NULL, 0, pos gives None instead of u'' */
|
2006-02-15 13:27:45 -04:00
|
|
|
return Py_BuildValue("(u#n)", &end, 0, end);
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
PyObject *PyCodec_ReplaceErrors(PyObject *exc)
|
|
|
|
{
|
|
|
|
PyObject *restuple;
|
2006-02-15 13:27:45 -04:00
|
|
|
Py_ssize_t start;
|
|
|
|
Py_ssize_t end;
|
|
|
|
Py_ssize_t i;
|
2002-09-02 10:14:32 -03:00
|
|
|
|
2015-05-18 10:08:38 -03:00
|
|
|
if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *res;
|
|
|
|
Py_UNICODE *p;
|
|
|
|
if (PyUnicodeEncodeError_GetStart(exc, &start))
|
|
|
|
return NULL;
|
|
|
|
if (PyUnicodeEncodeError_GetEnd(exc, &end))
|
|
|
|
return NULL;
|
|
|
|
res = PyUnicode_FromUnicode(NULL, end-start);
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (p = PyUnicode_AS_UNICODE(res), i = start;
|
|
|
|
i<end; ++p, ++i)
|
|
|
|
*p = '?';
|
|
|
|
restuple = Py_BuildValue("(On)", res, end);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return restuple;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
2015-05-18 10:08:38 -03:00
|
|
|
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;
|
|
|
|
if (PyUnicodeDecodeError_GetEnd(exc, &end))
|
|
|
|
return NULL;
|
2013-08-20 14:08:53 -03:00
|
|
|
return Py_BuildValue("(u#n)", &res, (Py_ssize_t)1, end);
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
2015-05-18 10:08:38 -03:00
|
|
|
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *res;
|
|
|
|
Py_UNICODE *p;
|
|
|
|
if (PyUnicodeTranslateError_GetStart(exc, &start))
|
|
|
|
return NULL;
|
|
|
|
if (PyUnicodeTranslateError_GetEnd(exc, &end))
|
|
|
|
return NULL;
|
|
|
|
res = PyUnicode_FromUnicode(NULL, end-start);
|
|
|
|
if (res == NULL)
|
|
|
|
return NULL;
|
|
|
|
for (p = PyUnicode_AS_UNICODE(res), i = start;
|
|
|
|
i<end; ++p, ++i)
|
|
|
|
*p = Py_UNICODE_REPLACEMENT_CHARACTER;
|
|
|
|
restuple = Py_BuildValue("(On)", res, end);
|
|
|
|
Py_DECREF(res);
|
|
|
|
return restuple;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 11:46:46 -03:00
|
|
|
wrong_exception_type(exc);
|
|
|
|
return NULL;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
|
|
|
|
{
|
2015-05-18 10:08:38 -03:00
|
|
|
if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *restuple;
|
|
|
|
PyObject *object;
|
|
|
|
Py_ssize_t start;
|
|
|
|
Py_ssize_t end;
|
|
|
|
PyObject *res;
|
|
|
|
Py_UNICODE *p;
|
|
|
|
Py_UNICODE *startp;
|
2013-08-06 10:56:26 -03:00
|
|
|
Py_UNICODE *e;
|
2010-05-09 11:46:46 -03:00
|
|
|
Py_UNICODE *outp;
|
2014-10-04 08:14:41 -03:00
|
|
|
Py_ssize_t ressize;
|
2010-05-09 11:46:46 -03:00
|
|
|
if (PyUnicodeEncodeError_GetStart(exc, &start))
|
|
|
|
return NULL;
|
|
|
|
if (PyUnicodeEncodeError_GetEnd(exc, &end))
|
|
|
|
return NULL;
|
|
|
|
if (!(object = PyUnicodeEncodeError_GetObject(exc)))
|
|
|
|
return NULL;
|
|
|
|
startp = PyUnicode_AS_UNICODE(object);
|
2014-10-04 08:14:41 -03:00
|
|
|
if (end - start > PY_SSIZE_T_MAX / (2+7+1)) {
|
|
|
|
end = start + PY_SSIZE_T_MAX / (2+7+1);
|
|
|
|
#ifndef Py_UNICODE_WIDE
|
2014-10-04 08:51:44 -03:00
|
|
|
if (0xD800 <= startp[end - 1] && startp[end - 1] <= 0xDBFF)
|
2014-10-04 08:14:41 -03:00
|
|
|
end--;
|
|
|
|
#endif
|
|
|
|
}
|
2013-08-06 10:56:26 -03:00
|
|
|
e = startp + end;
|
|
|
|
for (p = startp+start, ressize = 0; p < e;) {
|
|
|
|
Py_UCS4 ch = *p++;
|
|
|
|
#ifndef Py_UNICODE_WIDE
|
|
|
|
if ((0xD800 <= ch && ch <= 0xDBFF) &&
|
|
|
|
(p < e) &&
|
|
|
|
(0xDC00 <= *p && *p <= 0xDFFF)) {
|
|
|
|
ch = ((((ch & 0x03FF) << 10) |
|
|
|
|
((Py_UCS4)*p++ & 0x03FF)) + 0x10000);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
if (ch < 10)
|
2010-05-09 11:46:46 -03:00
|
|
|
ressize += 2+1+1;
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 100)
|
2010-05-09 11:46:46 -03:00
|
|
|
ressize += 2+2+1;
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 1000)
|
2010-05-09 11:46:46 -03:00
|
|
|
ressize += 2+3+1;
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 10000)
|
2010-05-09 11:46:46 -03:00
|
|
|
ressize += 2+4+1;
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 100000)
|
2010-05-09 11:46:46 -03:00
|
|
|
ressize += 2+5+1;
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 1000000)
|
2010-05-09 11:46:46 -03:00
|
|
|
ressize += 2+6+1;
|
|
|
|
else
|
|
|
|
ressize += 2+7+1;
|
|
|
|
}
|
|
|
|
/* allocate replacement */
|
|
|
|
res = PyUnicode_FromUnicode(NULL, ressize);
|
|
|
|
if (res == NULL) {
|
|
|
|
Py_DECREF(object);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/* generate replacement */
|
2013-08-06 10:56:26 -03:00
|
|
|
for (p = startp+start, outp = PyUnicode_AS_UNICODE(res); p < e;) {
|
2010-05-09 11:46:46 -03:00
|
|
|
int digits;
|
|
|
|
int base;
|
2013-08-06 10:56:26 -03:00
|
|
|
Py_UCS4 ch = *p++;
|
|
|
|
#ifndef Py_UNICODE_WIDE
|
|
|
|
if ((0xD800 <= ch && ch <= 0xDBFF) &&
|
|
|
|
(p < startp+end) &&
|
|
|
|
(0xDC00 <= *p && *p <= 0xDFFF)) {
|
|
|
|
ch = ((((ch & 0x03FF) << 10) |
|
|
|
|
((Py_UCS4)*p++ & 0x03FF)) + 0x10000);
|
|
|
|
}
|
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
*outp++ = '&';
|
|
|
|
*outp++ = '#';
|
2013-08-06 10:56:26 -03:00
|
|
|
if (ch < 10) {
|
2010-05-09 11:46:46 -03:00
|
|
|
digits = 1;
|
|
|
|
base = 1;
|
|
|
|
}
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 100) {
|
2010-05-09 11:46:46 -03:00
|
|
|
digits = 2;
|
|
|
|
base = 10;
|
|
|
|
}
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 1000) {
|
2010-05-09 11:46:46 -03:00
|
|
|
digits = 3;
|
|
|
|
base = 100;
|
|
|
|
}
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 10000) {
|
2010-05-09 11:46:46 -03:00
|
|
|
digits = 4;
|
|
|
|
base = 1000;
|
|
|
|
}
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 100000) {
|
2010-05-09 11:46:46 -03:00
|
|
|
digits = 5;
|
|
|
|
base = 10000;
|
|
|
|
}
|
2013-08-06 10:56:26 -03:00
|
|
|
else if (ch < 1000000) {
|
2010-05-09 11:46:46 -03:00
|
|
|
digits = 6;
|
|
|
|
base = 100000;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
digits = 7;
|
|
|
|
base = 1000000;
|
|
|
|
}
|
|
|
|
while (digits-->0) {
|
2013-08-06 10:56:26 -03:00
|
|
|
*outp++ = '0' + ch/base;
|
|
|
|
ch %= base;
|
2010-05-09 11:46:46 -03:00
|
|
|
base /= 10;
|
|
|
|
}
|
|
|
|
*outp++ = ';';
|
|
|
|
}
|
|
|
|
restuple = Py_BuildValue("(On)", res, end);
|
|
|
|
Py_DECREF(res);
|
|
|
|
Py_DECREF(object);
|
|
|
|
return restuple;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 11:46:46 -03:00
|
|
|
wrong_exception_type(exc);
|
|
|
|
return NULL;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static Py_UNICODE hexdigits[] = {
|
|
|
|
'0', '1', '2', '3', '4', '5', '6', '7',
|
|
|
|
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f'
|
|
|
|
};
|
|
|
|
|
|
|
|
PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
|
|
|
|
{
|
2015-05-18 10:08:38 -03:00
|
|
|
if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) {
|
2010-05-09 11:46:46 -03:00
|
|
|
PyObject *restuple;
|
|
|
|
PyObject *object;
|
|
|
|
Py_ssize_t start;
|
|
|
|
Py_ssize_t end;
|
|
|
|
PyObject *res;
|
|
|
|
Py_UNICODE *p;
|
|
|
|
Py_UNICODE *startp;
|
|
|
|
Py_UNICODE *outp;
|
2014-10-04 08:14:41 -03:00
|
|
|
Py_ssize_t ressize;
|
2010-05-09 11:46:46 -03:00
|
|
|
if (PyUnicodeEncodeError_GetStart(exc, &start))
|
|
|
|
return NULL;
|
|
|
|
if (PyUnicodeEncodeError_GetEnd(exc, &end))
|
|
|
|
return NULL;
|
|
|
|
if (!(object = PyUnicodeEncodeError_GetObject(exc)))
|
|
|
|
return NULL;
|
2014-10-04 08:14:41 -03:00
|
|
|
if (end - start > PY_SSIZE_T_MAX / (1+1+8))
|
|
|
|
end = start + PY_SSIZE_T_MAX / (1+1+8);
|
2010-05-09 11:46:46 -03:00
|
|
|
startp = PyUnicode_AS_UNICODE(object);
|
|
|
|
for (p = startp+start, ressize = 0; p < startp+end; ++p) {
|
2003-12-28 21:36:01 -04:00
|
|
|
#ifdef Py_UNICODE_WIDE
|
2010-05-09 11:46:46 -03:00
|
|
|
if (*p >= 0x00010000)
|
|
|
|
ressize += 1+1+8;
|
|
|
|
else
|
2003-12-28 21:36:01 -04:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
if (*p >= 0x100) {
|
|
|
|
ressize += 1+1+4;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
ressize += 1+1+2;
|
|
|
|
}
|
|
|
|
res = PyUnicode_FromUnicode(NULL, ressize);
|
2014-09-23 13:58:57 -03:00
|
|
|
if (res == NULL) {
|
|
|
|
Py_DECREF(object);
|
2010-05-09 11:46:46 -03:00
|
|
|
return NULL;
|
2014-09-23 13:58:57 -03:00
|
|
|
}
|
2010-05-09 11:46:46 -03:00
|
|
|
for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);
|
|
|
|
p < startp+end; ++p) {
|
|
|
|
Py_UNICODE c = *p;
|
|
|
|
*outp++ = '\\';
|
2003-12-28 21:36:01 -04:00
|
|
|
#ifdef Py_UNICODE_WIDE
|
2010-05-09 11:46:46 -03:00
|
|
|
if (c >= 0x00010000) {
|
|
|
|
*outp++ = 'U';
|
|
|
|
*outp++ = hexdigits[(c>>28)&0xf];
|
|
|
|
*outp++ = hexdigits[(c>>24)&0xf];
|
|
|
|
*outp++ = hexdigits[(c>>20)&0xf];
|
|
|
|
*outp++ = hexdigits[(c>>16)&0xf];
|
|
|
|
*outp++ = hexdigits[(c>>12)&0xf];
|
|
|
|
*outp++ = hexdigits[(c>>8)&0xf];
|
|
|
|
}
|
|
|
|
else
|
2003-12-28 21:36:01 -04:00
|
|
|
#endif
|
2010-05-09 11:46:46 -03:00
|
|
|
if (c >= 0x100) {
|
|
|
|
*outp++ = 'u';
|
|
|
|
*outp++ = hexdigits[(c>>12)&0xf];
|
|
|
|
*outp++ = hexdigits[(c>>8)&0xf];
|
|
|
|
}
|
|
|
|
else
|
|
|
|
*outp++ = 'x';
|
|
|
|
*outp++ = hexdigits[(c>>4)&0xf];
|
|
|
|
*outp++ = hexdigits[c&0xf];
|
|
|
|
}
|
|
|
|
|
|
|
|
restuple = Py_BuildValue("(On)", res, end);
|
|
|
|
Py_DECREF(res);
|
|
|
|
Py_DECREF(object);
|
|
|
|
return restuple;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
else {
|
2010-05-09 11:46:46 -03:00
|
|
|
wrong_exception_type(exc);
|
|
|
|
return NULL;
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
|
|
|
}
|
2002-11-21 16:08:33 -04:00
|
|
|
#endif
|
2002-09-02 10:14:32 -03:00
|
|
|
|
|
|
|
static PyObject *strict_errors(PyObject *self, PyObject *exc)
|
|
|
|
{
|
|
|
|
return PyCodec_StrictErrors(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2002-11-21 16:08:33 -04:00
|
|
|
#ifdef Py_USING_UNICODE
|
2002-09-02 10:14:32 -03:00
|
|
|
static PyObject *ignore_errors(PyObject *self, PyObject *exc)
|
|
|
|
{
|
|
|
|
return PyCodec_IgnoreErrors(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *replace_errors(PyObject *self, PyObject *exc)
|
|
|
|
{
|
|
|
|
return PyCodec_ReplaceErrors(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *xmlcharrefreplace_errors(PyObject *self, PyObject *exc)
|
|
|
|
{
|
|
|
|
return PyCodec_XMLCharRefReplaceErrors(exc);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
static PyObject *backslashreplace_errors(PyObject *self, PyObject *exc)
|
|
|
|
{
|
|
|
|
return PyCodec_BackslashReplaceErrors(exc);
|
|
|
|
}
|
2002-11-21 16:08:33 -04:00
|
|
|
#endif
|
2002-09-02 10:14:32 -03:00
|
|
|
|
2003-03-18 20:35:36 -04:00
|
|
|
static int _PyCodecRegistry_Init(void)
|
2000-03-10 18:57:27 -04:00
|
|
|
{
|
2002-09-02 10:14:32 -03:00
|
|
|
static struct {
|
2010-05-09 11:46:46 -03:00
|
|
|
char *name;
|
|
|
|
PyMethodDef def;
|
2002-09-02 10:14:32 -03:00
|
|
|
} methods[] =
|
|
|
|
{
|
2010-05-09 11:46:46 -03:00
|
|
|
{
|
|
|
|
"strict",
|
|
|
|
{
|
|
|
|
"strict_errors",
|
|
|
|
strict_errors,
|
|
|
|
METH_O,
|
|
|
|
PyDoc_STR("Implements the 'strict' error handling, which "
|
|
|
|
"raises a UnicodeError on coding errors.")
|
|
|
|
}
|
|
|
|
},
|
2002-11-21 16:08:33 -04:00
|
|
|
#ifdef Py_USING_UNICODE
|
2010-05-09 11:46:46 -03:00
|
|
|
{
|
|
|
|
"ignore",
|
|
|
|
{
|
|
|
|
"ignore_errors",
|
|
|
|
ignore_errors,
|
|
|
|
METH_O,
|
|
|
|
PyDoc_STR("Implements the 'ignore' error handling, which "
|
|
|
|
"ignores malformed data and continues.")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"replace",
|
|
|
|
{
|
|
|
|
"replace_errors",
|
|
|
|
replace_errors,
|
|
|
|
METH_O,
|
|
|
|
PyDoc_STR("Implements the 'replace' error handling, which "
|
|
|
|
"replaces malformed data with a replacement marker.")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"xmlcharrefreplace",
|
|
|
|
{
|
|
|
|
"xmlcharrefreplace_errors",
|
|
|
|
xmlcharrefreplace_errors,
|
|
|
|
METH_O,
|
|
|
|
PyDoc_STR("Implements the 'xmlcharrefreplace' error handling, "
|
|
|
|
"which replaces an unencodable character with the "
|
|
|
|
"appropriate XML character reference.")
|
|
|
|
}
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"backslashreplace",
|
|
|
|
{
|
|
|
|
"backslashreplace_errors",
|
|
|
|
backslashreplace_errors,
|
|
|
|
METH_O,
|
|
|
|
PyDoc_STR("Implements the 'backslashreplace' error handling, "
|
|
|
|
"which replaces an unencodable character with a "
|
|
|
|
"backslashed escape sequence.")
|
|
|
|
}
|
|
|
|
}
|
2002-11-21 16:08:33 -04:00
|
|
|
#endif
|
2002-09-02 10:14:32 -03:00
|
|
|
};
|
2003-03-18 20:35:36 -04:00
|
|
|
|
2004-03-24 18:22:12 -04:00
|
|
|
PyInterpreterState *interp = PyThreadState_GET()->interp;
|
2003-03-18 20:35:36 -04:00
|
|
|
PyObject *mod;
|
2004-07-07 22:55:58 -03:00
|
|
|
unsigned i;
|
2003-03-18 20:35:36 -04:00
|
|
|
|
|
|
|
if (interp->codec_search_path != NULL)
|
2010-05-09 11:46:46 -03:00
|
|
|
return 0;
|
2003-03-18 20:35:36 -04:00
|
|
|
|
|
|
|
interp->codec_search_path = PyList_New(0);
|
|
|
|
interp->codec_search_cache = PyDict_New();
|
|
|
|
interp->codec_error_registry = PyDict_New();
|
|
|
|
|
|
|
|
if (interp->codec_error_registry) {
|
2010-05-09 11:46:46 -03:00
|
|
|
for (i = 0; i < sizeof(methods)/sizeof(methods[0]); ++i) {
|
|
|
|
PyObject *func = PyCFunction_New(&methods[i].def, NULL);
|
|
|
|
int res;
|
|
|
|
if (!func)
|
|
|
|
Py_FatalError("can't initialize codec error registry");
|
|
|
|
res = PyCodec_RegisterError(methods[i].name, func);
|
|
|
|
Py_DECREF(func);
|
|
|
|
if (res)
|
|
|
|
Py_FatalError("can't initialize codec error registry");
|
|
|
|
}
|
2002-09-02 10:14:32 -03:00
|
|
|
}
|
2003-03-18 20:35:36 -04:00
|
|
|
|
|
|
|
if (interp->codec_search_path == NULL ||
|
2010-05-09 11:46:46 -03:00
|
|
|
interp->codec_search_cache == NULL ||
|
|
|
|
interp->codec_error_registry == NULL)
|
|
|
|
Py_FatalError("can't initialize codec registry");
|
2000-03-10 18:57:27 -04:00
|
|
|
|
2006-02-28 12:09:29 -04:00
|
|
|
mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0);
|
2003-03-18 20:35:36 -04:00
|
|
|
if (mod == NULL) {
|
2010-05-09 11:46:46 -03:00
|
|
|
if (PyErr_ExceptionMatches(PyExc_ImportError)) {
|
|
|
|
/* Ignore ImportErrors... this is done so that
|
|
|
|
distributions can disable the encodings package. Note
|
|
|
|
that other errors are not masked, e.g. SystemErrors
|
|
|
|
raised to inform the user of an error in the Python
|
|
|
|
configuration are still reported back to the user. */
|
|
|
|
PyErr_Clear();
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return -1;
|
2003-03-18 20:35:36 -04:00
|
|
|
}
|
|
|
|
Py_DECREF(mod);
|
|
|
|
return 0;
|
2000-03-10 18:57:27 -04:00
|
|
|
}
|