numpy/numpy — pull request #296 by thouis (2012-06-06 15:26:38+00:00)
Title: Index error info

Adds a check_and_adjust_index function in common.c that is used to verify indices and adjust them for negative indexing. Invalid indexes produce an IndexError, and the function returns -1. Otherwise, the index is adjusted in-place (for negative indices), and the function returns 0.

The IndexError produced by this function includes information about the invalid value, the array axis where it caused a problem, and the valid range (when this information is available).

Nearly all such indexing adjustment and validation in the numpy source have been replaced by calls to this function, and a few cases of ValueError in indexing have been replaced by IndexError for consistency.

A couple of open questions:

- In arraytypes.c.src:...fasttake(), there's no way to know the axis in the call to check_and_adjust_index(), so it can't report the axis in the case of an invalid index. Adding an argument to fasttake would allow it to be reported. I wasn't sure this was an acceptable change.

- In cases of operations that implicitly operate on a flattened array, the IndexError reports that bad indices are out of bound on axis=0, which might be confusing in some cases. One solution would be to add an argument to check_and_adjust_index() indicating whether the array being indexed was 1-dimensional, and not report an axis in this case.