diff Modules/arraymodule.c @ 36882:6a9c7165b17b legacy-trunk

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).
author Guido van Rossum <guido@python.org>
date Tue, 07 Mar 2006 18:50:55 +0000
parents e9a7a2784e17
children f731859e9b3b 4020cf6c383e
line wrap: on
line diff
--- a/Modules/arraymodule.c	Tue Mar 07 18:31:44 2006 +0000
+++ b/Modules/arraymodule.c	Tue Mar 07 18:50:55 2006 +0000
@@ -1569,19 +1569,17 @@
 	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 @@
 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;