[Python-checkins] cpython: PyUnicode_ReadChar() raises a IndexError if the index in invalid

victor.stinner python-checkins at python.org
Sun Oct 2 01:14:16 CEST 2011


http://hg.python.org/cpython/rev/ae2b07f9ede6
changeset:   72574:ae2b07f9ede6
user:        Victor Stinner <victor.stinner at haypocalc.com>
date:        Sun Oct 02 00:25:40 2011 +0200
summary:
  PyUnicode_ReadChar() raises a IndexError if the index in invalid

unicode_getitem() reuses PyUnicode_ReadChar()

files:
  Objects/unicodeobject.c |  29 +++++++++++++----------------
  1 files changed, 13 insertions(+), 16 deletions(-)


diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c
--- a/Objects/unicodeobject.c
+++ b/Objects/unicodeobject.c
@@ -2840,8 +2840,12 @@
 Py_UCS4
 PyUnicode_ReadChar(PyObject *unicode, Py_ssize_t index)
 {
-    if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) != -1) {
-        return PyErr_BadArgument();
+    if (!PyUnicode_Check(unicode) || PyUnicode_READY(unicode) == -1) {
+        PyErr_BadArgument();
+        return (Py_UCS4)-1;
+    }
+    if (index < 0 || index >= _PyUnicode_LENGTH(unicode)) {
+        PyErr_SetString(PyExc_IndexError, "string index out of range");
         return (Py_UCS4)-1;
     }
     return PyUnicode_READ_CHAR(unicode, index);
@@ -9808,18 +9812,11 @@
 }
 
 static PyObject *
-unicode_getitem(PyUnicodeObject *self, Py_ssize_t index)
-{
-    Py_UCS4 ch;
-
-    if (PyUnicode_READY(self) == -1)
-        return NULL;
-    if (index < 0 || index >= _PyUnicode_LENGTH(self)) {
-        PyErr_SetString(PyExc_IndexError, "string index out of range");
-        return NULL;
-    }
-
-    ch = PyUnicode_READ(PyUnicode_KIND(self), PyUnicode_DATA(self), index);
+unicode_getitem(PyObject *self, Py_ssize_t index)
+{
+    Py_UCS4 ch = PyUnicode_ReadChar(self, index);
+    if (ch == (Py_UCS4)-1)
+        return NULL;
     return PyUnicode_FromOrdinal(ch);
 }
 
@@ -10475,7 +10472,7 @@
 
     length = end - start;
     if (length == 1)
-        return unicode_getitem((PyUnicodeObject*)self, start);
+        return unicode_getitem(self, start);
 
     if (start < 0 || end < 0) {
         PyErr_SetString(PyExc_IndexError, "string index out of range");
@@ -11758,7 +11755,7 @@
             return NULL;
         if (i < 0)
             i += PyUnicode_GET_LENGTH(self);
-        return unicode_getitem(self, i);
+        return unicode_getitem((PyObject*)self, i);
     } else if (PySlice_Check(item)) {
         Py_ssize_t start, stop, step, slicelength, cur, i;
         const Py_UNICODE* source_buf;

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


More information about the Python-checkins mailing list