8000 MAINT: Use the same exception for all bad axis requests by eric-wieser · Pull Request #8584 · numpy/numpy · GitHub
[go: up one dir, main page]

Skip to content

MAINT: Use the same exception for all bad axis requests #8584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Feb 21, 2017
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
ENH: For compatibility, use an exception type that subclasses both or…
…iginal types
  • Loading branch information
eric-wieser committed Feb 20, 2017
commit eb642f1b6533fcd92e366377f5859e4ea56d5eed
3 changes: 3 additions & 0 deletions numpy/core/_internal.py
Original file line number Diff line number Diff line change
Expand Up @@ -630,3 +630,6 @@ def _gcd(a, b):
# Exception used in shares_memory()
class TooHardError(RuntimeError):
pass

class AxisError(ValueError, IndexError):
pass
4 changes: 2 additions & 2 deletions numpy/core/numeric.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
ERR_DEFAULT, PINF, NAN)
from . import numerictypes
from .numerictypes import longlong, intc, int_, float_, complex_, bool_
from ._internal import TooHardError
from ._internal import TooHardError, AxisError

bitwise_not = invert
ufunc = type(sin)
Expand Down Expand Up @@ -65,7 +65,7 @@
'True_', 'bitwise_not', 'CLIP', 'RAISE', 'WRAP', 'MAXDIMS', 'BUFSIZE',
'ALLOW_THREADS', 'ComplexWarning', 'full', 'full_like', 'matmul',
'shares_memory', 'may_share_memory', 'MAY_SHARE_BOUNDS', 'MAY_SHARE_EXACT',
'TooHardError',
'TooHardError', 'AxisError'
]


Expand Down
16 changes: 15 additions & 1 deletion numpy/core/src/multiarray/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,21 @@ check_and_adjust_axis(int *axis, int ndim)
{
/* Check that index is valid, taking into account negative indices */
if (NPY_UNLIKELY((*axis < -ndim) || (*axis >= ndim))) {
PyErr_Format(PyExc_IndexError,
/*
* Load the exception type, if we don't already have it. Unfortunately
* we don't have access to npy_cache_import here
*/
static PyObject *AxisError_cls = NULL;
if (AxisError_cls == NULL) {
PyObject *mod = PyImport_ImportModule("numpy.core._internal");

if (mod != NULL) {
AxisError_cls = PyObject_GetAttrString(mod, "AxisError");
Py_DECREF(mod);
}
}

PyErr_Format(AxisError_cls,
"axis %d is out of bounds for array of dimension %d",
*axis, ndim);
return -1;
Expand Down
0