diff options
author | Guido van Rossum <guido@python.org> | 2006-03-07 18:50:55 (GMT) |
---|---|---|
committer | Guido van Rossum <guido@python.org> | 2006-03-07 18:50:55 (GMT) |
commit | 38fff8c4e4276e4e57660a78f305e68bfa87874b (patch) | |
tree | 5f79c06159053f9c7113410fc69dccba01e331ab /Modules/arraymodule.c | |
parent | 9d7855076a8e030e30459de685e762f63bdecac6 (diff) | |
download | cpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.zip cpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.tar.gz cpython-38fff8c4e4276e4e57660a78f305e68bfa87874b.tar.bz2 |
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 'Modules/arraymodule.c')
-rw-r--r-- | Modules/arraymodule.c | 29 |
1 files changed, 11 insertions, 18 deletions
diff --git a/Modules/arraymodule.c b/Modules/arraymodule.c index 2318739..1650ff2 100644 --- a/Modules/arraymodule.c +++ b/Modules/arraymodule.c @@ -1569,19 +1569,17 @@ array_repr(arrayobject *a) return s; } +#define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX) + static PyObject* array_subscr(arrayobject* self, PyObject* item) { - if (PyInt_Check(item)) { - Py_ssize_t i = PyInt_AS_LONG(item); - if (i < 0) - i += self->ob_size; - return array_item(self, i); - } - else if (PyLong_Check(item)) { - Py_ssize_t i = PyInt_AsSsize_t(item); - if (i == -1 && PyErr_Occurred()) + 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 += self->ob_size; return array_item(self, i); @@ -1626,15 +1624,10 @@ array_subscr(arrayobject* self, PyObject* item) static int array_ass_subscr(arrayobject* self, PyObject* item, PyObject* value) { - if (PyInt_Check(item)) { - Py_ssize_t i = PyInt_AS_LONG(item); - if (i < 0) - i += self->ob_size; - return array_ass_item(self, i, value); - } - else if (PyLong_Check(item)) { - Py_ssize_t i = PyInt_AsSsize_t(item); - if (i == -1 && PyErr_Occurred()) + 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 -1; if (i < 0) i += self->ob_size; |