Coverage for pygeodesy/errors.py: 93%

264 statements  

« prev     ^ index     » next       coverage.py v7.2.2, created at 2023-05-20 11:54 -0400

1 

2# -*- coding: utf-8 -*- 

3 

4u'''Errors, exceptions, exception formatting and exception chaining. 

5 

6Error, exception classes and functions to format PyGeodesy errors, 

7including the setting of I{exception chaining} in Python 3+. 

8 

9By default, I{exception chaining} is turned I{off}. To enable I{exception 

10chaining}, use command line option C{python -X dev} I{OR} set env variable 

11C{PYTHONDEVMODE=1} or to any non-empyty string I{OR} set env variable 

12C{PYGEODESY_EXCEPTION_CHAINING=std} or to any non-empty string. 

13''' 

14 

15from pygeodesy.interns import MISSING, NN, _a_, _an_, _and_, _clip_, \ 

16 _COLON_, _COLONSPACE_, _COMMASPACE_, _datum_, \ 

17 _ellipsoidal_, _EQUAL_, _incompatible_, _invalid_, \ 

18 _len_, _name_, _no_, _not_, _or_, _SPACE_, \ 

19 _specified_, _UNDER_, _value_, _vs_, _with_ 

20from pygeodesy.lazily import _ALL_LAZY, _ALL_MODS as _MODS, _getenv, \ 

21 _pairs, _PYTHON_X_DEV 

22 

23__all__ = _ALL_LAZY.errors # _ALL_DOCS('_InvalidError', '_IsnotError') _UNDER 

24__version__ = '23.05.06' 

25 

26_box_ = 'box' 

27_default_ = 'default' 

28_kwargs_ = 'kwargs' # XXX _kwds_? 

29_limiterrors = True # imported by .formy 

30_multiple_ = 'multiple' 

31_name_value_ = repr('name=value') 

32_rangerrors = True # imported by .dms 

33_region_ = 'region' 

34_vs__ = _SPACE_(NN, _vs_, NN) 

35 

36try: 

37 _exception_chaining = None # not available 

38 _ = Exception().__cause__ # Python 3+ exception chaining 

39 

40 if _PYTHON_X_DEV or _getenv('PYGEODESY_EXCEPTION_CHAINING', NN): # == _std_ 

41 _exception_chaining = True # turned on, std 

42 raise AttributeError # allow exception chaining 

43 

44 _exception_chaining = False # turned off 

45 

46 def _error_cause(inst, cause=None): 

47 '''(INTERNAL) Set or avoid Python 3+ exception chaining. 

48 

49 Setting C{inst.__cause__ = None} is equivalent to syntax 

50 C{raise Error(...) from None} to avoid exception chaining. 

51 

52 @arg inst: An error instance (I{caught} C{Exception}). 

53 @kwarg cause: A previous error instance (I{caught} C{Exception}) 

54 or C{None} to avoid exception chaining. 

55 

56 @see: Alex Martelli, et.al., "Python in a Nutshell", 3rd Ed., page 163, 

57 O'Reilly, 2017, U{PEP-3134<https://www.Python.org/dev/peps/pep-3134>}, 

58 U{here<https://StackOverflow.com/questions/17091520/how-can-i-more- 

59 easily-suppress-previous-exceptions-when-i-raise-my-own-exception>} 

60 and U{here<https://StackOverflow.com/questions/1350671/ 

61 inner-exception-with-traceback-in-python>}. 

62 ''' 

63 inst.__cause__ = cause # None, no exception chaining 

64 return inst 

65 

66except AttributeError: # Python 2+ 

67 

68 def _error_cause(inst, **unused): # PYCHOK expected 

69 return inst # no-op 

70 

71 

72class _AssertionError(AssertionError): 

73 '''(INTERNAL) Format an C{AssertionError} with/-out exception chaining. 

74 ''' 

75 def __init__(self, *args, **kwds): 

76 _error_init(AssertionError, self, args, **kwds) 

77 

78 

79class _AttributeError(AttributeError): 

80 '''(INTERNAL) Format an C{AttributeError} with/-out exception chaining. 

81 ''' 

