|
11 | 11 |
|
12 | 12 | #include "../pytypes.h" |
13 | 13 | #include "smart_holder_sfinae_hooks_only.h" |
| 14 | +#include <exception> |
14 | 15 |
|
15 | 16 | /// Tracks the `internals` and `type_info` ABI version independent of the main library version. |
16 | 17 | /// |
@@ -291,21 +292,104 @@ inline internals **&get_internals_pp() { |
291 | 292 | return internals_pp; |
292 | 293 | } |
293 | 294 |
|
| 295 | +#if PY_VERSION_HEX >= 0x03030000 |
| 296 | +// forward decl |
| 297 | +inline void translate_exception(std::exception_ptr); |
| 298 | + |
| 299 | +template <class T, |
| 300 | + enable_if_t<std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0> |
| 301 | +bool handle_nested_exception(const T &exc, const std::exception_ptr &p) { |
| 302 | + std::exception_ptr nested = exc.nested_ptr(); |
| 303 | + if (nested != nullptr && nested != p) { |
| 304 | + translate_exception(nested); |
| 305 | + return true; |
| 306 | + } |
| 307 | + return false; |
| 308 | +} |
| 309 | + |
| 310 | +template <class T, |
| 311 | + enable_if_t<!std::is_same<std::nested_exception, remove_cvref_t<T>>::value, int> = 0> |
| 312 | +bool handle_nested_exception(const T &exc, const std::exception_ptr &p) { |
| 313 | + if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(exc))) { |
| 314 | + return handle_nested_exception(*nep, p); |
| 315 | + } |
| 316 | + return false; |
| 317 | +} |
| 318 | + |
| 319 | +#else |
| 320 | + |
| 321 | +template <class T> |
| 322 | +bool handle_nested_exception(const T &, std::exception_ptr &) { |
| 323 | + return false; |
| 324 | +} |
| 325 | +#endif |
| 326 | + |
| 327 | +inline bool raise_err(PyObject *exc_type, const char *msg) { |
| 328 | +#if PY_VERSION_HEX >= 0x03030000 |
| 329 | + if (PyErr_Occurred()) { |
| 330 | + raise_from(exc_type, msg); |
| 331 | + return true; |
| 332 | + } |
| 333 | +#endif |
| 334 | + PyErr_SetString(exc_type, msg); |
| 335 | + return false; |
| 336 | +}; |
| 337 | + |
294 | 338 | inline void translate_exception(std::exception_ptr p) { |
| 339 | + if (!p) { |
| 340 | + return; |
| 341 | + } |
295 | 342 | try { |
296 | | - if (p) std::rethrow_exception(p); |
297 | | - } catch (error_already_set &e) { e.restore(); return; |
298 | | - } catch (const builtin_exception &e) { e.set_error(); return; |
299 | | - } catch (const std::bad_alloc &e) { PyErr_SetString(PyExc_MemoryError, e.what()); return; |
300 | | - } catch (const std::domain_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; |
301 | | - } catch (const std::invalid_argument &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; |
302 | | - } catch (const std::length_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; |
303 | | - } catch (const std::out_of_range &e) { PyErr_SetString(PyExc_IndexError, e.what()); return; |
304 | | - } catch (const std::range_error &e) { PyErr_SetString(PyExc_ValueError, e.what()); return; |
305 | | - } catch (const std::overflow_error &e) { PyErr_SetString(PyExc_OverflowError, e.what()); return; |
306 | | - } catch (const std::exception &e) { PyErr_SetString(PyExc_RuntimeError, e.what()); return; |
| 343 | + std::rethrow_exception(p); |
| 344 | + } catch (error_already_set &e) { |
| 345 | + handle_nested_exception(e, p); |
| 346 | + e.restore(); |
| 347 | + return; |
| 348 | + } catch (const builtin_exception &e) { |
| 349 | + // Could not use template since it's an abstract class. |
| 350 | + if (auto *nep = dynamic_cast<const std::nested_exception *>(std::addressof(e))) { |
| 351 | + handle_nested_exception(*nep, p); |
| 352 | + } |
| 353 | + e.set_error(); |
| 354 | + return; |
| 355 | + } catch (const std::bad_alloc &e) { |
| 356 | + handle_nested_exception(e, p); |
| 357 | + raise_err(PyExc_MemoryError, e.what()); |
| 358 | + return; |
| 359 | + } catch (const std::domain_error &e) { |
| 360 | + handle_nested_exception(e, p); |
| 361 | + raise_err(PyExc_ValueError, e.what()); |
| 362 | + return; |
| 363 | + } catch (const std::invalid_argument &e) { |
| 364 | + handle_nested_exception(e, p); |
| 365 | + raise_err(PyExc_ValueError, e.what()); |
| 366 | + return; |
| 367 | + } catch (const std::length_error &e) { |
| 368 | + handle_nested_exception(e, p); |
| 369 | + raise_err(PyExc_ValueError, e.what()); |
| 370 | + return; |
| 371 | + } catch (const std::out_of_range &e) { |
| 372 | + handle_nested_exception(e, p); |
| 373 | + raise_err(PyExc_IndexError, e.what()); |
| 374 | + return; |
| 375 | + } catch (const std::range_error &e) { |
| 376 | + handle_nested_exception(e, p); |
| 377 | + raise_err(PyExc_ValueError, e.what()); |
| 378 | + return; |
| 379 | + } catch (const std::overflow_error &e) { |
| 380 | + handle_nested_exception(e, p); |
| 381 | + raise_err(PyExc_OverflowError, e.what()); |
| 382 | + return; |
| 383 | + } catch (const std::exception &e) { |
| 384 | + handle_nested_exception(e, p); |
| 385 | + raise_err(PyExc_RuntimeError, e.what()); |
| 386 | + return; |
| 387 | + } catch (const std::nested_exception &e) { |
| 388 | + handle_nested_exception(e, p); |
| 389 | + raise_err(PyExc_RuntimeError, "Caught an unknown nested exception!"); |
| 390 | + return; |
307 | 391 | } catch (...) { |
308 | | - PyErr_SetString(PyExc_RuntimeError, "Caught an unknown exception!"); |
| 392 | + raise_err(PyExc_RuntimeError, "Caught an unknown exception!"); |
309 | 393 | return; |
310 | 394 | } |
311 | 395 | } |
|
0 commit comments