Skip to content

Commit cb33a01

Browse files
Issue #28510: Clean up decoding error handlers.
Since PyUnicodeDecodeError_GetObject() always returns bytes, following PyBytes_AsString() can be replaced with PyBytes_AS_STRING().
1 parent 523c449 commit cb33a01

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

Python/codecs.c

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -867,17 +867,14 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
867867
Py_UCS4 c;
868868

869869
if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
870-
unsigned char *p;
870+
const unsigned char *p;
871871
if (PyUnicodeDecodeError_GetStart(exc, &start))
872872
return NULL;
873873
if (PyUnicodeDecodeError_GetEnd(exc, &end))
874874
return NULL;
875875
if (!(object = PyUnicodeDecodeError_GetObject(exc)))
876876
return NULL;
877-
if (!(p = (unsigned char*)PyBytes_AsString(object))) {
878-
Py_DECREF(object);
879-
return NULL;
880-
}
877+
p = (const unsigned char*)PyBytes_AS_STRING(object);
881878
res = PyUnicode_New(4 * (end - start), 127);
882879
if (res == NULL) {
883880
Py_DECREF(object);
@@ -1220,18 +1217,15 @@ PyCodec_SurrogatePassErrors(PyObject *exc)
12201217
return restuple;
12211218
}
12221219
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
1223-
unsigned char *p;
1220+
const unsigned char *p;
12241221
Py_UCS4 ch = 0;
12251222
if (PyUnicodeDecodeError_GetStart(exc, &start))
12261223
return NULL;
12271224
if (PyUnicodeDecodeError_GetEnd(exc, &end))
12281225
return NULL;
12291226
if (!(object = PyUnicodeDecodeError_GetObject(exc)))
12301227
return NULL;
1231-
if (!(p = (unsigned char*)PyBytes_AsString(object))) {
1232-
Py_DECREF(object);
1233-
return NULL;
1234-
}
1228+
p = (const unsigned char*)PyBytes_AS_STRING(object);
12351229
if (!(encode = PyUnicodeDecodeError_GetEncoding(exc))) {
12361230
Py_DECREF(object);
12371231
return NULL;
@@ -1338,7 +1332,7 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
13381332
}
13391333
else if (PyObject_TypeCheck(exc, (PyTypeObject *)PyExc_UnicodeDecodeError)) {
13401334
PyObject *str;
1341-
unsigned char *p;
1335+
const unsigned char *p;
13421336
Py_UCS2 ch[4]; /* decode up to 4 bad bytes. */
13431337
int consumed = 0;
13441338
if (PyUnicodeDecodeError_GetStart(exc, &start))
@@ -1347,10 +1341,7 @@ PyCodec_SurrogateEscapeErrors(PyObject *exc)
13471341
return NULL;
13481342
if (!(object = PyUnicodeDecodeError_GetObject(exc)))
13491343
return NULL;
1350-
if (!(p = (unsigned char*)PyBytes_AsString(object))) {
1351-
Py_DECREF(object);
1352-
return NULL;
1353-
}
1344+
p = (const unsigned char*)PyBytes_AS_STRING(object);
13541345
while (consumed < 4 && consumed < end-start) {
13551346
/* Refuse to escape ASCII bytes. */
13561347
if (p[start+consumed] < 128)

0 commit comments

Comments
 (0)