82 def __init__(self, *args, **kwds): 

83 _error_init(AttributeError, self, args, **kwds) 

84 

85 

86class _ImportError(ImportError): 

87 '''(INTERNAL) Format an C{ImportError} with/-out exception chaining. 

88 ''' 

89 def __init__(self, *args, **kwds): 

90 _error_init(ImportError, self, args, **kwds) 

91 

92 

93class _IndexError(IndexError): 

94 '''(INTERNAL) Format an C{IndexError} with/-out exception chaining. 

95 ''' 

96 def __init__(self, *args, **kwds): 

97 _error_init(IndexError, self, args, **kwds) 

98 

99 

100class _KeyError(KeyError): 

101 '''(INTERNAL) Format a C{KeyError} with/-out exception chaining. 

102 ''' 

103 def __init__(self, *args, **kwds): # txt=_invalid_ 

104 _error_init(KeyError, self, args, **kwds) 

105 

106 

107class _NameError(NameError): 

108 '''(INTERNAL) Format a C{NameError} with/-out exception chaining. 

109 ''' 

110 def __init__(self, *args, **kwds): 

111 _error_init(NameError, self, args, **kwds) 

112 

113 

114class _NotImplementedError(NotImplementedError): 

115 '''(INTERNAL) Format a C{NotImplementedError} with/-out exception chaining. 

116 ''' 

117 def __init__(self, *args, **kwds): 

118 _error_init(NotImplementedError, self, args, **kwds) 

119 

120 

121class _OverflowError(OverflowError): 

122 '''(INTERNAL) Format an C{OverflowError} with/-out exception chaining. 

123 ''' 

124 def __init__(self, *args, **kwds): # txt=_invalid_ 

125 _error_init(OverflowError, self, args, **kwds) 

126 

127 

128class _TypeError(TypeError): 

129 '''(INTERNAL) Format a C{TypeError} with/-out exception chaining. 

130 ''' 

131 def __init__(self, *args, **kwds): 

132 _error_init(TypeError, self, args, fmt_name_value='type(%s) (%r)', **kwds) 

133 

134 

135class _TypesError(_TypeError): 

136 '''(INTERNAL) Format a C{TypeError} with/-out exception chaining. 

137 ''' 

138 def __init__(self, name, value, *Types, **kwds): 

139 t = _not_(_an(_or(*(t.__name__ for t in Types)))) 

140 _TypeError.__init__(self, name, value, txt=t, **kwds) 

141 

142 

143class _ValueError(ValueError): 

144 '''(INTERNAL) Format a C{ValueError} with/-out exception chaining. 

145 ''' 

146 def __init__(self, *args, **kwds): # ..., cause=None, txt=_invalid_, ... 

147 _error_init(ValueError, self, args, **kwds) 

148 

149 

150class _ZeroDivisionError(ZeroDivisionError): 

151 '''(INTERNAL) Format a C{ZeroDivisionError} with/-out exception chaining. 

152 ''' 

153 def __init__(self, *args, **kwds): 

154 _error_init(ZeroDivisionError, self, args, **kwds) 

155 

156 

157class ClipError(_ValueError): 

158 '''Clip box or clip region issue. 

159 ''' 

160 def __init__(self, *name_n_corners, **txt_cause): 

161 '''New L{ClipError}. 

162 

163 @arg name_n_corners: Either just a name (C{str}) or 

164 name, number, corners (C{str}, 

165 C{int}, C{tuple}). 

166 @kwarg txt_cause: Optional C{B{txt}=str} explanation 

167 of the error and C{B{cause}=None} 

168 for exception chaining. 

169 ''' 

170 if len(name_n_corners) == 3: 

171 t, n, v = name_n_corners 

172 n = _SPACE_(t, _clip_, (_box_ if n == 2 else _region_)) 

173 name_n_corners = n, v 

174 _ValueError.__init__(self, *name_n_corners, **txt_cause) 

175 

176 

177class CrossError(_ValueError): 

178 '''Error raised for zero or near-zero vectorial cross products, 

179 occurring for coincident or colinear points, lines or bearings. 

180 ''' 

