summaryrefslogtreecommitdiff
path: root/Objects/stringobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-03-07 18:50:55 +0000
committerGuido van Rossum <guido@python.org>2006-03-07 18:50:55 +0000
commit8cfdc49ae03974b726502ccd7175ab9b7ced9e7b (patch)
tree2d513b7c885bab75ac6b9ee1d3f73f2ff6995ad3 /Objects/stringobject.c
parentfc889bb8c409057b5d2036767a29ad53c8f81fe1 (diff)
downloadcpython-8cfdc49ae03974b726502ccd7175ab9b7ced9e7b.tar.gz
Checking in the code for PEP 357.
This was mostly written by Travis Oliphant. I've inspected it all; Neal Norwitz and MvL have also looked at it (in an earlier incarnation).
Diffstat (limited to 'Objects/stringobject.c')
-rw-r--r--Objects/stringobject.c9
1 files changed, 6 insertions, 3 deletions
diff --git a/Objects/stringobject.c b/Objects/stringobject.c
index e2b7603b5a..16d542ae07 100644
--- a/Objects/stringobject.c
+++ b/Objects/stringobject.c
@@ -1187,16 +1187,19 @@ string_hash(PyStringObject *a)
return x;
}
+#define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX)
+
static PyObject*
string_subscript(PyStringObject* self, PyObject* item)
{
- if (PyInt_Check(item) || PyLong_Check(item)) {
- Py_ssize_t i = PyInt_AsSsize_t(item);
+ PyNumberMethods *nb = item->ob_type->tp_as_number;
+ if (nb != NULL && HASINDEX(item) && nb->nb_index != NULL) {
+ Py_ssize_t i = nb->nb_index(item);
if (i == -1 && PyErr_Occurred())
return NULL;
if (i < 0)
i += PyString_GET_SIZE(self);
- return string_item(self,i);
+ return string_item(self, i);
}
else if (PySlice_Check(item)) {
Py_ssize_t start, stop, step, slicelength, cur, i;