diff options
author | Guido van Rossum <guido@python.org> | 2007-10-19 22:06:24 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2007-10-19 22:06:24 (GMT) |
commit | 75a902db7859a4751743e98530c5d96a672641be (patch) | |
tree | a730c966dcfc993a800078004aae3a6094a75a6a /Objects | |
parent | 21431e85d505b9698c085c25cbf1b2997a352b85 (diff) | |
download | cpython-75a902db7859a4751743e98530c5d96a672641be.zip cpython-75a902db7859a4751743e98530c5d96a672641be.tar.gz cpython-75a902db7859a4751743e98530c5d96a672641be.tar.bz2 |
Patch 1280, by Alexandre Vassalotti.
Make PyString's indexing and iteration return integers.
(I changed a few of Alexandre's decisions -- GvR.)
Diffstat (limited to 'Objects')
-rw-r--r-- | Objects/stringobject.c | 35 |
1 files changed, 9 insertions, 26 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c index 699ae27..6371aa3 100644 --- a/Objects/stringobject.c +++ b/Objects/stringobject.c @@ -986,28 +986,6 @@ string_contains(PyObject *str_obj, PyObject *sub_obj) return stringlib_contains_obj(str_obj, sub_obj); } -static PyObject * -string_item(PyStringObject *a, register Py_ssize_t i) -{ - char pchar; - PyObject *v; - if (i < 0 || i >= Py_Size(a)) { - PyErr_SetString(PyExc_IndexError, "string index out of range"); - return NULL; - } - pchar = a->ob_sval[i]; - v = (PyObject *)characters[pchar & UCHAR_MAX]; - if (v == NULL) - v = PyString_FromStringAndSize(&pchar, 1); - else { -#ifdef COUNT_ALLOCS - one_strings++; -#endif - Py_INCREF(v); - } - return v; -} - static PyObject* string_richcompare(PyStringObject *a, PyStringObject *b, int op) { @@ -1110,7 +1088,12 @@ string_subscript(PyStringObject* self, PyObject* item) return NULL; if (i < 0) i += PyString_GET_SIZE(self); - return string_item(self, i); + if (i < 0 || i >= PyString_GET_SIZE(self)) { + PyErr_SetString(PyExc_IndexError, + "string index out of range"); + return NULL; + } + return PyInt_FromLong((unsigned char)self->ob_sval[i]); } else if (PySlice_Check(item)) { Py_ssize_t start, stop, step, slicelength, cur, i; @@ -1173,7 +1156,7 @@ static PySequenceMethods string_as_sequence = { (lenfunc)string_length, /*sq_length*/ (binaryfunc)string_concat, /*sq_concat*/ (ssizeargfunc)string_repeat, /*sq_repeat*/ - (ssizeargfunc)string_item, /*sq_item*/ + 0, /*sq_item*/ 0, /*sq_slice*/ 0, /*sq_ass_item*/ 0, /*sq_ass_slice*/ @@ -4147,8 +4130,8 @@ striter_next(striterobject *it) assert(PyString_Check(seq)); if (it->it_index < PyString_GET_SIZE(seq)) { - item = PyString_FromStringAndSize( - PyString_AS_STRING(seq)+it->it_index, 1); + item = PyInt_FromLong( + (unsigned char)seq->ob_sval[it->it_index]); if (item != NULL) ++it->it_index; return item; |