181 pass 

182 

183 

184class IntersectionError(_ValueError): # in .ellipsoidalBaseDI, .formy, ... 

185 '''Error raised for line or circle intersection issues. 

186 ''' 

187 def __init__(self, *args, **kwds): 

188 '''New L{IntersectionError}. 

189 ''' 

190 if args: 

191 _ValueError.__init__(self, _SPACE_(*args), **kwds) 

192 else: 

193 _ValueError.__init__(self, **kwds) 

194 

195 

196class LenError(_ValueError): # in .ecef, .fmath, .heights, .iters, .named 

197 '''Error raised for mis-matching C{len} values. 

198 ''' 

199 def __init__(self, where, **lens_txt): # txt=None 

200 '''New L{LenError}. 

201 

202 @arg where: Object with C{.__name__} attribute 

203 (C{class}, C{method}, or C{function}). 

204 @kwarg lens_txt: Two or more C{name=len(name)} pairs 

205 (C{keyword arguments}). 

206 ''' 

207 def _ns_vs_txt_x(cause=None, txt=_invalid_, **kwds): 

208 ns, vs = zip(*itemsorted(kwds)) # unzip 

209 return ns, vs, txt, cause 

210 

211 ns, vs, txt, x = _ns_vs_txt_x(**lens_txt) 

212 ns = _COMMASPACE_.join(ns) 

213 t = _MODS.streprs.Fmt.PAREN(where.__name__, ns) 

214 vs = _vs__.join(map(str, vs)) 

215 t = _SPACE_(t, _len_, vs) 

216 _ValueError.__init__(self, t, txt=txt, cause=x) 

217 

218 

219class LimitError(_ValueError): 

220 '''Error raised for lat- or longitudinal values or deltas exceeding 

221 the given B{C{limit}} in functions L{pygeodesy.equirectangular}, 

222 L{pygeodesy.equirectangular_}, C{nearestOn*} and C{simplify*} 

223 or methods with C{limit} or C{options} keyword arguments. 

224 

225 @see: Subclass L{UnitError}. 

226 ''' 

227 pass 

228 

229 

230class MGRSError(_ValueError): 

231 '''Military Grid Reference System (MGRS) parse or other L{Mgrs} issue. 

232 ''' 

233 pass 

234 

235 

236class NumPyError(_ValueError): 

237 '''Error raised for C{NumPy} issues. 

238 ''' 

239 pass 

240 

241 

242class ParseError(_ValueError): # in .dms, .elevations, .utmupsBase 

243 '''Error parsing degrees, radians or several other formats. 

244 ''' 

245 pass 

246 

247 

248class PointsError(_ValueError): # in .clipy, .frechet, ... 

249 '''Error for an insufficient number of points. 

250 ''' 

251 pass 

252 

253 

254class RangeError(_ValueError): 

255 '''Error raised for lat- or longitude values outside the B{C{clip}}, 

256 B{C{clipLat}}, B{C{clipLon}} in functions L{pygeodesy.parse3llh}, 

257 L{pygeodesy.parseDMS}, L{pygeodesy.parseDMS2} and L{pygeodesy.parseRad} 

258 or the given B{C{limit}} in functions L{pygeodesy.clipDegrees} and 

259 L{pygeodesy.clipRadians}. 

260 

261 @see: Function L{pygeodesy.rangerrors}. 

262 ''' 

263 pass 

264 

265 

266class TriangleError(_ValueError): # in .resections, .vector2d 

267 '''Error raised for triangle, inter- or resection issues. 

268 ''' 

269 pass 

270 

271 

272class SciPyError(PointsError): 

273 '''Error raised for C{SciPy} issues. 

274 ''' 

275 pass 

276 

277 

278class SciPyWarning(PointsError): 

