File indexing completed on 2025-08-28 09:02:09
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #pragma once
0011
0012 #include "common.h"
0013 #include "internals.h"
0014
0015 PYBIND11_NAMESPACE_BEGIN(PYBIND11_NAMESPACE)
0016 PYBIND11_NAMESPACE_BEGIN(detail)
0017
0018
0019
0020
0021
0022 inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &translators) {
0023 auto last_exception = std::current_exception();
0024
0025 for (auto &translator : translators) {
0026 try {
0027 translator(last_exception);
0028 return true;
0029 } catch (...) {
0030 last_exception = std::current_exception();
0031 }
0032 }
0033 return false;
0034 }
0035
0036 inline void try_translate_exceptions() {
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047
0048
0049
0050
0051
0052
0053 bool handled = with_internals([&](internals &internals) {
0054 auto &local_exception_translators = get_local_internals().registered_exception_translators;
0055 if (detail::apply_exception_translators(local_exception_translators)) {
0056 return true;
0057 }
0058 auto &exception_translators = internals.registered_exception_translators;
0059 if (detail::apply_exception_translators(exception_translators)) {
0060 return true;
0061 }
0062 return false;
0063 });
0064
0065 if (!handled) {
0066 set_error(PyExc_SystemError, "Exception escaped from default exception translator!");
0067 }
0068 }
0069
0070 PYBIND11_NAMESPACE_END(detail)
0071 PYBIND11_NAMESPACE_END(PYBIND11_NAMESPACE)