diff options
author | Eric Wieser <wieser.eric@gmail.com> | 2019-04-13 13:04:14 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2019-04-13 13:04:14 -0700 |
commit | b18e32efc3f5b77630a1d4f05399fc15c69831b3 (patch) | |
tree | 28cf8c7793e820cdf6668bd1141b5aa118cd51d6 | |
parent | d0e2609204e4fc55f5165c1437a556ee1d0e88b7 (diff) | |
parent | 3b8bcc33e1fb75c7fed4cc0d83fd2640da5e1e77 (diff) | |
download | numpy-b18e32efc3f5b77630a1d4f05399fc15c69831b3.tar.gz |
Merge pull request #13321 from eric-wieser/move-exceptions
MAINT: Move exceptions from core._internal to core._exceptions
-rw-r--r-- | numpy/core/_exceptions.py | 25 | ||||
-rw-r--r-- | numpy/core/_internal.py | 24 | ||||
-rw-r--r-- | numpy/core/numeric.py | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/common.h | 2 | ||||
-rw-r--r-- | numpy/core/src/multiarray/multiarraymodule.c | 2 |
5 files changed, 28 insertions, 27 deletions
diff --git a/numpy/core/_exceptions.py b/numpy/core/_exceptions.py index 227c08cd6..a1997ef6b 100644 --- a/numpy/core/_exceptions.py +++ b/numpy/core/_exceptions.py @@ -5,6 +5,7 @@ in python where it's easier. By putting the formatting in `__str__`, we also avoid paying the cost for users who silence the exceptions. """ +from numpy.core.overrides import set_module def _unpack_tuple(tup): if len(tup) == 1: @@ -96,3 +97,27 @@ class _UFuncOutputCastingError(_UFuncCastingError): ).format( self.ufunc.__name__, i_str, self.from_, self.to, self.casting ) + + +# Exception used in shares_memory() +@set_module('numpy') +class TooHardError(RuntimeError): + pass + + +@set_module('numpy') +class AxisError(ValueError, IndexError): + """ Axis supplied was invalid. """ + def __init__(self, axis, ndim=None, msg_prefix=None): + # single-argument form just delegates to base class + if ndim is None and msg_prefix is None: + msg = axis + + # do the string formatting here, to save work in the C code + else: + msg = ("axis {} is out of bounds for array of dimension {}" + .format(axis, ndim)) + if msg_prefix is not None: + msg = "{}: {}".format(msg_prefix, msg) + + super(AxisError, self).__init__(msg) diff --git a/numpy/core/_internal.py b/numpy/core/_internal.py index 1d3bb5584..82035e561 100644 --- a/numpy/core/_internal.py +++ b/numpy/core/_internal.py @@ -10,7 +10,6 @@ import re import sys from numpy.compat import unicode -from numpy.core.overrides import set_module from .multiarray import dtype, array, ndarray try: import ctypes @@ -795,29 +794,6 @@ def _gcd(a, b): def _lcm(a, b): return a // _gcd(a, b) * b -# Exception used in shares_memory() -@set_module('numpy') -class TooHardError(RuntimeError): - pass - -@set_module('numpy') -class AxisError(ValueError, IndexError): - """ Axis supplied was invalid. """ - def __init__(self, axis, ndim=None, msg_prefix=None): - # single-argument form just delegates to base class - if ndim is None and msg_prefix is None: - msg = axis - - # do the string formatting here, to save work in the C code - else: - msg = ("axis {} is out of bounds for array of dimension {}" - .format(axis, ndim)) - if msg_prefix is not None: - msg = "{}: {}".format(msg_prefix, msg) - - super(AxisError, self).__init__(msg) - - def array_ufunc_errmsg_formatter(dummy, ufunc, method, *inputs, **kwargs): """ Format the error message for when __array_ufunc__ gives up. """ args_string = ', '.join(['{!r}'.format(arg) for arg in inputs] + diff --git a/numpy/core/numeric.py b/numpy/core/numeric.py index 42fee4eb7..b4255e733 100644 --- a/numpy/core/numeric.py +++ b/numpy/core/numeric.py @@ -38,7 +38,7 @@ from .umath import (multiply, invert, sin, UFUNC_BUFSIZE_DEFAULT, ERR_LOG, ERR_DEFAULT, PINF, NAN) from . import numerictypes from .numerictypes import longlong, intc, int_, float_, complex_, bool_ -from ._internal import TooHardError, AxisError +from ._exceptions import TooHardError, AxisError bitwise_not = invert ufunc = type(sin) diff --git a/numpy/core/src/multiarray/common.h b/numpy/core/src/multiarray/common.h index 0e162903d..487d530a1 100644 --- a/numpy/core/src/multiarray/common.h +++ b/numpy/core/src/multiarray/common.h @@ -149,7 +149,7 @@ check_and_adjust_axis_msg(int *axis, int ndim, PyObject *msg_prefix) PyObject *exc; if (AxisError_cls == NULL) { - PyObject *mod = PyImport_ImportModule("numpy.core._internal"); + PyObject *mod = PyImport_ImportModule("numpy.core._exceptions"); if (mod != NULL) { AxisError_cls = PyObject_GetAttrString(mod, "AxisError"); diff --git a/numpy/core/src/multiarray/multiarraymodule.c b/numpy/core/src/multiarray/multiarraymodule.c index 1b825d318..2537fc003 100644 --- a/numpy/core/src/multiarray/multiarraymodule.c +++ b/numpy/core/src/multiarray/multiarraymodule.c @@ -4015,7 +4015,7 @@ array_shares_memory_impl(PyObject *args, PyObject *kwds, Py_ssize_t default_max_ } else if (result == MEM_OVERLAP_TOO_HARD) { if (raise_exceptions) { - npy_cache_import("numpy.core._internal", "TooHardError", + npy_cache_import("numpy.core._exceptions", "TooHardError", &too_hard_cls); if (too_hard_cls) { PyErr_SetString(too_hard_cls, "Exceeded max_work"); |