279 '''Error thrown for C{SciPy} warnings. 

280 

281 To raise C{SciPy} warnings as L{SciPyWarning} exceptions, Python 

282 C{warnings} must be filtered as U{warnings.filterwarnings('error') 

283 <https://docs.Python.org/3/library/warnings.html#the-warnings-filter>} 

284 I{prior to} C{import scipy} OR by setting env var U{PYTHONWARNINGS 

285 <https://docs.Python.org/3/using/cmdline.html#envvar-PYTHONWARNINGS>} 

286 OR by invoking C{python} with command line option U{-W<https://docs. 

287 Python.org/3/using/cmdline.html#cmdoption-w>} set to C{-W error}. 

288 ''' 

289 pass 

290 

291 

292class TRFError(_ValueError): # in .ellipsoidalBase, .trf, .units 

293 '''Terrestrial Reference Frame (TRF), L{Epoch}, L{RefFrame} 

294 or L{RefFrame} conversion issue. 

295 ''' 

296 pass 

297 

298 

299class UnitError(LimitError): # in .named, .units 

300 '''Default exception for L{units} issues for a value exceeding the 

301 C{low} or C{high} limit. 

302 ''' 

303 pass 

304 

305 

306class VectorError(_ValueError): # in .nvectorBase, .vector3d, .vector3dBase 

307 '''L{Vector3d}, C{Cartesian*} or C{*Nvector} issues. 

308 ''' 

309 pass 

310 

311 

312def _an(noun): 

313 '''(INTERNAL) Prepend an article to a noun based 

314 on the pronounciation of the first letter. 

315 ''' 

316 a = _an_ if noun[:1].lower() in 'aeinoux' else _a_ 

317 return _SPACE_(a, noun) 

318 

319 

320def _and(*words): 

321 '''(INTERNAL) Join C{words} with C{", "} and C{" and "}. 

322 ''' 

323 return _and_or(_and_, *words) 

324 

325 

326def _and_or(last, *words): 

327 '''(INTERNAL) Join C{words} with C{", "} and C{B{last}}. 

328 ''' 

329 t, w = NN, list(words) 

330 if w: 

331 t = w.pop() 

332 if w: 

333 w = _COMMASPACE_.join(w) 

334 t = _SPACE_(w, last, t) 

335 return t 

336 

337 

338def crosserrors(raiser=None): 

339 '''Report or ignore vectorial cross product errors. 

340 

341 @kwarg raiser: Use C{True} to throw or C{False} to ignore 

342 L{CrossError} exceptions. Use C{None} to 

343 leave the setting unchanged. 

344 

345 @return: Previous setting (C{bool}). 

346 

347 @see: Property C{Vector3d[Base].crosserrors}. 

348 ''' 

349 B = _MODS.vector3dBase.Vector3dBase 

350 t = B._crosserrors # XXX class attr! 

351 if raiser in (True, False): 

352 B._crosserrors = raiser 

353 return t 

354 

355 

356def _error_init(Error, inst, args, fmt_name_value='%s (%r)', txt=NN, 

357 cause=None, **kwds): # by .lazily 

358 '''(INTERNAL) Format an error text and initialize an C{Error} instance. 

359 

360 @arg Error: The error super-class (C{Exception}). 

361 @arg inst: Sub-class instance to be __init__-ed (C{_Exception}). 

362 @arg args: Either just a value or several name, value, ... 

363 positional arguments (C{str}, any C{type}), in 

364 particular for name conflicts with keyword 

365 arguments of C{error_init} or which can't be 

366 given as C{name=value} keyword arguments. 

367 @kwarg fmt_name_value: Format for (name, value) (C{str}). 

368 @kwarg txt: Optional explanation of the error (C{str}). 

369 @kwarg cause: Optional, caught error (L{Exception}), for 

370 exception chaining (supported in Python 3+). 

371 @kwarg kwds: Additional C{B{name}=value} pairs, if any. 

372 ''' 

373 def _fmtuple(pairs): 

374 return tuple(fmt_name_value % t for t in pairs) 

375 

376 t, n = (), len(args) 

377 if n > 2: 

378 s = _MODS.basics.isodd(n) 

379 t = _fmtuple(zip(args[0::2], args[1::2])) 

380 if s: # XXX _xzip(..., strict=s) 

381 t += args[-1:] 

382 elif n == 2: 

383 t = (fmt_name_value % args), 

384 elif n: # == 1 

385 t = str(args[0]), 

