cinap_lenrekadd hg and python4bb9e2215 years agoHistory
860 lines · 20.1 KB · C
RawDownload
    Py_ssize_t i;     if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {	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;    }    else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) {	Py_UNICODE res = Py_UNICODE_REPLACEMENT_CHARACTER;	if (PyUnicodeDecodeError_GetEnd(exc, &end))	    return NULL;	return Py_BuildValue("(u#n)", &res, 1, end);    }    else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) {	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;    }    else {	wrong_exception_type(exc);	return NULL;    }} PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc){    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {	PyObject *restuple;	PyObject *object;	Py_ssize_t start;	Py_ssize_t end;	PyObject *res;	Py_UNICODE *p;	Py_UNICODE *startp;	Py_UNICODE *outp;	int ressize;	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);	for (p = startp+start, ressize = 0; p < startp+end; ++p) {	    if (*p<10)		ressize += 2+1+1;	    else if (*p<100)		ressize += 2+2+1;	    else if (*p<1000)		ressize += 2+3+1;	    else if (*p<10000)		ressize += 2+4+1;#ifndef Py_UNICODE_WIDE	    else		ressize += 2+5+1;#else	    else if (*p<100000)		ressize += 2+5+1;	    else if (*p<1000000)		ressize += 2+6+1;	    else		ressize += 2+7+1;#endif	}	/* allocate replacement */	res = PyUnicode_FromUnicode(NULL, ressize);	if (res == NULL) {	    Py_DECREF(object);	    return NULL;	}	/* generate replacement */	for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);	    p < startp+end; ++p) {	    Py_UNICODE c = *p;	    int digits;	    int base;	    *outp++ = '&';	    *outp++ = '#';	    if (*p<10) {		digits = 1;		base = 1;	    }	    else if (*p<100) {		digits = 2;		base = 10;	    }	    else if (*p<1000) {		digits = 3;		base = 100;	    }	    else if (*p<10000) {		digits = 4;		base = 1000;	    }#ifndef Py_UNICODE_WIDE	    else {		digits = 5;		base = 10000;	    }#else	    else if (*p<100000) {		digits = 5;		base = 10000;	    }	    else if (*p<1000000) {		digits = 6;		base = 100000;	    }	    else {		digits = 7;		base = 1000000;	    }#endif	    while (digits-->0) {		*outp++ = '0' + c/base;		c %= base;		base /= 10;	    }	    *outp++ = ';';	}	restuple = Py_BuildValue("(On)", res, end);	Py_DECREF(res);	Py_DECREF(object);	return restuple;    }    else {	wrong_exception_type(exc);	return NULL;    }} 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){    if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) {	PyObject *restuple;	PyObject *object;	Py_ssize_t start;	Py_ssize_t end;	PyObject *res;	Py_UNICODE *p;	Py_UNICODE *startp;	Py_UNICODE *outp;	int ressize;	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);	for (p = startp+start, ressize = 0; p < startp+end; ++p) {#ifdef Py_UNICODE_WIDE	    if (*p >= 0x00010000)		ressize += 1+1+8;	    else#endif	    if (*p >= 0x100) {		ressize += 1+1+4;	    }	    else		ressize += 1+1+2;	}	res = PyUnicode_FromUnicode(NULL, ressize);	if (res==NULL)	    return NULL;	for (p = startp+start, outp = PyUnicode_AS_UNICODE(res);	    p < startp+end; ++p) {	    Py_UNICODE c = *p;	    *outp++ = '\\';#ifdef Py_UNICODE_WIDE	    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#endif	    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;    }    else {	wrong_exception_type(exc);	return NULL;    }}#endif static PyObject *strict_errors(PyObject *self, PyObject *exc){    return PyCodec_StrictErrors(exc);}  #ifdef Py_USING_UNICODEstatic 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);}#endif static int _PyCodecRegistry_Init(void){    static struct {	char *name;	PyMethodDef def;    } methods[] =    {	{	    "strict",	    {		"strict_errors",		strict_errors,		METH_O	    }	},#ifdef Py_USING_UNICODE	{	    "ignore",	    {		"ignore_errors",		ignore_errors,		METH_O	    }	},	{	    "replace",	    {		"replace_errors",		replace_errors,		METH_O	    }	},	{	    "xmlcharrefreplace",	    {		"xmlcharrefreplace_errors",		xmlcharrefreplace_errors,		METH_O	    }	},	{	    "backslashreplace",	    {		"backslashreplace_errors",		backslashreplace_errors,		METH_O	    }	}#endif    };     PyInterpreterState *interp = PyThreadState_GET()->interp;    PyObject *mod;    unsigned i;     if (interp->codec_search_path != NULL)	return 0;     interp->codec_search_path = PyList_New(0);    interp->codec_search_cache = PyDict_New();    interp->codec_error_registry = PyDict_New();     if (interp->codec_error_registry) {	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");	}    }     if (interp->codec_search_path == NULL ||	interp->codec_search_cache == NULL ||	interp->codec_error_registry == NULL)	Py_FatalError("can't initialize codec registry");     mod = PyImport_ImportModuleLevel("encodings", NULL, NULL, NULL, 0);    if (mod == NULL) {	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;    }    Py_DECREF(mod);    return 0;}

Lines 501–860 of 860. Previous lines