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