386 

387 if kwds: 

388 t += _fmtuple(itemsorted(kwds)) 

389 t = _or(*t) if t else _SPACE_(_name_value_, MISSING) 

390 

391 if txt is not None: 

392 x = str(txt) or (str(cause) if cause else _invalid_) 

393 C = _COMMASPACE_ if _COLON_ in t else _COLONSPACE_ 

394 t = C(t, x) 

395# else: # LenError, _xzip, .dms, .heights, .vector2d 

396# x = NN # XXX or t? 

397 Error.__init__(inst, t) 

398# inst.__x_txt__ = x # hold explanation 

399 _error_cause(inst, cause=cause if _exception_chaining else None) 

400 _error_under(inst) 

401 

402 

403def _error_under(inst): 

404 '''(INTERNAL) Remove leading underscore from instance' class name. 

405 ''' 

406 n = inst.__class__.__name__ 

407 if n.startswith(_UNDER_): 

408 inst.__class__.__name__ = n.lstrip(_UNDER_) 

409 return inst 

410 

411 

412def exception_chaining(error=None): 

413 '''Get an error's I{cause} or the exception chaining setting. 

414 

415 @kwarg error: An error instance (C{Exception}) or C{None}. 

416 

417 @return: If C{B{error} is None}, return C{True} if exception 

418 chaining is enabled for PyGeodesy errors, C{False} 

419 if turned off and C{None} if not available. If 

420 B{C{error}} is not C{None}, return it's error 

421 I{cause} or C{None}. 

422 

423 @note: To enable exception chaining for C{pygeodesy} errors, 

424 set env var C{PYGEODESY_EXCEPTION_CHAINING} to any 

425 non-empty value prior to C{import pygeodesy}. 

426 ''' 

427 return _exception_chaining if error is None else \ 

428 getattr(error, '__cause__', None) 

429 

430 

431def _incompatible(this): 

432 '''(INTERNAL) Format an C{"incompatible with ..."} text. 

433 ''' 

434 return _SPACE_(_incompatible_, _with_, this) 

435 

436 

437def _InvalidError(Error=_ValueError, **txt_name_values_cause): # txt=_invalid_, name=value [, ...] 

438 '''(INTERNAL) Create an C{Error} instance. 

439 

440 @kwarg Error: The error class or sub-class (C{Exception}). 

441 @kwarg txt_name_values: One or more C{B{name}=value} pairs 

442 and optionally, keyword argument C{B{txt}=str} 

443 to override the default C{B{txt}='invalid'} and 

444 C{B{cause}=None} for exception chaining. 

445 

446 @return: An B{C{Error}} instance. 

447 ''' 

448 return _XError(Error, **txt_name_values_cause) 

449 

450 

451def isError(obj): 

452 '''Check a (caught) exception. 

453 

454 @arg obj: The exception C({Exception}). 

455 

456 @return: C{True} if B{C{obj}} is a C{pygeodesy} error, 

457 C{False} if B{C{obj}} is a standard Python error 

458 of C{None} if neither. 

459 ''' 

460 return True if isinstance(obj, _XErrors) else ( 

461 False if isinstance(obj, Exception) else None) 

462 

463 

464def _IsnotError(*nouns, **name_value_Error_cause): # name=value [, Error=TypeError, cause=None] 

465 '''Create a C{TypeError} for an invalid C{name=value} type. 

466 

467 @arg nouns: One or more expected class or type names, usually nouns (C{str}). 

468 @kwarg name_value_Error_cause: One C{B{name}=value} pair and optionally, 

469 keyword argument C{B{Error}=TypeError} to override the default 

470 and C{B{cause}=None} for exception chaining. 

471 

472 @return: A C{TypeError} or an B{C{Error}} instance. 

473 ''' 

474 x, Error = _xkwds_pop_(name_value_Error_cause, cause=None, Error=TypeError) 

475 n, v = _xkwds_popitem(name_value_Error_cause) if name_value_Error_cause \ 

476 else (_name_value_, MISSING) # XXX else tuple(...) 

477 n = _MODS.streprs.Fmt.PARENSPACED(n, repr(v)) 

