[PATCH] A compromise on case - another suggestion
Peter Schneider-Kamp
petersc at stud.ntnu.no
Sat May 27 23:51:10 EDT 2000
I have made a quick-n-dirty patch which gives a more
informative IndexError (see below):
>>> [][0]
Traceback (innermost last):
File "<stdin>", line 1, in ?
IndexError: empty list cannot be subscripted
>>> [1,2,3][3]
Traceback (innermost last):
File "<stdin>", line 1, in ?
IndexError: list index 3 out of range [0..2]
>>> [1,2,3][-4]
Traceback (innermost last):
File "<stdin>", line 1, in ?
IndexError: list index -4 out of range [-3..-1]
What do you think? Should I implement a cleaner version
and send it to the patches list or is my approach
fundamentally flawed?
Amit Patel wrote:
>
> For IndexError, it'd be helpful to have some suggestions --
> such as knowing what the range was.
> Instead of:
>
> >>> ['a', 'b', 'c'][5]
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> IndexError: list index out of range
>
> How about:
>
> >>> ['a', 'b', 'c'][5]
> Traceback (innermost last):
> File "<stdin>", line 1, in ?
> IndexError: list index 5 out of range [0..3)
saving-human-time-so-we-can-watch-more-tv-ly y'rs
Peter
=====Begin context diff
*** python/dist/src/Objects/listobject.c Thu May 4 01:44:35 2000
--- python-mod/dist/src/Objects/listobject.c Sun May 28 05:17:20 2000
***************
*** 115,124 ****
return NULL;
}
if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
! if (indexerr == NULL)
! indexerr = PyString_FromString(
! "list index out of range");
! PyErr_SetObject(PyExc_IndexError, indexerr);
return NULL;
}
return ((PyListObject *)op) -> ob_item[i];
--- 115,136 ----
return NULL;
}
if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
! if (indexerr == NULL) {
! if (((PyListObject *)op) -> ob_size) {
! char temp[256];
! if (i<0) {
! sprintf(temp,"list index %d out of range [%d..-1]",
! i-((PyListObject *)op) -> ob_size,-1*((PyListObject *)op) -> ob_size);
! } else {
! sprintf(temp,"list index %d out of range [0..%d]",
! i,((PyListObject *)op) -> ob_size-1);
! }
! indexerr=PyString_FromString(temp);
! } else {
! indexerr=PyString_FromString("cannot get item from empty list");
! }
! }
! PyErr_SetObject(PyExc_IndexError, indexerr);
return NULL;
}
return ((PyListObject *)op) -> ob_item[i];
***************
*** 139,146 ****
}
if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Py_XDECREF(newitem);
! PyErr_SetString(PyExc_IndexError,
! "list assignment index out of range");
return -1;
}
p = ((PyListObject *)op) -> ob_item + i;
--- 151,169 ----
}
if (i < 0 || i >= ((PyListObject *)op) -> ob_size) {
Py_XDECREF(newitem);
! if (((PyListObject *)op) -> ob_size) {
! char temp[256];
! if (i<0) {
! sprintf(temp,"list index %d out of range [%d..-1]",
! i-((PyListObject *)op) -> ob_size,-1*((PyListObject *)op) -> ob_size);
! } else {
! sprintf(temp,"list index %d out of range [0..%d]",
! i,((PyListObject *)op) -> ob_size-1);
! }
! PyErr_SetString(PyExc_IndexError,temp);
! } else {
! PyErr_SetString(PyExc_IndexError,"cannot set item in empty list");
! }
return -1;
}
p = ((PyListObject *)op) -> ob_item + i;
***************
*** 332,339 ****
{
if (i < 0 || i >= a->ob_size) {
if (indexerr == NULL)
! indexerr = PyString_FromString(
! "list index out of range");
PyErr_SetObject(PyExc_IndexError, indexerr);
return NULL;
}
--- 355,373 ----
{
if (i < 0 || i >= a->ob_size) {
if (indexerr == NULL)
! if (a->ob_size) {
! char temp[256];
! if (i<0) {
! sprintf(temp,"list index %d out of range [%d..-1]",
! i-a->ob_size,-1*a->ob_size);
! } else {
! sprintf(temp,"list index %d out of range [0..%d]",
! i,a->ob_size-1);
! }
! indexerr=PyString_FromString(temp);
! } else {
! indexerr=PyString_FromString("cannot get item from empty list");
! }
PyErr_SetObject(PyExc_IndexError, indexerr);
return NULL;
}
***************
*** 547,554 ****
{
PyObject *old_value;
if (i < 0 || i >= a->ob_size) {
! PyErr_SetString(PyExc_IndexError,
! "list assignment index out of range");
return -1;
}
if (v == NULL)
--- 581,599 ----
{
PyObject *old_value;
if (i < 0 || i >= a->ob_size) {
! if (a->ob_size) {
! char temp[256];
! if (i<0) {
! sprintf(temp,"list index %d out of range [%d..-1]",
! i-a->ob_size,-1*a->ob_size);
! } else {
! sprintf(temp,"list index %d out of range [0..%d]",
! i,a->ob_size-1);
! }
! PyErr_SetString(PyExc_IndexError,temp);
! } else {
! PyErr_SetString(PyExc_IndexError,"cannot set item in empty list");
! }
return -1;
}
if (v == NULL)
***************
*** 697,703 ****
if (i < 0)
i += self->ob_size;
if (i < 0 || i >= self->ob_size) {
! PyErr_SetString(PyExc_IndexError, "pop index out of range");
return NULL;
}
v = self->ob_item[i];
--- 742,756 ----
if (i < 0)
i += self->ob_size;
if (i < 0 || i >= self->ob_size) {
! char temp[256];
! if (i<0) {
! sprintf(temp,"list index %d out of range [%d..-1]",
! i-self->ob_size,-1*self->ob_size);
! } else {
! sprintf(temp,"list index %d out of range [0..%d]",
! i,self->ob_size-1);
! }
! PyErr_SetString(PyExc_IndexError,temp);
return NULL;
}
v = self->ob_item[i];
*** python/dist/src/Python/ceval.c Mon May 8 16:06:50 2000
--- python-mod/dist/src/Python/ceval.c Sun May 28 05:17:53 2000
***************
*** 857,865 ****
if (i < 0)
i += PyList_GET_SIZE(v);
if (i < 0 ||
! i >= PyList_GET_SIZE(v)) {
! PyErr_SetString(PyExc_IndexError,
! "list index out of range");
x = NULL;
}
else {
--- 857,876 ----
if (i < 0)
i += PyList_GET_SIZE(v);
if (i < 0 ||
! i >= PyList_GET_SIZE(v)) {
! if (PyList_GET_SIZE(v)) {
! char temp[256];
! if (i<0) {
! sprintf(temp,"list index %d out of range [%d..-1]",
! i-PyList_GET_SIZE(v),-1*PyList_GET_SIZE(v));
! } else {
! sprintf(temp,"list index %d out of range [0..%d]",
! i,PyList_GET_SIZE(v)-1);
! }
! PyErr_SetString(PyExc_IndexError,temp);
! } else {
! PyErr_SetString(PyExc_IndexError,"empty list cannot be subscripted");
! }
x = NULL;
}
else {
More information about the Python-list
mailing list