summaryrefslogtreecommitdiffstats
path: root/Objects/listobject.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/listobject.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/listobject.c')
-rw-r--r--Objects/listobject.c18
1 files changed, 8 insertions, 10 deletions
diff --git a/Objects/listobject.c b/Objects/listobject.c
index 0ff61e2..966d659 100644
--- a/Objects/listobject.c
+++ b/Objects/listobject.c
@@ -2452,11 +2452,14 @@ PyDoc_STRVAR(list_doc,
"list() -> new list\n"
"list(sequence) -> new list initialized from sequence's items");
+#define HASINDEX(o) PyType_HasFeature((o)->ob_type, Py_TPFLAGS_HAVE_INDEX)
+
static PyObject *
list_subscript(PyListObject* 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)
@@ -2503,14 +2506,9 @@ list_subscript(PyListObject* self, PyObject* item)
static int
list_ass_subscript(PyListObject* self, PyObject* item, PyObject* value)
{
- if (PyInt_Check(item)) {
- Py_ssize_t i = PyInt_AS_LONG(item);
- if (i < 0)
- i += PyList_GET_SIZE(self);
- return list_ass_item(self, i, value);
- }
- else if (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 -1;
if (i < 0)