Backport of #2897 (ndindex failing)

This backports all relevant changes from:

https://github.com/numpy/numpy/pull/2897

I squashed them into just one commit.
Add a missing part of the test

This was already committed in master, but not in the 1.7 branch.
Merge pull request #2924 from certik/backport2897

Backport of #2897 (ndindex failing)