478 t = _not_(_an(_or(*nouns)) if nouns else _specified_) 

479 return _XError(Error, n, txt=t, cause=x) 

480 

481 

482def itemsorted(adict, *args, **asorted_reverse): 

483 '''Return the items of C{B{adict}} sorted I{alphabetically, case-insensitively} 

484 and in I{ascending} order. 

485 

486 @arg args: Optional argument(s) for method C{B{adict}.items(B*{args})}. 

487 @kwarg asorted_reverse: Use keyword argument C{B{asorted}=False} for 

488 I{case-sensitive} sorting and C{B{reverse}=True} for 

489 results in C{descending} order. 

490 ''' 

491 def _un(item): 

492 return item[0].lower() 

493 

494 # see .rhumb.Rhumb and ._RhumbLine 

495 a, r = _xkwds_get_(asorted_reverse, asorted=True, reverse=False) \ 

496 if asorted_reverse else (True, False) 

497 items = adict.items(*args) if args else adict.items() 

498 return sorted(items, reverse=r, key=_un if a else None) 

499 

500 

501def limiterrors(raiser=None): 

502 '''Get/set the throwing of L{LimitError}s. 

503 

504 @kwarg raiser: Choose C{True} to raise or C{False} to 

505 ignore L{LimitError} exceptions. Use 

506 C{None} to leave the setting unchanged. 

507 

508 @return: Previous setting (C{bool}). 

509 ''' 

510 global _limiterrors 

511 t = _limiterrors 

512 if raiser in (True, False): 

513 _limiterrors = raiser 

514 return t 

515 

516 

517def _or(*words): 

518 '''(INTERNAL) Join C{words} with C{", "} and C{" or "}. 

519 ''' 

520 return _and_or(_or_, *words) 

521 

522 

523def _parseX(parser, *args, **name_values_Error): # name=value[, ..., Error=ParseError] 

524 '''(INTERNAL) Invoke a parser and handle exceptions. 

525 

526 @arg parser: The parser (C{callable}). 

527 @arg args: Any B{C{parser}} arguments (any C{type}s). 

528 @kwarg name_values_Error: Any C{B{name}=value} pairs and 

529 optionally, C{B{Error}=ParseError} keyword 

530 argument to override the default. 

531 

532 @return: Parser result. 

533 

534 @raise ParseError: Or the specified C{B{Error}}. 

535 ''' 

536 try: 

537 return parser(*args) 

538 except Exception as x: 

539 E = _xkwds_pop(name_values_Error, Error=type(x) if isError(x) else 

540 ParseError) 

541 raise _XError(E, **_xkwds(name_values_Error, cause=x)) 

542 

543 

544def rangerrors(raiser=None): 

545 '''Get/set the throwing of L{RangeError}s. 

546 

547 @kwarg raiser: Choose C{True} to raise or C{False} to ignore 

548 L{RangeError} exceptions. Use C{None} to leave 

549 the setting unchanged. 

550 

551 @return: Previous setting (C{bool}). 

552 ''' 

553 global _rangerrors 

554 t = _rangerrors 

555 if raiser in (True, False): 

556 _rangerrors = raiser 

557 return t 

558 

559 

560def _SciPyIssue(x, *extras): # PYCHOK no cover 

561 if isinstance(x, (RuntimeWarning, UserWarning)): 

562 Error = SciPyWarning 

563 else: 

564 Error = SciPyError # PYCHOK not really 

565 t = _SPACE_(str(x).strip(), *extras) 

566 return Error(t, cause=x) 

567 

568 

569def _xattr(obj, **name_default): 

570 '''(INTERNAL) Get an C{obj}'s attribute by C{name}. 

571 ''' 

572 if len(name_default) == 1: 

573 for n, d in name_default.items(): 

574 return getattr(obj, n, d) 

575 raise _xkwds_Error(_xattr, obj, name_default) 

576 

577 

578def _xdatum(datum1, datum2, Error=None): 

579 '''(INTERNAL) Check for datum, ellipsoid or rhumb mis-match. 

580 ''' 

581 if Error: 

