[Python-checkins] cpython: Issue #28510: Clean up decoding error handlers.

serhiy.storchaka python-checkins at python.org
Sun Oct 23 02:45:13 EDT 2016


https://hg.python.org/cpython/rev/e5e05ac07aee
changeset:   104649:e5e05ac07aee
user:        Serhiy Storchaka <storchaka at gmail.com>
date:        Sun Oct 23 09:44:50 2016 +0300
summary:
  Issue #28510: Clean up decoding error handlers.

Since PyUnicodeDecodeError_GetObject() always returns bytes, following
PyBytes_AsString() can be replaced with PyBytes_AS_STRING().

files:
  Python/codecs.c |  21 ++++++---------------
  1 files changed, 6 insertions(+), 15 deletions(-)


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

-- 
Repository URL: https://hg.python.org/cpython


More information about the Python-checkins mailing list