PyObject *PyCodec_ReplaceErrors(PyObject *exc) { Py_ssize_t start, end, i, len; if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { PyObject *res; int kind; void *data; if (PyUnicodeEncodeError_GetStart(exc, &start)) return NULL; if (PyUnicodeEncodeError_GetEnd(exc, &end)) return NULL; len = end - start; res = PyUnicode_New(len, '?'); if (res == NULL) return NULL; kind = PyUnicode_KIND(res); data = PyUnicode_DATA(res); for (i = 0; i < len; ++i) PyUnicode_WRITE(kind, data, i, '?'); assert(_PyUnicode_CheckConsistency(res, 1)); return Py_BuildValue("(Nn)", res, end); } else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { if (PyUnicodeDecodeError_GetEnd(exc, &end)) return NULL; return Py_BuildValue("(Cn)", (int)Py_UNICODE_REPLACEMENT_CHARACTER, end); } else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { PyObject *res; int kind; void *data; if (PyUnicodeTranslateError_GetStart(exc, &start)) return NULL; if (PyUnicodeTranslateError_GetEnd(exc, &end)) return NULL; len = end - start; res = PyUnicode_New(len, Py_UNICODE_REPLACEMENT_CHARACTER); if (res == NULL) return NULL; kind = PyUnicode_KIND(res); data = PyUnicode_DATA(res); for (i=0; i < len; i++) PyUnicode_WRITE(kind, data, i, Py_UNICODE_REPLACEMENT_CHARACTER); assert(_PyUnicode_CheckConsistency(res, 1)); return Py_BuildValue("(Nn)", res, end); } else { wrong_exception_type(exc); return NULL; } }
PyObject *PyCodec_ReplaceErrors(PyObject *exc) { PyObject *restuple; Py_ssize_t start; Py_ssize_t end; 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, (Py_ssize_t)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_IgnoreErrors(PyObject* exc) noexcept { Py_ssize_t end; if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { if (PyUnicodeEncodeError_GetEnd(exc, &end)) return NULL; } else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { if (PyUnicodeDecodeError_GetEnd(exc, &end)) return NULL; } else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { if (PyUnicodeTranslateError_GetEnd(exc, &end)) return NULL; } else { wrong_exception_type(exc); return NULL; } /* ouch: passing NULL, 0, pos gives None instead of u'' */ return Py_BuildValue("(u#n)", &end, 0, end); }
PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) { PyObject *object; Py_ssize_t i; Py_ssize_t start; Py_ssize_t end; PyObject *res; unsigned char *outp; int ressize; Py_UCS4 c; if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { unsigned char *p; if (PyUnicodeDecodeError_GetStart(exc, &start)) return NULL; if (PyUnicodeDecodeError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeDecodeError_GetObject(exc))) return NULL; if (!(p = (unsigned char*)PyBytes_AsString(object))) { Py_DECREF(object); return NULL; } res = PyUnicode_New(4 * (end - start), 127); if (res == NULL) { Py_DECREF(object); return NULL; } outp = PyUnicode_1BYTE_DATA(res); for (i = start; i < end; i++, outp += 4) { unsigned char c = p[i]; outp[0] = '\\'; outp[1] = 'x'; outp[2] = Py_hexdigits[(c>>4)&0xf]; outp[3] = Py_hexdigits[c&0xf]; } assert(_PyUnicode_CheckConsistency(res, 1)); Py_DECREF(object); return Py_BuildValue("(Nn)", res, end); }
PyObject *PyCodec_IgnoreErrors(PyObject *exc) { Py_ssize_t end; if (PyObject_IsInstance(exc, PyExc_UnicodeEncodeError)) { if (PyUnicodeEncodeError_GetEnd(exc, &end)) return NULL; } else if (PyObject_IsInstance(exc, PyExc_UnicodeDecodeError)) { if (PyUnicodeDecodeError_GetEnd(exc, &end)) return NULL; } else if (PyObject_IsInstance(exc, PyExc_UnicodeTranslateError)) { if (PyUnicodeTranslateError_GetEnd(exc, &end)) return NULL; } else { wrong_exception_type(exc); return NULL; } return Py_BuildValue("(Nn)", PyUnicode_New(0, 0), end); }
PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc) { PyObject *object; Py_ssize_t i; Py_ssize_t start; Py_ssize_t end; PyObject *res; unsigned char *outp; int ressize; Py_UCS4 c; if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { const unsigned char *p; if (PyUnicodeDecodeError_GetStart(exc, &start)) return NULL; if (PyUnicodeDecodeError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeDecodeError_GetObject(exc))) return NULL; p = (const unsigned char*)PyBytes_AS_STRING(object); res = PyUnicode_New(4 * (end - start), 127); if (res == NULL) { Py_DECREF(object); return NULL; } outp = PyUnicode_1BYTE_DATA(res); for (i = start; i < end; i++, outp += 4) { unsigned char c = p[i]; outp[0] = '\\'; outp[1] = 'x'; outp[2] = Py_hexdigits[(c>>4)&0xf]; outp[3] = Py_hexdigits[c&0xf]; } assert(_PyUnicode_CheckConsistency(res, 1)); Py_DECREF(object); return Py_BuildValue("(Nn)", res, end); } if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeEncodeError)) { if (PyUnicodeEncodeError_GetStart(exc, &start)) return NULL; if (PyUnicodeEncodeError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeEncodeError_GetObject(exc))) return NULL; } else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeTranslateError)) { if (PyUnicodeTranslateError_GetStart(exc, &start)) return NULL; if (PyUnicodeTranslateError_GetEnd(exc, &end)) return NULL; if (!(object = PyUnicodeTranslateError_GetObject(exc))) return NULL; } else { wrong_exception_type(exc); return NULL; } if (end - start > PY_SSIZE_T_MAX / (1+1+8)) end = start + PY_SSIZE_T_MAX / (1+1+8); for (i = start, ressize = 0; i < end; ++i) { /* object is guaranteed to be "ready" */ c = PyUnicode_READ_CHAR(object, i); if (c >= 0x10000) { ressize += 1+1+8; } else if (c >= 0x100) { ressize += 1+1+4; } else ressize += 1+1+2; } res = PyUnicode_New(ressize, 127); if (res == NULL) { Py_DECREF(object); return NULL; } outp = PyUnicode_1BYTE_DATA(res); for (i = start; i < end; ++i) { c = PyUnicode_READ_CHAR(object, i); *outp++ = '\\'; if (c >= 0x00010000) { *outp++ = 'U'; *outp++ = Py_hexdigits[(c>>28)&0xf]; *outp++ = Py_hexdigits[(c>>24)&0xf]; *outp++ = Py_hexdigits[(c>>20)&0xf]; *outp++ = Py_hexdigits[(c>>16)&0xf]; *outp++ = Py_hexdigits[(c>>12)&0xf]; *outp++ = Py_hexdigits[(c>>8)&0xf]; } else if (c >= 0x100) {