[Numpy-discussion] better error message possible?

Thouis (Ray) Jones thouis at gmail.com
Mon Jun 4 17:00:28 EDT 2012


On Mon, Jun 4, 2012 at 4:27 PM, Thouis (Ray) Jones <thouis at gmail.com> wrote:
> On Fri, Jun 1, 2012 at 6:56 PM, Chris Withers <chris at simplistix.co.uk> wrote:
>> On 01/06/2012 16:39, Benjamin Root wrote:
>>>
>>>
>>>      > >>> import numpy
>>>      > >>> numpy.zeros(10)[-123]
>>>      > Traceback (most recent call last):
>>>      >   File "<stdin>", line 1, in <module>
>>>      > IndexError: index out of bounds
>>>      >
>>>      > ...could say this:
>>>      >
>>>      > >>> numpy.zeros(10)[-123]
>>>      > Traceback (most recent call last):
>>>      >   File "<stdin>", line 1, in <module>
>>>      > IndexError: -123 is out of bounds
>>>
>>>     Only that no-one has implemented it, I guess. If you want to then
>>>     that'd be cool :-).
>>>
>>>     To be generally useful for debugging, it would probably be good for
>>>     the error message to also mention which dimension is involved, and/or
>>>     the actual size of the array in that dimension. You can also get such
>>>     error messages from expressions like 'arr[i, j, k]', after all, where
>>>     it's even less obvious what went wrong.
>>>
>>>     -- Nathaniel
>>>
>>>
>>> +1, please!
>>
>> Indeed, sadly I'm not a C developer. It's a pet bugbear of mine that
>> Python's built-in exceptions often tell you what went wrong but not what
>> data caused the error, even when it's easily to hand when raising the
>> exception.
>
> I could look into this.  There are only ~10 places the code generates
> this error, so it should be a pretty minor change.

My initial estimate was low, but not overly so.  An initial pass at
adding index/dimension information to IndexErrors is here:
https://github.com/thouis/numpy/tree/index_error_info

A typical result:

>>> numpy.zeros(3)[5]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index 5 out of bounds in dimension 0

I thought it best to have erroring indices report their initial value:

>>> numpy.zeros(3)[-15]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: index -15 out of bounds in dimension 0

This is different from some places in the code where IndexErrors
already had bad index and dimension information (including the maximum
value possible for an index in that dimension).  I left these alone,
though most of them would report that the bad index was -12 instead of
-15.  For instance:
https://github.com/thouis/numpy/blob/index_error_info/numpy/core/src/multiarray/mapping.c#L1640

Also there were a few indexing errors that were throwing ValueErrors.
I changed these to IndexErrors.

If someone could give this a cursory review before I issue a PR, I'd
appreciate it.  I don't expect that most of these code paths are
heavily exercised in the tests (but I could be wrong).

Ray Jones



More information about the NumPy-Discussion mailing list