10000 ENH: Delay assembling of the error message until `__str__` is called · numpy/numpy@11f7edf · GitHub
[go: up one dir, main page]

Skip to content

Commit 11f7edf

Browse files
BvB93eric-wieser
andcommitted
ENH: Delay assembling of the error message until __str__ is called
Co-Authored-By: Eric Wieser <425260+eric-wieser@users.noreply.github.com>
1 parent 16964ae commit 11f7edf

File tree

2 files changed

+15
-42
lines changed

2 files changed

+15
-42
lines changed

numpy/core/_exceptions.py

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -180,47 +180,30 @@ class AxisError(ValueError, IndexError):
180180
181181
"""
182182

183-
__slots__ = ("axis", "ndim")
183+
__slots__ = ("axis", "ndim", "_msg")
184184

185185
def __init__(self, axis, ndim=None, msg_prefix=None):
186-
# single-argument form just delegates to base class
187-
if ndim is None and msg_prefix is None:
188-
msg = axis
186+
if ndim is msg_prefix is None:
187+
# single-argument form: directly set the error message
188+
self._msg = axis
189189
self.axis = None
190190
self.ndim = None
191-
192-
# do the string formatting here, to save work in the C code
193191
else:
194-
msg = ("axis {} is out of bounds for array of dimension {}"
195-
.format(axis, ndim))
196-
if msg_prefix is not None:
197-
msg = "{}: {}".format(msg_prefix, msg)
192+
self._msg = msg_prefix
198193
self.axis = axis
199194
self.ndim = ndim
200195

201-
super().__init__(msg)
202-
203-
@classmethod
204-
def _reconstruct(cls, axis, ndim, args):
205-
"""Auxiliary instance constructor used by `__reduce__`.
206-
207-
Directly assign all instance attributes without further sanitization.
208-
209-
This method avoids the `__init__` constructor in order to:
210-
* Circumvent dealing with its axis-based overload.
211-
* Avoid extracting the message prefix from ``self.args`` if applicable.
212-
213-
"""
214-
self = super().__new__(cls)
215-
self.axis = axis
216-
self.ndim = ndim
217-
self.args = args
218-
return self
196+
def __str__(self):
197+
axis = self.axis
198+
ndim = self.ndim
219199

220-
def __reduce__(self):
221-
"""Return state information for `pickle`."""
222-
cls = type(self)
223-
return cls._reconstruct, (self.axis, self.ndim, self.args)
200+
if axis is ndim is None:
201+
return self._msg
202+
else:
203+
msg = f"axis {axis} is out of bounds for array of dimension {ndim}"
204+
if self._msg is not None:
205+
msg = f"{self._msg}: {msg}"
206+
return msg
224207

225208

226209
@_display_as_base

numpy/core/tests/test__exceptions.py

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -86,13 +86,3 @@ def test_pickling(self, args):
8686
attr1 = getattr(exc, name)
8787
attr2 = getattr(exc2, name)
8888
assert attr1 == attr2, name
89-
90-
def test_pickling_compat(self, args):
91-
"""Test that `AxisError` instances pickled prior to NumPy 1.22
92-
can still be unpickled.
93-
"""
94-
# Attach a `__reduce__` method equivalent to the one used prior to 1.22
95-
exc = np.AxisError(*args)
96-
exc.__reduce__ = super(np.AxisError, exc).__reduce__
97-
98-
assert pickle.loads(pickle.dumps(exc))

0 commit comments

Comments
 (0)
0