|
9 | 9 | */ |
10 | 10 |
|
11 | 11 | #pragma once |
12 | | - |
13 | 12 | #include "detail/class.h" |
14 | 13 | #include "detail/dynamic_raw_ptr_cast_if_possible.h" |
| 14 | +#include "detail/exception_translation.h" |
15 | 15 | #include "detail/init.h" |
16 | 16 | #include "detail/using_smart_holder.h" |
17 | 17 | #include "attr.h" |
@@ -97,24 +97,6 @@ inline std::string replace_newlines_and_squash(const char *text) { |
97 | 97 | return result.substr(str_begin, str_range); |
98 | 98 | } |
99 | 99 |
|
100 | | -// Apply all the extensions translators from a list |
101 | | -// Return true if one of the translators completed without raising an exception |
102 | | -// itself. Return of false indicates that if there are other translators |
103 | | -// available, they should be tried. |
104 | | -inline bool apply_exception_translators(std::forward_list<ExceptionTranslator> &translators) { |
105 | | - auto last_exception = std::current_exception(); |
106 | | - |
107 | | - for (auto &translator : translators) { |
108 | | - try { |
109 | | - translator(last_exception); |
110 | | - return true; |
111 | | - } catch (...) { |
112 | | - last_exception = std::current_exception(); |
113 | | - } |
114 | | - } |
115 | | - return false; |
116 | | -} |
117 | | - |
118 | 100 | #if defined(_MSC_VER) |
119 | 101 | # define PYBIND11_COMPAT_STRDUP _strdup |
120 | 102 | #else |
@@ -1040,40 +1022,7 @@ class cpp_function : public function { |
1040 | 1022 | throw; |
1041 | 1023 | #endif |
1042 | 1024 | } catch (...) { |
1043 | | - /* When an exception is caught, give each registered exception |
1044 | | - translator a chance to translate it to a Python exception. First |
1045 | | - all module-local translators will be tried in reverse order of |
1046 | | - registration. If none of the module-locale translators handle |
1047 | | - the exception (or there are no module-locale translators) then |
1048 | | - the global translators will be tried, also in reverse order of |
1049 | | - registration. |
1050 | | -
|
1051 | | - A translator may choose to do one of the following: |
1052 | | -
|
1053 | | - - catch the exception and call py::set_error() |
1054 | | - to set a standard (or custom) Python exception, or |
1055 | | - - do nothing and let the exception fall through to the next translator, or |
1056 | | - - delegate translation to the next translator by throwing a new type of exception. |
1057 | | - */ |
1058 | | - |
1059 | | - bool handled = with_internals([&](internals &internals) { |
1060 | | - auto &local_exception_translators |
1061 | | - = get_local_internals().registered_exception_translators; |
1062 | | - if (detail::apply_exception_translators(local_exception_translators)) { |
1063 | | - return true; |
1064 | | - } |
1065 | | - auto &exception_translators = internals.registered_exception_translators; |
1066 | | - if (detail::apply_exception_translators(exception_translators)) { |
1067 | | - return true; |
1068 | | - } |
1069 | | - return false; |
1070 | | - }); |
1071 | | - |
1072 | | - if (handled) { |
1073 | | - return nullptr; |
1074 | | - } |
1075 | | - |
1076 | | - set_error(PyExc_SystemError, "Exception escaped from default exception translator!"); |
| 1025 | + try_translate_exceptions(); |
1077 | 1026 | return nullptr; |
1078 | 1027 | } |
1079 | 1028 |
|
|
0 commit comments