582 E1, E2 = datum1.ellipsoid, datum2.ellipsoid 

583 if E1 != E2: 

584 raise Error(E2.named2, txt=_incompatible(E1.named2)) 

585 elif datum1 != datum2: 

586 t = _SPACE_(_datum_, repr(datum1.name), _not_, repr(datum2.name)) 

587 raise _AssertionError(t) 

588 

589 

590def _xellipsoidal(**name_value): 

591 '''(INTERNAL) Check an I{ellipsoidal} item. 

592 

593 @return: The B{C{value}} if ellipsoidal. 

594 

595 @raise TypeError: Not ellipsoidal B{C{value}}. 

596 ''' 

597 try: 

598 for n, v in name_value.items(): 

599 if v.isEllipsoidal: 

600 return v 

601 break 

602 else: 

603 n = v = MISSING 

604 except AttributeError: 

605 pass 

606 raise _TypeError(n, v, txt=_not_(_ellipsoidal_)) 

607 

608 

609def _XError(Error, *args, **kwds): 

610 '''(INTERNAL) Format an C{Error} or C{_Error}. 

611 ''' 

612 try: # C{_Error} style 

613 return Error(*args, **kwds) 

614 except TypeError: # no keyword arguments 

615 pass 

616 e = _ValueError(*args, **kwds) 

617 E = Error(str(e)) 

618 if _exception_chaining: 

619 _error_cause(E, cause=e.__cause__) # PYCHOK OK 

620 return E 

621 

622 

623_XErrors = _TypeError, _ValueError 

624# map certain C{Exception} classes to the C{_Error} 

625_X2Error = {AssertionError: _AssertionError, 

626 AttributeError: _AttributeError, 

627 ImportError: _ImportError, 

628 IndexError: _IndexError, 

629 KeyError: _KeyError, 

630 NameError: _NameError, 

631 NotImplementedError: _NotImplementedError, 

632 OverflowError: _OverflowError, 

633 TypeError: _TypeError, 

634 ValueError: _ValueError, 

635 ZeroDivisionError: _ZeroDivisionError} 

636 

637 

638def _xError(x, *args, **kwds): 

639 '''(INTERNAL) Embellish a (caught) exception. 

640 

641 @arg x: The exception (usually, C{_Error}). 

642 @arg args: Embelishments (C{any}). 

643 @kwarg kwds: Embelishments (C{any}). 

644 ''' 

645 return _XError(type(x), *args, **_xkwds(kwds, cause=x)) 

646 

647 

648def _xError2(x): # in .fsums 

649 '''(INTERNAL) Map an exception to 2-tuple (C{_Error} class, error C{txt}). 

650 

651 @arg x: The exception instance (usually, C{Exception}). 

652 ''' 

653 X = type(x) 

654 E = _X2Error.get(X, X) 

655 if E is X and not isError(x): 

656 E = _NotImplementedError 

657 t = repr(x) 

658 else: 

659 t = str(x) 

660 return E, t 

661 

662 

663try: 

664 _ = {}.__or__ # {} | {} # Python 3.9+ 

665 

666 def _xkwds(kwds, **dflts): 

667 '''(INTERNAL) Override C{dflts} with specified C{kwds}. 

668 ''' 

669 return (dflts | kwds) if kwds else dflts 

670 

671except AttributeError: 

672 from copy import copy as _copy 

673 

674 def _xkwds(kwds, **dflts): # PYCHOK expected 

675 '''(INTERNAL) Override C{dflts} with specified C{kwds}. 

676 ''' 

677 d = dflts 

678 if kwds: 

679 d = _copy(d) 

680 d.update(kwds) 

681 return d 

682 

683 

684def _xkwds_bool(inst, **kwds): # in .frechet, .hausdorff, .heights 

685 '''(INTERNAL) Set applicable C{bool} properties/attributes. 

686 ''' 

687 for n, v in kwds.items(): 

688 b = getattr(inst, n, None) 

689 if b is None: # invalid bool attr 

690 t = _SPACE_(_EQUAL_(n, repr(v)), 'for', inst.__class__.__name__) # XXX .classname 

