diff options
author | Sebastian Berg <sebastianb@nvidia.com> | 2022-11-18 14:50:57 +0100 |
---|---|---|
committer | Sebastian Berg <sebastianb@nvidia.com> | 2022-11-30 12:07:51 +0100 |
commit | b0f318b38e7dd305c3ca93e6c912e1391dda999e (patch) | |
tree | 37d8b856b9f38d84c7c3e8256fab1cb9bdc39c2f /numpy/exceptions.py | |
parent | 3c2717c9a923926ca197bee88b4c5b646b1b93d3 (diff) | |
download | numpy-b0f318b38e7dd305c3ca93e6c912e1391dda999e.tar.gz |
API: Add new exceptions module and move exception exposed via numeric
This means moving ComplexWarning, TooHardError, and AxisError.
Diffstat (limited to 'numpy/exceptions.py')
-rw-r--r-- | numpy/exceptions.py | 123 |
1 files changed, 123 insertions, 0 deletions
diff --git a/numpy/exceptions.py b/numpy/exceptions.py new file mode 100644 index 000000000..4a63263a5 --- /dev/null +++ b/numpy/exceptions.py @@ -0,0 +1,123 @@ +from ._utils import set_module as _set_module + +__all__ = ["ComplexWarning", "TooHardError", "AxisError"] + + +@_set_module('numpy') +class ComplexWarning(RuntimeWarning): + """ + The warning raised when casting a complex dtype to a real dtype. + + As implemented, casting a complex number to a real discards its imaginary + part, but this behavior may not be what the user actually wants. + + """ + pass + + + +# Exception used in shares_memory() +@_set_module('numpy') +class TooHardError(RuntimeError): + """max_work was exceeded. + + This is raised whenever the maximum number of candidate solutions + to consider specified by the ``max_work`` parameter is exceeded. + Assigning a finite number to max_work may have caused the operation + to fail. + + """ + + pass + + +@_set_module('numpy') +class AxisError(ValueError, IndexError): + """Axis supplied was invalid. + + This is raised whenever an ``axis`` parameter is specified that is larger + than the number of array dimensions. + For compatibility with code written against older numpy versions, which + raised a mixture of `ValueError` and `IndexError` for this situation, this + exception subclasses both to ensure that ``except ValueError`` and + ``except IndexError`` statements continue to catch `AxisError`. + + .. versionadded:: 1.13 + + Parameters + ---------- + axis : int or str + The out of bounds axis or a custom exception message. + If an axis is provided, then `ndim` should be specified as well. + ndim : int, optional + The number of array dimensions. + msg_prefix : str, optional + A prefix for the exception message. + + Attributes + ---------- + axis : int, optional + The out of bounds axis or ``None`` if a custom exception + message was provided. This should be the axis as passed by + the user, before any normalization to resolve negative indices. + + .. versionadded:: 1.22 + ndim : int, optional + The number of array dimensions or ``None`` if a custom exception + message was provided. + + .. versionadded:: 1.22 + + + Examples + -------- + >>> array_1d = np.arange(10) + >>> np.cumsum(array_1d, axis=1) + Traceback (most recent call last): + ... + numpy.AxisError: axis 1 is out of bounds for array of dimension 1 + + Negative axes are preserved: + + >>> np.cumsum(array_1d, axis=-2) + Traceback (most recent call last): + ... + numpy.AxisError: axis -2 is out of bounds for array of dimension 1 + + The class constructor generally takes the axis and arrays' + dimensionality as arguments: + + >>> print(np.AxisError(2, 1, msg_prefix='error')) + error: axis 2 is out of bounds for array of dimension 1 + + Alternatively, a custom exception message can be passed: + + >>> print(np.AxisError('Custom error message')) + Custom error message + + """ + + __slots__ = ("axis", "ndim", "_msg") + + def __init__(self, axis, ndim=None, msg_prefix=None): + if ndim is msg_prefix is None: + # single-argument form: directly set the error message + self._msg = axis + self.axis = None + self.ndim = None + else: + self._msg = msg_prefix + self.axis = axis + self.ndim = ndim + + def __str__(self): + axis = self.axis + ndim = self.ndim + + if axis is ndim is None: + return self._msg + else: + msg = f"axis {axis} is out of bounds for array of dimension {ndim}" + if self._msg is not None: + msg = f"{self._msg}: {msg}" + return msg |