@@ -180,47 +180,30 @@ class AxisError(ValueError, IndexError):
180
180
181
181
"""
182
182
183
- __slots__ = ("axis" , "ndim" )
183
+ __slots__ = ("axis" , "ndim" , "_msg" )
184
184
185
185
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
189
189
self .axis = None
190
190
self .ndim = None
191
-
192
- # do the string formatting here, to save work in the C code
193
191
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
198
193
self .axis = axis
199
194
self .ndim = ndim
200
195
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
219
199
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
224
207
225
208
226
209
@_display_as_base
0 commit comments