691 raise _AttributeError(t, txt=_not_('applicable')) 

692 if v in (False, True) and v != b: 

693 setattr(inst, NN(_UNDER_, n), v) 

694 

695 

696def _xkwds_Error(where, kwds, name_txt, txt=_default_): 

697 # Helper for _xkwds_get, _xkwds_pop and _xkwds_popitem below 

698 f = _COMMASPACE_.join(_pairs(kwds) + _pairs(name_txt)) 

699 f = _MODS.streprs.Fmt.PAREN(where.__name__, f) 

700 t = _multiple_ if name_txt else _no_ 

701 t = _SPACE_(t, _EQUAL_(_name_, txt), _kwargs_) 

702 return _AssertionError(f, txt=t) 

703 

704 

705def _xkwds_get(kwds, **name_default): 

706 '''(INTERNAL) Get a C{kwds} value by C{name}, or the C{default}. 

707 ''' 

708 if len(name_default) == 1: 

709 for n, d in name_default.items(): 

710 return kwds.get(n, d) 

711 raise _xkwds_Error(_xkwds_get, kwds, name_default) 

712 

713 

714def _xkwds_get_(kwds, **names_defaults): 

715 '''(INTERNAL) Yield each C{kwds} value or its C{default} 

716 in I{case-insensitive, alphabetical} order. 

717 ''' 

718 for n, d in itemsorted(names_defaults): 

719 yield kwds.get(n, d) 

720 

721 

722def _xkwds_not(*args, **kwds): 

723 '''(INTERNAL) Return C{kwds} with a value not in C{args}. 

724 ''' 

725 return dict((n, v) for n, v in kwds.items() if v not in args) 

726 

727 

728def _xkwds_pop(kwds, **name_default): 

729 '''(INTERNAL) Pop a C{kwds} value by C{name}, or the C{default}. 

730 ''' 

731 if len(name_default) == 1: 

732 for n, d in name_default.items(): 

733 return kwds.pop(n, d) 

734 raise _xkwds_Error(_xkwds_pop, kwds, name_default) 

735 

736 

737def _xkwds_pop_(kwds, **names_defaults): 

738 '''(INTERNAL) Pop and yield each C{kwds} value or its C{default} 

739 in I{case-insensitive, alphabetical} order. 

740 ''' 

741 for n, d in itemsorted(names_defaults): 

742 yield kwds.pop(n, d) 

743 

744 

745def _xkwds_popitem(name_value): 

746 '''(INTERNAL) Return exactly one C{(name, value)} item. 

747 ''' 

748 if len(name_value) == 1: # XXX TypeError 

749 return name_value.popitem() # XXX AttributeError 

750 raise _xkwds_Error(_xkwds_popitem, (), name_value, txt=_value_) 

751 

752 

753def _xzip(*args, **strict): # PYCHOK no cover 

754 '''(INTERNAL) Standard C{zip(..., strict=True)}. 

755 ''' 

756 s = _xkwds_get(strict, strict=True) 

757 if s: 

758 _zip = _MODS.basics._zip 

759 if _zip is zip: # < (3, 10) 

760 t = _MODS.streprs.unstr(_xzip.__name__, *args, strict=s) 

761 raise _NotImplementedError(t, txt=None) 

762 return _zip(*args) 

763 return zip(*args) 

764 

765# **) MIT License 

766# 

767# Copyright (C) 2016-2023 -- mrJean1 at Gmail -- All Rights Reserved. 

768# 

769# Permission is hereby granted, free of charge, to any person obtaining a 

770# copy of this software and associated documentation files (the "Software"), 

771# to deal in the Software without restriction, including without limitation 

772# the rights to use, copy, modify, merge, publish, distribute, sublicense, 

773# and/or sell copies of the Software, and to permit persons to whom the 

774# Software is furnished to do so, subject to the following conditions: 

775# 

776# The above copyright notice and this permission notice shall be included 

777# in all copies or substantial portions of the Software. 

778# 

779# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS 

780# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 

781# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 

782# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR 

783# OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 

784# ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 

785# OTHER DEALINGS IN THE SOFTWARE.