summaryrefslogtreecommitdiffstats
path: root/Objects/classobject.c
diff options
context:
space:
mode:
authorGuido van Rossum <guido@python.org>2006-03-07 18:50:55 (GMT)
committerGuido van Rossum <guido@python.org>2006-03-07 18:50:55 (GMT)
commit38fff8c4e4276e4e57660a78f305e68bfa87874b (patch)
tree5f79c06159053f9c7113410fc69dccba01e331ab /Objects/classobject.c
parent9d7855076a8e030e30459de685e762f63bdecac6 (diff)
downloadcpython-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 'Objects/classobject.c')
-rw-r--r--Objects/classobject.c38
1 files changed, 38 insertions, 0 deletions
diff --git a/Objects/classobject.c b/Objects/classobject.c
index 34afb9e..037252d 100644
--- a/Objects/classobject.c
+++ b/Objects/classobject.c
@@ -1733,6 +1733,43 @@ instance_nonzero(PyInstanceObject *self)
return outcome > 0;
}
+static Py_ssize_t
+instance_index(PyInstanceObject *self)
+{
+ PyObject *func, *res;
+ Py_ssize_t outcome;
+ static PyObject *indexstr = NULL;
+
+ if (indexstr == NULL) {
+ indexstr = PyString_InternFromString("__index__");
+ if (indexstr == NULL)
+ return -1;
+ }
+ if ((func = instance_getattr(self, indexstr)) == NULL) {
+ if (!PyErr_ExceptionMatches(PyExc_AttributeError))
+ return -1;
+ PyErr_Clear();
+ PyErr_SetString(PyExc_TypeError,
+ "object cannot be interpreted as an index");
+ return -1;
+ }
+ res = PyEval_CallObject(func, (PyObject *)NULL);
+ Py_DECREF(func);
+ if (res == NULL)
+ return -1;
+ if (PyInt_Check(res) || PyLong_Check(res)) {
+ outcome = res->ob_type->tp_as_number->nb_index(res);
+ }
+ else {
+ PyErr_SetString(PyExc_TypeError,
+ "__index__ must return an int or a long");
+ outcome = -1;
+ }
+ Py_DECREF(res);
+ return outcome;
+}
+
+
UNARY(instance_invert, "__invert__")
UNARY(instance_int, "__int__")
UNARY(instance_long, "__long__")
@@ -2052,6 +2089,7 @@ static PyNumberMethods instance_as_number = {
(binaryfunc)instance_truediv, /* nb_true_divide */
(binaryfunc)instance_ifloordiv, /* nb_inplace_floor_divide */
(binaryfunc)instance_itruediv, /* nb_inplace_true_divide */
+ (lenfunc)instance_index, /* nb_index */
};
PyTypeObject PyInstance_Type = {