Issue #28510: Clean up decoding error handlers.

Since PyUnicodeDecodeError_GetObject() always returns bytes, following
PyBytes_AsString() can be replaced with PyBytes_AS_STRING().
This commit is contained in:
Serhiy Storchaka 2016-10-23 09:44:50 +03:00
parent 523c449ca0
commit cb33a01bbc

View file

@ -867,17 +867,14 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
Py_UCS4 c; Py_UCS4 c;
if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
unsigned char *p; const unsigned char *p;
if (PyUnicodeDecodeError_GetStart(exc, &start)) if (PyUnicodeDecodeError_GetStart(exc, &start))
return NULL; return NULL;
if (PyUnicodeDecodeError_GetEnd(exc, &end)) if (PyUnicodeDecodeError_GetEnd(exc, &end))
return NULL; return NULL;
if (!(object = PyUnicodeDecodeError_GetObject(exc))) if (!(object = PyUnicodeDecodeError_GetObject(exc)))
return NULL; return NULL;
if (!(p = (unsigned char*)PyBytes_AsString(object))) { p = (const unsigned char*)PyBytes_AS_STRING(object);
Py_DECREF(object);
return NULL;
}
res = PyUnicode_New(4 * (end - start), 127); res = PyUnicode_New(4 * (end - start), 127);
if (res == NULL) { if (res == NULL) {
Py_DECREF(object); Py_DECREF(object);
@ -1220,7 +1217,7 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
return restuple; return restuple;
} }
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
unsigned char *p; const unsigned char *p;
Py_UCS4 ch = 0; Py_UCS4 ch = 0;
if (PyUnicodeDecodeError_GetStart(exc, &start)) if (PyUnicodeDecodeError_GetStart(exc, &start))
return NULL; return NULL;
@ -1228,10 +1225,7 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
return NULL; return NULL;
if (!(object = PyUnicodeDecodeError_GetObject(exc))) if (!(object = PyUnicodeDecodeError_GetObject(exc)))
return NULL; return NULL;
if (!(p = (unsigned char*)PyBytes_AsString(object))) { p = (const unsigned char*)PyBytes_AS_STRING(object);
Py_DECREF(object);
return NULL;
}
if (!(encode = PyUnicodeDecodeError_GetEncoding(exc))) { if (!(encode = PyUnicodeDecodeError_GetEncoding(exc))) {
Py_DECREF(object); Py_DECREF(object);
return NULL; return NULL;
@ -1338,7 +1332,7 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
} }
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) { else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
PyObject *str; PyObject *str;
unsigned char *p; const unsigned char *p;
Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */ Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */
int consumed = 0; int consumed = 0;
if (PyUnicodeDecodeError_GetStart(exc, &start)) if (PyUnicodeDecodeError_GetStart(exc, &start))
@ -1347,10 +1341,7 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
return NULL; return NULL;
if (!(object = PyUnicodeDecodeError_GetObject(exc))) if (!(object = PyUnicodeDecodeError_GetObject(exc)))
return NULL; return NULL;
if (!(p = (unsigned char*)PyBytes_AsString(object))) { p = (const unsigned char*)PyBytes_AS_STRING(object);
Py_DECREF(object);
return NULL;
}
while (consumed < 4 && consumed < end-start) { while (consumed < 4 && consumed < end-start) {
/* Refuse to escape ASCII bytes. */ /* Refuse to escape ASCII bytes. */
if (p[start+consumed] < 128) if (p[start+consumed] < 128)